jsoftware / jsource

J engine source mirror
Other
645 stars 91 forks source link

Use reciprocal for float division with repeated divisor #149

Open moon-chilled opened 1 year ago

moon-chilled commented 1 year ago

Needs newton iterations to get exact result. How many? Research needed (but any reasonable number will still be a win).

mlochbaum commented 1 year ago

If FMA is available, this page gives 2 FMA + 1 mul for any divisor, and 1 FMA + 1 mul for the majority of them (>60% verifiable with a cheap test, >98% actually but hard and slow to test). The page presents the 32-bit case but the paper's general. And of course multiply-by-reciprocal works for exact positive or negative powers of 2.

moon-chilled commented 1 year ago

Oh, wow, thanks! I spent some time looking for prior art on this, but all I could find was itanium-related stuff (they give you an approximate reciprocal with a bunch of crap bits on the bottom, and you need to do ~8 fma on top of that to get a correct result).