Next Article in Journal
Recent Progress towards Chemically-Specific Coarse-Grained Simulation Models with Consistent Dynamical Properties
Previous Article in Journal
Design and Implementation of a Microcontroller Based Active Controller for the Synchronization of the Petrzela Chaotic System
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

A Modification of the Fast Inverse Square Root Algorithm

by
Cezary J. Walczyk
1,
Leonid V. Moroz
2 and
Jan L. Cieśliński
1,*
1
Wydział Fizyki, Uniwersytet w Białymstoku, ul. Ciołkowskiego 1L, 15-245 Białystok, Poland
2
Department of Security Information and Technology, Lviv Polytechnic National University, st. Kn. Romana 1/3, 79000 Lviv, Ukraine
*
Author to whom correspondence should be addressed.
Computation 2019, 7(3), 41; https://doi.org/10.3390/computation7030041
Submission received: 28 June 2019 / Revised: 12 August 2019 / Accepted: 14 August 2019 / Published: 18 August 2019
(This article belongs to the Section Computational Engineering)

Abstract

:
We present a new algorithm for the approximate evaluation of the inverse square root for single-precision floating-point numbers. This is a modification of the famous fast inverse square root code. We use the same “magic constant” to compute the seed solution, but then, we apply Newton–Raphson corrections with modified coefficients. As compared to the original fast inverse square root code, the new algorithm is two-times more accurate in the case of one Newton–Raphson correction and almost seven-times more accurate in the case of two corrections. We discuss relative errors within our analytical approach and perform numerical tests of our algorithm for all numbers of the type float.

1. Introduction

Floating-point arithmetic has became widely used in many applications such as 3D graphics, scientific computing and signal processing [1,2,3,4,5], implemented both in hardware and software [6,7,8,9,10]. Many algorithms can be used to approximate elementary functions [1,2,10,11,12,13,14,15,16,17,18]. The inverse square root function ( x 1 / x ) is of particular importance because it is widely used in 3D computer graphics, especially in lightning reflections [19,20,21], and has many other applications; see [22,23,24,25,26,27,28,29,30,31,32,33,34,35,36]. All of these algorithms require an initial seed to start the approximation. The more accurate is the initial seed, the fewer iterations are needed. Usually, the initial seed is obtained from a look-up table (LUT), which is memory consuming.
In this paper, we consider an algorithm for computing the inverse square root using the so-called magic constant instead of an LUT [37,38,39,40]. The zeroth approximation (initial seed) for the inverse square root of a given floating-point number is obtained by a logical right shift by one bit and subtracting this result from an specially-chosen integer (“magic constant”). Both operations are performed on bits of the floating-point number interpreted as an integer. Then, a more accurate value is produced by a certain number (usually one or two) of standard Newton–Raphson iterations. The following code realizes the fast inverse square root algorithm in the case of single-precision IEEE Standard 754 floating-point numbers (type float).
The code InvSqrt (see Algorithm 1) consists of two main parts. Lines 4 and 5 produce in a very inexpensive way a quite good zeroth approximation of the inverse square root of a given positive floating-point number x. Lines 6 and 7 apply the Newton–Raphson corrections twice (often, a version with just one iteration is used, as well). Originally, R was proposed as 0 x 5 F 3759 D F ; see [37,38]. More details, together with a derivation of a better magic constant, are given in Section 2.
Algorithm 1:InvSqrt.
1.float InvSqrt(float x){
2.   float halfnumber = 0.5f*x;
3.   int i = *(int*) &x;
4.   i = R - ( i > > 1 );
5.   y = *(float*) &i;
6.   y = y*(1.5f - halfnumber*y*y);
7.   y = y*(1.5f - halfnumber*y*y);
8.   return y;
9. }
InvSqrt is characterized by a high speed, more that three-times higher than computing the inverse square root using library functions. This property was discussed in detail in [41]. The errors of the fast inverse square root algorithm depend on the choice of the “magic constant” R. In several theoretical papers [38,41,42,43,44] (see also Eberly’s monograph [19]), attempts were made to determine analytically the optimal value of the magic constant (i.e., to minimize errors). In general, this optimal value can depend on the number of iterations, which is a general phenomenon [45]. The derivation and comprehensive mathematical description of all the steps of the fast inverse square root algorithm were given in our recent paper [46]. We found the optimum value of the magic constant by minimizing the final maximum relative error.
In the present paper, we develop our analytical approach to construct an improved algorithm (InvSqrt1) for fast computing of the inverse square root; see Algorithm 2 in Section 4. The proposed modification does not increase the speed of data processing, but increases, in a significant way, the accuracy of the output. In both codes, InvSqrt and InvSqrt1, magic constants serve as a low-cost way of generating a reasonably accurate first approximation of the inverse square root. These magic constants turn out to be the same. The main novelty of the new algorithm is in the second part of the code, which is changed significantly. In fact, we propose a modification of the Newton–Raphson formulae, which has a similar computational cost, but improve the accuracy by several fold.

2. Analytical Approach to the Algorithm InvSqrt

