june 2007 computer arithmetic, function evaluationslide 1 vi function evaluation topics in this part...

26
June 2007 Computer Arithmetic, Function Evaluation Slide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC Algorithms Chapter 23 Variation in Function Evaluation Chapter 24 Arithmetic by Table Lookup Learn hardware algorithms for evaluating useful functions Divisionlike square-rooting algorithms Evaluating sin x, tanh x, ln x, . . . by series expansion Function evaluation via convergence computation Use of tables: the ultimate in simplicity and flexibility

Upload: sherman-horn

Post on 13-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 1

VI Function Evaluation

Topics in This PartChapter 21 Square-Rooting Methods

Chapter 22 The CORDIC Algorithms

Chapter 23 Variation in Function Evaluation

Chapter 24 Arithmetic by Table Lookup

Learn hardware algorithms for evaluating useful functions• Divisionlike square-rooting algorithms• Evaluating sin x, tanh x, ln x, . . . by series expansion• Function evaluation via convergence computation• Use of tables: the ultimate in simplicity and flexibility

Page 2: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 2

23 Variations in Function Evaluation

Chapter Goals

Learning alternate computation methods (convergence and otherwise) for somefunctions computable through CORDIC

Chapter Highlights

Reasons for needing alternate methods: Achieve higher performance or precision Allow speed/cost tradeoffsOptimizations, fit to diverse technologies

Page 3: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 3

Variations in Function Evaluation: Topics

Topics in This Chapter

23.1. Additive / Multiplicative Normalization

23.2. Computing Logarithms

23.3. Exponentiation

23.4. Division and Square-Rooting, Again

23.5. Use of Approximating Functions

23.6. Merged Arithmetic

Page 4: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 4

23.1 Additive / Multiplicative Normalization

u (i+1) = f(u

(i), v (i), w

(i))

v (i+1) = g(u

(i), v (i), w

(i))

w (i+1) = h(u

(i), v (i), w

(i))

u (i+1) = f(u

(i), v (i))

v (i+1) = g(u

(i), v (i))

Additive normalization: Normalize u via addition of terms to it

Constant

Desiredfunction

Guide the iteration such that one of the values converges to a constant (usually 0 or 1); this is known as normalization

The other value then converges to the desired function

Multiplicative normalization: Normalize u via multiplication of terms

Additive normalization is more desirable, unless the multiplicative terms are of the form 1 ± 2a (shift-add) or multiplication leads to much faster convergence compared with addition

Page 5: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 5

Convergence Methods You Already Know

CORDICExample of additive normalization x(i+1) = x(i) – di y(i)

2–i

y(i+1) = y(i) + di x(i) 2–i

z(i+1) = z(i) – di e(i)

Division by repeated multiplicationsExample of multiplicative normalization

d (i+1) = d

(i) (2 d

(i)) Set d (0) = d; iterate until d

(m) 1

z (i+1) = z

(i) (2 d

(i)) Set z (0) = z; obtain z/d = q z

(m)

Force y or z to 0 byadding terms to it

Force d to 1 bymultiplying terms with it

Page 6: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 6

23.2 Computing Logarithms

x (i+1) = x

(i) c

(i) = x (i)

(1 + di 2–i)

y (i+1) = y

(i) – ln c (i) = y

(i) – ln(1 + di 2–i)

Read out from table

Force x (m) to 1

y (m) converges to y + ln x

di {1, 0, 1}

0

Why does this multiplicative normalization method work?

x (m) = x c

(i) 1 c

(i) 1/x

y (m) = y – ln c

(i) = y – ln (c (i)) = y – ln(1/x) y + ln x

If starting with y = 0, y(m) = lnx

x (0) = x, y

(0) =y

Page 7: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 7

23.2 Computing Logarithms

Convergence domain: 1/(1 + 2–i) x 1/(1 – 2–i) or 0.21 x 3.45

Number of iterations: k, for k bits of precision; for large i, ln(1 2–i) 2–i

Use directly for x [1, 2).

For x = 2q s, we have:

ln x = q ln 2 + ln s = 0.693 147 180 q + ln s

Radix-4 version can be devised

For log2x = log2(2qs) = q+log2s

= q +log2e x ln s

=q + 1.442 695 041 x ln s

Page 8: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 8

Computing Binary Logarithms via Squaring 1

