[PATCH v3 1/3] math: Make rsqrtf correctly rounded
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Tue Nov 3 20:53:16 GMT 2026
Hi Adhemerval,
This is much faster than the previous version - however it adds a lot of complex
checks, so the rounding mode variant still beats it...
+ uint32_t m = ix << 8;
+
+ /* The expression (1.0 / x) * sqrt (x) fails to return correct rounding
+ for x=0x1.7bf15p-128*2^(2k) and x=0x1.ab6f7ap-125*2^(2k) with k>=0, for
+ rndu (where the returned value is 1 ulp too large). */
+ if (__glibc_unlikely (ix == 0 || ix >= EXPONENT_MASK || ix == 0x2f7e2au
+ || m == 0xbdf8a800u || m == 0x55b7bd00u))
+ {
We can merge one of these checks with the exponent check and use 16 bits for the
other checks (which is accurate enough). The hot path goes from 23 instructions to 18
and TP improves by 8%, latency by 0.5%.
uint32_t m = (ix >> 8) & 65535;
if (__glibc_unlikely ((ix - 0x300000) >= EXPONENT_MASK - 0x300000
|| m == 0xbdf8 || m == 0x55b7))
This gets almost the same performance as my version with the rounding mode check,
except that denormals are ~7% slower - but denormals are rare.
This still passes in all rounding modes of course.
Cheers,
Wilco
More information about the Libc-alpha
mailing list