In this paper, we confine ourselves to positive single-precision floating-point numbers (type float). Normal floating-point numbers can be represented as:
x = ( 1 + m x ) 2 e x
where m x [ 0 , 1 ) and e x is an integer (note that this formula does not hold for subnormal numbers). In the case of the IEEE-754 standard, a floating-point number is encoded by 32 bits. The first bit corresponds to a sign (in our case, this bit is simply equal to zero); the next eight bits correspond to an exponent e x ; and the last 23 bits encode a mantissa m x . The integer encoded by these 32 bits, denoted by I x , is given by:
I x = N m ( B + e x + m x )
where N m = 2 23 and B = 127 (thus B + e x = 1 , 2 , , 254 ). Lines 3 and 5 of the InvSqrt code interpret a number as an integer (2) or float (1), respectively. Lines 4, 6, and 7 of the code can be written as:
I y 0 = R I x / 2 , y 1 = 1 2 y 0 ( 3 y 0 2 x ) , y 2 = 1 2 y 1 ( 3 y 1 2 x ) .
The first equation produces, in a surprisingly simple way, a good zeroth approximation y 0 of the inverse square root y = 1 / x . Of course, this needs a very special form of R. In particular, in the single precision case, we have e R = 63 ; see [46]. The next equations can be easily recognized as the Newton–Raphson corrections. We point out that the code InvSqrt is invariant with respect to the scaling:
x x ˜ = 2 2 n x , y k y ˜ k = 2 n y k ( k = 0 , 1 , 2 ) ,
like the equality y = 1 / x itself. Therefore, without loss of the generality, we can confine our analysis to the interval:
A ˜ : = [ 1 , 4 ) .
The tilde will denote quantities defined on this interval. In [46], we showed that the function y ˜ 0 defined by the first equation of (3) can be approximated with a very good accuracy by the piece-wise linear function y ˜ 00 given by:
y ˜ 00 ( x ˜ , t ) = 1 4 x ˜ + 3 4 + 1 8 t for x ˜ [ 1 , 2 ) 1 8 x ˜ + 1 2 + 1 8 t for x ˜ [ 2 , t ) 1 16 x ˜ + 1 2 + 1 16 t for x ˜ [ t , 4 )
where:
t = 2 + 4 m R + 2 N m 1 ,
and m R : = N m 1 R N m 1 R ( m R is the mantissa of the floating-point number corresponding to R). Note that the parameter t, defined by (7), is uniquely determined by R.
The only difference between y 0 produced by the code InvSqrt and y 00 given by (6) is the definition of t, because t related to the code depends (although in a negligible way) on x. Namely,
| y ˜ 00 y ˜ 0 | 1 4 N m 1 = 2 25 2.98 × 10 8 .
Taking into account the invariance (4), we obtain:
y 00 y 0 y 0 2 24 5.96 × 10 8 .
These estimates do not depend on t (in other words, they do not depend on R). The relative error of the zeroth approximation (6) is given by:
δ ˜ 0 ( x ˜ , t ) = x ˜ y ˜ 00 ( x ˜ , t ) 1
This is a continuous function with local maxima at:
x ˜ 0 I = ( 6 + t ) / 6 , x ˜ 0 I I = ( 4 + t ) / 3 , x ˜ 0 I I I = ( 8 + t ) / 3 ,
given respectively by:
δ ˜ 0 ( x ˜ 0 I , t ) = 1 + 1 2 1 + t 6 3 / 2 , δ ˜ 0 ( x ˜ 0 I I , t ) = 1 + 2 1 3 1 + t 4 3 / 2 , δ ˜ 0 ( x ˜ 0 I I I , t ) = 1 + 2 3 1 + t 8 3 / 2 .
In order to study the global extrema of δ ˜ 0 ( x ˜ , t ) , we need also boundary values:
δ ˜ 0 ( 1 , t ) = δ ˜ 0 ( 4 , t ) = 1 8 t 4 , δ ˜ 0 ( 2 , t ) = 2 4 1 + t 2 1 , δ ˜ 0 ( t , t ) = t 2 1 ,
which are, in fact, local minima. Taking into account:
δ ˜ 0 ( 1 , t ) δ ˜ 0 ( t , t ) = 1 8 t 2 2 0 , δ ˜ 0 ( 2 , t ) δ ˜ 0 ( t , t ) = 2 8 t 2 2 0 ,
we conclude that:
min x ˜ A ˜ δ ˜ 0 ( x ˜ , t ) = δ ˜ 0 ( t , t ) < 0 .
Because δ ˜ 0 ( x ˜ 0 I I I , t ) < 0 for t ( 2 , 4 ) , the global maximum is one of the remaining local maxima:
max x ˜ A ˜ δ ˜ 0 ( x ˜ , t ) = max { δ ˜ 0 ( x ˜ 0 I , t ) , δ ˜ 0 ( x ˜ 0 I I , t ) } .
Therefore,
max x A ˜ | δ ˜ 0 ( x ˜ , t ) | = max { | δ ˜ 0 ( t , t ) | , δ ˜ 0 ( x ˜ 0 I , t ) , δ ˜ 0 ( x ˜ 0 I I , t ) } .
In order to minimize this value with respect to t, i.e., to find t 0 r such that:
max x A ˜ | δ ˜ 0 ( x ˜ , t 0 r ) | < max x A ˜ | δ ˜ 0 ( x ˜ , t ) | for t t 0 r ,
we observe that | δ ˜ 0 ( t , t ) | is a decreasing function of t, while both maxima ( δ ˜ 0 ( x ˜ 0 I , t ) and δ ˜ 0 ( x ˜ 0 I I , t ) ) are increasing functions. Therefore, it is sufficient to find t = t 0 I and t = t 0 I I such that:
| δ ˜ 0 ( t 0 I , t 0 I ) | = δ ˜ 0 ( x ˜ 0 I , t 0 I ) , | δ ˜ 0 ( t 0 I I , t 0 I I ) | = δ ˜ 0 ( x ˜ 0 I I , t 0 I I ) ,
and to choose the greater of these two values. In [46], we showed that:
| δ ˜ 0 ( t 0 I , t 0 I ) | < | δ ˜ 0 ( t 0 I I , t 0 I I ) | .
Therefore, t 0 r = t 0 I I , and:
δ ˜ 0 max : = min t ( 2 , 4 ) max x A ˜ | δ ˜ 0 ( x ˜ , t ) | = | δ ˜ 0 ( t 0 r , t 0 r ) | .
The following numerical values result from these calculations [46]:
t 0 r 3.7309796 , R 0 = 0 x 5 F 37642 F , δ ˜ 0 max 0.03421281 .
Newton–Raphson corrections for the zeroth approximation ( y ˜ 00 ) will be denoted by y ˜ 0 k ( k = 1 , 2 , ). In particular, we have:
y ˜ 01 ( x ˜ , t ) = 1 2 y ˜ 00 ( x ˜ , t ) ( 3 y ˜ 00 2 ( x ˜ , t ) x ˜ ) , y ˜ 02 ( x ˜ , t ) = 1 2 y ˜ 01 ( x ˜ , t ) ( 3 y ˜ 01 2 ( x ˜ , t ) x ˜ ) .
and the corresponding relative error functions will be denoted by δ ˜ k ( x ˜ , t ) :
δ ˜ k ( x ˜ , t ) : = y ˜ 0 k ( x ˜ , t ) y ˜ y ˜ = x ˜ y ˜ 0 k ( x ˜ , t ) 1 , ( k = 0 , 1 , 2 , ) ,
where we included also the case k = 0 ; see (10). The obtained approximations of the inverse square root depend on the parameter t directly related to the magic constant R. The value of this parameter can be estimated by analyzing the relative error of y ˜ 0 k ( x ˜ , t ) with respect to 1 / x ˜ . As the best estimation, we consider t = t k ( r ) minimizing the relative error δ ˜ k ( x ˜ , t ) :
t t k ( r ) δ ˜ k max max x ˜ A ˜ | δ ˜ k ( x ˜ , t k ( r ) ) | < max x ˜ A ˜ | δ ˜ k ( x ˜ , t ) | .
We point out that in general, the optimum value of the magic constant can depend on the number of Newton–Raphson corrections. Calculations carried out in [46] gave the following results:
t 1 r = t 2 r = 3.7298003 , R 1 r = R 2 r = 0 x 5 F 375 A 86 , δ ˜ 1 max 1.75118 × 10 3 , δ ˜ 2 max 4.60 × 10 6 .
We omit the details of the computations except one important point. Using (24) to express y ˜ 0 k by δ ˜ k and x ˜ , we can rewrite (23) as:
δ ˜ k ( x ˜ , t ) = 1 2 δ ˜ k 1 2 ( x ˜ , t ) ( 3 + δ ˜ k 1 ( x ˜ , t ) ) , ( k = 1 , 2 , ) .
The quadratic dependence on δ ˜ k 1 means that every Newton–Raphson correction improves the accuracy by several orders of magnitude (until the machine precision is reached); compare (26).
The Formula (27) suggests another way of improving the accuracy because the functions δ ˜ k are always non-positive for any k 1 . Roughly speaking, we are going to shift the graph of δ ˜ k upwards by an appropriate modification of the Newton–Raphson formula. In the next section, we describe the general idea of this modification.

