faster math functions l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Faster Math Functions PowerPoint Presentation
Download Presentation
Faster Math Functions

Loading in 2 Seconds...

play fullscreen
1 / 215

Faster Math Functions - PowerPoint PPT Presentation


  • 385 Views
  • Uploaded on

Faster Math Functions. Robin Green R&D Programmer Sony Computer Entertainment America. What Is This Talk About?. This is an Advanced Lecture There will be equations Programming experience is assumed Writing your own Math functions Optimize for Speed Optimize for Accuracy

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

Faster Math Functions


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
faster math functions

Faster Math Functions

Robin Green

R&D Programmer

Sony Computer Entertainment America

what is this talk about
What Is This Talk About?
  • This is an Advanced Lecture
    • There will be equations
    • Programming experience is assumed
  • Writing your own Math functions
    • Optimize for Speed
    • Optimize for Accuracy
    • Optimize for Space
    • Understand the trade-offs
running order
Running Order
  • Part One – 10:00 to 11:00
    • Floating Point Recap
    • Measuring Error
    • Incremental Methods
      • Sine and Cosine
  • Part Two – 11:15 to 12:30
    • Table Based Methods
    • Range Reduction
    • Polynomial Approximation
running order4
Running Order
  • Part Three – 2:00 to 4:00
    • Fast Polynomial Evaluation
    • Higher Order functions
      • Tangent
      • Arctangent, Arcsine and Arccosine
  • Part Four – 4:15 to 6:00
    • More Functions
      • Exponent and Logarithm
      • Raising to a Power
    • Q&A
floating point standards
Floating Point Standards
  • IEEE 754 is undergoing revision.
    • In process right now.
  • Get to know the issues.
    • Quiet and Signaling NaNs.
    • Specifying Transcendental Functions.
    • Fused Multiply-Add instructions.
history of ieee 7549
History of IEEE 754
  • IEEE754 ratified in 1985 after 8 years of meetings.
  • A story of pride, ignorance, political intrigue, industrial secrets and genius.
  • A battle of Good Enough vs. The Best.
timeline the dark ages
Timeline: The Dark Ages
  • Tower of Babel
    • On one machine, values acted as non-zero for add/subtract and zero for multiply-divide.
    • On another platform, some values would overflow if multiplied by 1.0, but could grow by addition.
    • On another platform, multiplying by 1.0 would remove the lowest 4 bits of your value.
    • Programmers got used to storing numbers like this

b = b * 1.0;

if(b==0.0) error;

else return a/b;

b = (a + a) - a;

timeline 8087 needs the best
Timeline: 8087 needs “The Best”
  • Intel decided the 8087 has to appeal to the new mass market.
    • Help “normal” programmers avoid the counterintuitive traps.
    • Full math library in hardware, using only 40,000 gates.
    • Kahan, Coonen and Stone prepare draft spec, the K-C-S document.
timeline ieee meetings
Timeline: IEEE Meetings
  • Nat Semi, IBM, DEC, Zilog, Motorola, Intel all present specifications.
    • Cray and CDC do not attend…
  • DEC with VAX has largest installed base.
    • Double float had 8-bit exponent.
    • Added an 11-bit “G” format to match K-C-S, but with a different exponent bias.
  • K-C-S has mixed response.
    • Looks complicated and expensive to build.
    • But there is a rationale behind every detail.
timeline the big argument
Timeline: The Big Argument
  • K-C-S specified Gradual Underflow.
  • DEC didn’t.
timeline the big argument14
Timeline: The Big Argument
  • Both Cray and VAX had no way of detecting flush-to-zero.
  • Experienced programmers could add extra code to handle these exceptions.
  • How to measure the Cost/Benefit ratio?
timeline trench warfare
Timeline: Trench Warfare
  • DEC vs. Intel
    • DEC argued that Gradual Underflow was impossible to implement on VAX and too expensive.
    • Intel had cheap solutions that they couldn’t share (similar to a pipelined cache miss).
  • Advocates fought for every inch
    • George Taylor from U.C.Berkeley built a drop-in VAX replacement FPU.
    • The argument for “impossible to build” was broken.
timeline trench warfare16
Timeline: Trench Warfare
  • DEC turned to theoretical arguments
    • If DEC could show that GU was unnecessary then K-C-S would be forced to be identical to VAX.
  • K-C-S had hard working advocates
    • Prof Donald Knuth, programming guru.
    • Dr. J.H. Wilkinson, error-analysis & FORTRAN.
  • Then DEC decided to force the impasse…
timeline showdown
Timeline: Showdown
  • DEC found themselves a hired gun
    • U.Maryland Prof G.W.Stewart III, a highly respected numerical analyst and independent researcher
  • In 1981 in Boston, he delivered his verdict verbally…

“On balance, I think Gradual Underflow is the right thing to do.”

timeline aftermath
Timeline: Aftermath
  • By 1984, IEEE 754 had been implemented in hardware by:
  • It was the de facto standard long before being a published standard.
  • Intel
  • AMD
  • Apple
  • IBM
  • Nat. Semi.
  • Weitek
  • Zilog
  • AT&T
the format
The Format
  • Sign, Exponent, Mantissa
    • Mantissa used to be called “Significand”
  • Why base2?
    • Base2 has the smallest “wobble”.
    • Base2 also has the hidden bit.
      • More accuracy than any other base for N bits.
      • Base3 arguments always argue using fixed-point values
  • Why 32, 64 and 80-bit formats?
    • Because 8087 could only do 64-bits of carry propagation in a cycle!
