A Very Fast 64–Bit Date Algorithm: 30-40% faster
26 points by fanf
26 points by fanf
The ARM changes probably help for other RISCy things like Power ISA or MIPS that also have limited space for immediates in a single instruction.
782432, 126464, 191360, 977792, 65536 ... have been carefully selected to each be divisible by 32. One can use a compile-time check to divide these constants by 32 if the target is ARM (or use the smaller constants and scale by 32 for x64). When doing so, they all fit under 16–bit in size
Yes, true on arm64, which devotes a heck of a lot of opcode space to MOVZ/MOVK/MOVN. And yes that will also work on Power and MIPS.
RISC-V only has 12 bit constants that can be contained in add/and/or/xor instructions. BUT it can load 20 bit constants with the LUI instruction -- but they loaded into the most significant bits of a 32 bit register, or 32 bit lower half, sign extended, of a 64 bit register.
So, rather than divide those constants by 32, you can multiply them by 4096/32 = 128, and then similarly adjust the later shift. The largest one 977792 becomes 125,157,376 (30556<<12) which is well below the point of turning negative.
Given: days = Days since epoch
I usually use epoch seconds for timestamps.
Can this algorithm still be used by feeding it with "epoch days = epoch seconds / 86400" without considering leap seconds?
Yes, because unix time is always 86400 seconds per day. https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html#tag_04_19