2 ms·
In most cases your compiler should do the clever work of turning your division or modulo operation into easier to do bit banging... but only if you're operating
by Symmetry 7y ago
In most cases your compiler should do the clever work of turning your division or modulo operation into easier to do bit banging... but only if you're operating on a reasonable constant. Powers of two are best but you can do other constant divisions by stringing together 1-latency operations in ways that are still far faster than division.
- caf 7y agoYes - as long as x is unsigned, then (x % 1024) will be compiled to the same thing as (x & 1023) with any reasonable compiler. If your hashtable dynamically resizes though, (x % size) will use a full divide. You could keep the log2 of the size around instead, and rely on (x % (1U << size_shift)) being optimised (this works on gcc: https://godbolt.org/z/HbGnJH https://godbolt.org/z/HbGnJH) but (x & (size - 1)) might be easier at that point.