why a biased exponent
Why A Biased Exponent?
  • For sorting.
  • Biased towards underflow.

exp_max = 127;

exp_min = -126;

    • Small number reciprocals will never Overflow.
    • Large numbers will use Gradual Underflow.
the format22
The Format
  • Note the Symmetry
rounding
Rounding
  • IEEE says operations must be “exactly rounded towards even”.
  • Why towards even?
    • To stop iterations slewing towards infinity.
    • Cheap to do using hidden “guard digits”.
  • Why support different rounding modes?
    • Used in special algorithms, e.g. decimal to binary conversion.
rounding24
Rounding
  • How to round irrational numbers?
    • Impossible to round infinite numbers accurately.
    • Called the Table Makers Dilemma.
      • In order to calculate the correct rounding, you need to calculate worst case values to infinite precision.
      • E.g. Sin(x) = 0.02310000000000000007
  • IEEE754 just doesn’t specify functions
    • Recent work looking into worst case values
special values
Special Values
  • Zero
    • 0.0 = 0x00000000
  • NaN
    • Not an number.
    • NaN = sqrt(-x), 0*infinity, 0/0, etc.
    • Propagates into later expressions.
special values26
Special Values
  • ±Infinity
    • Allows calculation to continue without overflow.
  • Why does 0/0=NaN when ±x/0=±infinity?
    • Because of limit values.
    • a/b can approach many values, e.g.
signed zero
Signed Zero
  • Basically, WTF?
    • Guaranteed that +0 = -0, so no worries.
  • Used to recover the sign of an overflowed value
    • Allows 1/(1/x) = x as x®+inf
    • Allows log(0)=-inf and log(-x)=NaN
    • In complex math, sqrt(1/-1) = 1/sqrt(-1) only works if you have signed zero
destructive cancellation
Destructive Cancellation
  • The nastiest problem in floating point.
  • Caused by subtracting two very similar values
    • For example, in quadratic equation if b2≈ 4ac
    • In fixed point…
    • Which gets renormalised with no signal that almost all digits have been lost.

1.10010011010010010011101- 1.10010011010010010011100 0.00000000000000000000001

compiler optimizations
Compiler “Optimizations”
  • Floating Point does not obey the laws of algebra.
    • Replace x/2 with 0.5*x – good
    • Replace x/10 with 0.1*x – bad
    • Replace x*y-x*z with x*(y-z) – bad if y≈z
    • Replace (x+y)+z with x+(y+z) – bad
  • A good compiler will not alter or reorder floating point expressions.
    • Compilers should flag bad constants, e.g.float x = 1.0e-40;
decimal to binary conversion
Decimal to Binary Conversion
  • In order to reconstruct the correct binary value from a decimal constant

Single float : 9 digits

Double float : 17digits

    • Loose proof in the Proceedings
      • works by analyzing the number of representable values in sub-ranges of the number line, showing a need for between 6 and 9 decimal digits for single precision
measuring error
Measuring Error
  • Absolute Error
    • Measures the size of deviation, but tell us nothing about the significance
    • The abs() is often ignored for graphing
measuring error33
Measuring Error
  • Absolute Error sometimes written ULPs
    • Units in the Last Place
measuring error34
Measuring Error
  • Relative Error
    • A measure of how important the error is.
example smoothstep function
Example: Smoothstep Function
  • Used for ease-in ease-out animations and anti-aliasing hard edges
    • Flat tangents at x=0 and x=1
smoothstep approximation
Smoothstep Approximation
  • A cheap polynomial approximation
    • From the family of Hermite blending functions.
incremental methods
Incremental Methods

Q: What is the fastest method to calculate sine and cosine of an angle?

A: Just two instructions.

There are however two provisos.

  • You have a previous answer to the problem.
  • You are taking equally spaced steps.
resonant filter
int N = 64;

float a = sin(2PI/N);

float c = 1.0f;

float s = 0.0f;

for(int i=0; i<M; ++i) {

output_sin = s;

output_cos = c;

c = c – s*a;

s = s + c*a;

...

}

Example using 64 steps per cycle.

NOTE: new s uses the previously updated c.

Resonant Filter
goertzels algorithm
A more accurate algorithm

Uses two previous samples (Second Order)

Calculates x = sin(a+n*b) for all integer n

float cb = 2*cos(b);

float s2 = sin(a+b);

float s1 = sin(a+2*b);

float c2 = cos(a+b);

float c1 = cos(a+2*b);

float s,c;

for(int i=0; i<m; ++i) {

s = cb*s1-s2;

c = cb*c1-c2;

s2 = s1; c2 = c1;

s1 = s; c1 = c;

output_sin = s;

output_cos = c;

...

}

Goertzels Algorithm
goertzels initialization
Goertzels Initialization
  • Needs careful initialization
    • You must account for a three iteration lag

// N steps over 2PI radians

float b = 2PI/N;

// subtract three steps from initial value

float new_a = a – 3.0f * b;

table based algorithms
Table Based Algorithms
  • Traditionally the sine/cosine table was the fastest possible algorithm
    • With slow memory accesses, it no longer is
  • New architectures resurrect the technique
    • Vector processors with closely coupled memory
    • Large caches with small tables forced in-cache
  • Calculate point samples of the function
    • Hash off the input value to find the nearest samples
    • Interpolate these closest samples to get the result
precalculating gradients
Precalculating Gradients
  • Given an index i, the approximation is…
  • Which fits nicely into a 4-vector…