3. Modified Newton–Raphson Formulas

The Formula (27) shows that errors introduced by Newton–Raphson corrections are nonpositive, i.e., they take values in intervals [ δ ˜ k max , 0 ] , where k = 1 , 2 , . Therefore, it is natural to introduce a correction term into the Newton–Raphson formulas (23). We expect that the corrections will be roughly half of the maximal relative error. Instead of the maximal error, we introduce two parameters, d 1 and d 2 . Thus, we get modified Newton–Raphson formulas:
y ˜ 11 ( x ˜ , t , d 1 ) = 2 1 y ˜ 00 ( x ˜ , t ) ( 3 y ˜ 00 2 ( x ˜ , t ) x ˜ ) + d 1 2 x ˜ , y ˜ 12 ( x ˜ , t , d 1 , d 2 ) = 2 1 y ˜ 11 ( x ˜ , t , d 1 ) ( 3 y ˜ 11 2 ( x ˜ , t , d 1 ) x ˜ ) + d 2 2 x ˜ ,
where zeroth approximation is assumed in the form (6). In the following section, the term 1 / x ˜ will be replaced by some approximations of y ˜ , transforming (28) into a computer code. In order to estimate a possible gain in accuracy, in this section, we temporarily assume that y ˜ is the exact value of the inverse square root. The corresponding error functions,
δ ˜ k ( x ˜ , t , d 1 , , d k ) = x ˜ y ˜ 1 k ( x ˜ , t , d 1 , , d k ) 1 , k { 0 , 1 , 2 , } ,
(where y ˜ 10 ( x ˜ , t ) : = y ˜ 00 ( x ˜ , t ) ), satisfy:
δ ˜ k = 1 2 δ ˜ k 1 2 ( 3 + δ ˜ k 1 ) + d k 2 ,
where: δ ˜ 0 ( x ˜ , t ) = δ ˜ 0 ( x ˜ , t ) . Note that:
δ ˜ 1 ( x ˜ , t , d 1 ) = δ ˜ 1 ( x ˜ , t ) + 1 2 d 1 .
In order to simplify notation, we usually will suppress the explicit dependence on d j . We will write, for instance, δ ˜ 2 ( x ˜ , t ) instead of δ ˜ 2 ( x ˜ , t , d 1 , d 2 ) .
The corrections of the form (28) will decrease relative errors in comparison with the results of earlier papers [38,46]. We have three free parameters ( d 1 , d 2 , and t) to be determined by minimizing the maximal error (in principle, the new parameterization can give a new estimation of the parameter t). By analogy to (25), we are going to find t = t ( 0 ) minimizing the error of the first correction (25):
t t ( 0 ) max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t ( 0 ) ) | < max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t ) | ,
where, as usual, A ˜ = [ 1 , 4 ] .
The first of Equation (30) implies that for any t, the maximal value of δ ˜ 1 ( x ˜ , t ) equals 1 2 d 1 and is attained at zeros of δ ˜ 0 ( x ˜ , t ) . Using the results of Section 2, including (15), (16), (20), and (21), we conclude that the minimum value of δ ˜ 1 ( x ˜ , t ) is attained either for x ˜ = t or for x ˜ = x 0 I I (where there is the second maximum of δ ˜ 0 ( x ˜ , t ) ), i.e.,
min x ˜ A ˜ δ ˜ 1 ( x ˜ , t ) = min δ ˜ 1 ( t , t ) , δ ˜ 1 ( x 0 I I , t )
Minimization of | δ ˜ 1 ( x ˜ , t ) | can be done with respect to t and with respect to d 1 (these operations obviously commute), which corresponds to:
max x ˜ A ˜ δ ˜ 1 ( x ˜ , t ( 0 ) ) δ ˜ 1 max = min x ˜ A ˜ δ ˜ 1 ( x ˜ , t ( 0 ) ) .
Taking into account:
max x ˜ A ˜ δ ˜ 1 ( x ˜ , t ( 0 ) ) = d 1 2 , min x ˜ A ˜ δ ˜ 1 ( x ˜ , t ( 0 ) ) = δ ˜ 1 ( t ( 0 ) , t ( 0 ) ) = δ ˜ 1 max + d 1 2 ,
we get from (34):
δ ˜ 1 max = 1 2 d 1 = 1 2 δ ˜ 1 max 8.7559 × 10 4 ,
where:
δ ˜ 1 max : = min t ( 2 , 4 ) max x A ˜ | δ ˜ 1 ( x ˜ , t ) | .
and the numerical value of δ ˜ 1 max is given by (26). These conditions are satisfied for:
t ( 0 ) = t 1 ( r ) 3.7298003 .
In order to minimize the relative error of the second correction, we use equation analogous to (34):
max x ˜ A ˜ δ ˜ 2 ( x ˜ , t ( 0 ) ) δ ˜ 2 max = min x ˜ A ˜ δ ˜ 2 ( x ˜ , t ( 0 ) ) ,
where from (30), we have:
max x ˜ A ˜ δ ˜ 2 ( x ˜ , t ( 0 ) ) = d 2 2 , min x ˜ A ˜ δ ˜ 2 ( x ˜ , t ( 0 ) ) = 1 2 δ ˜ 1 max 2 3 + δ ˜ 1 max + d 2 2 .
Hence:
δ ˜ 2 max = 1 4 δ ˜ 1 max 2 3 + δ ˜ 1 max .
Expressing this result in terms of formerly computed δ ˜ 1 max and δ ˜ 2 max , we obtain:
δ ˜ 2 max = 1 8 δ ˜ 2 max + 3 32 δ ˜ 1 max 3 5.75164 × 10 7 δ ˜ 2 max 7.99 ,
where:
δ ˜ 2 max = 1 2 δ ˜ 1 max 2 ( 3 δ ˜ 1 max ) .
Therefore, the above modification of Newton–Raphson formulas decreases the relative error two times after one iteration and almost eight times after two iterations as compared to the standard InvSqrt algorithm.
In order to implement this idea in the form of a computer code, we have to replace the unknown 1 / x ˜ (i.e., y ˜ ) on the right-hand sides of (28) by some numerical approximations.

