FDLIBM (freeware LIBM) has a pretty good documented version of sqrt. e_sqrt.c .
There is one version that uses integer arithmetic and a repetition formula that changes one bit at a time.
Another method uses the Newton method. It starts with black magic and a lookup table to get the first 8 bits, and then applies the repeat formula
y_{i+1} = 1/2 * ( y_i + x / y_i)
where x is the number we started from. This is the Babylonian method of the Heron method. It dates back to the Hero of Alexandra in the first century AD.
There is another method called Quick Reverse Square Root or reciproot. which uses some "malicious hacking bit level floating point" to find the value 1 / sqrt (x). i = 0x5f3759df - ( i >> 1 ); It uses binary float representation using mantissa and exponent. If our number x is (1 + m) * 2 ^ e, where m is the mantissa, e is the exponent, and the result is y = 1 / sqrt (x) = (1 + n) * 2 ^ f. Taking magazines
lg(y) = - 1/2 lg(x) f + lg(1+n) = -1/2 e - 1/2 lg(1+m)
So, we see that the exponential part of the result is -1/2 a measure of the number. Black magic basically performs a bitwise bias of the exponent and uses linear approximation on the mantissa.
Once you have a good first approximation, you can use Newton's methods to get a better result, and finally some bit level to correct the last digit.
Salix alba Sep 27 '16 at 5:55 2016-09-27 05:55
source share