I wrote a range limited isa_leapyear(year) good in 1800..2200.

Your look is appreciated; I do not understand this:

The first version runs 50x more slowly than the second version.
The first is better practice, is there a way to make it behave?

They differ only in placing the bit table (local vs const global).
I ran both through code_typed(), the function code is the same.

this is the way of the snail:

function is_proximal_leapyear(year::Int)
  LeapYearBitTbl =  
  [ 0xfdfffffe, 0xffffffff, 0xfffff7ff, 0x0000000f ]

  y400 = convert(Uint32, year-1800)
  y100 = y400 >> 2
  mask = one(Uint32) << (y100 & 0x0000000f)
  indx = 1 +  (y100 >>  5)
  ((LeapYearBitTbl[indx] $ mask) + (y400 & 0x00000003)) === zero(Uint32)
end

this way is lovely fast:

const LeapYearBitTable = 
  [ 0xfdfffffe, 0xffffffff, 0xfffff7ff, 0x0000000f ];

function isa_proximal_leapyear(year::Int)
  y400 = convert(Uint32, year-1800)
  y100 = y400 >> 2
  mask = one(Uint32) << (y100 & 0x0000000f)
  indx = 1 +  (y100 >>  5)
  ((LeapYearBitTable[indx] $ mask) + (y400 & 0x00000003)) === zero(Uint32)
end

Reply via email to