3 ms·
Well, idiv being super fast is new to me, but I guess it's relative.
by leni536 4y ago
Well, idiv being super fast is new to me, but I guess it's relative.
- cperciva 4y agoInteger division is sufficiently famously slow that people (and compilers) go to great lengths to avoid it... which makes it rare enough that there's very little point implementing it. If you really need integer division, you can typically optimize it by doing a FP iteration and then fixing up the approximate result.
- Tuna-Fish 4y agoThe worst case integer divide times on newest Intel/AMD cpus are 15/18 for 64bit and 12/13 for 32-bit. Both do early out for easy values at around 10 cycles. Integer division is still on the slower side operations on the machines, but the advice to avoid division at all costs is outdated. In comparison, the FP divide instructions are in the ~13/14 cycles, it would be hard for you to shuffle data to that side and back and not be slower. (And no, they are not doing this internally -- both have separate integer divide units.)
- magicalhippo 4y agoThese things change over time, but at least a fair chunk of recent Intel CPUs had idiv handled by the floating-point unit. That is, it couldn't do an idiv and a fdiv in parallel.
- astrange 4y agoAs long as they're pipelined, slow operations are okay as long as you have enough other work to fill the pipeline until their result comes back.