how accurate is my table
How Accurate Is My Table?
  • The largest error occurs when two samples straddle the highest curvature.
    • Given a stepsize of ∆x, the error E is:
    • e.g. for 16 samples, the error will be:
how big should my table be
How Big Should My Table Be?
  • Turning the problem around, how big should a table be for an accuracy of E?
    • We just invert the expression…
how big should my table be58
How Big Should My Table Be?
  • We can replace the arccos() with a small angle approximation, giving us a looser bound.
  • Applying this to different accuracies gives us a feel for where tables are best used.
range reduction61
Range Reduction
  • We need to map an infinite range of input values x onto a finite working range [0..C].
  • For most transcendentals we use a technique called “Additive Range Reduction”
    • Works like y = x mod C but without a divide.
    • We just work out how many copies of C to subtract from x to get it within the target range.
additive range reduction
Additive Range Reduction
  • We remap 0..C into the 0..1 range by scaling
  • We then truncate towards zero (e.g. convert to int)
  • We then subtract k copies of C from x.

const float C = range;const float invC = 1.0f/C;x = x*invC;

int k = (int)(x*invC);// or (x*invC+0.5f);

float y = x – (float)k*C;

high accuracy range reduction
High Accuracy Range Reduction
  • Notice that y = x-k*C has a destructive subtraction.
  • Avoid this by encoding C in several constants.
    • First constant C1 is a rational that has M bits of C’s mantissa, e.g. PI = 201/64 = 3.140625
    • Second constant C2 = C - C1
    • Overall effect is to encode C using more bits than machine accuracy.

float n = (float)k;float y = (x – n*C1) – n*C2;

truncation towards zero
Truncation Towards Zero
  • Another method for truncation
    • Add the infamous 1.5 * 224 constant to your float
    • Subtract it again
    • You will have lost the fractional bits of the mantissa
    • This technique requires you know the range of your input parameter…

A = 123.45 = 1111011.01110011001100110

B = 1.5*2^24 = 1100000000000000000000000.

A = A+B = 1100000000000000001111011.

A = A-B = 1111011.00000000000000000

quadrant tests
Quadrant Tests
  • Instead of range reducing to a whole cycle, let’s use C=Pi/2 - a quarter cycle
    • The lower bits of k now holds which quadrant our angle is in
  • Why is this useful?
    • Because we can use double angle formulas
    • A is our range reduced angle.
    • B is our quadrant offset angle.
double angle formulas
Double Angle Formulas
  • With four quadrants, the double angle formulas now collapses into this useful form
a sine function
A Sine Function
  • Leading to code like this:

float table_sin(float x){ const float C = PI/2.0f; const float invC = 2.0f/PI; int k = (int)(x*invC); float y = x-(float)k*C; switch(k&3) { case 0: return sintable(y); case 1: return sintable(TABLE_SIZE-y); case 2: return -sintable(TABLE_SIZE-y); default: return –sintable(y); } return 0;}

more quadrants
More Quadrants
  • Why stop at just four quadrants?
    • If we have more quadrants we need to calculate both the sine and the cosine of y.
    • This is called the reconstruction phase.
    • Precalculate and store these constants.
    • For little extra effort, why not return both the sine AND cosine of the angle at the same time?
    • This function traditionally called sincos()in FORTRAN libraries
sixteen segment sine
Sixteen Segment Sine

float table_sin(float x){ const float C = PI/2.0f; const float invC = 2.0f/PI; int k = (int)(x*invC); float y = x-(float)k*C; float s = sintable(y); float c = costable(y); switch(k&15) { case 0: return s; case 1: return s*0.923879533f + c*0.382683432f; case 2: return s*0.707106781f + c*0.707106781f; case 3: return s*0.382683432f + c*0.923879533f; case 4: return c; ... } return 0;}

math function forms
Math Function Forms
  • Most math functions follow three phases of execution
  • This is a pattern you will see over and over
    • Especially when we meet Polynomial Approximations

1. Range Reduction

2. Approximation

3. Reconstruction

infinite series
Infinite Series
  • Most people learn about approximating functions from Calculus and Taylor series
  • If we had infinite time and infinite storage, this would be the end of the lecture.
taylor series
Taylor Series
  • Taylor series are generated by repeated differentiation
    • More strictly, the Taylor Series around x=0 is called the Maclauren series
  • Usually illustrated by graphs of successive approximations fitting to a sine curve.
properties of taylor series
Properties Of Taylor Series
  • This series shows all the signs of convergence
    • Alternating signs
    • Rapidly increasing divisor
  • If we truncate at the 7th order, we get:
graph of taylor series error
Graph of Taylor Series Error
  • The Taylor Series, however, has problems
    • The problem lies in the error
    • Very accurate for small values but is exponentially bad for larger values.
  • So we just reduce the range, right?
    • This improves the maximal error.
    • Bigger reconstruction cost, large errors at boundaries.
    • The distribution of error remain the same.
  • How about generating series about x=Pi/4
    • Improves the maximal error.
    • Now you have twice as many coefficients.
taylor 7 th order for 0 pi 280
Taylor 7th Order for 0..Pi/2
  • And now the bad news.
taylor series conclusion
Taylor Series Conclusion
  • For our purposes a Taylor series is next to useless
    • Wherever you squash error it pops back up somewhere else.
    • Sine is a well behaved function, the general case is much worse.
  • We need a better technique.
    • Make the worst case nearly as good as the best case.
