PowerBASIC Users Meeting Point
Faster leap year code. - Printable Version

+- PowerBASIC Users Meeting Point (http://pump.richheimer.de)
+-- Forum: User to User Discussions (http://pump.richheimer.de/forumdisplay.php?fid=3)
+--- Forum: Source Code Library (http://pump.richheimer.de/forumdisplay.php?fid=8)
+--- Thread: Faster leap year code. (/showthread.php?tid=10)



Faster leap year code. - Dale Yarker - 09-07-2024

This is roughly 3 times faster than the usual three MOD operations method.
Code:
  ! push eax          'for pseudo MOD, dividend and quotient
  ! mov FebDays, 28&  'not a leap year pre-set
  ! mov eax, 3???
  ! and eax, Year    'conditionally equivalent MOD 4
  ! jz MOD100        '0 is possible leap year
  ! jmp Done
  MOD100:
  ! push ebx          'for divisor
  ! push edx          'for high part of dividend, remainder (MOD)
  ! xor edx, edx
  ! mov eax, Year
  ! mov ebx, 100???
  ! div ebx
  ! cmp edx, 0???    'does MOD 100 = 0 ?
  ! pop edx
  ! pop ebx
  ! jz MOD400        '0 is possibly not a leap year
  ! jmp Is29Days      'non 0 is a leap year
  MOD400:
  ! and eax, 3???    'EAX has Year\100, so pseudo MOD 4 again
  ! jnz Done
  ! Is29Days:
  ! mov FebDays, 29&
  Done:
  ! pop eax '
(here pseudo means conditionally equivalent, the condition is the divisor being a power of 2)

Longer description at: http://www.yarker-dsyc.info/D_Notebook/Programs/Misc/IsLeap-FebruaryDays.html