Simple Effective Fast Inverse Square Root Algorithm with Two Magic Constants
Total Page:16
File Type:pdf, Size:1020Kb
Oleh Horyachyy, Leonid Moroz, Viktor Otenko / International Journal of Computing, 18(4) 2019, 461-470 Print ISSN 1727-6209 [email protected] On-line ISSN 2312-5381 www.computingonline.net International Journal of Computing SIMPLE EFFECTIVE FAST INVERSE SQUARE ROOT ALGORITHM WITH TWO MAGIC CONSTANTS Oleh Horyachyy, Leonid Moroz, Viktor Otenko Lviv Polytechnic National University, 12 Bandera str., 79013 Lviv, Ukraine, [email protected], [email protected], [email protected], http://lpnu.ua Paper history: Abstract: The purpose of this paper is to introduce a modification of Fast Received 09 January 2019 Inverse Square Root (FISR) approximation algorithm with reduced relative Received in revised form 04 June 2019 Accepted 25 October 2019 errors. The original algorithm uses a magic constant trick with input floating- Available online 31 December 2019 point number to obtain a clever initial approximation and then utilizes the classical iterative Newton-Raphson formula. It was first used in the computer game Quake III Arena, causing widespread discussion among scientists and Keywords: inverse square root; programmers, and now it can be frequently found in many scientific applications, FISR algorithm; although it has some drawbacks. The proposed algorithm has such parameters of initial approximation; the modified inverse square root algorithm that minimize the relative error and magic constant; includes two magic constants in order to avoid one floating-point multiplication. IEEE 754 standard; floating-point arithmetic; In addition, we use the fused multiply-add function and iterative methods of FMA function; higher order in the second iteration to improve the accuracy. Such algorithms do maximum relative error; not require storage of large tables for initial approximation and can be effectively Newton-Raphson; used on field-programmable gate arrays (FPGAs) and other platforms without Householder. hardware support for this function. Copyright © Research Institute for Intelligent Computer Systems, 2019. All rights reserved. 1. INTRODUCTION rule, the initial approximation is formed using a lookup table (LUT – lookup table) [1-2]. However, a When solving many important problems in the group of iterative algorithms is also known that do field of digital signal processing, computer 3D not use LUT [6, 8-15]. Here, the initial graphics, scientific computing, etc., it is necessary to approximation is formed using integer arithmetic in use floating-point arithmetic [1-7] as the most the form of so-called magic constant, and upon accurate and common way of representing real transition from integer arithmetic to floating-point numbers in computers. In particular, a common arithmetic, the resulting approximation is fed into an practical task that arises when working with iterative process using corresponding Newton- floating-point numbers is the calculation of Raphson formulae. elementary functions, including the inverse square In this article, we consider algorithms for root: calculating the inverse square root using magic constants, which offers reduced relative errors and 1 y . (1) applies to floating-point numbers represented in the x IEEE 754 standard format. In addition, an important advantage of the proposed algorithms is a reduction The algorithms implementing the inverse square (by one) in the number of multiplication operations. root calculation are widely described in scientific The best-known version of this algorithm called literature [1, 6, 8-14]. Most of them are iterative and Fast Inverse Square Root (FISR) [8, 15-18], which require the formation of an initial approximation. was used in the computer game Quake III Arena [7], Moreover, the more precise the initial is given below: approximation, the fewer iterations are needed to calculate a function with the required accuracy. As a 1. float InvSqrt1 (float x){ 461 Oleh Horyachyy, Leonid Moroz, Viktor Otenko / International Journal of Computing, 18(4) 2019, 461-470 2. float half = 0.5f * x; which is slightly worse than declared by the authors, 3. int i = *(int*)&x; even when using the fused multiply-add (FMA) 4. i = 0x5F3759DF - (i>>1); function to increase the accuracy of calculations. 5. x = *(float*)&i; Lemaitre et al. [6] suggest using for type float 6. x = x*(1.5f - half*x*x); Householder’s method of order 4 with fused 7. x = x*(1.5f - half*x*x); operations. For initial approximation they consider 8. return x; magic constant R = 0x5f375a86 [8]. Thus, their 9. } proposed algorithm has the form: This InvSqrt1 code, written in the C/C++ 1. float InvSqrt3 (float x){ language, implements a fast algorithm for inverse 2. int i = *(int*)&x; square root calculation. In line 3, we represent the 3. i = 0x5F375A86 - (i>>1); bits of the input variable x (float) as the variable i 4. float y = *(float*)&i; (int). Lines 4 and 5 determine the initial 5. float a = x*y*y; 6. float t = fmaf(0.2734375f, a, -1.40625f); approximation y0 of the inverse square root. Note that the InvSqrt1 algorithm reuses the variable x for 7. t = fmaf(a, t, 2.953125f); 8. t = fmaf(a, t, -3.28125f); this purpose. It then becomes a part of the iterative 9. y = y*fmaf(a, t, 2.4609375f); process. Here in line 4, R = 0x5f3759df is a “magic 10. return y; constant”. In line 5, we represent the bits of the 11. } variable i (int) back into the variable x (float). Note that henceforth in the code, the variable x acts It has maximum relative error as a new approximation to y . Lines 6 and 7 contain two classic successive Newton-Raphson iterations to = 6.58∙10–7, (4) refine the results. If the maximum relative error of 1max calculations after the second iteration is denoted by or 20.54 (out of 24 possible) bits of accuracy. 2max , then the accuracy of this algorithm is only Consequently, there is still a need to develop algorithms of higher accuracy, also for IEEE 754 –6 2max = 4.73∙10 , or log2 ( 2 max ) =17.69 (2) numbers of higher precision, such as double and quadruple, without forgetting about the speed of correct bits. These results are obtained by practical computations. calculations on the Intel Core i7-7700HQ platform The remainder of this paper is organized as using the method that will be discussed in more follows. Section 2 further gives a theoretical detail in Section 3. description of the aforementioned algorithms with Compared to this algorithm, the following magic constant. In Sections 3 and 4, we elucidate the algorithm with a magic constant from [12] has better proposed algorithms and give their implementations accuracy, namely 20.37 correct bits: in C++. Section 5 contains simulation results. Conclusions are given in Section 6. 1. float InvSqrt2 (float x){ 2. float half = 0.5f * x; 2. ANALYTICAL DESCRIPTION OF 3. int i = *(int*)&x; KNOWN ALGORITHMS 4. i = 0x5F376908 - (i>>1); 5. x = *(float*)&i; In this section, we briefly present the main results of [11-12] to explain how InvSqrt1, InvSqrt2, and 6. x = x*(1.5008789f - half*x*x); InvSqrt3 work. Assume that we have a positive 7. x = x*(1.5000006f - half*x*x); floating-point number 8. return x; 9. } Ex x (1 mx ) 2 , (5) It has different magic constant and two modified Newton-Raphson iterations. The maximum relative where error of the algorithm is approximately mx [0,1) (6) –7 2max = 7.37∙10 , (3) Ex log2 (x) floor(log2 (x)) . (7) 462 Oleh Horyachyy, Leonid Moroz, Viktor Otenko / International Journal of Computing, 18(4) 2019, 461-470 In the IEEE 754 standard, the floating-point 35 45 189 t ,t ,t , number x is encoded by 32, 64, or 128 bits for three 1 128 2 32 3 64 basic binary floating-point formats. In this paper, we (16) 105 315 will mostly consider single precision (float type) t4 ,t5 . numbers (as in the above algorithms), which have 32 32 128 bits, but all considerations can also be generalized for other data types, as it will be shown later in As proven in [11-12], in order to know the Section 4. The first bit corresponds to a sign (sign behaviour of the relative error for y0 in the whole field). In our case, this bit is equal to zero. The next range of normalized floating-point numbers, it eight bits correspond to a biased exponent Ex suffices to consider the range x[1,4) . In this range, (exponent field), and the last 23 bits encode a a piecewise linear analytical approximation y0 of fractional part of the mantissa mx (mantissa field). the function consists of three separate parts: The integer encoded by these 32 bits, I , is given by x 1 3 1 y01 x t , x [1,2) (17) I (bias E m )N , (8) 4 4 8 x x x m 1 1 1 y02 x t , x [2,t) (18) where N 223 and bias 127 for float. Line 4 of 8 2 8 m 1 1 1 the code InvSqrt1 or InvSqrt2 and line 3 of InvSqrt3 y03 x t , x [t,4) , (19) can be written as: 16 2 16 where I R I / 2 . (9) y0 x t 2 4mR 2/ Nm (20) The result of subtracting the integer I x /2 from and the fractional part of the mantissa of the magic the magic constant R is the integer number I y , 0 constant R is determined by the formula which is being represented as a float (lines 5 and 4 respectively) and gives the initial (zeroth) piecewise mR R / Nm R / Nm . (21) linear approximation y0 of the function 1/ x . Further on, this approximation is refined (lines 6-7 In this case, the maximum relative error of such and 5-9). The InvSqrt1 and InvSqrt2 algorithms use piecewise linear approximations does not exceed the two classical (in the case of the InvSqrt1 algorithm) value 1/(2Nm ) .