orthogonal polynomials83
Orthogonal Polynomials
  • Families of polynomials with interesting properties.
    • Named after the mathematicians who discovered them
    • Chebyshev, Laguerre, Jacobi, Legendre, etc.
  • Integrating the product of two O.P.s returns zero if the two functions are different.
    • Where w(x) is a weighting function.
orthogonal polynomials84
Orthogonal Polynomials
  • Why should we care?
    • If we replace Pi(x) an arbitrary function f(x), we end up with a scalar value that states how similar f(x) is to Pj(x).
    • This process is called projection and is often notated as
  • Orthogonal polynomials can be used to approximate functions
    • Much like a Fourier Transform, they can break functions into approximating components.
chebyshev polynomials
Chebyshev Polynomials
  • Lets take a concrete example
    • The Chebyshev Polynomials Tn(x)
chebyshev plots
Chebyshev Plots
  • The first five Chebyshev polynomials
chebyshev approximation
Chebyshev Approximation
  • A worked example.
    • Let’s approximate f(x) = sin(x) over [-π..π] using Chebyshev Polynomials.
    • First, transform the domain into [-1..1]
chebyshev approximation88
Chebyshev Approximation
  • Calculate coefficient kn for each Tn(x)

Where the constant cn and weighting function w(x) are

chebyshev coefficients
Chebyshev Coefficients
  • The resulting coefficients
    • This is an infinite series, but we truncate it to produce an approximation to g(x)
chebyshev reconstruction
Chebyshev Reconstruction
  • Reconstruct the polynomial in x
    • Multiply through using the coefficients kn
chebyshev result
Chebyshev Result
  • Finally rescale the domain back to [-π..π]
    • Giving us the polynomial approximation
chebyshev result92
Chebyshev Result
  • The approximated function f(x)
chebyshev absolute error
Chebyshev Absolute Error
  • The absolute error sin(x)-f(x)
chebyshev relative error
Chebyshev Relative Error
  • The relative error tells a different story…
chebyshev approximation95
Chebyshev Approximation
  • Good points
    • Approximates an explicit, fixed range
    • Uses easy to generate polynomials
    • Integration is numerically straightforward
    • Orthogonal Polynomials used as basis for new techniques
      • E.g. Spherical Harmonic Lighting
  • Bad points
    • Imprecise control of error
    • No clear way of deciding where to truncate series
    • Poor relative error performance
pafnuty chebyshev 1821 1894
Chebyshev described what the error for the best approximating polynomial should look like.

An Order-N approximation error curve should:

Change sign N+1 times

Touch the maximal error N+2 times

This function minimizes the maximal error, so it is called the Minimax Polynomial

Pafnuty Chebyshev 1821-1894
evgeny remez 1896 1975
The algorithm to generate these polynomials is the Remez Exchange Algorithm

The algorithm requires:

A function to be approximated

A range of approximation

The required order of approximation

A weighting function

Evgeny Remez 1896-1975
remez exchange algorithm
Remez Exchange Algorithm
  • The algorithm works by generating a set of zero crossings for the error curve, e.g.
  • These are solved for a,b and c as a matrix inversion. The maximal error is found and one xn is altered. Repeat until minimized.
remez exchange algorithm100
Remez Exchange Algorithm
  • The algorithm is very sensitive to error and requires large number representations(e.g. 50 digits or more) and numerical solvers.
    • The best place to find these tools is in a mathematical package like Maple or Mathematica.
    • In Maple, a raw call to minimax() will use all coefficients available, so we will have to set up the calculation…
forming the minimax inequality
Forming The Minimax Inequality
  • Start by looking at the Taylor Expansion of sine to get a feel for what the end result
    • Every odd power, first coefficient is 1.0
  • Transform the problem into finding P(x2) in
the minimax inequality
The Minimax Inequality
  • Next, we form the minimax inequality expressing our desire to minimize the relative error
the minimax inequality103
The Minimax Inequality
  • Divide through by x3
the minimax inequality104
The Minimax Inequality
  • We want only every other power, sosubstitute y =x2
the function to approximate
The Function to Approximate
  • We solve for P(y) giving us
  • With the weighting function
final details
Final Details
  • Before we can run this through the minimax algorithm, we need to convert to a Taylor Series
    • This stops the sin() function being evaluated and greatly speeds the calculation
  • We also transform our approximation range
calculating the minimax poly
Calculating The Minimax Poly
  • We run these values through the minimax algorithm:
  • It returns (to 5 d.p.):
  • Finally, we resubstitute back into the original equation

minimax(p(y), y=0..Pi^2/4, [2,0], w(y));

minimax polynomial error
Minimax Polynomial Error
  • Maximal error = 1.108e-6 at x=1.5707
magic numbers
Magic Numbers
  • Comparing the results
  • An enormous increase in accuracy with a tiny change in coefficients.
    • We must be very sure of our coefficients.
    • We must pay attention to floating point rounding.
side note ps2 esin instruction
Side Note: PS2 ESIN Instruction
  • The manual entry for ESIN has a small error in it.
improving on perfection
Improving On Perfection
  • We can optimize polynomials for floating point representation
    • This will guarantee better accuracy for small angles, as the first term has the most effect in these cases.
    • Requires a smaller range than Pi/2, so we will use 0..Pi/4
  • Using a different form of minimax inequality, we can specify the first coefficient:
optimizing for floating point
Optimizing For Floating Point
  • Now we have to specify the first coefficient as a single-precision float.
  • Multiply k by 224, round to zero and divide by 224 to create an exactly representable number.
