3 ms·
How about 15 bytes? (x86, month in ecx, result in eax) // 28 + ((0x3bbeecc>>m>>m)&3); b8 cc ee bb 03 mov eax,0x3bbeecc d3 f8 sar eax,cl
by OMGWTF 12y ago
How about 15 bytes? (x86, month in ecx, result in eax)
// 28 + ((0x3bbeecc>>m>>m)&3);
b8 cc ee bb 03 mov eax,0x3bbeecc
d3 f8 sar eax,cl
d3 f8 sar eax,cl
83 e0 03 and eax,0x3
83 c0 1c add eax,0x1c
- saalweachter 12y agoI feel like you should be able to get away with a 2-byte constant of 0x3bec, but I haven't been able to simplify the bitshift amount enough, from (n >> 2 << 2) + 2 * (n % 2). ETA: I think the simplest I can get it is: days(n) = 28 + (0x1df6 << (1^n&3) >> n) & 3 My intuition is that the 1^n&3 isn't going to be cheaper.
- toast0 12y agoI think you can save a byte on the add with 04 1c add al, 0x1c (but i've never written any x86 assembly)
- OMGWTF 12y agoYou are right. I was lazy, I compiled it with gcc -O2 and extracted the assembler with objdump -Mintel -d.
- userbinator 12y agoIn fact you can save 2 bytes if you only care about the result in AL, bringing it down to 13 bytes: b8 cc ee bb 03 mov eax,0x3bbeecc d3 f8 sar eax,cl d3 f8 sar eax,cl 24 03 and al, 3 04 1c add al, 28