For x [1, 2), log2 x is a fractional number y = (. y–1y–2y–3 . . . y–l)two

x = 2y = 2

x 2 = 22y = 2 y–1 = 1 iff x

2 2

(. y–1y–2y–3 . . . y–l)two

(y–1. y–2y–3 . . . y–l)two

Once y–1 has been determined,

if y–1 = 0, we are back at the original situation;

otherwise, divide both sides of the equation above by 2 to get:

x 2/2 = 2 /2 = 2

(1 . y–2y–3 . . . y–l)two (. y–2y–3 . . . y–l)two

Page 9: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 9

Computing Binary Logarithms via Squaring 2

The complete procedure for computing log2x for 1≦x≦2 is :

For i from 1 to l do x←x2

if x ≧ 2 then y-i = 1 ; x←x/2else y-i = 0

endifendfor

Page 10: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 10

Computing Binary Logarithms via Squaring 3

log x

Squarer

Initialized to x

value 2 iff this bit is 1

2

Radix Shift 0 1

Point

Fig. 23.1 Hardware elements needed for computing log2 x.

Generalization to base b:

x = b , y–1 = 1 iff x 2 b(. y–1y–2y–3 . . . y–l)two

Page 11: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 11

23.3 Exponentiation 1

x (i+1) = x

(i) – ln c

(i) = x (i)

– ln(1 + di 2–i)

y (i+1) = y

(i) c

(i) = y (i)

(1 + di 2–i)

Read out from table

Force x (m) to 0

y (m) converges to y ex

di {1, 0, 1}1

Why does this additive normalization method work?

x (m) = x – ln c

(i) 0 ln c (i)

x

y (m) = y c

(i) = y exp(ln c (i)) = y exp( ln c

(i)) y ex

If starting from y = 1y

(m) = ex

Computing ex

Page 12: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 12

23.3 Exponentiation 2

Convergence domain: ln (1 – 2–i) x ln (1 + 2–i) or –1.24 x 1.56

Number of iterations: k, for k bits of precision; for large i, ln(1 2–i) 2–i

Can eliminate half the iterations becauseln(1 + ) = – 2/2 + 3/3 – . . . for 2 < ulpand we may write y

(k) = y

(k/2) (1 + x

(k/2))

Radix-4 version can be devised

Page 13: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 13

General Exponentiation, or Computing xy 1

x y = (e

ln x) y

= e y ln x So, compute natural log, multiply, exponentiate

When y is an integer, we can exponentiate by repeated multiplication (need to consider only positive y; for negative y, compute reciprocal)

In particular, when y is a constant, the methods used are reminiscent of multiplication by constants (Section 9.5)

Page 14: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 14

General Exponentiation, or Computing xy 2

Example: x 25 = ((((x)2x)2)2)2x [4 squarings and 2 multiplications]

Noting that 25 = (1 1 0 0 1)two, leads to a general procedure

Computing x y, when y is an unsigned integer

Initialize the partial result to 1 Scan the binary representation of y, starting at its MSB, and repeat If the current bit is 1, multiply the partial result by x If the current bit is 0, do not change the partial result Square the partial result before the next step (if any)

25 = (1 1 0 0 1)two

x 25 = ((((1x)2x)2 )2 )2x1 1 0 0 1

Page 15: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 15

Faster Exponentiation via Recoding 1

Example: x 31 = ((((x)2x)2x)2x)2x [4 squarings and 4 multiplications]

Note that 31 = (1 1 1 1 1)two = (1 0 0 0 01)two

x 31 = (((((1x)2)2)2)2)2 / x [5 squarings and 1 division]

Computing x y, when y is an integer encoded in BSD format

Initialize the partial result to 1 Scan the binary representation of y, starting at its MSB, and repeat If the current digit is 1, multiply the partial result by x If the current digit is 0, do not change the partial result If the current digit is 1, divide the partial result by x Square the partial result before the next step (if any)

1 0 0 0 01

Page 16: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 16

Faster Exponentiation via Recoding 2

Radix-4 example: 31 = (1 1 1 1 1)two = (1 0 0 0 01)two = (2 0 1)four

x 31 = ((x2)4)4 / x

(2 0 1)four

x 31 = ((1x2)4 )4 / x

Page 17: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 17

23.4 Division and Square-Rooting, Again

s (i+1) = s

(i) –

(i) d

q (i+1) = q

(i) + (i)