optimizing for floating point113
Optimizing For Floating Point
  • Solving the Minimax Inequality for P(x) and reinserting the answer gives us
  • Maximal error = 6.275e-9 at x=0.7855
    • About twice the error of the unoptimized version but with better accuracy for small angles.
optimizing for small angles
Optimizing for Small Angles
  • Most functions are bound by the accuracy of their first coefficient
    • In the same way we split the range reduction into two coefficients, we can do the same for the first coefficient of a polynomial, e.g.
    • Where a0+a1 = k, the leading coefficient.
polynomial evaluation
Polynomial Evaluation
  • Now we have our coefficients, we need to evaluate them efficiently.
    • When presented with a polynomial of the form
    • Most of us would produce code like

y = c*x*x + b*x + a;

large polynomials
Large Polynomials
  • Faced with a larger polynomial…… you can see the need for better methods

y = C*x*x*x*x*x*x*x + B*x*x*x*x*x + A*x*x*x + x;

horner form
Horner Form
  • Most good programmers will use Horner Form to encode the polynomial
  • This also codes well if you have multiply-add instructions available
  • Horner Form is used by nearly all libraries, with coefficients stored as arrays of values

z = x*x;y = (((C*z + B)*z + A)*z + 1)*x;

the problem with horner form
The Problem With Horner Form
  • Horner Form leads to very serial computations
    • Later instructions are waiting for earlier results.
    • A lot of instructions are stalled in the pipeline.
    • Makes no use of available SIMD instructions.
  • There is a better way – Estrin’s Method.
    • Comes from parallel computation research
    • Only documented in Knuth – nowhere online!
estrin s method
Estrin’s Method
  • Stems from the observation that by bracketing, we can produce regular sub-expressions
  • The trick is to keep a running copy of x2N as “glue” between each sub expression…
estrin s method table
Estrin’s Method Table

p0(x) = A

p1(x) = (Bx+A)

p2(x) = (C)x2+(Bx+A)

p3(x) = (Dx+C)x2+(Bx+A)

p4(x) = (E)x4+((Dx+C)x2+(Bx+A))

p5(x) = (Fx+E)x4+((Dx+C)x2+(Bx+A))

p6(x) = ((G)x2+(Fx+E))x4+((Dx+C)x2+(Bx+A))

p7(x) = ((Hx+G)x2+(Fx+E))x4+((Dx+C)x2+(Bx+A))

p8(x) = (I)x8+(((Hx+G)x2+(Fx+E))x4+((Dx+C)x2+(Bx+A)))

p9(x) = (Jx+I)x8+(((Hx+G)x2+(Fx+E))x4+((Dx+C)x2+(Bx+A)))

p10(x)= ((K)x2+(Jx+I))x8+(((Hx+G)x2+(Fx+E))x4+((Dx+C)x2+(Bx+A)))

p11(x)= ((Lx+K)x2+(Jx+I))x8+(((Hx+G)x2+(Fx+E))x4+((Dx+C)x2+(Bx+A)))

packing estrin s into simd
Packing Estrin’s Into SIMD
  • Evaluating the polynomial takes O(log N) time
    • Proves that you can improve on Horner Form
  • Each row should be evaluated in parallel
    • Estrin’s assumes N processing units
    • We only have four-way SIMD instructions
    • We will still have serialization stalls
  • Many architectures don’t allow interaction between vector elements
    • We have to split calculations across SIMD registers
    • We can often use redundant calculations to speed evaluation
brute force searching
Brute Force Searching
  • One tool can find the perfect evaluation order for every architecture – Brute Force
    • We generate trees of expressions
    • Flatten the tree into an ordered sequence of instructions
    • Evaluate the sequence w.r.t a particular architecture
  • Each run can generate millions of trees to test
    • Run the program overnight, keeping the best result so far
    • Distribute the calculation over many machines
coding using simd
Coding Using SIMD
  • Using SIMD instructions to code this
    • Temptation is to gather all coefficients into a 4-vector
    • Calculate [x, x3, x5, x7] and do one big multiply
    • Finally sum the result

...

calculate [ x, x3, x5, x7 ]

...

N := [ A, B, C, D ]

* [ x, x3, x5, x7 ]

...

sum the elements of N

...

coding with simd
Coding With SIMD
  • This turns out to be very slow
    • You are only using one parallel instruction!
    • Better to think of calculating in two pairs

...

calculate [ x, x2, x, x2 ]

...

N := [ A, B, C, D ]

* [ x, x2, x, x2 ]

N := [ Ax, Bx2, Cx, Dx2 ]

* [ -, -, x2, x2 ]

...

sum the elements of N

...

summing elements
Summing Elements
  • Summing the elements of a vector is very serial if you don’t have a special instruction
    • Better to split the problem over two registers.

A.x = A.x + A.y

.

.

.

A.x = A.x + A.z

.

.

.

A.x = A.x + A.w

.

.

.

Finished.

A.x = A.x + A.y

B.x = B.x + B.y

.

.

.

A.x = A.x + B.x

.

.

.

Finished.

simd insights
SIMD Insights
  • The more work you have to do, the more opportunities for parallelisation
    • Easier to code higher power polynomials as O(log n) starts to be more and more effective
    • Use redundant elements as “free duplicate values” to avoid moving elements around
    • You end up distributing coefficients in small groups
  • SinCos a good example
    • More efficient to do both sine and cosine at the same time

Coeff1 := [A, A, E, E]

Coeff2 := [B, B, F, F]

Coeff3 := [C, C, G, G]

Coeff4 := [D, D, H, H]

