Square Rooting Algorithms For Integer and Floating-Point Numbers
Square Rooting Algorithms For Integer and Floating-Point Numbers
I. INTRODUCTION
OMPUTATION of square root is a basic and essential
operation in many areas of science and engineering. It
may be rated, in importance, next to the four basic arithmetic
operations: addition, subtraction, multiplication, and division. Some typical applications of square root are in: complex
variables, trigonometry, solution of quadratic equations, twodimensional modeling, error computation, statistics, graphics,
image processing, and many others. Computation of gradients
in edge detection process or geometrical distances between
pixels in an image are obvious examples in image processing
that require frequent use of square rooting. Also in signal processing applications such as adaptive filtering, square root is
predominantly used [11. In fact, in some of the latest developments in digital signal processors the square root is included in
the normal instruction set instead of being a macroinstruction
PI.
Probably the most widely used method of evaluating the
square root of a number is the Newton-Raphson technique.
This technique is a simple and effective iteration procedure
which improves the first approximation of the root, progressively. However, the method does not provide any scheme
for getting a close first estimate; besides, it requires division
operation which by itself is another recursive process in computer arithmetic, and many DSP units, simply, do not have a
division instruction [2].There are several developments in re-
O<e<&.
(1)
X o = X - 2m+
+ 1.
(2)
Now, if Y and Y Oare the square roots of X and X O ,respectively, then we must have
Y O= Y - e .
(3)
1026
111. INITIAL
ESTIMATE
OF THE SQUARE
ROOT
(4)
But, Y is an m bit integer, and the smallest value Y can have
is
Y + y =2m
(11)
=K ( X
where K
Combining (1 1 ) and (12) it follows that
which agrees with the upper limit given in ( 1 ) .
Lemma 1 provides an important criteria for efficient evaluation of square roots within a prescribed precision. It may
loosely be restated as follows: by ignoring the bit values in
the lower half plus one digits, one can still compute the
square root of an integer within an accuracy up to the last
digit (LSB) of the root.
Lemma 2 is a generalized version of Lemma 1, and could
be used mainly for noninteger (fraction or floating-point) numbers. By applying Lemma 2 one may, in fact, remove the fixed
value assigned to the upper limit of e, i.e., 2 1 / 2 ,and make it
more adjustable and appropriate for computation of square
roots with a selective accuracy.
Lemma 2: Let X be an n bit integer. Replacing the k least
significant bits of X by zeros will reduce the square root of
X by e where
0<
- e < 2k-(n+1)/2.
(7)
2k - 1
e=--2Y - e
where Y is the square root of X and, similar to ( 5 ) , the smallest value Y can get is
y = 2("-')/2 + e .
(9)
Z = Y +Ky2
(13)
( 14)
or, alternatively,
=Z -K y 2 .
Z+z=2m
(15)
X =y2
=z2 - 2 2 .
(16)
X
22 = 2m
X
+ 2m = 2m
+ z + 2.
-
(17)
X = 2"(Z - z ) = z2- z 2 .
(18)
It follows from Lemma 1 (as well as Lemma 2) that in order
to evaluate the square root of an integer X with 2m digits It is evident from (18) that, first of all, Z 2 Y , and second,
and for a desired accuracy up to the LSB we can practically the difference between Z and Y becomes the largest when Z
ignore the m 1 least significant bits of X. And for integers is at its lowest value (or z at its highest value), in which case
with odd number of digits one can always apply a left shift the bit maps of Z and z are represented as
to make the number of bits even, and later adjust the square
Z = 1 1 OOO... 0, and ~ = 0 OO...
1
0.
root so obtained by multiplying it with a constant, as will be
discussed later. As a consequence of this discussion, and in This simply means that z = Z/3, and after substituting the
order to facilitate our analysis we ignore the m 1 LSB's and values into ( 1 8) we get
replace them by zeros, for the rest of the discussion, unless
Y 2 = Z2 - 1/9Z2 = 8/9Z2
otherwise stated.
1027
which results in
Z =
= 1.06Y.
(19)
Equation (19) gives the worst case values for Z, where the
difference between Z and Y is the largest and equal to 6%,
as indicated. At the other extreme, Z is equal to the exact
square root of X. This is true when z is at its minimum
(which is 1 and practically negligible). However, due to the
quadratic nature of both the error function and X , in terms
of Y , the average increase in Z is reduced below the normal
6/2 = 3/. Experimental results show that this average is
about 1.7%; and in fact, a mean accuracy of f0.85% can be
claimed if we multiply Z by a proper constant.
Algorithm 1: For a given integer X with 2m number of
bits, one can always obtain integer 2, the initial estimate of the
square root of X,through the following two-step procedure:
a) apply m 1 right shifts to X , and
b) insert a 1 bit to the left of the most significant bit
yi = Z +K&I
(23)
(MSB) of the result in step 1).
In the next section, we are going to develop a new iteration where K = 2-(m+l)is a constant which represents m + 1 shifts
technique for the accurate evaluation of the square root of X, to the right.
5) Find di = yi - yi-1. If d; is greater than zero, then let
through modification of Z.
i = i 1 and go to step 4; else continue.
6) Evaluate the twos complement of yi to get Y i . If the
IV . ITERATION
PROCEDURE
We showed in the previous section that we were able to number of bits of X was originally odd then multiply Y ; by
evaluate the first estimate of the square root of X with a mean the constant 0.7071067. The process is now terminated and
accuracy better than 1% ( f0.85%). In this section, we try to Y = Yi is the square root of X.
+K y i .
z,
and
(21)
(22)
=z
+ Ky;-l.
(23)
It is shown (see the Appendix) that the procedure always converges to y (or Y , the precise square root of X ) , and for the
error function defined as
eo = (z- Y ) / y and e, = ( Y ,
we find
Y)/Y
(24)
V. SQUARE
ROOTSOF REALNUMBERS
In this section, we consider X being a floating point number with its fraction being normalized for radix 2. This is
achieved by shifting the fraction to the left (or right) until the
bit to the immediate right of the radix point is the most significant 1 bit, and decreasing (or increasing) the exponent
accordingly. It is important to note that if X is normalized so
is Y , the square root of X.This is evident from the fact that
has value denoted by 1/2 5 Xf < 1,
the fraction of X,Xf,
and if Y f is the fraction of Y then 212/2 5 Y f < 1. In order
to evaluate the square root of a real number in normalized
format we have to evaluate both the fraction as well as the
exponent part of the square root. But the exponent part is
simply evaluated by dividing the exponent of X by 2. More
precisely, if X , is even then Ye = XJ2 where X , and Y e
are the exponents of X and Y , respectively. If, however, X,
is odd then Y e is obtained as Ye = ( X , 1)/2. Now, to compensate for the extra half exponent, added in the later case, we
clearly need to modify the fraction part and this is performed
by multiplying Y f by the constant 0.7071067. Note that the
new modified Y f still remains normalized.
Next we present an algorithmic procedure to evaluate Y f ,
the fraction of Y , for a given X f . In order to simplify the analysis, we chose to drop the subscripts and assume X , Y , Z , . . .
to represent Xf, Y f , Z f . . ., respectively.
First we show that
z= (1.o + X)/2.0
(26)
1028
TABLE I
THESIMULATION
TIME(ms) OF THE SQUARE
ROOTALGORITHM
BY
PA AND THE PROPOSEDMETHOD
47134012
1956421
.17
.17
.17
.16
.15
1067001974
34978139
13579
.17
TABLE I1
THESIMULATION
TIME(ms) OF THE SQUARE
ROOTALGORITHM
BY
NR AND THE PROPOSED
METHOD
I
Data
3786541.91
35689.3512
57.9812
4.815
0.534
0.0004581
mlmethod
5.2
3.8
2.2
1.5
1.25
2.4
SquareRoot
1945.903931
188.916260
7.614540
2.216981
0.730754
0.021582
x =z*-(l.O-Z)2
=2z
1.0
(27)
-X)/2.0.
(28)
or simply
Z
= (1.0
+X)/2.0
and z
= (1.0
Wmethod
1.7
1.12
1.11
1.1
1.o
1.3
Thepmposedmethod
0.8
0.85
0.67
0.75
0.92
0.6
VI. SIMULATION
RESULTS
029
-JZ+l.
2-Jz
APPENDIX
Convergence: We are going to show that the iteration process, denoted by (23), always converges to Y , the precise
square root of X . First, we expand yl as
= y 2 - (U - Y i - l U
+ Yi-I).
(AI)
ei-l = ( Y j P l- Y ) / Y = (U - yi-l)/Y
(A2)
y 2 - ei-iY(y
+ yi-1).
(A3)
=Y
(44)
Yi we obtain