Computing q = z / d In digit-recurrence division, (i) is the next

quotient digit and the addition for q turns into concatenation; more generally,

(i) can be any estimate for the difference between the partial quotient q

(i) and the final quotient q

Because s (i)

becomes successively smaller as it converges to 0, scaled versions of the recurrences above are usually preferred. In the following, s

(i) stands for s

(i) r

i and q (i) for q

(i) r

i :

s (i+1) = rs

(i) –

(i) d Set s

(0) = z and keep s (i) bounded

q (i+1) = rq

(i) + (i)

Set q (0) = 0 and find q * = q

(m) r

–m

In the scaled version, (i)

is an estimate for r (r i–m

q – q (i)) = r (r

i q * - q

(i)), where q * = r

–m q represents the true quotient

Page 18: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 18

Square-Rooting via Multiplicative Normalization

Idea: If z is multiplied by a sequence of values (c (i))2, chosen so that the

product z (c (i))2 converges to 1, then z c

(i) converges to z

x (i+1) = x

(i) (1 + di 2–i)2 = x

(i) (1 + 2di 2–i + di

2 2–2i) x

(0) = z, x

(m) 1

y (i+1) = y

(i) (1 + di 2–i) y

(0) = z, y

(m) z

What remains is to devise a scheme for choosing di values in {–1, 0, 1}

di = 1 for x (i)

< 1 – = 1 – 2–i di = –1 for x (i)

> 1 + = 1 + 2–i

To avoid the need for comparison with a different constant in each step, a scaled version of the first recurrence is used in which u

(i) = 2i (x (i) – 1):

u (i+1) = 2(u

(i) + 2di) + 2–i+1(2di u (i) + di

2) + 2–2i+1di2

u (i) u

(0) = z – 1, u

(m) 0

y (i+1) = y

(i) (1 + di 2–i) y

(0) = z, y

(m) z

Radix-4 version can be devised: Digit set [–2, 2] or {–1, –½, 0, ½, 1}

Page 19: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 19

Square-Rooting via Additive Normalization

Idea: If a sequence of values c (i) can be obtained such that z – (c

(i))2 converges to 0, then c

(i) converges to z

x (i+1)

= z – (y (i+1))2 = z – (y

(i) + c

(i))2 = x (i)

+ 2di y (i)

2–i – di2

2–2i x

(0) = z, x

(m) 0

y (i+1) = y

(i) + c

(i) = y

(i) – di 2–i y

(0) = 0, y

(m) z

What remains is to devise a scheme for choosing di values in {–1, 0, 1}

di = 1 for x (i)

< – = – 2–i di = –1 for x (i)

> + = + 2–i

To avoid the need for comparison with a different constant in each step, a scaled version of the first recurrence may be used in which u

(i) = 2i x (i):

u (i+1) = 2(u

(i) + 2di y (i) – di

2 2–i

) u (0)

= z , u (i) bounded

y (i+1) = y

(i) – di 2–i y

(0) = 0, y

(m) z

Radix-4 version can be devised: Digit set [–2, 2] or {–1, –½, 0, ½, 1}

Page 20: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 20

23.5 Use of Approximating Functions

Convert the problem of evaluating the function f to that of function g approximating f, perhaps with a few pre- and postprocessing operations

Approximating polynomials need only additions and multiplications

Polynomial approximations can be derived from various schemes

The Taylor-series expansion of f(x) about x = a is

f(x) = j=0 to f (j)

(a) (x – a) j / j!

The error due to omitting terms of degree > m is:

f (m+1)

(a + (x – a)) (x – a)m+1 / (m + 1)! 0 < < 1

Setting a = 0 yields the Maclaurin-series expansion

f(x) = j=0 to f (j)

(0) x j / j!

and its corresponding error bound:

f (m+1)

(x) xm+1 / (m + 1)! 0 < < 1

Efficiency in computation can be gained via Horner’s method and incremental evaluation

Page 21: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 21

Some Polynomial Approximations (Table 23.1)–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––Func Polynomial approximation Conditions–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

1/x 1 + y + y 2 + y

3 + . . . + y i + . . . 0 < x < 2, y = 1 – x

ex 1 + x /1! + x 2/2! + x

3/3! + . . . + x i /i ! + . . .

ln x –y – y 2/2 – y

3/3 – y 4/4 – . . . – y

i /i – . . . 0 < x 2, y = 1 – x