more transcendental functions
More Transcendental Functions
  • What about the other Transcendentals?
    • Tangent, Arctangent, Logarithm, Exponent, Roots, Powers
  • Let’s take a look at how well they are approximated by polynomials
    • For each power, make an approximation (e.g. minimax between [0..1])
    • Calculate the maximum error within the range
    • Find the negative base-2 log of the error
  • This value tells us how many bits of accuracy we have for each approximation
interpretation
Interpretation
  • Sine and Exponent give us 4 bits per power
    • Will only need a 7th order approximation for single floats
  • Why are tangent and arcsine so badly behaved?
    • We will find out shortly…
  • Square Root will never be approximated by a polynomial.
    • Less than half a bit of accuracy per power
    • We must use Newton’s Method with good initial guesses
hardware square root
Hardware Square Root
  • You will never out perform a hardware square root
    • To avoid serializing the Newtons Iterations hardware designs use a redundant signed-digit number system.
    • Carry propagation happens only once, at the end of the calculation
tangent definitions
Tangent Definitions
  • Tangent is defined as the ratio of sine over cosine
  • For infrequent use this calculation is good enough
    • Rememember to test for cos(x)=0 and return BIGNUM
    • Has twice the inaccuracy of sin or cosine
using trig identities
Using Trig Identities
  • We must range reduce as much as possible
    • Using trigonometric identities to help us
  • We record the sign, force x to positive
    • We will replace the sign during reconstruction
    • We have halved our input range
range reduction141
Range Reduction
  • The next identity is the fun one
  • This tells us two things

1. The range 0..Pi/2 is repeated over and over.

2. There is a sweet spot at Pi/4 where both sides are equal.

minimax approximations
Minimax Approximations
  • Tangent between 0..Pi/4 is simple enough
    • Looking at the Taylor Series, it uses odd powers.
    • Apart from being strangely resistant to polynomial approximation, it is a straightforward minimax.
cotangent minimax
Cotangent Minimax
  • But how to approximate Cotangent?
    • It has infinite values and high curvature
  • There is a trick using small angles
    • The arctangent is just cosine/sine
    • As x®0 the sine®x and cosine®1
    • Meaning that as x®0 then cotangent ® 1/x
cotangent minimax148
Cotangent Minimax
  • We can use a different minimax form
  • This has an interesting property
    • The reciprocal is based on nothing but x
    • We can start a reciprocal instruction way up the pipeline and use the result after calculating the difference polynomial
final tangent form
Final Tangent Form
  • The final form of the tangent function
rational polynomials
Rational Polynomials
  • Minimax can also produce rational polynomials
    • A pair of polynomials that are divided by each other to produce the final result
  • Rational Polynomials can be very accurate for low powers
    • If you can take the cost of a divide or reciprocal at the end of the approximation process
    • Popular for high accuracy functions
rational tangent
Rational Tangent
  • Rational approximation to tangent
    • Gives 1.3e-8 accuracy using fewer operations
    • Can improve this by dividing through by denominator constant, saving us a load-constant instruction
inverse trig functions
Inverse Trig Functions
  • Arctangent
    • The inverse of a tangent
    • Maps [–inf..+inf] to [–p/2..p/2]
  • Arcsine and Arccosine
    • Asin maps [-1..+1] to [–p/2..p/2]
    • Acos maps [-1..+1] to [p..0]
why only arctangent
Why only Arctangent?
  • Atan, Acos and Asin are closely related
range reduction156
Range Reduction
  • Arctan has these interesting properties
  • We only need to define arctan between 0..1
    • That is one huge range reduction
arctan taylor series
Arctan Taylor Series
  • The Taylor series for Arctangent looks strangely familiar, yet different…
    • It converges very, very slowly
    • Doesn’t even converge for |x|>1
    • For 1e-8 accuracy you need to evaluate 7071 terms!
    • We need polynomial approximations to make the function a viable calculation
minimax inequality
Minimax Inequality
  • The minimax form is very straightforward
  • But gives us very poor accuracy
    • A 13th power minimax approximation over 0..1 gives a maximal error of 0.9!
    • We need to reduce the range a lot more
double angle formulation
Double Angle Formulation
  • Arctangent is the inverse of a tangent
    • So let’s assume x = tan(a+b) so that arctan(x) = a+b.What are a and b ?
the x z mapping
The X-Z Mapping
  • How do we use this?
    • It remaps input x into the smaller z-range
    • We can subdivide 0..1 into, say, 2 segments, each with their own b offset and k parameter.
  • What value of k to choose?
    • If z(0) = -z(1) we have equal distribution either side of the graph, which happens when k = sqrt(2)-1
    • Or divide by equal input values – ¼, ½, ¾
designing your function
Now you have a set of tools to play with:

Range reduction to 0..1

Compliment if x>1.0

Choice of X-Z map

Equal-Angle maps are most accurate

Equal-Input maps are faster to range reduce

Choice of minimax order

Designing Your Function

float arctan(x)

{

float k,b,sign = +1.0f;

bool compl = false;

// remove sign

if(x<0.0) { s = -s; y = -y; }

// compliment if x>1.0

if(x>1.0) { compl = true;

x = 1/x;}

// x-z range reduce

if(x<0.5) { k = 0.25f;

b = arctan(k); }

else { k = 0.75f;

b = arctan(k); }

y = (y-k)/(1+k*y);

// poly approx

y = b + arctan_poly(x);

// finish compliment

if(compl) y:=Pi/2 - y;

// reinstate sign

return y*sign;

}

