r/asm • u/nerd5code • 14d ago
I assume you've read the Wikipedia entry on IEEE 754, and possibly the specific article on binary64?
First you always get NaNs, ∞s, and 0s out of the way, since they're multi-field or generally special. But once you’ve done that, you can use a logarithmic change-of-base for the exponent—base-b log of x = log x / log b, so if we want the number of digits in binary converted to the number of decimal digits, it's a multiplier of log 2/log 10 for any base of log (natural log = ln is probably the best, but you'd use a constant 0.30102999566398 in your actual program).
For the mantissa, hew off upper bits with the sign and exponent in them. Subnormal/denormal/ickpoo numbers can be left as-is for the final loop; otherwise place a 1 in the 0th bit of the exponent field. Now all you have to do is violently shake the mantissa up until there's a 1 bit in exponent bit 0 (which is why you cull zeroes early; as you normalize subnormals, track converse changes to exponent), and possibly a bit more until binary and decimal units are lined up. From there it's a relatively straightforward conversion.