4. New Algorithm with Higher Accuracy

Approximating 1 / x ˜ in Formulas (28) by values on the left-hand sides, we transform (28) into:
y ˜ 21 = 1 2 y ˜ 20 ( 3 y ˜ 20 2 x ˜ ) + 1 2 d 1 y ˜ 21 , y ˜ 22 = 1 2 y ˜ 21 ( 3 y ˜ 21 2 x ˜ ) + 1 2 d 2 y ˜ 22 ,
where y ˜ 2 k ( k = 1 , 2 , ) depend on x ˜ , t and d j (for 1 j k ). We assume y ˜ 20 y ˜ 00 , i.e., the zeroth approximation is still given by (6). We can see that y ˜ 21 and y ˜ 22 can be explicitly expressed by y ˜ 20 and y ˜ 21 , respectively.
Parameters d 1 and d 2 have to be determined by minimization of the maximum error. We define error functions in the usual way:
Δ k ( 1 ) = y ˜ 2 k y ˜ y ˜ = x ˜ y ˜ 2 k 1 .
Substituting (44) into (43), we get:
Δ 1 ( 1 ) ( x ˜ , t , d 1 ) = d 1 2 d 1 1 2 d 1 δ ˜ 0 2 ( x ˜ , t ) ( 3 + δ ˜ 0 ( x ˜ , t ) ) = d 1 + 2 δ ˜ 1 ( x ˜ , t ) 2 d 1 ,
Δ 2 ( 1 ) ( x ˜ , t , d , d 2 ) = d 2 2 d 2 1 2 d 2 Δ 1 ( 1 ) ( x ˜ , t , d 1 ) 2 3 + Δ 1 ( 1 ) ( x ˜ , t , d 1 ) .
The equation (45) expresses Δ 1 ( 1 ) ( x ˜ , t , d 1 ) as a linear function of the nonpositive function δ ˜ 1 ( x ˜ , t ) with coefficients depending on the parameter d 1 . The optimum parameters t and d 1 will be estimated by the procedure described in Section 3. First, we minimize the amplitude of the relative error function, i.e., we find t ( 1 ) such that:
max x ˜ A ˜ Δ 1 ( 1 ) ( x ˜ , t ( 1 ) ) min x ˜ A ˜ Δ 1 ( 1 ) ( x ˜ , t ( 1 ) ) max x ˜ A ˜ Δ 1 ( 1 ) ( x ˜ , t ) min x ˜ A ˜ Δ 1 ( 1 ) ( x ˜ , t )
for all t t ( 1 ) . Second, we determine d 1 ( 1 ) such that:
max x ˜ A ˜ Δ 1 ( 1 ) ( x ˜ , t ( 1 ) , d 1 ( 1 ) ) = min x ˜ A ˜ Δ 1 ( 1 ) ( x ˜ , t ( 1 ) , d 1 ( 1 ) ) .
Thus, we have:
max x ˜ A ˜ | Δ 1 ( 1 ) ( x ˜ , t ( 1 ) , d 1 ( 1 ) ) | max x ˜ A ˜ | Δ 1 ( 1 ) ( x ˜ , t , d 1 ) |
for all real d 1 and t ( 2 , 4 ) . Δ 1 ( 1 ) ( x ˜ , t ) is an increasing function of δ ˜ 1 ( x ˜ , t ) ; hence:
d 1 ( 1 ) 2 max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t 1 ( 1 ) ) | 2 d 1 ( 1 ) = d 1 ( 1 ) 2 d 1 ( 1 ) ,
which is satisfied for:
d 1 ( 1 ) = max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t 1 ( 1 ) ) | = δ ˜ 1 max .
Thus, we can find the maximum error of the first correction Δ 1 ( 1 ) ( x ˜ , t 1 ( 1 ) ) (presented in Figure 1):
max x ˜ A ˜ | Δ 1 ( 1 ) ( x ˜ , t ( 1 ) ) | = max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t ( 1 ) ) | 2 max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t ( 1 ) ) | ,
which assumes the minimum value for t ( 1 ) = t 1 ( r ) :
Δ 1 max ( 1 ) = max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t 1 ( r ) ) | 2 max x ˜ A ˜ | δ ˜ 1 ( x ˜ , t 1 ( r ) ) | = δ ˜ 1 max 2 δ ˜ 1 max 8.7636 × 10 4 δ ˜ 1 max 2.00 .
This result practically coincides with δ ˜ 1 max given by (36).
Analogously, we can determine the value of d 2 ( 1 ) (assuming that t = t ( 1 ) is fixed):
d 2 ( 1 ) max x ˜ A ˜ | Δ 1 ( 1 ) 2 ( x ˜ , t ( 1 ) ) ( 3 + Δ 1 ( 1 ) ( x ˜ , t ( 1 ) ) ) | 2 d 2 ( 1 ) = d 2 ( 1 ) 2 d 2 ( 1 ) .
Now, the deepest minimum comes from the global maximum:
max x ˜ A ˜ | Δ 1 ( 1 ) 2 ( x ˜ , t ( 1 ) ) ( 3 + Δ 1 ( 1 ) ( x ˜ , t ( 1 ) ) ) | = 2 δ ˜ 1 max 2 ( 3 δ ˜ 1 max ) ( 2 δ ˜ 1 max ) 3 .
Therefore, we get:
d 2 ( 1 ) = δ ˜ 1 max 2 ( 3 δ ˜ 1 max ) ( 2 δ ˜ 1 max ) 3 1.15234 × 10 6 ,
and the maximum error of the second correction is given by:
Δ 2 max ( 1 ) = d 2 ( 1 ) 2 d 2 ( 1 ) 5.76173 × 10 7 δ ˜ 2 max 7.98 ,
which is very close to the value of δ ˜ 2 max given by (42).
Thus, we have obtained the algorithm InvSqrt1, see Algorithm 2, which looks like InvSqrt with modified values of the numerical coefficients.
Algorithm 2:InvSqrt1.
1.float InvSqrt1(float x){
2.   float simhalfnumber = 0.500438180f*x;
3.   int i = *(int*) &x;
4.   i = 0x5F375A86 - ( i > > 1 );
5.   y = *(float*) &i;
6.   y = y*(1.50131454f - simhalfnumber*y*y);
7.   y = y*(1.50000086f - 0.999124984f*simhalfnumber*y*y);
8.   return y;
9. }
Comparing I n v S q r t 1 with I n v S q r t , we easily see that the number of algebraic operations in I n v S q r t 1 is greater by one (an additional multiplication in Line 7, corresponding to the second iteration of the modified Newton–Raphson procedure). We point out that the magic constants for InvSqrt and InvSqrt1 are the same.