using arctan for arccosine
Using Arctan for Arccosine
  • The ideal example for using IEEE754 flags
    • To convert from arctan to arccosine we need to cope as x tends towards zero
    • Arccos(0.0) = Arctan(+inf) = Pi/2
    • We must remember to reset the divide-by-zero flag before returning from arccos(0.0)
exponent the anti log
Exponent: The Anti-Log
  • All exponents can be expressed as powers of the Euler Constant.
range reduction166
Range Reduction
  • Exponent also uses additive range reduction, much like sine and cosine
  • This means we break x into two parts
    • 2 raised to some exponent plus a fractional residual.
    • Which floating point numbers already do!
polynomial approximation167
Polynomial Approximation
  • Looking at the Taylor series
  • We can subtract the 1 and minimax it over the range [0..ln(2)/2] with
the exponent function
Two methods of coding

1. Breaking your input into exponent and mantissa and reassembling at the end

2. Using tables for 2N to keep all work in float registers, but only supporting a small range of input

The Exponent Function

float fast_exp(x)

{

const float C = 2.f/ln(2);

const float iC = ln(2)/2.f;

// range reduction

int N = (int)(x*C);

float y = x-N*invC;

// approximation

y = exp_poly(y) + 1.0f;

// reconstruction

return power2[N>>1]*y;

}

semi table based reconstruction
Semi-Table Based Reconstruction
  • New research has better methods for reconstructing the power of two
    • There are a family of algorithms by Tang where K > 1 in
    • To reconstruct, say, 2N/128 we calculate
    • Where the last two terms are from 8 and 16 entry tables
fast float exponent
Fast Float Exponent
  • How about just constructing a float directly from an integer power?
    • Remember how the IEEE exponent works.
    • float = sign * 2(exponent + 127) * 1.mantissa
    • Note also that :

01011100000100000000000000000000

fast float exponent171
Fast Float Exponent
  • Take an integer in [-126..127], e.g. 12
  • Integer multiply it by integer 223 / ln(2)
  • Add in the bias offset integer 127 * 223

00000000000000000000000000001100

* 00000000101110001010101000111011

= 00001000101001111111101011000101

+ 00111111100000000000000000000000

= 01001000001001111111101011000110

= 172011.09375

fast float exponent172
Fast Float Exponent
  • Plotting the function 1/(1-exp(-x)) shows an offset:
fast exponent fixed
Fast Exponent Fixed
  • Subtracting 0.7*223 = 5872025 removes the offset
increasing resolution
Increasing Resolution
  • The problem with single precision floats
    • there are only 255 exponent values we can use hence the staircase effect.
  • Solution:
    • Allow the input power to have some fractional bits.
    • Take float, multiply it by 23 = 8 and floor it, e.g. 12.125 = 97
    • Multiply by 2(23-3) / ln(2), add IEEE bias 127*223
    • Pick how many fractional bits you want, but the bias changes each time
      • e.g. if bits = 6 then bias = 3*217

00000000000000000000000001100001

logarithms
Logarithms
  • Ln(x) returns exponents
    • Defined in the range [0..+infinity]
  • There are two main logarithm functions
    • log(x) refers to the logarithm base 10
    • ln(x) refers to the Natural Logarithm base e
  • They are freely convertible
taylor series180
Taylor Series
  • The Taylor Series comes in two forms
    • First is closely related to the arctangent series
    • Second is simpler but converges as slowly
range reduction181
Range Reduction
  • Range reduction for ln uses multiplicative range reduction
    • Given that we store floating point numbers as exponent and mantissa
    • we find that the logarithm of such a number is
dissecting floats
Dissecting Floats
  • How do we split a float into exponent and mantissa?
    • On a PC it’s easy, you just mask and shift bits.
    • Remember to subtract 127 from the exponent.
  • On the PS2 Vector Units, we have a problem.
    • Only 16 bit integer registers – not enough to hold even the entire mantissa.
    • No way to shift the exponent into lower bits
    • Float-to-int conversion loses the exponent
  • But there is a trick…
extracting the exponent
Extracting The Exponent
  • What happens if you convert a float to a float?
    • Pretend the contents of a floating-point register is an integer value and convert this “integer” into a float.
    • For example, take the value 257*1.125
    • sign = 0exponent = 57 + 127 = 184mantissa = 1.125
    • This bit pattern represents the integer 1544552448

01011100000100000000000000000000

extracting the exponent184
Extracting The Exponent
  • Multiply 1544552448 by 1/223 to rescale the decimal point
  • Subtract the exponent bias of 127
  • Convert to integer to obtain the true exponent
a mathematical oddity bitlog
A Mathematical Oddity: Bitlog
  • A real mathematical oddity
    • The integer log2 of a 16 bit integer
    • Given an N-bit value, locate the leftmost nonzero bit.
    • b = the bitwise position of this bit, where 0 = LSB.
    • n = the NEXT three bits (ignoring the highest 1)
  • Bitlog is exactly 8 times larger than log2(x)-1
bitlog example
Bitlog Example
  • For example take the number 88

88 = 10110002

b = 6th bit

n = 0112 = 3

bitlog(88) = 8*(6-1)+3

= 43

    • (43/8)+1 = 6.375
    • Log2(88) = 6.4594…
  • This relationship holds down to bitlog(8)
integer powers
Integer Powers
  • Raising to an integer power
    • multiply all powers of x where N has a bit set
  • If N<0 then calculate 1/result
  • If x<0 then record sign and reinstate later
    • Note that odd powers of –ve number lose the sign
