That’s really cool! I never would have guessed that a manual long division implementation would take the crown. I‘d be curious how M1 series chips perform.
When I was doing work on the GHC Haskell compiler’s ARM backend, one thing that surprised me was how low latency the integer divide instruction is on M CPUs, it’s like two cycles IIRC. They must have dedicated a huge amount of chip area to achieve that. They really designed a CPU where they decided to do all the things you don’t do when trying to save money.
Pulling out an Adler Lake P core or Zen 4, drinking what? 5-10watts per (non-hyper) core to humble an M1 and only reaching half the throughput by your numbers 7-9 cycles vs 18-19.
I'm comparing E-cores, which are at least in the pretending to be the same power envelope.
It's apples-to-oranges on many accounts, right. But Zen 4's latency numbers should be equal to Zen 4c's, which are AMDs E-core equivalents (no clue on relative power usage though).
For what it's worth, M1's E-cores have 21-cycle latency for division. Of course here division latency is much more an area question (and how much target software needs it), not power. And that's still 64÷64→64-bit division, compared to x86's 128÷64→64-bit (and also x86's division instr computes both quotient and remainder, though that's a rather small cost around that of a multiply at worst).
In tackling the same problem I was able to get better performance than long division on my Ice Lake by using a look-up table based approach to retrieve 16-bit reciprocals, an implementation being available here. The method was shared with me by u/YumiYumiYumi.
Unfortunately the reciprocal approach doesn't really work without AVX-512 VBMI (i.e. can't be efficiently translated to AVX2), but it's faster than long division if the CPU supports VBMI.
2
u/CandyCrisis Dec 21 '24
That’s really cool! I never would have guessed that a manual long division implementation would take the crown. I‘d be curious how M1 series chips perform.
2
u/FUZxxl Dec 22 '24
I wonder if this would perform better if non-restoring division was used.
2
u/HugeONotation Dec 22 '24
In tackling the same problem I was able to get better performance than long division on my Ice Lake by using a look-up table based approach to retrieve 16-bit reciprocals, an implementation being available here. The method was shared with me by u/YumiYumiYumi.
1
u/olawlor Dec 21 '24
Nice writeup! I'm curious if you tried 'cvtt' (convert with truncate), which has round toward zero built in?
On my machines it benchmarks as fast as no rounding, though still not quite as fast as the rcp versions.