5. Numerical Experiments

The new algorithm I n v S q r t 1 was tested on the processor Intel Core i5-3470 using the compiler TDM-GCC 4.9.2 32-bit. Using the same hardware for testing the code I n v S q r t , we obtained practically the same values of errors as those obtained by Lomont [38]. The same results were obtained also on Intel i7-5700. In this section, we analyze the rounding errors for the code InvSqrt 1 .
Applying algorithm I n v S q r t 1 , we obtain relative errors InvSqrt 1 ( x ˜ ) characterized by “oscillations” with a center slightly shifted with respect to the analytical approximate solution y ˜ 22 ( x ˜ , t ( 1 ) ) ; see Figure 2. The observed blur can be expressed by a relative deviation of the numerical result from y ˜ 22 ( x ˜ ) :
ε ( 1 ) ( x ˜ ) = InvSqrt 1 ( x ˜ ) y ˜ 22 ( x ˜ , t ( 1 ) ) y ˜ 22 ( x ˜ , t ( 1 ) ) .
The values of this error are distributed symmetrically around the mean value ε ( 1 ) :
ε ( 1 ) = 2 1 N m 1 x [ 1 , 4 ) ε ( 1 ) ( x ˜ ) = 1.398 × 10 8
enclosing the range:
ε ( 1 ) ( x ˜ ) [ 9.676 × 10 8 , 6.805 × 10 8 ] ,
see Figure 3. The blur parameters of the function ε ( 1 ) ( x ˜ , t ) show that the main source of the difference between analytical and numerical results is the use of precision float and, in particular, rounding of constant parameters of the function InvSqrt 1 . We point out that in this case, the amplitude of the error oscillations is about 40 % greater than the amplitude of oscillations of ( y ˜ 00 y ˜ 0 ) / y ˜ 0 (i.e., in the case of I n v S q r t ); see the right part of Figure 2 in [46]. It is worth noting that for the first Newton–Raphson correction, the blur is of the same order, but due to a much higher error value in this case, its effect is negligible (i.e., Figure 1 would be practically the same with or without the blur). The maximum numerical errors practically coincide with the analytical result (53), i.e.,
Δ 1 , N min ( 1 ) 8.76 × 10 4 , Δ 1 , N max ( 1 ) 8.76 × 10 4 .
In the case of the second Newton–Raphson correction, we compared results produced by I n v S q r t 1 with exact values of the inverse square root for all numbers x of the type float such that e x [ 126 , 128 ) . The range of errors was the same for all these intervals (except e x = 126 ):
Δ 2 ; N ( 1 ) ( x ) = sqrt ( x ) InvSqrt 1 ( x ) 1 . ( 6.62 × 10 7 , 6.35 × 10 7 ) .
For e x = 126 , the interval of errors was slightly wider: ( 6.72 × 10 7 , 6.49 × 10 7 ) , which can be explained by the fact that the analysis presented in this paper is not applicable to subnormal numbers; see (1). Therefore, our tests showed that relative errors for all numbers of the type float belong to the interval ( Δ 2 , N min ( 1 ) , Δ 2 , N max ( 1 ) ) , where:
Δ 2 , N min ( 1 ) 6.72 × 10 7 , Δ 2 , N max ( 1 ) 6.49 × 10 7 .
These values are significantly higher than the analytical result ( 5.76 × 10 7 ) (see (57)), but are still much lower than the analogous error for the algorithm I n v S q r t ( 4.60 × 10 6 ; see [46]).

