Pythagorean-Triple-Based Table Generation for Trigonometric Function - - PowerPoint PPT Presentation

pythagorean triple based table generation for
SMART_READER_LITE
LIVE PREVIEW

Pythagorean-Triple-Based Table Generation for Trigonometric Function - - PowerPoint PPT Presentation

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation MetaLibm Project Annual Meeting2015 Hugues de Lassus Saint-Genis Advisors: David Defour and Guillaume Revy DALI project-team, University of Perpignan Via


slide-1
SLIDE 1

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation

MetaLibm Project Annual Meeting’2015 Hugues de Lassus Saint-Geniès Advisors: David Defour and Guillaume Revy

DALI project-team, University of Perpignan Via Domitia LIRMM, University of Montpellier 2 CNRS UMR 5506

18th February 2015

slide-2
SLIDE 2

Who I am in 30”

Paris

→ 2008: bac S 2008 → 2011: CPGE

Grenoble

2 1 1

2011 → 2013: Grenoble INP S2 2013: univ. autonome de Basse-Californie

Ensenada, Mexico

August 2013

February 2014: internships (Vérimag, Argosim)

February 2014

October 2014: DALI/UPVD/LIRMM

Perpignan

October 2014

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 1 / 24

slide-3
SLIDE 3

1

DALI in MetaLibm

2

Background on Function Evaluation

3

Primitive Pythagorean Triples

4

Experimental Results

5

Conclusion and Perspectives

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 2 / 24

slide-4
SLIDE 4

DALI in the MetaLibm Project

Architecture-aware code generation:

◮ Parallelism, vector operations? ◮ FPU? ◮ Memory management, caches? ◮ Features like FMA, if-conversion?

Focus on elementary functions Contributions: high quality code generation chain MetaLibm function interval architecture accuracy

  • ptimized

code

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 2 / 24

slide-5
SLIDE 5

Basic Scheme for Function Evaluation

x no yes Special input Floating-point number unpacking Normalization Range redution Sign and exponent computation Significand approximation Rounding decision Correct rounding computation Special output selection Result construction r {±0, ±∞, NaN, . . .}

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 3 / 24

slide-6
SLIDE 6

Basic Scheme for Function Evaluation

x no yes Special input Floating-point number unpacking Normalization Range redution Sign and exponent computation Significand approximation Rounding decision Correct rounding computation Special output selection Result construction r {±0, ±∞, NaN, . . .}

We focus on: sine and cosine range reduction based on tabulated values combined with polynomial approximations

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 3 / 24

slide-7
SLIDE 7

Basic Scheme for Sine and Cosine Evaluation

Let y be a floating-point number, y ∈ F.

1 Range reduction

x = y − k π 2 , such that x ∈ (−π 4 , π 4 ], k ∈ Z

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 4 / 24

slide-8
SLIDE 8

Basic Scheme for Sine and Cosine Evaluation

Let y be a floating-point number, y ∈ F.

1 Range reduction

x = y − k π 2 , such that x ∈ (−π 4 , π 4 ], k ∈ Z

2 Keeping only values in [0, π/4]:

x ← s · x with s = sign(x)

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 4 / 24

slide-9
SLIDE 9

Basic Scheme for Sine and Cosine Evaluation

Let y be a floating-point number, y ∈ F.

1 Range reduction

x = y − k π 2 , such that x ∈ (−π 4 , π 4 ], k ∈ Z

2 Keeping only values in [0, π/4]:

x ← s · x with s = sign(x)

3 Splitting x into high and low parts xh and xℓ

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 4 / 24

slide-10
SLIDE 10

Basic Scheme for Sine and Cosine Evaluation

Let y be a floating-point number, y ∈ F.

1 Range reduction

x = y − k π 2 , such that x ∈ (−π 4 , π 4 ], k ∈ Z

2 Keeping only values in [0, π/4]:

x ← s · x with s = sign(x)

3 Splitting x into high and low parts xh and xℓ 4 Table lookup of precomputed sine and cosine of xh

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 4 / 24

slide-11
SLIDE 11

Basic Scheme for Sine and Cosine Evaluation

Let y be a floating-point number, y ∈ F.

1 Range reduction

x = y − k π 2 , such that x ∈ (−π 4 , π 4 ], k ∈ Z

2 Keeping only values in [0, π/4]:

x ← s · x with s = sign(x)

3 Splitting x into high and low parts xh and xℓ 4 Table lookup of precomputed sine and cosine of xh 5 Polynomial approximations of sine and cosine of xℓ

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 4 / 24

slide-12
SLIDE 12

Basic Scheme for Sine and Cosine Evaluation

Let y be a floating-point number, y ∈ F.

1 Range reduction

x = y − k π 2 , such that x ∈ (−π 4 , π 4 ], k ∈ Z

2 Keeping only values in [0, π/4]:

x ← s · x with s = sign(x)

3 Splitting x into high and low parts xh and xℓ 4 Table lookup of precomputed sine and cosine of xh 5 Polynomial approximations of sine and cosine of xℓ 6 Reconstruction using basic trigonometric relations:

sin(x) = sin(xh) · cos(xℓ) + cos(xh) · sin(xℓ) Then sin(y) depending on value of k ≡ 4 and s.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 4 / 24

slide-13
SLIDE 13

Basic Scheme for Sine and Cosine Evaluation

Let y be a floating-point number, y ∈ F.

1 Range reduction

x = y − k π 2 , such that x ∈ (−π 4 , π 4 ], k ∈ Z

2 Keeping only values in [0, π/4]:

x ← s · x with s = sign(x)

3 Splitting x into high and low parts xh and xℓ 4 Table lookup of precomputed sine and cosine of xh 5 Polynomial approximations of sine and cosine of xℓ 6 Reconstruction using basic trigonometric relations:

sin(x) = sin(xh) · cos(xℓ) + cos(xh) · sin(xℓ) Then sin(y) depending on value of k ≡ 4 and s.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 4 / 24

slide-14
SLIDE 14

Sources of Error in the Classical Method

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ) ⊖ sinh ⊗ sin(xℓ) sin(x) = sinh ⊗ cos(xℓ) ⊕ cosh ⊗ sin(xℓ) cosh sinh cos(xℓ + corr) sin(xℓ + corr) cos(xℓ) sin(xℓ) xh xℓ

Errors appear in: x, inherently by the range reduction

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 5 / 24

slide-15
SLIDE 15

Sources of Error in the Classical Method

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ) ⊖ sinh ⊗ sin(xℓ) sin(x) = sinh ⊗ cos(xℓ) ⊕ cosh ⊗ sin(xℓ) cosh sinh cos(xℓ + corr) sin(xℓ + corr) cos(xℓ) sin(xℓ) xh xℓ

Errors appear in: x, inherently by the range reduction sinh and cosh, which are rounded approximations

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 5 / 24

slide-16
SLIDE 16

Sources of Error in the Classical Method

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ) ⊖ sinh ⊗ sin(xℓ) sin(x) = sinh ⊗ cos(xℓ) ⊕ cosh ⊗ sin(xℓ) cosh sinh cos(xℓ + corr) sin(xℓ + corr) cos(xℓ) sin(xℓ) xh xℓ

Errors appear in: x, inherently by the range reduction sinh and cosh, which are rounded approximations sin(xℓ) and cos(xℓ), which are computed by polynomial evaluation

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 5 / 24

slide-17
SLIDE 17

Sources of Error in the Classical Method

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ) ⊖ sinh ⊗ sin(xℓ) sin(x) = sinh ⊗ cos(xℓ) ⊕ cosh ⊗ sin(xℓ) cosh sinh cos(xℓ + corr) sin(xℓ + corr) cos(xℓ) sin(xℓ) xh xℓ

Errors appear in: x, inherently by the range reduction sinh and cosh, which are rounded approximations sin(xℓ) and cos(xℓ), which are computed by polynomial evaluation in the reconstruction process

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 5 / 24

slide-18
SLIDE 18

Sources of Error in the Classical Method

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ) ⊖ sinh ⊗ sin(xℓ) sin(x) = sinh ⊗ cos(xℓ) ⊕ cosh ⊗ sin(xℓ) cosh sinh cos(xℓ + corr) sin(xℓ + corr) cos(xℓ) sin(xℓ) xh xℓ

Errors appear in: x, inherently by the range reduction sinh and cosh, which are rounded approximations sin(xℓ) and cos(xℓ), which are computed by polynomial evaluation in the reconstruction process Drawback: additional bits in computation steps for accuracy purpose

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 5 / 24

slide-19
SLIDE 19

