Is there any way to get correct rounding with the i387 fsqrt instruction?…
…aside from changing the precision mode in the x87 control word – I know that’s possible, but it’s not a reasonable solution because it has nasty reentrancy-type issues where the precision mode will be wrong if the sqrt operation is interrupted.
The issue I’m dealing with is as follows: the x87 fsqrt opcode performs a correctly-rounded (per IEEE 754) square root operation in the precision of the fpu registers, which I’ll assume is extended (80-bit) precision. However, I want to use it to implement efficient single and double precision square root functions with the results correctly rounded (per the current rounding mode). Since the result has excess precision, the second step of converting the result to single or double precision rounds again, possibly leaving a not-correctly-rounded result.
With some operations it’s possible to work around this with biases. For instance, I can avoid excess precision in the results of addition by adding a bias in the form of a power of two that forces the 52 significant bits of a double precision value into the last 52 bits of the 63-bit extended-precision mantissa. But I don’t see any obvious way to do such a trick with square root.
Any clever ideas?
(Also tagged C because the intended application is implementation of the C sqrt and sqrtf functions.)
First, let’s get the obvious out of the way: you should be using SSE instead of x87. The SSE
sqrtssandsqrtsdinstructions do exactly what you want, are supported on all modern x86 systems, and are significantly faster as well.Now, if you insist on using x87, I’ll start with the good news: you don’t need to do anything for float. You need
2p + 2bits to compute a correctly rounded square-root in a p-bit floating-point format. Because80 > 2*24 + 2, the additional rounding to single-precision will always round correctly, and you have a correctly rounded square root.Now the bad news:
80 < 2*53 + 2, so no such luck for double precision. I can suggest several workarounds; here’s a nice easy one off the top of my head.y = round_to_double(x87_square_root(x));aandbsuch thaty*y = a + bexactly.r = x - a - b.if (r == 0) return yif (r > 0), lety1 = y + 1 ulp, and computea1,b1s.t.y1*y1 = a1 + b1. Comparer1 = x - a1 - b1tor, and return eitheryory1, depending on which has the smaller residual (or the one with zero low-order bit, if the residuals are equal in magnitude).if (r < 0), do the same thing fory1 = y - 1 ulp.This proceedure only handles the default rounding mode; however, in the directed rounding modes, simply rounding to the destination format does the right thing.