6. Conclusions

In this paper, we presented a modification of the famous code InvSqrt for fast computation of the inverse square root of single-precision floating-point numbers. The new code had the same magic constant, but the second part (which consisted of Newton–Raphson iterations) was modified. In the case of one Newton–Raphson iteration, the new code InvSqrt1 had the same computational cost as InvSqrt and was two-times more accurate. In the case of two iterations, the computational cost of the new code was only slightly higher, but its accuracy was higher by almost seven times.
The main idea of our work consisted of modifying coefficients in the Newton–Raphson method and demanding that the maximal error be as small as possible. Such modifications can be constructed if the distribution of errors for Newton–Raphson corrections is not symmetric (like in the case of the inverse square root, when they are non-positive functions).

Author Contributions

Conceptualization, L.V.M.; formal analysis, C.J.W.; investigation, C.J.W., L.V.M., and J.L.C.; methodology, C.J.W. and L.V.M.; visualization, C.J.W.; writing, original draft, J.L.C.; writing, review and editing, J.L.C.

Funding

This research received no external funding.

Conflicts of Interest

The authors declare no conflict of interest.

References

  1. Ercegovac, M.D.; Lang, T. Digital Arithmetic; Morgan Kaufmann: Burlington, MA, USA, 2003. [Google Scholar]
  2. Parhami, B. Computer Arithmetic: Algorithms and Hardware Designs, 2nd ed.; Oxford University Press: New York, NY, USA, 2010. [Google Scholar]
  3. Diefendorff, K.; Dubey, P.K.; Hochsprung, R.; Scales, H. AltiVec extension to PowerPC accelerates media processing. IEEE Micro 2000, 20, 85–95. [Google Scholar] [CrossRef]
  4. Harris, D. A Powering Unit for an OpenGL Lighting Engine. In Proceedings of the 35th Asilomar Conference on Singals, Systems, and Computers, Pacific Grove, CA, USA, 4–7 November 2001; pp. 1641–1645. [Google Scholar]
  5. Sadeghian, M.; Stine, J. Optimized Low-Power Elementary Function Approximation for Chybyshev series Approximation. In Proceedings of the 46th Asilomar Conference on Signal Systems and Computers, Pacific Grove, CA, USA, 4–7 November 2012. [Google Scholar]
  6. Russinoff, D.M. A Mechanically Checked Proof of Correctness of the AMD K5 Floating Point Square Root Microcode. Form. Methods Syst. Des. 1999, 14, 75–125. [Google Scholar] [CrossRef]
  7. Cornea, M.; Anderson, C.; Tsen, C. Software Implementation of the IEEE 754R Decimal Floating-Point Arithmetic. In Software and Data Technologies (Communications in Computer and Information Science); Springer: Berlin/Heidelberg, Germany, 2008; Volume 10, pp. 97–109. [Google Scholar]
  8. Muller, J.-M.; Brisebarre, N.; Dinechin, F.; Jeannerod, C.-P.; Lefèvre, V.; Melquiond, G.; Revol, N.; Stehlé, D.; Torres, S. Hardware Implementation of Floating-Point Arithmetic. In Handbook of Floating-Point Arithmetic; Birkhäuser: Basel, Switzerland, 2010; pp. 269–320. [Google Scholar]
  9. Muller, J.-M.; Brisebarre, N.; Dinechin, F.; Jeannerod, C.-P.; Lefèvre, V.; Melquiond, G.; Revol, N.; Stehlé, D.; Torres, S. Software Implementation of Floating-Point Arithmetic. In Handbook of Floating-Point Arithmetic; Birkhäuser: Basel, Switzerland, 2010; pp. 321–372. [Google Scholar]
  10. Viitanen, T.; Jääskeläinen, P.; Esko, O.; Takala, J. Simplified floating-point division and square root. In Proceedings of the IEEE International Conference on Acoustics Speech and Signal Process, Vancouver, BC, Canada, 26–31 May 2013; pp. 2707–2711. [Google Scholar]
  11. Ercegovac, M.D.; Lang, T. Division and Square Root: Digit Recurrence Algorithms and Implementations; Kluwer Academic Publishers: Boston, MA, USA, 1994. [Google Scholar]
  12. Liu, W.; Nannarelli, A. Power Efficient Division and Square root Unit. IEEE Trans. Comp. 2012, 61, 1059–1070. [Google Scholar] [CrossRef]
  13. Deng, L.X.; An, J.S. A low latency High-throughput Elementary Function Generator based on Enhanced double rotation CORDIC. In Proceedings of the IEEE Symposium on Computer Applications and Communications (SCAC), Weihai, China, 26–27 July 2014. [Google Scholar]
  14. Nguyen, M.X.; Dinh-Duc, A. Hardware-Based Algorithm for Sine and Cosine Computations using Fixed Point Processor. In Proceedings of the 11th International Conference on Electrical Engineering/Electronics Computer, Telecommuncations and Information Technology, Nakhon Ratchasima, Thailand, 14–17 May 2014. [Google Scholar]
  15. Cornea, M. Intel® AVX-512 Instructions and Their Use in the Implementation of Math Functions; Intel Corporation, 2015. Available online: http://arith22.gforge.inria.fr/slides/s1-cornea.pdf (accessed on 27 June 2019).
  16. Jiang, H.; Graillat, S.; Barrio, R.; Yang, C. Accurate, validated and fast evaluation of elementary symmetric functions and its application. Appl. Math. Comput. 2016, 273, 1160–1178. [Google Scholar] [CrossRef]
  17. Fog, A. Software Optimization Resources, Instruction Tables: Lists of Instruction Latencies, Throughputs and Micro-Operation Breakdowns for Intel, AMD and VIA CPUs. Available online: http://www.agner.org/optimize/ (accessed on 27 June 2019).
  18. Moroz, L.; Samotyy, W. Efficient floating-point division for digital signal processing application. IEEE Signal Process. Mag. 2019, 36, 159–163. [Google Scholar] [CrossRef]
  19. Eberly, D.H. GPGPU Programming for Games and Science; CRC Press: Boca Raton, FL, USA, 2015. [Google Scholar]
  20. Ide, N.; Hirano, M.; Endo, Y.; Yoshioka, S.; Murakami, H.; Kunimatsu, A.; Sato, T.; Kamei, T.; Okada, T.; Suzuoki, M. 2.44-GFLOPS 300-MHz Floating-Point Vector-Processing Unit for High-Performance 3D Graphics Computing. IEEE J. Solid-State Circuits 2000, 35, 1025–1033. [Google Scholar] [CrossRef]
  21. Oberman, S.; Favor, G.; Weber, F. AMD 3DNow! technology: Architecture and implementations. IEEE Micro 1999, 19, 37–48. [Google Scholar] [CrossRef]
  22. Kwon, T.J.; Draper, J. Floating-point Division and Square root Implementation using a Taylor-Series Expansion Algorithm with Reduced Look-Up Table. In Proceedings of the 51st Midwest Symposium on Circuits and Systems, Knoxville, TN, USA, 10–13 August 2008. [Google Scholar]
  23. Hands, T.O.; Griffiths, I.; Marshall, D.A.; Douglas, G. The fast inverse square root in scientific computing. J. Phys. Spec. Top. 2011, 10, A2-1. [Google Scholar]
  24. Blinn, J. Floating-point tricks. IEEE Comput. Graph. Appl. 1997, 17, 80–84. [Google Scholar] [CrossRef]
  25. Janhunen, J. Programmable MIMO Detectors. Ph.D. Thesis, University of Oulu, Tampere, Finland, 2011. [Google Scholar]
  26. Stanislaus, J.L.V.M.; Mohsenin, T. High Performance Compressive Sensing Reconstruction Hardware with QRD Process. In Proceedings of the IEEE International Symposium on Circuits and Systems (ISCAS’12), Seoul, Korea, 20–23 May 2012. [Google Scholar]
  27. Avril, Q.; Gouranton, V.; Arnaldi, B. Fast Collision Culling in Large-Scale Environments Using GPU Mapping Function; ACM Eurographics Parallel Graphics and Visualization: Cagliari, Italy, 2012. [Google Scholar]
  28. Schattschneider, R. Accurate High-resolution 3D Surface Reconstruction and Localisation Using a Wide-Angle Flat Port Underwater Stereo Camera. Ph.D. Thesis, University of Canterbury, Christchurch, New Zealand, 2014. [Google Scholar]
  29. Zafar, S.; Adapa, R. Hardware architecture design and mapping of “Fast Inverse Square Root’s algorithm”. In Proceedings of the International Conference on Advances in Electrical Engineering (ICAEE), Tamilnadu, India, 9–11 January 2014; pp. 1–4. [Google Scholar]
  30. Hänninen, T.; Janhunen, J.; Juntti, M. Novel detector implementations for 3G LTE downlink and uplink. Analog. Integr. Circ. Sig. Process. 2014, 78, 645–655. [Google Scholar] [CrossRef]
  31. Li, Z.Q.; Chen, Y.; Zeng, X.Y. OFDM Synchronization implementation based on Chisel platform for 5G research. In Proceedings of the 2015 IEEE 11th International Conference on ASIC (ASICON), Chengdu, China, 3–6 November 2015. [Google Scholar]
  32. Hsu, C.J.; Chen, J.L.; Chen, L.G. An Efficient Hardware Implementation of HON4D Feature Extraction for Real-time Action Recognition. In Proceedings of the 2015 IEEE International Symposium on Consumer Electronics (ISCE), Madrid, Spain, 24–26 June 2015. [Google Scholar]
  33. Hsieh, C.H.; Chiu, Y.F.; Shen, Y.H.; Chu, T.S.; Huang, Y.H. A UWB Radar Signal Processing Platform for Real-Time Human Respiratory Feature Extraction Based on Four-Segment Linear Waveform Model. IEEE Trans. Biomed. Circ. Syst. 2016, 10, 219–230. [Google Scholar] [CrossRef] [PubMed]
  34. Lv, J.D.; Wang, F.; Ma, Z.H. Peach Fruit Recognition Method under Natural Environment. In Eighth International Conference on Digital Image Processing (ICDIP 2016), Chengdu, China, 20–22 May 2016; Falco, C.M., Jaing, X.D., Eds.; SPIE: Bellingham, WA, USA, 2016; Volume 10033, p. 1003317. [Google Scholar]
  35. Sangeetha, D.; Deepa, P. Efficient Scale Invariant Human Detection using Histogram of Oriented Gradients for IoT Services. In Proceedings of the 2017 30th International Conference on VLSI Design and 2017 16th International Conference on Embedded Systems, Hyderabad, India, 7–11 January 2017; pp. 61–66. [Google Scholar]
  36. Lin, J.; Xu, Z.G.; Nukada, A.; Maruyama, N.; Matsuoka, S. Optimizations of Two Compute-bound Scientific Kernels on the SW26010 Many-core Processor. In Proceedings of the 46th International Conference on Parallel Processing, Bristol, UK, 14–17 August 2017; pp. 432–441. [Google Scholar]
  37. Quake III Arena, id Software 1999. Available online: https://github.com/id-Software/Quake-III-Arena/blob/master/code/game/q_math.c#L552 (accessed on 27 June 2019).
  38. Lomont, C. Fast Inverse Square Root, Purdue University. Tech. Rep.. 2003. Available online: http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf (accessed on 27 June 2019).
  39. Warren, H.S. Hacker’s Delight, 2nd ed.; Pearson Education: London, UK, 2013. [Google Scholar]
  40. Martin, P. Eight Rooty Pieces. Overload J. 2016, 135, 8–12. [Google Scholar]
  41. Robertson, M. A Brief History of InvSqrt. Bachelor’s Thesis, University of New Brunswick, Fredericton, NB, Canada, 2012. [Google Scholar]
  42. Self, B. Efficiently Computing the Inverse Square Root Using Integer Operations. Available online: https://sites.math.washington.edu/~morrow/336_12/papers/ben.pdf (accessed on 27 June 2019).
  43. McEniry, C. The Mathematics Behind the Fast Inverse Square Root Function Code. Tech. Rep.. August 2007. Available online: https://web.archive.org/web/20150511044204/http://www.daxia.com/bibis/upload/406Fast_Inverse_Square_Root.pdf (accessed on 27 June 2019).
  44. Eberly, D. An Approximation for the Inverse Square Root Function. 2015. Available online: http://www.geometrictools.com/Documentation/ApproxInvSqrt.pdf (accessed on 27 June 2019).
  45. Kornerup, P.; Muller, J.-M. Choosing starting values for certain Newton–Raphson iterations. Theor. Comp. Sci. 2006, 351, 101–110. [Google Scholar] [CrossRef]
  46. Moroz, L.; Walczyk, C.J.; Hrynchyshyn, A.; Holimath, V.; Cieśliński, J.L. Fast calculation of inverse square root with the use of magic constant—Analytical approach. Appl. Math. Comput. 2018, 316, 245–255. [Google Scholar] [CrossRef]