Reducing the Error on Tabulated Values1

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ + corr) ⊖ sinh ⊗ sin(xℓ + corr) sin(x) = sinh ⊗ cos(xℓ + corr) ⊕ cosh ⊗ sin(xℓ + corr) cosh sinh cos(xℓ + corr) sin(xℓ + corr) xh xℓ corr

Gal’s accurate table method 1 more source of error: correction term corr reduces the error on sinh, cosh, corr by making them very close to floating-point numbers

1Gal and Bachelis, “An Accurate Elementary Mathematical Library for the IEEE

Floating Point Standard”.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 6 / 24

slide-20
SLIDE 20

Our Method for Reducing the Error on Tabulated Values

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ + corr) ⊖ sinh ⊗ sin(xℓ + corr) sin(x) = sinh ⊗ cos(xℓ + corr) ⊕ cosh ⊗ sin(xℓ + corr) cosh sinh cos(xℓ + corr) sin(xℓ + corr) xh xℓ corr

Our method: still 1 more source of error: correction term corr but sinh and cosh stored exactly, that is, without any rounding error

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 7 / 24

slide-21
SLIDE 21

Our Method for Reducing the Error on Tabulated Values

x ∈ [0, π

4 ]

Splitting x into (xh, xℓ) Table lookup Polynomial approximations Reconstruction cos(x) = cosh ⊗ cos(xℓ + corr) ⊖ sinh ⊗ sin(xℓ + corr) sin(x) = sinh ⊗ cos(xℓ + corr) ⊕ cosh ⊗ sin(xℓ + corr) cosh sinh cos(xℓ + corr) sin(xℓ + corr) xh xℓ corr

Our method: still 1 more source of error: correction term corr but sinh and cosh stored exactly, that is, without any rounding error How? With Pythagorean triples.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 7 / 24

slide-22
SLIDE 22

Pythagorean Triples

Let TP and TPP be the sets of pythagorean triples and prime pythagorean triples, respectively. TP = {(a, b, c) ∈ N∗3 | a2 + b2 = c2} TPP = {(a, b, c) ∈ TP | a, b, c coprime}

α b a c

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 8 / 24

slide-23
SLIDE 23

Pythagorean Triples

Let TP and TPP be the sets of pythagorean triples and prime pythagorean triples, respectively. TP = {(a, b, c) ∈ N∗3 | a2 + b2 = c2} TPP = {(a, b, c) ∈ TP | a, b, c coprime}

α b a c

For each pythagorean triple (a, b, c): sin(α) = a/c cos(α) = b/c

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 8 / 24

slide-24
SLIDE 24

Pythagorean Triples

500 1000 1500 2000 2500 3000 3500 4000 500 1000 1500 2000 2500 3000 3500 4000 π/4

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 9 / 24

slide-25
SLIDE 25

Pythagorean Triples

500 1000 1500 2000 2500 3000 3500 4000 500 1000 1500 2000 2500 3000 3500 4000 π/4

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 9 / 24

slide-26
SLIDE 26

Pythagorean Triples

500 1000 1500 2000 2500 3000 3500 4000 500 1000 1500 2000 2500 3000 3500 4000 π/4

corr

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 9 / 24

slide-27
SLIDE 27

Pythagorean Triples

500 1000 1500 2000 2500 3000 3500 4000 500 1000 1500 2000 2500 3000 3500 4000 π/4

corr b a c

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 9 / 24

slide-28
SLIDE 28

Primitive Pythagorean Triples Generation

One easy way: ternary-trees with root (3, 4, 5)2 3 linear relationships 3 children/node, e.g:3

  1 −2 2 2 −1 2 2 −2 3   ,   −1 2 2 2 −1 2 −2 2 3   ,   1 2 2 2 1 2 2 2 3  

2Price, “The Pythagorean Tree: A New Species”. 3Barning, “On Pythagorean and quasi-Pythagorean triangles and a generation

process with the help of unimodular matrices”.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 10 / 24

slide-29
SLIDE 29

Primitive Pythagorean Triples Generation

One easy way: ternary-trees with root (3, 4, 5)2 3 linear relationships 3 children/node, e.g:3

  1 −2 2 2 −1 2 2 −2 3   ,   −1 2 2 2 −1 2 −2 2 3   ,   1 2 2 2 1 2 2 2 3  