integer power function
As simple as it sounds

Loop through bits of N

Keep squaring x each loop

Careful noting and reinstating signs

float powi(float x,int N)

{

float y,w;

bool ns = false,xs = false;

// record signs

if(N<0) ns = true, N = -N;

if(x<0.0f) xs = true, x = -x;

// setup initial powers

if(N&1) y = x;

else y = 1.0f, xs = false;

w = x; N >>= 1;

while(N) {

w = w * w;

if(N&1) y *= w;

N >>= 1;

}

// reinstate signs

if(xs) y = -y;

if(ns) y = 1.0f/y;

}

Integer Power Function
x n approximation
XN Approximation
  • From Graphics Gems 4

approximation

actual

raising to a power
Raising to a Power
  • The canonical method is to do the calculation in two parts – log and exponent
    • Range reduction only needs to be done once.
    • MUST use extended accuracy in internal workings.
      • Log2(x) must have 8+ extra bits of accuracy
    • The most expensive operation in the toolbox.
    • Also the most difficult to write.
      • e.g. calculating 56156 naively gives an error of 1052 ULPS!
floating point power hack
Floating Point Power Hack
  • The log and power can be faked
    • If we force a float through the int-to-float conversion, the result is approximately logarithmic.
    • And conversely the inverse operation approximates
  • Credited to Matthew Jones, Infogrames
expanding out
Expanding Out
  • Combine these two operations to approximate the power function
    • So we have an extra term to subtract before we can use the approximate power.
floating point power hack195
Floating Point Power Hack
  • So, the algorithm goes like this
    • First, convert our float into logarithmic space
    • We multiply this value by the power we want to raise it to.
    • Then we subtract the magic constant
    • And finally reconvert back to a float

float a = float_as_int(x);

a = a * power;

magic = float_as_int(1.0f) * (power-1.0f);

a = a – magic;

a = int_as_float(x);

scaling the result
Scaling the Result
  • We can scale the resulting value
    • by reformulating the magic number
    • Can be rewritten to give us a 255 * xN for no additional work

magic = float_as_int(1.0f) * (power-1.0f);

a = a – magic;

magic = float_as_int(255.0f) – float_as_int(1.0f) * power;

a = a + magic;

another approach
Another Approach
  • An interesting effect
    • When raising pixel intensities in the range [0..255] to a power, for most of the range the value is less than one intensity level
    • As noted by Beaudoin & Guardado in “Direct3D ShaderX: Vertex and Pixel Tips and Tricks”
low power approximation
Low Power Approximation
  • Higher powers look a bit like lower powers scaled and offset
    • Example: Using x4 to approximate x16
low power approximation202
Low Power Approximation
  • Our function is zero until it is greater than one part in 255
low power approximation203
Low Power Approximation
  • New power approximation is:
    • Where n<<m and a and b are:
improved error
Improved Error
  • Using a binary search, improved values of a and b can minimize the maximal error
low power approx problem
Low Power Approx Problem…
  • There is however a problem
    • The paper doesn’t seem to have obvious “best” valuesfor a and b.
      • The results are just presented as a table of values with errors. The choice is already made for you.
    • Closer powers don’t seem to reduce the error
      • so there is no obvious way to control error, relative or absolute.
  • What is going on?
    • Let’s plot the maximal error for every power approximating every other as a surface…
low power approx problem207
Low Power Approx Problem…
  • The original example was a global minimum!

x4 approx of x16

conclusions209
Conclusions
  • Get to know IEEE754
    • It’s being updated as we speak
    • If your implementation differs, you will understand it’s weaknesses
  • Always test both absolute and relative error
    • Functions may look accurate on a graph
    • Relative error will show the tough weaknesses
conclusions210
Conclusions
  • Incremental Algorithms
    • Used for generating sequences of trig operations
    • Some of the fastest techniques around
  • Table Based Algorithms
    • Don’t discount them just yet
    • Tables are usually far too big – check the size
    • Use range reduction and reconstruction on tables
conclusions211
Conclusions
  • Polynomial Approximations
    • Some of the most flexible techniques around
    • Range Reduce, Approximate, Reconstruct
    • Choose between
      • Aggressive or simple range reduction
      • Polynomial order
      • Parallel polynomial evaluation techniques
      • Expensive or simple reconstruction
      • Semi table-based reconstruction
    • Use Maple or Mathematica to your advantage
conclusions212
Conclusions
  • New techniques
    • Floating Point Hacks
      • An exciting new set of techniques
    • Gaining support form hardware designers
      • IA64 architecture uses 8-bit mantissa lookup for 1/x
    • Combine with semi table-based reconstruction
      • Accuracy over a limited range
      • Research to be done on accurate approximations
references
References
  • Many of the original books are out of print
      • Researchers work with piles of photocopies!
    • Hart, J.F., Computer Approximations, John Wiley & Sons, 1968
    • Cody & Waite, Software Manual for the Elementary Functions, Prentice Hall, 1980
    • Goldberg, Steve, What Every Computer Scientist Should Know About Floating Point Arithmetic, ACM Computing Surveys, March, 1991
    • Muller, J.M., Elementary Functions: Algorithms and Implementations, Birkhaüser, 1997
    • Tang, Ping Tak Peter, Table Lookup Algorithms for Elementary Functions and Their Error Analysis, Proceedings of 10th Symposium on Computer Arithmetic, 1991
website
Website
  • Notes available from:

http://research.scea.com/

robin_green@playstation.sony.com