9 ms·
Shift, add instead of multiply or divide This is usually not worth it on modern superscalar processors, where multiply is fast and pipelined. It's a win mostly
by Animats 10y ago
Shift, add instead of multiply or divide
This is usually not worth it on modern superscalar processors, where multiply is fast and pipelined. It's a win mostly on Arduino-class CPUs. If you need more than one operation to replace a multiply, such as an add and shift or a bit operation, the replacement is probably slower.
The costly operation in the example is accessing a large 2D array along the non-dense axis. For big enough arrays, that's a cache miss every time.
- yorwba 10y ago> The costly operation in the example is accessing a large 2D array along the non-dense axis. For big enough arrays, that's a cache miss every time. I half-remember hearing about memory-prefetchers smart enough to detect this kind of strided access and fill the cache accordingly.
- mnw21cam 10y agoThen it turns from a cache miss problem to a memory bandwidth problem. If you are only using a small proportion of the cache line in your calculation, then you effectively multiply the data transferred to the CPU.
- andreareina 10y agoThis is precisely the sort of thing you let the compiler worry about. Using godbolt.org I see that a constant multiplication by 30 is compiled to an `imul` in x86-64 (both clang and gcc) and `lsl`, `sub`, `lsl` in ARM64 (gcc).
- legulere 10y agoRegardless if it is worth it or not: Every compiler does such basic peephole optimisations.