3, 4, 5 5, 12, 13 15, 8, 17 21, 20, 29

2Price, “The Pythagorean Tree: A New Species”. 3Barning, “On Pythagorean and quasi-Pythagorean triangles and a generation

process with the help of unimodular matrices”.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 10 / 24

slide-30
SLIDE 30

Primitive Pythagorean Triples with c ≤ 212

500 1000 1500 2000 2500 3000 3500 4000 500 1000 1500 2000 2500 3000 3500 4000

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 11 / 24

slide-31
SLIDE 31

Primitive Pythagorean Triples with c ≤ 212

500 1000 1500 2000 2500 3000 3500 4000 500 1000 1500 2000 2500 3000 3500 4000 π/4

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 11 / 24

slide-32
SLIDE 32

Primitive Pythagorean Triples with c ≤ 212

500 1000 1500 2000 2500 3000 3500 4000 500 1000 1500 2000 2500 3000 3500 4000 π/4

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 11 / 24

slide-33
SLIDE 33

Utility of Primitive Pythagorean Triples: Solution 1

For each table entry: store a, b, c as float or double exactly compute and store corr = xh − arcsin(a/c)

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 12 / 24

slide-34
SLIDE 34

Utility of Primitive Pythagorean Triples: Solution 1

For each table entry: store a, b, c as float or double exactly compute and store corr = xh − arcsin(a/c) Reconstruction steps:

1 Table lookup of a, b, c, corr 2 Polynomial approximations

Cℓ ≈ cos(xℓ + corr) and Sℓ ≈ sin(xℓ + corr)

3 Result reconstruction:

sin(x) = (a ⊗ Cℓ ⊕ b ⊗ Sℓ) ⊘ c

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 12 / 24

slide-35
SLIDE 35

Utility of Primitive Pythagorean Triples: Solution 1

For each table entry: store a, b, c as float or double exactly compute and store corr = xh − arcsin(a/c) Reconstruction steps:

1 Table lookup of a, b, c, corr 2 Polynomial approximations

Cℓ ≈ cos(xℓ + corr) and Sℓ ≈ sin(xℓ + corr)

3 Result reconstruction:

sin(x) = (a ⊗ Cℓ ⊕ b ⊗ Sℓ) ⊘ c ⊖ Additional cost of 1 division by c slower, more rounding.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 12 / 24

slide-36
SLIDE 36

Utility of Primitive Pythagorean Triples: Solution 2

For each table entry: store exactly A = a

c · k and B = b c · k such that A, B ∈ N

include 1

k in polynomial coefficients

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 13 / 24

slide-37
SLIDE 37

Utility of Primitive Pythagorean Triples: Solution 2

For each table entry: store exactly A = a

c · k and B = b c · k such that A, B ∈ N

include 1

k in polynomial coefficients

⇒ k should be a small lowest common multiple (lcm) of all possible combinations of one triple hypothenuse (c) per table entry

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 13 / 24

slide-38
SLIDE 38

Utility of Primitive Pythagorean Triples: Solution 2

For each table entry: store exactly A = a

c · k and B = b c · k such that A, B ∈ N

include 1

k in polynomial coefficients

⇒ k should be a small lowest common multiple (lcm) of all possible combinations of one triple hypothenuse (c) per table entry Reconstruction steps:

1 Table-lookup of A, B, corr 2 Polynomial approximations

Sℓ ≈ sin(xℓ + corr) k and Cℓ ≈ cos(xℓ + corr) k

3 Result reconstruction:

sin(x) = A ⊗ Cℓ ⊕ B ⊗ Sℓ

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 13 / 24

slide-39
SLIDE 39

Table Generation Algorithm

Inputs

◮ p: table index size ◮ n: log2(·) of the bound on considered hypothenuse triples

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 14 / 24

slide-40
SLIDE 40

Table Generation Algorithm

Inputs

◮ p: table index size ◮ n: log2(·) of the bound on considered hypothenuse triples

Algorithm steps

1

Compute triples with c ≤ 2n

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 14 / 24

slide-41
SLIDE 41

Table Generation Algorithm

Inputs

◮ p: table index size ◮ n: log2(·) of the bound on considered hypothenuse triples

Algorithm steps

1

Compute triples with c ≤ 2n

2

Determine all the possible lcm among computed hypotenuses

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 14 / 24

slide-42
SLIDE 42

Table Generation Algorithm