Figure 1. Graph of the function Δ 1 ( 1 ) ( x ˜ , t ( 1 ) ) .
Figure 1. Graph of the function Δ 1 ( 1 ) ( x ˜ , t ( 1 ) ) .
Computation 07 00041 g001
Figure 2. Solid lines represent function Δ 2 ( 1 ) ( x ˜ , t ( 1 ) ) . Its vertical shifts by ± 6 × 10 8 are denoted by dashed lines. Finally, dots represent relative errors for 4000 random values x ( 2 126 , 2 128 ) produced by algorithm I n v S q r t 1 .
Figure 2. Solid lines represent function Δ 2 ( 1 ) ( x ˜ , t ( 1 ) ) . Its vertical shifts by ± 6 × 10 8 are denoted by dashed lines. Finally, dots represent relative errors for 4000 random values x ( 2 126 , 2 128 ) produced by algorithm I n v S q r t 1 .
Computation 07 00041 g002
Figure 3. Relative error ε ( 1 ) arising during the float approximation of corrections y ˜ 22 ( x ˜ , t ) . Dots represent errors determined for 2000 random values x ˜ [ 1 , 4 ) . Solid lines represent maximum ( max i ) and minimum ( min i ) values of relative errors (intervals [ 1 , 2 ) and [ 2 , 4 ) were divided into 64 equal intervals, and then, extremum values were determined in all these intervals).
Figure 3. Relative error ε ( 1 ) arising during the float approximation of corrections y ˜ 22 ( x ˜ , t ) . Dots represent errors determined for 2000 random values x ˜ [ 1 , 4 ) . Solid lines represent maximum ( max i ) and minimum ( min i ) values of relative errors (intervals [ 1 , 2 ) and [ 2 , 4 ) were divided into 64 equal intervals, and then, extremum values were determined in all these intervals).
Computation 07 00041 g003

Share and Cite

MDPI and ACS Style

Walczyk, C.J.; Moroz, L.V.; Cieśliński, J.L. A Modification of the Fast Inverse Square Root Algorithm. Computation 2019, 7, 41. https://doi.org/10.3390/computation7030041

AMA Style

Walczyk CJ, Moroz LV, Cieśliński JL. A Modification of the Fast Inverse Square Root Algorithm. Computation. 2019; 7(3):41. https://doi.org/10.3390/computation7030041

Chicago/Turabian Style

Walczyk, Cezary J., Leonid V. Moroz, and Jan L. Cieśliński. 2019. "A Modification of the Fast Inverse Square Root Algorithm" Computation 7, no. 3: 41. https://doi.org/10.3390/computation7030041

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop