5
Table-Lookup Algorithms for Elementary Functions and Their Error Analysis Ping Tak Peter Tang Mathematics and Computer Science Division Argonne National Laboratory 9700 South Cass Ave. Argonne, IL 60439-4801 Abstract Table-lookup algorithms for calculating elementary func- tions offer superior speed and accuracy when compared with more traditional algorithms. We show that, with careful design, it is feasible to implement table-lookup algorithms in hardware. Furthermore, we present a uni- form approach to carry out a tight error analysis for such implementations. 1 Introduction Since the adoption of IEEE Standard 754 for floating- point arithmetic [7 , there has been a revival of interest accuracy (see [12], [5], and [8], for example). A common thread of the recent work in this area is table-lookup al- gorithms. From a mathematical and algorithmic point of view, table-lookup algorithms are straightforward generalizations of more traditional algorithms such as those in [2] and [4]. According to W. Kahan, J. C. P. Miller had already pointed out in late 1950s that such approaches would be desirable as soon as memory be- came inexpensive. More recently, work by Gal ([3]) and the new runtime library by IBM ([l] and 61) are notable recent efforts use rather large tables and implement the functions in software, we illustrate here that, with care- ful design, the table sizes can be made so small that these table-lookup algorithms become easily realizable in hardware. Despite the size reduction, the speed and accuracy benefits brought about by table-lookup alge rithms are preserved. Furthermore, we show that these table-lookup algorithms lend themselves to a uniform error analysis that yields tight error bounds. Typically, a theoretical bound of 0.57 unit in the last place (ulp) can be obtained for an implementation whose maximum error observed over several million arguments is 0.55 The rest of the paper is organized as follows. Sec- tion 2 presents the general idea behind table-lookup al- gorithms. Section 3 illustrates the idea by three specific examples. Section 4 presents a uniform approach for a tight error analysis and an illustrative example. Sec- tion 5 discusses the feasibility of hardware implementa- tions of table-lookup algorithms. Section 6 makes some in implementing e 1 ementary functions to near-perfect in promoting table-lookup algorithms. A lthough these ulp. concluding remarks on the advantages of table-lookup algorithms over CORDIC ([9]) and ordinary (without table-lookup) polynomial algorithms. 2 Table-Lookup Algorithms Let f be the function to be implemented and I be the domain of interest. A typical table-lookup algorithm contains a set of “breakpoints” Ck , k = 1,2, . . ., N in I and a table of N approximations Tk to f(ck). For any input argument z E I, the algorithm calculates f(z) in three steps. Reduction: For this given z, the algorithm selects an In general, ck is the appropriate breakpoint ck. tion transformation” breakpoint closest to z.) It t I, en applies a “reduc- T = R(X, Ck). A typical case is R(z, ck) = 2: - Ck. using some approximation formula Approximation: The algorithm now calculates f(r) Very often, p is a polynomial. Reconstruction: Based on the reduction transforma- tion R, the values f(ck) and f(r , and the nature off, the algorithm calculates f (zl by a reconstruc- tion formula S : Although a traditional polynomial or rational-function- based algorithm (see [2] and [4]) can also be expressed in these three steps, there are two properties peculiar to a table-lookup algorithm. First, the reduction process in a table-lookup algorithm is much more flexible since, unlike a traditional algorithm, the choice of breakpoints U.S. us. Govemment Copyright. work not protected by 232

[IEEE Comput. Soc. Press 10th IEEE Symposium on Computer Arithmetic - Grenoble, France (26-28 June 1991)] [1991] Proceedings 10th IEEE Symposium on Computer Arithmetic - Table-lookup

  • Upload
    ptp

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: [IEEE Comput. Soc. Press 10th IEEE Symposium on Computer Arithmetic - Grenoble, France (26-28 June 1991)] [1991] Proceedings 10th IEEE Symposium on Computer Arithmetic - Table-lookup

Table-Lookup Algorithms for Elementary Functions and Their Error Analysis

Ping Tak Peter Tang Mathematics and Computer Science Division

Argonne National Laboratory 9700 South Cass Ave.

Argonne, IL 60439-4801

Abstract Table-lookup algorithms for calculating elementary func- tions offer superior speed and accuracy when compared with more traditional algorithms. We show that, with careful design, it is feasible to implement table-lookup algorithms in hardware. Furthermore, we present a uni- form approach to carry out a tight error analysis for such implementations.

1 Introduction Since the adoption of IEEE Standard 754 for floating- point arithmetic [7 , there has been a revival of interest

accuracy (see [12], [5], and [8], for example). A common thread of the recent work in this area is table-lookup al- gorithms. From a mathematical and algorithmic point of view, table-lookup algorithms are straightforward generalizations of more traditional algorithms such as those in [2] and [4]. According to W. Kahan, J . C. P. Miller had already pointed out in late 1950s that such approaches would be desirable as soon as memory be- came inexpensive. More recently, work by Gal ([3]) and the new runtime library by IBM ([l] and 61) are notable

recent efforts use rather large tables and implement the functions in software, we illustrate here that, with care- ful design, the table sizes can be made so small that these table-lookup algorithms become easily realizable in hardware. Despite the size reduction, the speed and accuracy benefits brought about by table-lookup a l g e rithms are preserved. Furthermore, we show that these table-lookup algorithms lend themselves to a uniform error analysis that yields tight error bounds. Typically, a theoretical bound of 0.57 unit in the last place (ulp) can be obtained for an implementation whose maximum error observed over several million arguments is 0.55

The rest of the paper is organized as follows. Sec- tion 2 presents the general idea behind table-lookup al- gorithms. Section 3 illustrates the idea by three specific examples. Section 4 presents a uniform approach for a tight error analysis and an illustrative example. Sec- tion 5 discusses the feasibility of hardware implementa- tions of table-lookup algorithms. Section 6 makes some

in implementing e 1 ementary functions to near-perfect

in promoting table-lookup algorithms. A lthough these

ulp.

concluding remarks on the advantages of table-lookup algorithms over CORDIC ([9]) and ordinary (without table-lookup) polynomial algorithms.

2 Table-Lookup Algorithms Let f be the function to be implemented and I be the domain of interest. A typical table-lookup algorithm contains a set of “breakpoints” Ck , k = 1 , 2 , . . . , N in I and a table of N approximations T k to f ( c k ) . For any input argument z E I , the algorithm calculates f (z) in three steps.

Reduction: For this given z, the algorithm selects an In general, ck is the appropriate breakpoint ck.

tion transformation” breakpoint closest to z.) It t I, en applies a “reduc-

T = R(X, C k ) .

A typical case is R ( z , ck) = 2: - C k .

using some approximation formula Approximation: The algorithm now calculates f ( r )

Very often, p is a polynomial.

Reconstruction: Based on the reduction transforma- tion R, the values f ( c k ) and f ( r , and the nature o f f , the algorithm calculates f (zl by a reconstruc- tion formula S :

Although a traditional polynomial or rational-function- based algorithm (see [2] and [4]) can also be expressed in these three steps, there are two properties peculiar to a table-lookup algorithm. First, the reduction process in a table-lookup algorithm is much more flexible since, unlike a traditional algorithm, the choice of breakpoints

U.S. us. Govemment Copyright.

work not protected by 232

Page 2: [IEEE Comput. Soc. Press 10th IEEE Symposium on Computer Arithmetic - Grenoble, France (26-28 June 1991)] [1991] Proceedings 10th IEEE Symposium on Computer Arithmetic - Table-lookup

is basically independent of the function f in question. In most situations, the breakpoints are chosen so that the reduced argument r can be computed efficiently on the particular machine in question. Second, in a table- lookup algorithm, the magnitude of the reduced argu- ment r can be made as small as one wishes, limited only by the table size one can accommodate. We now illustrate these ideas by three realistic examples.

3 The functions 2", log 2 , and sin t are among the most commonly used elementary functions. We have pur- posely chosen different bases for the exponential and logarithm functions (base 2 and e, respectively) to illus- trate the flexibility of table-lookup algorithms. The algorithms here calculate the functions on "pri- mary" domains. Transformations of ar uments to such domains are standard and well known ?see [2] and [4]).

3.1 2" on [-1,1]

Reduction: Find the breakpoint ck = k/32,k =

Algorithms for 2", logz, and sinz

0, 1, . . . ,31 such that

13: - (m + ck)l I 1/64,

where m = -1, 0, or 1. Calculate r by r = [z - (m + ck)] . (log 2). Note that Irl I log 2/64.

Approximation: Approximate e' - 1 by a polynomial P(T) 7

p ( r ) = r + p l r 2 + . . . + pnrn+' .

The coefficients p l , p2 , . . . , p , can be determined by applying the Remes algorithm [14] to the function e' - 1 on the interval [- log2/64,log2/64].

Reconstruction: Reconstruct 2" by the relationships

2" = 2mtck . e '

= x

2" {2"k + Y k (e' - 1)) 2"{2'k + Yk . p ( r ) }

x 2m{Tk + T k . p ( r ) ) .

T k x 2'*, k = 0, 1 , . . . ,31, are the tabulated values.

3.2 logz on [1,2]

Reduction: If t < compute log(t) by a simple polynomial approximation and exit. This polyne mial is designed to approximate log(z) on [l, (see [13] for details). Otherwise, find the break- point ck = 1 + k/64, k = 0 , 1 , . . ., 64 such that

I Z - c ~ I 5 1/128.

Calculate r by T = 2(z - ck)/(z + ck). Note that Irl 5 1/128.

Approximation: Approximate log(z/ck) by an odd polynomial p( r ) :

p ( r ) = r + p l r 3 + p 2 r 5 + . . . + p n r Z n + l .

The coefficients p l , p2 , . . . , p , can be determined by applying the Remes algorithm to the function log([l + r/2]/[1 - 7/21) on the interval [0, l/l28] using odd polynomials. Note that

1 r 3 l r 5 = 2 ( ( 3 + & ) +&) +-.) .

Reconstruction: Reconstruct log z by the relation- ships

log(z) = log(ck) + b(Z/Ck) log(ck) + p(.) T k + p ( r ) .

Tk x log(ck), k = 0 , 1 , . . . , 6 4 , are the tabulated values.

3.3 sinx on [ 0 , ~ / 4 ]

Reduction: If 1x1 < 1/16, calculate s inz by a sim- ple polynomial approximation. Otherwise, find the breakpoint cjk of the form

C j k = 2-'(1+k/8) j = 1 , 2 , 3 , 4 ; k = 0 , 1 , . . . , 7

that is closest to z. Calculate r by r = z - cjk. Note that Irl 5 1/32.

by polynomials p and q , respectively: Approximation: Approximate sin r - r and cos r - 1

p ( r ) = p l r 3 + p a r 5 + . . . + pnrZn+l, q ( r ) = q l r 2 + q2r4 + . . . + qmrzm.

The coefficients p l , p a , . . . , p , ; q 1 , q 2 , . . . , qm can be determined by applying the Remes algorithm to the functions sin r - r and cos r - 1 on the interval [0,1/32] using odd and even polynomials, respec- tively.

Reconstruction: Reconstruct sin( z) by the relation- ships

sin(z) = - -

N N

x

233

Page 3: [IEEE Comput. Soc. Press 10th IEEE Symposium on Computer Arithmetic - Grenoble, France (26-28 June 1991)] [1991] Proceedings 10th IEEE Symposium on Computer Arithmetic - Table-lookup

Table 1: A Realistic Set of Algorithms for IEEE Double Precision

No. of Function Table Entries

No. of Coefficients

32 64 64

n = 5 I n = 2 n = 3 , m = 3

To conclude this section, we tabulate in Table 1 the ta- ble size and coefficient requirements for a realistic set of algorithms tailored to IEEE double precision. Clearly, this is one particular choice out of the many possible combinations of breakpoints and approximating poly- nomials. Take 2“, for example: one can reduce the ta- ble size to 16 by increasing n to 6 (hence the price of an add and multiply . Optimal choice is an engineering

question.

4 Error Analysis

issue whose answer d epends on the particular project in

Recall the three steps of calculating f at I:

Reduction: r = R(z, ck).

Approximation: p ( r ) = f ( r )

Reconstruction: f(z) = S ( f ( c k ) , f(r)) S(Tk, P(r ) )

Because of inexact computations, we obtain + instead of r , p instead of p , and S instead of S. Hence, the computed result is

m!, @(+))

The goal of the error analysis is to estimate accurately the difference

I S ( f ( c k ) , f ( r ) ) - S ( w w ) l . We apply the triangular inequality. Thus,

I S ( f ( C k ) , f(.)) - S ( ~ k , W ) l 5 (S(f(ck)r f(r)) - S(f(Ck)r .f(+))I +

I S ( f ( C k ) l f(+>> - S(f (Ck) , P(+))l+

IS(f(Ck)r P ( + ) ) - m k , @(+))I 5 E i + E 2 + E 3 .

In most situations,

El 5 constant. If(r) - f ( + ) 1 x constant . lf’(r)I . Ir - +I

E2 5 constant . mfx If(t) - p ( t ) l . and

El can be easily estimated because the reduction pro- cess %? is usually so simple that Ir - +[ can be estimated tightly. E2 can also be easily estimated since the nu- merical value

If(t) - P(t)l

is obtained when the polynomial is sought, usually by the use of the Remes algorithm. The maximum is taken over the domain of approximation. Let us make a short digression for those interested in our implementation of the Remes algorithm. The ver- sion of Remes algorithm we use is the one-point ex- change algorithm (see [lo] for example) as opposed to a multiple-exchange algorithm ([14]). Moreover, the ex- change scheme is based on the simplex algorithm which is a generalization of that based on sign alternations (see [ll] for details). These specifics allow the algorithm to handle non-Haar systems as well as approximation of discontinuous functions. Moreover, the domain does not need to be discretized, because

E p = m:x If(t) - dt ) l

is calculated by searching the zeros of f ’ ( t ) -$( t ) by nu- merical root finder. Consequently, the numerical value of Ep we obtain is accurate to many digits. This ac- curacy is definitely sufficient for the purpose of error analysis, because most of the time we need only one or two digits of Ep (see below). We return to the discussion on errors. The rounding error

in calculating the polynomial and reconstruction is usu- ally the most difficult to estimate tightly. With the use of table-lookup algorithms, however, the analysis is greatly simplified. The reason is that the magnitude of the reduced argument r (or +) is typically so small that rounding errors associated with r k , k 2 2, are practi- cally zero. The simplicity of the analysis in [12] and [13] illustrates the situation. To illustrate the ideas here, we carry out the analysis of 2= for a typical IEEE double-precision implementation. Let E denote 1 ulp of 1, i.e., E = 2 - 5 2 . Clearly, the sub- traction s := z - ( m + c k ) is exact. Hence the errors in the reduction step are those caused by the multiplica- tion by log2 and the fact that the value “log2” used is only a 53-bit approximation:

E3 = IS(f(Ck), P(;.)) - m, P(+))l

r = slog2, and r = s(l0g 2 + 6,) + 6 2 ,

where 61 is the error in the approximation to log 2 , and 62 is the error caused by the finite-precision product. Now,

log2 = .69314718.. . E 2-’ . [l, 2) . Therefore, rounding (as opposed to truncating) log 2 to working precision (53 bits in our case) gives an error no bigger than f ;E = 2-’c. Hence 161 I 5 2-’~. Next,

( S . (log2 + &I)( 5 I0g2/64 + 61/64.

234

Page 4: [IEEE Comput. Soc. Press 10th IEEE Symposium on Computer Arithmetic - Grenoble, France (26-28 June 1991)] [1991] Proceedings 10th IEEE Symposium on Computer Arithmetic - Table-lookup

Since log2/64+61/64 E 2 - 7 . [ l , 2 ) , rounding the product s(log2/64 + 61) to working precision gives an error no bigger than 2-8c. Hence, IS2( 5 2-'c. Therefore,

Ir - i J 5 Isbll + 1b21 5 r7€. Hence,

Next, the best approximating polynomial p obtained by a Remes algorithm gives

I (et - 1 ) - p(t)l 5 2-63 = 2 - l 1 e , It1 5 log2/64. Thus,

Finally, we estimate the errors in computing p ( i ) and the final reconstruction. Since 2'k is not representable in 53 bits, we use 2 variables TI and T2 where TI is 2ck rounded to 53 bits and T z is a correction term that makes TI + T2 = 2ck for all practical purposes. (Note that a T z having 6 significant bits will be sufficient.) The reconstruction is

lE2l 5 2-11€.

2 ° K + (Tl * P + T2)). Scaling by 2'" is exact. The last add contributes no more than 0.5 ulp of error. Estimating the error in TI * p + T2 is simple: Since li21 < 2-12 , the only significant error in calculating p is the last add. Thus the computed result is

(2" + & ) ( p + 6 2 ) + T2 + 63 = 2"p + T2 + 61p + 622'* + 63,

where Ipl 5 2 - 6 , 1611 5 2 - ' ~ , 1621 5 2-7c , and 1631 5 2 4 € . The rounding error is bounded by

1 1 ~ ~ 1 5 uip + (r7 + 2-6 + 2 - 7 € . 2"

1 - 2 < - + ( 2 - 7 + 2 - 5 )

5 0 . 5 4 ~ 1 ~ .

Consequently,

(Ell + + 1331 5 0 . 5 5 6 ~ 1 ~ . Note that in the case ck = 0 (in particular, 121 5 1/64), the bound given here is pesimistic because no error will be committed in TI * p + T2, for TI = 1 and Tz = 0.

5 Feasibility of Hardware Implementa-

Table-lookup algorithms have been implemented suc- cessfully in software ([6] and [ 1 2 ] , for example). With today's VLSI technology and the algorithms' flexibility, hardware implementations also seem extremely feasible. In what follows, we reexamine the algorithm for 2" in Section 3.1 from the point of view of hardware imple- mentation.

tions

Table Size: The table required here has 32 double- precision values. Assuming 8 bytes of storage per entry although one can device more compact stor-

table is easily accommodated by today's standard. age sc I emes), this table is only 2 kbits. Such a

Reduction and Table Lookup: Consider the reduc- tion for 121 2 1 / 1 6 where 3: is an IEEE double- precision number. Thus

3: = ( -1 ) ' . 2k . ( l . b l b 2 . . . b52)base2,

where s E (0, l } , IC E { - 1 , - 2 } . Now, mathemati- cally,

m + ck = round-to-integer(32~)/32,

and both m and Ck are determined by the seven bits of information S , lsb(k), and ( 6 1 , b 2 , . . . , b5} . Clearly, simple multiplexers can be constructed to deliver both m + c k and Tk.

Approximation and Reconstruction: Most hard- ware performs IEEE sums and products via an in- ternal data format that has a longer mantissa and a wider exponent field than the working precision in question (say double precision or double-extended precision) in question. The longer mantissa typ- ically contains the guard, round, and sticky bits that ensure correct IEEE rounding; and the in- ternal exponent field typically contains two extra bits designed to accommodate products of working- precision values of extreme magnitudes, say, two smallest denormalized numbers. Rounding and ex- ception handling are usually performed separately during conversion of internal formats back to the working precision.

It is not hard to see that the approximation and reconstruction steps in Section 3.1 can be carried out solely in the internal format. First, provided the internal exponent field has two extra bits, the recurrence

p ( r ) = r + p l y 2 + . . . + p 5 P r + r ' r . (p1 + r . (p2 + ' ' . + r ' p 5 ) . . .) =

and the reconstruction

where Irl 5 log 2/64 will not overflow or underflow. (Note that the coefficients p j x l / j ! are moderate in magnitude.) Second, note that there is no need to round any of the intermediate results to work- ing precision. In fact, keeping the extra guard and round bits would enhance the accuracy. Perform- ing these two steps without the need of rounding or error checking will clearly save a good amount of time compared to full IEEE operations everywhere.

235

Page 5: [IEEE Comput. Soc. Press 10th IEEE Symposium on Computer Arithmetic - Grenoble, France (26-28 June 1991)] [1991] Proceedings 10th IEEE Symposium on Computer Arithmetic - Table-lookup

6 Concluding Remarks Table-lookup algorithms offer several advantages over traditional polynomial/rational-funtion algorithms and CORDIC algorithms. In comparison, a table-lookup al- gorithm is generally

1. faster because it requires less work in the approxi- mation steps,

the approximation step is tiny, and 2. more accurate because the rounding error made in

3. amenable to tight error analysis.

CORDIC algorithms have been attractive from a hard- ware point of view because they require only shifts and adds (see [9] for example). The price one has to pay for this simplicity is a relatively large number of iterations of pseudo-division (reduction) and pseudo- multiplication (reconstruction). Advances in VLSI tech- nology have now made it possible to realize basic oper- ations such as primitive floating-point adds and multi- plies (that is, without rounding or exception handling) on the order of a few clocks on modern hardware. In such hardware environments, table-lookup algorithms are extremely feasible alternatives to CORDIC.

Acknowledgement This work was supported by the Applied Mathematical Sciences subprogram of the Office of Energy Research, U. S. Department of Energy, under Contract W-31-109- Eng-38.

References R. C. Agarwal, J . W. Cooley, F. G. Gustavson, J . B. Shearer, G . Slishman, and B. Tuckerman, New scalar and vector elementary functions for the IBM System/37O, IBM Journal of Research and Devel- opment, 30, no. 2, March 1986, pp. 126-144.

W. Cody and W. Waite, Software Manual for the Elementary Functions, Prentice-Hall, Englewood Cliffs, N.J., 1980.

S. Gal, Computing elementary functions: A new approach for achieving high accuracy and good performance, in Accurate Scientific Computations, Lecture Notes in Computer Science, Vol. 235, Springer, New York, 1985, pp. 1-16.

J . F. Hart et al., Computer Approximations, John Wiley and Sons, New York, 1968.

D. Hough, Elementary functions based upon IEEE arithmetic, Mini/Micro West Conference Record, Electronic Conventions, Inc., Los Angeles, 1983.

IBM Elementary Math Library, Programming RPQ P81005, Program number 5799-BTB, Program Ref- erence and Operations Manual, SH20-2230-1, Au- gust 1984.

IEEE standard for binary floating-point arithmetic, ANSI/IEEE Standard '754-1985, Institute of Elec- trical and Electronic Engineers, New York, 1985.

P. W. Markstein, Computation of elementary func- tions on the IBM RISC System/6000 processor, IBM Journal of Research and Development, 34, no. 1, January 1990, pp. 111-119.

R. Nave, Implementation of transcendental func- tions on a numeric processor, Microprocessing and Microprogramming, 11, 1983, pp. 221-225.

M. J . D. Powell, Approximation Theory and Meth- ods, Cambridge University Press, Cambridge, 1981.

P. T . P. Tang, A fast algorithm for linear complex Chebyshev approximations, Mathematics of Com- putation, 51, no. 184, October 1988, pp. 721-739.

P. T . P. Tang, Table-driven implementation of the exponential function in IEEE floating-point arith- metic, AGM Transactions on Mathematical Soft- ware, 15, no. 2, June 1989, pp. 144-157.

P. T. P Tang, Table-driven implementation of the logarithm function in IEEE floating-point arith- metic, AGM Transactions on Mathematical Soft- ware, 16, no. 2, December 1990, pp. 378-400.

L. Veidinger, On the numerical determination of the best approximation in the Chebyshev sense, Numerische Mathematik, 2, 1960, pp. 99-105.

236