I have seen compilers take modulus operation and turn it into a shift or an AND operation with optimization on high level for some special cases.hMx wrote:Are you really sure? Yes, I know that / and % are slower than *, but 33% overall slowdown sounds ridiculous.hgm wrote:Well, / and % are 'infinitely slow' even on modern hardware, and using a single % operation per node slowed down Crafty by ~33%.
I suspect that the lldiv function must have been used to get that kind of a slowdown. I have seen awful performance for lldiv.
Q:\>type mod.asm
Code: Select all
; Listing generated by Microsoft (R) Optimizing Compiler Version 18.00.40629.0
include listing.inc
INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES
PUBLIC remainder
; Function compile flags: /Ogtpy
; File q:\mod.c
_TEXT SEGMENT
val$ = 8
remainder PROC
; 4 : return val / 262144;
shr rcx, 18
mov rax, rcx
; 5 : }
ret 0
remainder ENDP
_TEXT ENDS
END
Code: Select all
unsigned long long remainder(unsigned long long val)
{
return val / 262144;
}