Inputs

◮ p: table index size ◮ n: log2(·) of the bound on considered hypothenuse triples

Algorithm steps

1

Compute triples with c ≤ 2n

2

Determine all the possible lcm among computed hypotenuses

3

Search for a lcm k common to all table entries

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 14 / 24

slide-43
SLIDE 43

Table Generation Algorithm

Inputs

◮ p: table index size ◮ n: log2(·) of the bound on considered hypothenuse triples

Algorithm steps

1

Compute triples with c ≤ 2n

2

Determine all the possible lcm among computed hypotenuses

3

Search for a lcm k common to all table entries

4

For each entry, search for a triple with hypothenuse k and minimizing correction term

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 14 / 24

slide-44
SLIDE 44

Table Generation Algorithm

Inputs

◮ p: table index size ◮ n: log2(·) of the bound on considered hypothenuse triples

Algorithm steps

1

Compute triples with c ≤ 2n

2

Determine all the possible lcm among computed hypotenuses

3

Search for a lcm k common to all table entries

4

For each entry, search for a triple with hypothenuse k and minimizing correction term

Outputs

◮ table with entries of the form

table[i] = {Ai, Bi, corri}

◮ polynomial approximants of sin(xℓ+corr)

k

and cos(xℓ+corr)

k

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 14 / 24

slide-45
SLIDE 45

Experimental Protocol

./search_lcm --address=1 --algorithm=exhaustive \

  • -omit-first n p

means:

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 15 / 24

slide-46
SLIDE 46

Experimental Protocol

./search_lcm --address=1 --algorithm=exhaustive \

  • -omit-first n p

means:

1 ⌈ π

4 · 2p⌉ table entries

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 15 / 24

slide-47
SLIDE 47

Experimental Protocol

./search_lcm --address=1 --algorithm=exhaustive \

  • -omit-first n p

means:

1 ⌈ π

4 · 2p⌉ table entries

2 search k among generated hypotenuses less than 2n

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 15 / 24

slide-48
SLIDE 48

Experimental Protocol

./search_lcm --address=1 --algorithm=exhaustive \

  • -omit-first n p

means:

1 ⌈ π

4 · 2p⌉ table entries

2 search k among generated hypotenuses less than 2n 3 discard first entry: triple (0, 1, 1) is fine

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 15 / 24

slide-49
SLIDE 49

Example for (n, p) = (5, 2)

./search_lcm --address=1 --algorithm=exhaustive \

  • -omit-first 5 2
  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 16 / 24

slide-50
SLIDE 50

Example for (n, p) = (5, 2)

./search_lcm --address=1 --algorithm=exhaustive \

  • -omit-first 5 2

Output table:

static const double lcm = 25; static const double table [4][3] = { { /* A = */ 0 , /* B = */ 25, /* corr = */ 0 x0000000000000000 }, /* corr ~ 0 */ /* angle ~ 0 */ /* ppt = (0,1,1) */ { /* A = */ 7 , /* B = */ 24, /* corr = */ 0 xbfa14d7623b1c6e8 }, /* corr ~

  • 0.0337941 */

/* angle ~ 0.283794 */ /* ppt = (7 ,24 ,25) */ { /* A = */ 15, /* B = */ 20, /* corr = */ 0 xbfc25e3e8c9a7b84 }, /* corr ~

  • 0.143501 */

/* angle ~ 0.643501 */ /* ppt = (3,4,5) */ { /* A = */ 20, /* B = */ 15, /* corr = */ 0 xbfc6b19c1586ed40 } /* corr ~

  • 0.177295 */

/* angle ~ 0.927295 */ /* ppt = (4,3,5) */ };

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 16 / 24

slide-51
SLIDE 51

Example for (n, p) = (5, 2)

./search_lcm --address=1 --algorithm=exhaustive \

  • -omit-first 5 2

Output table:

static const double lcm = 25; static const double table [4][3] = { { /* A = */ 0 , /* B = */ 25, /* corr = */ 0 x0000000000000000 }, /* corr ~ 0 */ /* angle ~ 0 */ /* ppt = (0,1,1) */ { /* A = */ 7 , /* B = */ 24, /* corr = */ 0 xbfa14d7623b1c6e8 }, /* corr ~

  • 0.0337941 */

/* angle ~ 0.283794 */ /* ppt = (7 ,24 ,25) */ { /* A = */ 15, /* B = */ 20, /* corr = */ 0 xbfc25e3e8c9a7b84 }, /* corr ~

  • 0.143501 */

/* angle ~ 0.643501 */ /* ppt = (3,4,5) */ { /* A = */ 20, /* B = */ 15, /* corr = */ 0 xbfc6b19c1586ed40 } /* corr ~

  • 0.177295 */

/* angle ~ 0.927295 */ /* ppt = (4,3,5) */ }; 5 10 15 20 25 5 10 15 20 25 Stored PT Selected PPT π/4

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 16 / 24

slide-52
SLIDE 52

Exhaustive Search

Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60 GHz (32 cores) 125 GB RAM p n kmin time (s) kmax t (s) hypotenuses 3 10 725 ≪ 1 725 ≪ 1 125 4 14 10, 625 0.01 13, 325 0.02 1, 712 5 17 130, 645 0.14 130, 645 0.08 12, 463 6 21 1, 676, 285 6 1, 698, 385 3.8 179, 632 7 25 32, 846, 125 1, 000 32, 846, 125 65 2, 635, 323

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 17 / 24

slide-53
SLIDE 53

Exhaustive Search

Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60 GHz (32 cores) 125 GB RAM p n kmin time (s) kmax t (s) hypotenuses 3 10 725 ≪ 1 725 ≪ 1 125 4 14 10, 625 0.01 13, 325 0.02 1, 712 5 17 130, 645 0.14 130, 645 0.08 12, 463 6 21 1, 676, 285 6 1, 698, 385 3.8 179, 632 7 25 32, 846, 125 1, 000 32, 846, 125 65 2, 635, 323 Current bottlenecks: searching the lcm and memory Finding k for a 10-bit addressed table is desperate with this exhaustive technique

◮ our estimations show that n ≈ 37

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 17 / 24

slide-54
SLIDE 54

Focus on Exhaustive Search for (n, p) = (25, 7)