ln x 2 [z + z 3/3 + z

5/5 + . . . + z 2i+1/(2i + 1) + . . . ] x > 0, z =

x–1x+1

sin x x – x 3/3! + x

5/5! – x 7/7! +

. . . + (–1)i

x2i+1/(2i + 1)! + . . .

cos x 1 – x 2/2! + x

4/4! – x 6/6! +

. . . + (–1)i

x2i /(2i )! +

. . .

tan–1 x x – x

3/3 + x 5/5 – x

7/7 + . . .

+ (–1)i x2i+1/(2i + 1) +

. . . –1 < x < 1

sinh x x + x 3/3! + x

5/5! + x 7/7! +

. . . + x2i+1/(2i + 1)! +

. . .

cosh x 1 + x 2/2! + x

4/4! + x 6/6! +

. . . + x2i

/(2i )! + . . .

tanh–1x x + x 3/3 + x

5/5 + x 7/7 +

. . . + x2i+1/(2i + 1) +

. . . –1 < x < 1–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Page 22: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 22

Function Evaluation via Divide-and-Conquer

Let x in [0, 4) be the (l + 2)-bit significand of a floating-point number or its shifted version. Divide x into two chunks x H and x L:

x = x H + 2–t x L

0 x H < 4 t + 2 bits

0 x L < 1 l – t bits

t bits

x H in [0, 4) x L in [0, 1)

The Taylor-series expansion of f(x) about x = x H is

f(x) = j=0 to f (j)

(x H) (2–t x L)

j / j!

A linear approximation is obtained by taking only the first two terms

f(x) f (x H) + 2–t x L f (x H)

If t is not too large, f and/or f (and other derivatives of f, if needed) can be evaluated via table lookup

Page 23: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 23

Approximation by the Ratio of Two Polynomials

Example, yielding good results for many elementary functions

f(x)

a(5)x5 + a(4)x4 + a(3)x3 + a(2)x2 + a(1)x + a(0)

b(5)x5 + b(4)x4 + b(3)x3 + b(2)x2 + b(1)x + b(0)

Using Horner’s method, such a “rational approximation” needs 10 multiplications, 10 additions, and 1 division

Page 24: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 24

23.6 Merged ArithmeticOur methods thus far rely on word-level building-block operations such as addition, multiplication, shifting, . . .

Sometimes, we can compute a function of interest directly without breaking it down into conventional operations

Example: merged arithmetic for inner product computation

z = z (0) + x

(1) y

(1) + x (2)

y (2) + x

(3) y

(3)

x(1) y(1)

x(3) y(3)

x(2) y(2)

z(0)

Fig. 23.2 Merged-arithmetic computation of an inner product followed by accumulation.

Page 25: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 25

Example of Merged Arithmetic Implementation

Example: Inner product computation

z = z (0) + x

(1) y

(1) + x (2)

y (2) + x

(3) y

(3)

x(1) y(1)

x(3) y(3)

x(2) y(2)

z(0)

Fig. 23.3 Tabular representation of the dot matrix for inner-product computation and its reduction.

1 4 7 10 13 10 7 4 16 FAs 2 4 6 8 8 6 4 2 10 FAs + 1 HA 3 4 4 6 6 3 3 1 9 FAs1 2 3 4 4 3 2 1 1 4 FAs + 1 HA1 3 2 3 3 2 1 1 1 3 FAs + 2 HAs2 2 2 2 2 1 1 1 1 5-bit CPA

Fig. 23.2

Page 26: June 2007 Computer Arithmetic, Function EvaluationSlide 1 VI Function Evaluation Topics in This Part Chapter 21 Square-Rooting Methods Chapter 22 The CORDIC

June 2007 Computer Arithmetic, Function Evaluation Slide 26

Another Merged Arithmetic Example

Approximation of reciprocal (1/x) and reciprocal square root (1/x) functions with 29-30 bits of precision, so that a long floating-point result can be obtained with just one iteration at the end [Pine02]

u v w 1.

c Table

b Table

a Table

Squarer Radix-4 Booth

Radix-4 Booth

Partial products gen Partial products gen

9 bits 24 bits 19 bits

30 bits 20 bits 12 bits

16 bits

Multioperand adder

30 bits, carry-save

Double-precision significand f(x) = c + bv + av

2

1 square

Comparable to a multiplier

2 mult’s

2 adds