Skip to content

Comment on When static makes your C code 10 times faster

Comments

"When modulus is static, gcc / clang know that it is private to the current compilation unit, and therefore they can inline the value itself. Then, they turn the expensive div into a much cheaper and – since

mod’ing by a power of two -- is equal to bitwise and of that number minus one!

All you need to do is keep the bits lower than that power of two, which is what the and will do."

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.