[Sun Feb 15 11:04:05 2015] Start Pythagorean -triple -based table generation tool *** table address size (# bits): 7 *** maximum hypothenuse size (# bits): 25 ***

  • mit

first table cell: yes *** algorithm: exhaustive search *** addressing mode: with

  • ffset

*** max angle: 0.789062 rad *** entry width: 0.0078125 rad ***

  • utput: cout

[Sun Feb 15 11:04:06 2015] Triple generation

  • n 25 bits ...

done. [Sun Feb 15 11:04:12 2015] Triple sort by angle division ... done. *** 101 angle divisions are used [Sun Feb 15 11:04:22 2015] Lowest common multiple computation ... done. *** computed lcm: 32846125 [Sun Feb 15 11:05:09 2015] Triple table building ... done. [Sun Feb 15 11:05:10 2015] Table printing

  • ut ...

done [Sun Feb 15 11:05:10 2015] End Pythagorean -triple -based table generation tool

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 18 / 24

slide-55
SLIDE 55

Optimization of LCM Searching

For kmin: search lcm in [2n−1, 2n] algorithm called big_c

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 19 / 24

slide-56
SLIDE 56

Optimization of LCM Searching

For kmin: search lcm in [2n−1, 2n] algorithm called big_c p n kmin time (s) hypotenuses 5 17 130, 645 0.05 6, 044 6 21 1, 676, 285 4.5 87, 633 7 25 32, 846, 125 670 1, 290, 663 ⊕ ≈ twice less hypotenuses ⊖ still a bottleneck: too many triples/hypotenuses!

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 19 / 24

slide-57
SLIDE 57

First Heuristic

p n kmin prime factorization triples 5 17 130, 645 5 · 17 · 29 · 53 21, 588 6 21 1, 676, 285 5 · 13 · 17 · 37 · 41 338, 660 7 25 32, 846, 125 53 · 13 · 17 · 29 · 41 5, 365, 290 For hypotenuses in [2n−1, 2n], only keep those ending by 5

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 20 / 24

slide-58
SLIDE 58

First Heuristic

p n kmin prime factorization triples 5 17 130, 645 5 · 17 · 29 · 53 21, 588 6 21 1, 676, 285 5 · 13 · 17 · 37 · 41 338, 660 7 25 32, 846, 125 53 · 13 · 17 · 29 · 41 5, 365, 290 For hypotenuses in [2n−1, 2n], only keep those ending by 5 p n kmin time (s) triples hypotenuses 6 21 1, 676, 285 1.6 225, 788 18, 505 7 25 32, 846, 125 213 3, 576, 819 270, 360 Roughly: ×3 speedup compared to big_c

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 20 / 24

slide-59
SLIDE 59

Second heuristic

Observation: k is always a product of small prime factors of the form 4n + 1 Pythagorean primes4 Pythagorean primes less than 70: p = {5, 13, 17, 29, 37, 41, 53, 61}

4Fässler, “Multiple Pythagorean Number Triples”.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 21 / 24

slide-60
SLIDE 60

Second heuristic

Observation: k is always a product of small prime factors of the form 4n + 1 Pythagorean primes4 Pythagorean primes less than 70: p = {5, 13, 17, 29, 37, 41, 53, 61} During the generation, only keep triples with hypotenuse c =

  • p[i]ri · Q

with Q < 5 and ri = 0

  • r

1 if p[i] = 5

4Fässler, “Multiple Pythagorean Number Triples”.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 21 / 24

slide-61
SLIDE 61

Second heuristic

Observation: k is always a product of small prime factors of the form 4n + 1 Pythagorean primes4 Pythagorean primes less than 70: p = {5, 13, 17, 29, 37, 41, 53, 61} During the generation, only keep triples with hypotenuse c =

  • p[i]ri · Q

with Q < 5 and ri = 0

  • r

1 if p[i] = 5 n p kmin time (s) triples hypotenuses 21 6 1,676,285 0.09 1,566 43 25 7 32,846,125 0.8 3,308 48 28 8 243,061,325 4.3 5,217 49

4Fässler, “Multiple Pythagorean Number Triples”.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 21 / 24

slide-62
SLIDE 62

Limit of our Implementation

With such results. . . why not try with p = 9?

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 22 / 24

slide-63
SLIDE 63

Limit of our Implementation

With such results. . . why not try with p = 9? For p = 9, n ≈ 33 from our estimations.

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 22 / 24

slide-64
SLIDE 64

Limit of our Implementation

With such results. . . why not try with p = 9? For p = 9, n ≈ 33 from our estimations. ./search_lcm --address=1 --omit-first \

  • -algorithm=big_c 33 9

n p kmin time (s) triples hypotenuses 33 9 SIGSEGV – – –

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 22 / 24

slide-65
SLIDE 65

Limit of our Implementation

With such results. . . why not try with p = 9? For p = 9, n ≈ 33 from our estimations. ./search_lcm --address=1 --omit-first \

  • -algorithm=big_c 33 9

n p kmin time (s) triples hypotenuses 33 9 SIGSEGV – – – Limit: recursion depth in generation function for p = 9, it was 40,310 when program crashed

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 22 / 24

slide-66
SLIDE 66

Conclusion

A new approach using primitive pythagorean triples for sine and cosine evaluation:

⊲ removing the error on two tabulated terms ⊲ concentrating the error on half the terms in the reconstruction

A prototype able to compute:

⊲ size-variable tables up to 8 indexing bits ⊲ size-matching approximation polynomials with Sollya

  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 23 / 24

slide-67
SLIDE 67

Perspectives

Generation of tables addressed by more than 8 bits Evaluation of the actual accuracy and performance of sine and cosine implementations using this approach Integration in a full code-generation chain Number theory might help us: ignore bad triples directly build hypotenuses composed of Pythagorean primes select the best triple for each table entry, reducing the lcm-search to

  • ne computation
  • H. de Lassus Saint-Geniès

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation 24 / 24

slide-68
SLIDE 68

Pythagorean-Triple-Based Table Generation for Trigonometric Function Evaluation

MetaLibm Project Annual Meeting’2015 Hugues de Lassus Saint-Geniès Advisors: David Defour and Guillaume Revy

DALI project-team, University of Perpignan Via Domitia LIRMM, University of Montpellier 2 CNRS UMR 5506

18th February 2015

slide-69
SLIDE 69

Perspectives

Experiment shows that for p = 10, n needs only be 20 to get at least

  • ne triple/entry (forgetting first entry).

50 100 150 200 250 300 350 400 100 200 300 400 500 600 700 800 Number of PPT wrt entry number

⊖ Still, computing ≈ 200800 combinations is not yet foreseen. . .