Function Implementation
Ryan Robucci
1. References
- \(^\dagger\) Koren, Israel. Computer arithmetic algorithms, 2nd ed. AK Peters/CRC Press, 2001.
2. Overview
- Some Classes of Functions
- Arithmetic Functions: +,-,/,*
- Elementary Functions: sqrt, exp,log
- Arbitrary Functions over some range
- Implementation Concepts
- Size, power, delay
- Single Cycle: Combinatorial Optimization of size, power, critical
path delay
- e.g. various fast adder and multiplier architectures
- Use of Shift operations as multiplications
- Iterative Techniques
- size, power, critical path delay now augmented by *cycle count *
- Can trade work/cycle with number of cycles
- New concept: convergence rate (# bits of answer accuracy per cycle)
- Use of lookup tables
3. Multi-Cycle Iterative Hardware Implementation
The computation for Iterative methods can distributed in time
- e.g. one iteration per clock cycle
- can support convergence testing and a variable number of iterations
- If the iterations are minimal, one could attempt to use a fast clock
in the iterative processor
- requires multi-clock domain design to interface the surrounding system
- If combinatorial path delay allows, implementing multiple iterations in cascaded combinatorial logic minimizes the number of clock cycles required
4. Distribution in Space
- Iterative methods with a finite/static number of iterations (or at
least a manageable upper bound) can be implemented in combinatoral
logic
- hardware for each iteration can be tailored to the parameters of a
specific iteration
- ex: multiplying by z=z×i in iteration i
- a multi-cycle iterative processor might use a multiplier to support multiplying by the variable i
- an unrolled hardware iteration only needs to support multiplying by a constant
- ex: multiplying by z=z×i in iteration i
5. Pipelining
- Iterative methods lend themselves well to pipelining (esp. for a
static number of iterations), allowing throughput matched to the pace
of surrounding system
- might require more hardware/area (and perhaps power)
- pipeline stages may perform one OR MORE iterations
- greater combinatorial path delay from cascading the combinatorial logic of two or more iterations
- if the clock rate can be maintained, performing more than one iteration in a given stage minimizes the number of stages and hence latency
- pipelining ( like combinatorial unrolling) allows hardware stages to
be tailored to a given iteration (e.g. multiplying by a constant
versus a variable)
6. Lookup Tables for Complex Operations and Initial Guess
- In general, a LUT can substitute for complex calculation for when the number of input cases to consider is small
- Can reduce run-time cycles with precomputed leading iterations
- implement difficult-to-compute initial guess values for globally complex functions, use methods like Newton-Raphson Iteration to iterate locally to a precise solution
7. Comparisons
- the first class of functions to consider are comparisons
- covered last lecture:
- Equality Comparison (==)
- greater-than and less-than comparisons
- Full Comparator (>,<,==)
8. Multiplication
- Various Combinational circuits may be discussed in other courses
(computer architecture, VLSI, and/or arithmetic circuit course)
- Basic
- Size: proportional to square of operand size
- Delay: proportional to operand length
- Basic
- Multiplication by low-density constants
- When a given an optimized hardware multiplier, larger multiplications can be performed using that Karatsuba Algorithm
- Iterative Techniques
- Already Covered:
- Multiplication by low density constants
- Karatsuba Algorithm: for building larger multipliers from smaller
9. Iterative Multiplication: Shift-add
- Sequential Multiplication using iterative leftshift of input operand (Unsigned)
- Assume \(X\) of \(m\) bits \(x_{m-1}x_{m-2}...x_{1}x_{0}\) and \(Y\) of \(n\) bits
- Start with \(Z=Y \cdot x_{0}\) where, \(x_{0}\in\{0,1\}\)
- For \(i = 1 \to m-1\), \(Y=\underbrace{Y\times2}_{\rm Y \texttt{<<} 1}\),\(Z=Z+Y \cdot x_{i}\)
Example 1001 x 1101
1101 x 1001 ---- 1101 x 1(lsb) 1101 x 0 1101 x 0 1101 x 1 --------- 1110101
-other varients may be discussed later in the course
10. Division
- Typically Iterative approach to solve Q=N/D by Q*D +R = N, reducing R as much as possible
- Like shift-add multiplication, but construct operand Q on the fly to achieve Q*D = N
- Start with D =0
- Going from MSB to LSB, set each bit to one if Q*D<N (e.g. don't overshoot) understanding that bit i weights D by 2^i
- More advanced technique involves using each bit to represent +1 or -1 rather than 0 or 1 and this leads to some optimizations
11. Simple Iterative ("Long") Division
by Example: Solve Q=10.125/3 Try to subtract \(3 \times W_i\) starting with the largest weight without overshooting (negative remainder) to choose which weights to use. Record which weights were used Sum weights at the end
12. Division Circuit Depiction
13. Division by Convergence
- Problem Statement: Find \(Q\) , where \(Q=\frac{N}{D}\), given N and D …but converge faster than 1-bit per iteration
- Alternative problem statement towards solution:
- find a factor \(R\) such that \(D \times R=1\), then find Q using \(Q = N \times R\)
- \(\frac{N}{D}=\frac{N\cdot R}{D\cdot R}=\frac{Q}{1}\)
- R will be computed over many stages by finding many factors \(R_i\) and \(R\) is taken to be
\[R=R_1 \cdot R_2 \cdot R_3 \cdot ...\] - D input assumed to already be normalized to 1: \((...(((D \cdot R_1) \cdot R_2) \cdot R_3 )\cdot ...)\)
- find a factor \(R\) such that \(D \times R=1\), then find Q using \(Q = N \times R\)
- Algorithm
- Start with a \(D_0\) that it is a "normalized fraction" (binary 0.1xxxxxx…), found by scaling D by a power of 2 as needed so that \[\frac{1}{2} \le D \lt 1 \]
- Iterations: each iteration is two multiplications and a two's complement operation
- \(D_{i+1}=D_i \cdot R_i\) (converges to 1)
- \(N_{i+1}=N_i \cdot R_i\) (converges to answer)
- \(R_{i+1}={\rm TWO} - D_{i+1}\)
14. Quadratic Convergence
- Division by convergence is an example of an algorithm that exhibits quadratic convergence: meaning that on average you get more than one bit per iteration
- The error bound is cut in half in the first iteration, cut by another 4 in the next, another 16 in the next etc…
- Double the number of bits each iteration ( BETTER THAN 1-BIT PER ITERATION OF "LONG" DIVISION!)
- \(D_0=\texttt{.1xxxxx....}\),
- \(D_1=\texttt{.1\textcolor{red}{1}xxxx....}\)
- \(D_2=\texttt{.11\textcolor{red}{11}xx....}\),
- \(D_3=\texttt{.1111\textcolor{red}{1111}xx..}\)
15. Newton-Raphson Iteration
- Newton-Raphson Iteration is a method to iteravely search for a zero of a function, i.e. \(f(x)=0\)
- key idea in Newton-Raphson Iteration is to update the current to find a zero using two pieces of information about the function
- the value of f at x: \(f(x)\)
- the derivate of f at x: \(f'(x)\)
- to solve for \(\frac{1}{x}=D\), let \(f(x)=\frac{1}{x}-D\), which goes to zero at \(x=\frac{1}{D}\)
- Need implementation for function and a multiplication by its derivative
- \(x_{x+1} = x_i -\frac{f(x_i)}{\left( \frac{d f(x_i)}{d x_i}\right) }\)
- \(\frac{d f(x_i)}{d x_i} = - \frac{1}{x^2}\)
- which sets up the Iteration:
- \(x_{i+1} = x_i (2 - D \cdot x_i)\), that will approach \(\frac{1}{D}\)
- Also exhibits quadratic converge
16. Lookup Tables
- A function y=f(x) can be defined as mapping from an input of any n-bit x to an m-bit y
- Therefore a memory can implement a function directly
- A lookup table can also support acceleration of computation in other ways…
17. Lookup Table for Initial Guess (bypass initial iterations)
- "first approximation" for iterative methods can be pre-calculated from a table to reduce required number of iterations
- Ex: Div by Reciprocation
- Given j bits to pre-calculate, a table of \(2^j\) entries (initial approximation) should be provided of the desired accuracy of n bits
- optimal choice is function (1/x) evaluated center of interval (proof omitted).
18. Lookup tables combined with Interpolation
- Interpolation may be linear, cubic spline,..
- finite set of points in a table, e.g. \(f(x_3)\),\(f(x_4)\)
- Estimate \(f(x)\) in the interval \(x_i \ge x \ge x_{i+1}\) with \(x_i\) and \(x_{i+1}\) stored in a LUT
- Linear Interpolation:
- \[f(x)= f(x_i) + m \cdot (x_{i+1}-x_i);\]
\(m=\frac{f(x_{i+1})-f(x_i)}{x_{i+1}-x_{i}}\)
- \[f(x)= f(x_i) + m \cdot (x_{i+1}-x_i);\]
- need to design mapping scale offset for encoding:
digital encoding = \((x-\rm offset)\cdot \frac{1}{\rm scale} \cdot 2^m\) - Choice of LUT Indexing for Linear Interpolation
- May use uniformly spaced points to create equal-sized intervals, or non-uniform spacing
- Irregular spacing requires a search for the relevant entries \(x_i \le x \le x_{i+1}\) while uniform intervals (\(\Delta\)) allow a more direct referencing to an offset related to \(x/\Delta\)
- Not all non-uniform spacing is irregular, for instance indexes of logarithmically spaced samples (10 per decade) can also be directly computed
- A useful approach for uniform interval indexes is to use an interval size such that the first few bits of x are used directly as the index into the LUT
19. Polynomial Approximation
- Variants
- Taylor Series Expansion (from Calculus course, truncate expansion to desired polynomial length)
Chebyshev Polynomials can be better
- Ratio of Polynomials
- Piecewise Approximation
- Can break into arbitrary intervals or use first few bits of input x to define intervals
and then choose approximation for y=f(x) base on which interval
- Can break into arbitrary intervals or use first few bits of input x to define intervals
20. Exponentiation using LUT for Partial Results
- Consider \(Y=e^{X}\) , \(X\) is 32-bit UNSIGNED fractional argument, \(X = \sum_{i=32}^{i=1} = x_i 2^{-i}\)
- $Y=e\left(x-1 ⋅ 2-1+x-2 ⋅ 2-2+…+x-31 ⋅ 2-31 + x-32 ⋅ 2-32\right)$
\[ = e^{x_{-1} \cdot 2^{-1}} \times e^{x_{-2} \cdot 2^{-2}} \times ... \times e^{x_{-31} \cdot 2^{-31}} \times e^{x_{-32} \cdot 2^{-32}}\] - Y can be iteratively computed as \(Y = Y \times e^{x_{-i} \cdot 2^{-i}}\) with Y initialized to 1 One choice for implementing the iteration is to use a lookup table storing 32 precomputed entries \(T_i=e^{2^{-i}}\) \(Y = \begin{cases}Y \times T_i & \text{if } x_i==1 \\ Y & \text{otherwise} \end{cases}\)
- This requires a LU and multiply per iteration
- But, there is a way to construct the iterations and LUT to make the multiplication very cheap ( scale by a scale)…
- Interestingly, it involves a lookup table for \(\ln()\) instead of \(\exp()\) with a pair of coupled iteration equations$
21. Piecewise Approximation
- Piecewise Polynomial Approximation
- Break function into multiple intervals
- Create Polynomial Approximation for each interval (e.g. Taylor series expansion around middle of interval)
- Store polynomial approximation for each interval in a lookup table (LUT)
- During run:
- decide interval to use,
- use LUT to recall parameters for that interval
- compute approximation