Saint-Geni Hugues De Lassus

David Ès

Guillaume Defour

Revy

Exact Lookup Tables for the Evaluation of Trigonometric and Hyperbolic Functions

Keywords: Elementary function approximation, table-based range reduction, exact lookup tables, computer arithmetic. !

Elementary mathematical functions are pervasively used in many applications such as electronic calculators, computer simulations, or critical embedded systems. Their evaluation is always an approximation, which usually makes use of mathematical properties, precomputed tabulated values, and polynomial approximations. Each step generally combines error of approximation and error of evaluation on finite-precision arithmetic. When they are used, tabulated values generally embed rounding error inherent to the transcendence of elementary functions. In this article, we propose a general method to use error-free values that is worthy when two or more terms have to be tabulated in each table row. For the trigonometric and hyperbolic functions, we show that Pythagorean triples can lead to such tables in little time and memory usage. When targeting correct rounding in double precision for the same functions, we also show that this method saves memory and floating-point operations by up to 29% and 42%, respectively.

INTRODUCTION

T HE representation formats and the behavior of binary floating-point arithmetics available on general-purpose processors are defined by the IEEE 754-2008 standard [START_REF]IEEE Standard for Floating-Point Arithmetic[END_REF]. For basic arithmetic operations such as +, -, ×, ÷, √ , or fma, this standard requires the system to return the correct rounding of the exact result, according to one of four rounding-direction attributes (to nearest ties to even, toward -∞, toward +∞, and toward 0): this property guarantees the quality of the result. However, due to the Table Maker's Dilemma (TMD) [START_REF] Lefèvre | Toward correctly rounded transcendentals[END_REF] and the difficulties in developing accurate and efficient evaluation schemes, correct rounding is only recommended for elementary functions defined in the IEEE 754-2008 standard.

The algorithms developed for the evaluation of elementary functions, such as the logarithm, the exponential, or the trigonometric and hyperbolic functions, can be classified into at least two categories. The first category concerns algorithms based on small and custom operators combined with tabulated values that target small accuracies, typically less than 30 bits [START_REF] Sarma | Faithful bipartite ROM reciprocal tables[END_REF], [START_REF] De Dinechin | Multipartite table methods[END_REF], [START_REF] Defour | A new scheme for table-based evaluation of functions[END_REF]. The second category concerns algorithms that usually target single or double precision (24 or 53 bits of precision) with an implementation on general processors that relies on the available hardware units [START_REF]CR-Libm, a library of correctly rounded elementary functions in double-precision[END_REF], [START_REF] Ziv | Fast evaluation of elementary mathematical functions with correctly rounded last bit[END_REF].

Implementations of those functions that target correct rounding are usually divided into two or more phases [START_REF] Muller | Elementary Functions: Algorithms and Implementation[END_REF]. A quick phase is first performed: it is based on a fast approximation which provides a few more bits than the targeted format, which makes correct rounding possible most of the time at a reasonable cost. When correct rounding is not possible, a much slower accurate phase is executed. The quick phase uses operations with a precision slightly greater than the targeted precision, while the accurate phase is based on a much larger precision. For example, in the correctly rounded library CR-Libm, the quick phase for the sine and cosine functions in double precision targets 66 bits while the accurate phase corresponds to 200 bits [6, § 9]. And then in order to guarantee that an implementation actually computes correctly rounded results, a proof of correctness has to be built. This proof is based on the mandatory number of bits required to ensure correct rounding, which is linked with the search for the worst cases for the TMD [START_REF] Lefèvre | Hardest-to-round cases[END_REF].

The design of such correctly rounded implementations is a real challenge, as it requires to control and limit every source of numerical error [START_REF] Chevillard | Efficient and accurate computation of upper bounds of approximation errors[END_REF]. Indeed these implementations involve various steps, including range reduction, polynomial evaluation, and reconstruction. And at each of these steps, errors may occur. Since those errors accumulate and propagate up to the final result, any solution or algorithm that reduces them will have an impact on the simplicity of the proof of correctness and the performance of the implementation.

Evaluation of Elementary Functions

In this article, we deal with the implementation of trigonometric and hyperbolic functions. In order to better understand the challenges that have to be faced when designing such elementary function implementation, let us detail the function evaluation process with a focus on methods based on table lookups for trigonometric and hyperbolic sine and cosine. For this purpose, let y be a machine-representable floating-point number, taken as the input to the considered functions. The internal design of these implementations is based on the following four-step process:

1) A first range reduction, based on mathematical properties, narrows the domain of the function to a smaller one. For the trigonometric functions, properties of periodicity and symmetry lead to evaluating f q ∈ {± sin, ± cos} at input

|x| = |y -q • π/2| ∈ [0, π/4],
where q ∈ Z and the choice of f q depends on the function to evaluate, q mod 4, and sign(x) [START_REF] Muller | Elementary Functions: Algorithms and Implementation[END_REF]. For the hyperbolic functions, let

|x| = |y -q • ln(2)| ∈ [0, ln(2)/2] ,
with q ∈ Z. Addition formulas can then be used together with the analytic expressions for the hyperbolic functions involving the exponential, which, for the sine, gives: sinh(y) = 2 q-1 -2 -q-1 • cosh|x| ± 2 q-1 + 2 -q-1 • sinh|x|.

2) A second range reduction, based on tabulated values, further reduces the range on which polynomial evaluations are to be performed. The argument x is split into two parts, x h and x , such that:

x = x h + x with |x | ≤ 2 -p-1 . (1)
The term x h is the p-bit value of the form

x h = i • 2 -p with i = x • 2 p , (2)
and where i ∈ N. The integer i is used to address a table of n = κ • 2 p-1 + 1 rows, with κ ∈ {π/2, ln(2)}. This table holds precomputed values of either trigonometric or hyperbolic sines and cosines of x h , which we indifferently name S h and C h . 3) Meanwhile, polynomial approximations to the trigonometric or hyperbolic sine and cosine on the interval [-2 p-1 , 2 -p-1 ], denoted by P S and P C , are evaluated at input x . 4) Finally, a reconstruction step allows to compute the final result using the precomputed values retrieved at step 2 and the computed values from step 3. For the trigonometric sine, if we assume that q = 0 mod 4 and sign(x) = 1 so that f q = sin, one has to perform the following reconstruction:

sin(y) = S h • P C (x ) + C h • P S (x ),
while the reconstruction for the hyperbolic sine is:

sinh(y) = 2 q-1 -2 -q-1 • (C h • P C (x ) + S h • P S (x )) ± 2 q-1 + 2 -q-1 • (S h • P C (x ) + C h • P S (x )) .
Satisfactory solutions already exist to address the first range reduction [START_REF] Payne | Radian reduction for trigonometric functions[END_REF], [START_REF] Daumas | Modular Range Reduction: a New Algorithm for Fast and Accurate Computation of the Elementary Functions[END_REF], [START_REF] Brisebarre | A new range-reduction algorithm[END_REF], the generation and evaluation of accurate and efficient polynomial evaluation schemes [START_REF] De Dinechin | Optimizing polynomials for floating-point implementation[END_REF], [START_REF] Brisebarre | Efficient polynomial L ∞approximations[END_REF], [START_REF] Mouilleron | Automatic generation of fast and certified code for polynomial evaluation[END_REF], and the reconstruction step. The interested reader can find more details in [START_REF] Muller | Elementary Functions: Algorithms and Implementation[END_REF].

In this article, we address the second range reduction based on tabulated values for the trigonometric and hyperbolic sine and cosine. At this step, each tabulated value embeds a rounding error. Our objective is to remove the error on these values, and consequently to concentrate the error due to range reduction in the reduced argument used in the polynomial evaluation. Precisely the proposed method relies on finding precomputed values with remarkable properties that simplify and accelerate the evaluation of these functions. These properties are threefold. First, each pair of values holds the exact images of a reference argument under the functions, that is, without any rounding error. For this purpose, we require these values to be rational numbers. Second, each numerator and denominator of these rational values should be exactly representable in a representation format available in hardware, that is, as an integer or a floating-point number. Third, all rational values must share the same denominator. This enables us to shift the division by this denominator into the polynomial coefficients. These three properties lead to tabulated values that are exact and representable on single machine words. For the trigonometric and hyperbolic functions, Pythagorean triples give rational numbers that fulfill these three properties.

Overview of this Article

In this article, we extend a previous work on the trigonometric functions sin and cos presented in [START_REF] De | Range reduction based on pythagorean triples for trigonometric function evaluation[END_REF] to the hyperbolic functions sinh and cosh, and we show that our concept is general enough to be applied to other functions. More precisely, the contributions of this article are the following:

1) A general method that eliminates rounding errors from tabulated values; 2) And a formally justified algorithm to apply this method to trigonometric and hyperbolic functions.

This article is organized as follows: Section 2 gives some background on existing solutions for the second range reduction step. Section 3 details the properties of the proposed tables and demonstrates how it is possible to remove two sources of error involved during this step. Section 4 presents our approach to build exact lookup tables for trigonometric and hyperbolic functions using Pythagorean triples. Then, Section 5 presents some experimental results, which show that we can precompute exact lookup tables up to 10 indexing bits reasonably fast with the help of suitable heuristics. Some comparison results with classical approaches are given in Section 6, which show that our method can lower the table sizes and the number of floating-point operations performed during the reconstruction. Finally, an example with a toy table is also presented in Section 7, before a conclusion in Section 8.

TABLE-LOOKUP RANGE REDUCTIONS

While sometimes the second range reduction uses fast but inaccurate hardware approximations, in practice it is often implemented using table lookups. 1 This section presents three solutions that address this step with the sine function to illustrate them. Those methods are general and apply directly to the trigonometric cosine and the hyperbolic functions as well.

Tang's Tables

Tang proposed a general method to implement elementary functions that relies on hardware-tabulated values [START_REF] Tang | Table-lookup algorithms for elementary functions and their error analysis[END_REF]. Given the reduced argument x as in Equation ( 1), Tang's method uses the upper part x h to retrieve two tabulated values S h and C h that are good approximations of sin(x h ) and cos(x h ), respectively, rounded to the destination format. If an implementation targets correct-rounding, then those approximations are generally stored as floating-point expansions [START_REF] Shewchuk | Adaptive precision floating-point arithmetic and fast robust geometric predicates[END_REF]. In practice, an expansion of size n consists in representing a given number as the unevaluated sum of n floating-point numbers so that the rounding error be reduced compared to a regular floating-point number. If we denote by • i (x) the rounded value of a real number x to the nearest floating-point number of precision i, and by a real number ε -i the rounding error such that |ε -i | ≤ 2 -i , then, for the trigonometric functions, we have:

S h = • 53j (sin(x h )) = sin(x h ) • (1 + ε -53j ) and C h = • 53j (cos(x h )) = cos(x h ) • (1 + ε -53j ),
where j is the number of non-overlapping floating-point numbers used to represent S h and C h .

In parallel to the extraction of the values S h and C h , the evaluation of two polynomial approximations P S (x) and P C (x) is performed. They respectively approach the sine and cosine functions over the interval covered by x , namely [-2 -p-1 , 2 -p-1 ]. Finally, the result of sin(x) is reconstructed as follows:

sin(x) ≈ S h • P C (x ) + C h • P S (x ).
Tang's method is well suited for hardware implementations on modern architectures. It takes advantage of the capability on these architectures to access tabulated values in memory and to perform floating-point computations concurrently. Once the argument x is split into the two parts x h and x , memory units can provide the two tabulated values S h and C h , while floating-point units evaluate the polynomials P S and P C . As the degree of the polynomials decreases when the table size increases, the objective is to find parameters so that the polynomial evaluations take as long as memory accesses, on average [START_REF] Defour | Cache-optimised methods for the evaluation of elementary functions[END_REF].

Tang's tables store images of regularly spaced inputs, rounded to the destination format. This rounding error is problematic when seeking a correctly rounded implementation in double precision, since worst cases for trigonometric and hyperbolic functions require more than 118 bits of accuracy [START_REF] Lefevre | Worst cases for correct rounding of the elementary functions in double precision[END_REF], [START_REF]Worst cases for correct rounding of the elementary functions in double precision[END_REF]. A solution consists in storing values on three double-precision floating-point numbers [START_REF]CR-Libm, a library of correctly rounded elementary functions in double-precision[END_REF] and using costly extended-precision operators.

Gal's Accurate Tables

In Tang's method, S h and C h are approximations of sin(x h ) and cos(x h ), respectively. They are rounded according to the format used in the table and the targeted accuracy for the final result. To increase the accuracy of these tabulated values, Gal suggested a method to transfer some of the errors due to rounding over the reduced argument [START_REF]Computing elementary functions: A new approach for achieving high accuracy and good performance[END_REF]. This consists in introducing small corrective terms on the values x h , hereafter denoted by corr. For each input entry x h of the table, one corr term is carefully chosen to ensure that both sin(x h + corr) and cos(x h + corr) are "very close" to floating-point machine numbers. In [START_REF] Gal | An accurate elementary mathematical library for the IEEE floating point standard[END_REF], Gal and Bachelis were able to find corr values such that

S h = • 53j (sin(x h + corr)) = sin (x h + corr) • (1 + ε -10-53j ) and C h = • 53j (cos(x h + corr)) = cos (x h + corr) • (1 + ε -10-53j )
for each row of an 8-bit-indexed table. This corresponds to 10 extra bits of accuracy for both tabulated values of sine and cosine compared to Tang's tabulated values, thanks to a small perturbation corr on the values x h . For the trigonometric and hyperbolic functions, such corr values are produced by a random sampling with an expected sample size of 2 18 [25]. This method entails storing the corrective terms corr along with the values S h and C h , which will usually make Gal's accurate tables larger than Tang's tables when targeting correct rounding. The value sin(x) is reconstructed as follows:

sin(x) = S h • P C (x -corr) + C h • P S (x -corr),
where P C (x) and P S (x) are polynomials approximating cos(x) and sin(x), respectively, on the interval covered by x -corr. Gal's solution requires an exhaustive search in order to find one corrective term for each entry x h , within a search space that grows exponentially with the number of extra bits for S h and C h . Stehlé and Zimmermann proposed an improvement based on lattice reduction and worst cases to accelerate the precomputation [START_REF] Stehlé | Gal's accurate tables method revisited[END_REF]. They were able to increase the accuracy of Gal's tables by 11 extra bits, which translates to 21 extra bits compared to Tang's tables.

Brisebarre et al.'s (M, p, k)-Friendly Points

In 2012, Brisebarre, Ercegovac, and Muller proposed a new method for trigonometric sine and cosine evaluation with a few table lookups and additions in hardware [START_REF] Brisebarre | M, p, k)friendly points: A table-based method for trigonometric function evaluation[END_REF], [START_REF] Wang | M, p, k)-friendly points: A table-based method to evaluate trigonometric functions[END_REF]. Their approach consists in tabulating four values a, b, z, and x, defined as:

z = 1/ a 2 + b 2 and x ≈ arctan(b/a),
where a and b are small particular integers. The reconstruction then corresponds to:

sin(x) = b • cos(x -x) + a • sin(x -x) • z.
The values (a, b, z, x) are chosen amongst specific points with integer coordinates (a, b) called (M, p, k)-friendly points. These points are recoded using the canonical recoding, which minimizes the number of non-zero digits in table entries [START_REF] Reitwiesner | Binary arithmetic[END_REF]. More precisely, a and b must be positive integers lower than M , so that the number z = 1/ √ a 2 + b 2 has less than k non-zero bits in the first p bits of its canonical recoding. These requirements make hardware multiplications by a, b, and z much more acceptable, since they are equivalent to just a few additions. Compared to other hardware evaluation schemes, this solution reduces by 50% the area on FPGAs for 24-bit accuracy. Overall, this is a suitable method for competitive low and medium-precision hardware approximations.

Exact Lookup Table for Trigonometric Functions

Gal's accurate tables presented in Section 2.2 consist in finding almost regularly spaced inputs for which the images by the functions to approximate are close to machine numbers. This virtually increases the accuracy of the stored values by a few extra bits, slightly reducing the error bounds in the reconstruction step. However, there are still errors in the reduced argument as well as in tabulated approximations.

In 2015, we proposed a method to tabulate error-free values for the sine and cosine functions [START_REF] De | Range reduction based on pythagorean triples for trigonometric function evaluation[END_REF]. It is an improvement over Gal's tables as tabulated approximations for the sine and cosine are stored exactly, so that extended precision computations involving these values shall be less expensive. This can be noticed through the accurate phase costs presented in the article. In an exact lookup table the corrective terms are inexact, but they are added to the reduced argument, which is already inexact. As this addition can be performed with arbitrary precision, the presented method concentrates the error in a single term besides freeing tabulated values from rounding error. This reduces the memory footprint of the table when targeting high precision, compared to Tang's and Gal's tables, which need to store asymptotically twice as many bits.

It is similar to the (M, p, k)-friendly points method, as it relies on integer values like a and b in Section 2.3. It differs from it though, since the equivalent of 1/z is made exactly representable as an integer. This allows for a reconstruction step like Gal's, at the difference that it involves two exact terms out of four. This improvement was made possible by using the interesting structure of Pythagorean triples [START_REF] Sierpi | Pythagorean triangles[END_REF].

DESIGN OF AN EXACT LOOKUP TABLE

In this article, the proposed improvement is based on the following observation: After the first range reduction, the reduced number x is assumed to be an irrational number. Therefore, it has to be rounded to some finite precision, which means that after the second range reduction, only x contains a rounding error. Gal's method adds an exact corrective term corr to x that allows to increase the accuracy of transcendental tabulated values. Instead, we suggest not to worry about inexact corrective terms, as long as they make tabulated values exactly representable. This way, the error is solely concentrated in the reduced argument x -corr used during the polynomial evaluations. Now, let us characterize these exactly representable values and then explain how the evaluation scheme can benefit of this property. For this purpose, let f and g be the functions to approximate, κ ∈ {π/2, ln(2)} the additive constant for the first range reduction, and y an input floating-point number. As seen in the introduction, the reduced argument x obtained after the first range reduction is such that

x = |y -q • κ| with q ∈ Z, 0 ≤ x ≤ κ/2.
As y is rational, q is an integer, and κ is irrational, then unless q = 0, x must be irrational, and it has to be rounded to a floating-point number x, with a precision j greater than the targeted format such that: x = x • (1 + ε -j ). We should mention that x is generally represented as a floating-point expansion of size 2 or more to reach an accuracy of at least j bits. As seen in Equation ( 1), the second range reduction splits x into two parts, x h and x , such that

x = x h + x and |x | ≤ 2 -p-1 .
As shown in Equation ( 2), the value x h is then used to compute an address i in the table T made of n rows. This table

T holds exact precomputed values of k • f (x h + corr i ) and k • g(x h + corr i ),
where k is an integer scale factor that makes both tabulated values integers, and corr i is an irrational corrective term, precomputed for each table entry i, such that |corr i | ≤ 2 -p-1 . To build such values, we assume the following properties:

1) The functions f and g are right invertible on the domain [0, (n

-1/2) • 2 -p ].
This allows corrective terms to be determined. 2) Corrective terms are such that

|corr i | ≤ 2 -p-1 .
3) For each table entry i, the values f (x h + corr i ) and g(x h + corr i ) are rational numbers with the same denominator, that is:

f (x h + corr i ) = η i k i and g(x h + corr i ) = γ i k i with η i , γ i ∈ Z and k i ∈ Z * . 4) Let k = lcm(k 0 , . . . , k n-1 ), i.e. the least common multiple (LCM) of the denominators k i . Let F i = k • f (x h + corr i ) and G i = k • g(x h + corr i ). It is clear that the values F i = k • η i /k i and G i = k • γ i /k i are integers
, but we will also assume that they are representable as one floating-point machine number each, e.g. they fit on 53 bits if using the binary64/double precision format.

With numbers satisfying those properties, the reconstruction step corresponds to

f (x) = G i • P f /k (x -corr i ) + F i • P g/k (x -corr i )
for some i in {0, . . . , n -1}, where

• P f /k (x) and P g/k (x) are polynomial approximations of the functions f (x)/k and g(x)/k, respectively, on the interval

[-2 -p-1 -max i (corr i ), 2 -p-1 -min i (corr i )].
• and F i , G i , and • j (corr i ) are gathered from T . The integers F i and G i are stored without error as one floating-point number each. The third tabulated value • j (corr i ) is an approximation of the corrective term corr i such that:

x h + corr i ∈ f -1 F i k ∪ g -1 G i k
rounded to the targeted precision j.

As can be seen, the new reduced argument x -corr i covers a wider range than just x . In the worst case, this interval can have its length doubled, making the polynomial approximations potentially more expensive. Actually, those polynomials approximate functions that usually do not variate much on such reduced intervals, so that their degree need rarely be increased to achieve the same precision as for an interval that may be up to twice as narrow. This was tested with some of our tables with Sollya's guessdegree function [START_REF] Chevillard | Sollya: An environment for the development of numerical codes[END_REF]. In unfortunate cases for which the degree should be increased, rarely more than one additional monomial will be required, which barely adds two floating-point operations (one addition and one multiplication). Furthermore, such additional operations can often benefit from instruction parallelism in the evaluation scheme.

Also note that instead of considering polynomial approximations of f (x) and g(x) directly, we propose to incorporate a division by k into the polynomial coefficients. Therefore, we consider approximations P f /k (x) and P g/k (x) of f (x)/k and g(x)/k, respectively, which avoid the prohibitive cost of the division and the associated rounding error.

EXACT LOOKUP TABLES FOR TRIGONOMETRIC AND HYPERBOLIC FUNCTIONS

The proposed lookup table for the second range reduction brings several benefits over existing solutions. However, building such a table of error-free values is not trivial, since the integers F i , G i , and k are not always straightforward, especially for transcendental functions. For the trigonometric and hyperbolic functions, we rely on some useful results on Pythagorean triples. These objects are briefly described in the first part of this section. Then, we present a method to efficiently build exact lookup tables for the trigonometric and hyperbolic functions.

Pythagorean Triples

Pythagorean triples are a set of mathematical objects from Euclidean geometry which have been known and studied since ancient Babylonia [31, ch. 6]. There exist several definitions of Pythagorean triples that usually differ on authorized values. We choose the following one:

Definition 1. A triple of non-negative integers (a, b, c) = 0 is a Pythagorean triple if and only if a 2 + b 2 = c 2 .
A Pythagorean triple (a, b, c) for which a, b, and c are coprime is called a primitive Pythagorean triple (PPT). In the following, we will refer to the set of PPTs as PPT. Recall that we are looking for several rational numbers which hold exact values for the trigonometric and hyperbolic functions. Yet a PPT and its multiples share the same rational values a/b, b/c, . . . For example, the well known PPT (3, 4, 5) and all its multiples can be associated to the ratio a/b = 3/4. Therefore we can restrict our search to primitive Pythagorean triples only, and apply a scale factor afterwards if needed.

According to the fundamental trigonometric and hyperbolic identities, we have

∀x ∈ R, cos(x) 2 + sin(x) 2 = 1 cosh(x) 2 -sinh(x) 2 = 1. (3)
It follows from Definition 1 and Equation ( 3) that all Pythagorean triples can be mapped to rational values of trigonometric or hyperbolic sine and cosine. Indeed, let (a, b, c) be a Pythagorean triple. Without loss of generality, we can assume that b = 0. Then we have: Hence, for each Pythagorean triple (a, b, c),

a 2 + b 2 = c 2 ⇐⇒ a c 2 + b c 2 = 1 ⇐⇒ c b 2 - a b 2 = 1.
assuming b = 0, ∃θ ∈ [0, π/2[ , cos(θ) = b c and sin(θ) = a c ∃ϕ ∈ R ≥0 , cosh(ϕ) = c b and sinh(ϕ) = a b .
The consequences of these properties are twofold: Firstly a PPT and its multiples share the same angles θ and ϕ.

Secondly any Pythagorean triple can be mapped to the sides of a right triangle (possibly degenerated into a segment), the hypotenuse of which is the third item c of the triple. Hence, in the following, the word "hypotenuse" is used to refer to the third item of a Pythagorean triple, while the word "legs" is used to refer to its first and second items.

Building Subsets of Primitive Pythagorean Triples

The set of primitive Pythagorean triples PPT is unbounded. Figure 1 represents the subset of all PPTs with a hypotenuse c < 2 12 . It shows that PPTs rapidly cover a wide range of angles over [0, π/2] as c increases. But, as our exact lookup tables need one triple per row, one may ask if there will always be at least one PPT that matches each row, no matter how narrow the entry interval of a row can be. Actually, a result due to P. Shiu states that the set of trigonometric angles covered by Pythagorean triples is dense in [0, π/4]. This is formalized by Theorem 1.

Theorem 1 (Shiu 1983 [32]). ∀θ ∈ [0, π/4], ∀δ > 0, there exists a primitive Pythagorean triple with a corresponding trigonometric angle θ , such that |θ -θ | < δ.

Proof. (From [START_REF] Shiu | The shapes and sizes of pythagorean triangles[END_REF].) Let (a, b, c) ∈ PPT. It is well-known that, assuming that a is even, (a, b, c) can be rewritten (2mn, m 2n 2 , m 2 +n 2 ) where m, n are coprime integers satisfying m > n > 0. It follows that

tan(θ ) = m 2 -n 2 2mn = 1 2 m n - n m .
Let us write t = tan(θ ) and r = m/n so that we have r 2 -2tr -1 = 0 and hence

r = t + t 2 + 1 = tan(θ ) + sec(θ ).
Note that we do not have r = t -√ t 2 + 1 because r > 0. It is now easy to prove the theorem. Let 0 ≤ θ ≤ π/4 and u = tan(θ) + sec(θ). One can choose a sequence of rational numbers r 0 , r 1 , r 2 , . . . converging to u, where

r k = m k n k , k = 0, 1, 2, . . .
such that m k and n k are positive and coprime. Let

x = 2m k n k , y = m 2 k -n 2 k , z = m 2 k + n 2
k with corresponding angle θ k . The angles θ 0 , θ 1 , θ 2 , . . . tend to θ, therefore the θ k 's can approximate θ arbitrarily closely, as required.

By symmetry of the Pythagorean triples (a, b, c) and (b, a, c), the density of the aforementioned set can be extended to the interval [0, π/2]. A corollary is the density of the set of hyperbolic angles covered by Pythagorean triples in R + . This is stated by Corollary 1 below.

Corollary 1. ∀ϕ ∈ R + , ∀δ > 0, there exists a primitive Pythagorean triple with an associated hyperbolic angle ϕ , such that |ϕ -ϕ | < δ.

Proof. By analogy, replace tan(ϕ ) by sinh(ϕ ), and sec(ϕ ) by cosh(ϕ ) in Shiu's proof of Theorem 1. We have

sinh(ϕ ) = m 2 -n 2 2mn = 1 2 m n - n m .
Let us write s = sinh(ϕ ) and r = m/n so that we have r 2 -2sr -1 = 0 and hence r = s + s 2 + 1 = sinh(ϕ ) + cosh(ϕ ).

Let ϕ ≥ 0 and u = sinh(ϕ) + cosh(ϕ). One can choose a sequence of rational numbers r 0 , r 1 , r 2 , . . . converging to u, where

r k = m k n k , k = 0, 1, 2, . . .
such that m k and n k are positive and coprime. Let

x = 2m k n k , y = m 2 k -n 2 k , z = m 2 k + n 2
k with corresponding hyperbolic angle ϕ k . It follows that the hyperbolic angles ϕ 0 , ϕ 1 , ϕ 2 , . . . tend to ϕ and that the ϕ k 's can approximate ϕ arbitrarily closely, as required.

Although we are now certain that there will always be an infinite number of PPTs for each row of our tables, these theorems do not give any bounds on the size of the PPTs. In practice, we will see that the PPT sizes allow us to build tables in double precision indexed by usual numbers of indexing bits (up to 13 or so). Also, we still have to find an easy means to generate PPTs efficiently. In this article, we make use of the Barning-Hall tree [START_REF] Barning | On pythagorean and quasi-pythagorean triangles and a generation process with the help of unimodular matrices[END_REF], [START_REF] Hall | 232. genealogy of pythagorean triads[END_REF], which exhibits a ternary structure that links any PPT to three different PPTs. From any PPT represented as a column vector, the Barning-Hall tree allows to compute three new PPTs by multiplying the former with the matrices

  1 -2 2 2 -1 2 2 -2 3   ,   -1 2 2 -2 1 2 -2 2 3   , and   1 2 2 2 1 2 2 2 3   . ( 4
)
It has been proven that all PPTs can be generated from the root (3, 4, 5) with increasing hypotenuse lengths [START_REF] Price | The Pythagorean Tree: A New Species[END_REF]. For every generated PPT (a, b, c), we also consider its symmetric PPT (b, a, c), because it may be interesting for three reasons: Hence the first step of PPT generation using the Barning-Hall tree is the following: multiplying the matrices in Equation ( 4) by the root (3, 4, 5) taken as a column vector, one gets the three new PPTs [START_REF] Defour | A new scheme for table-based evaluation of functions[END_REF][START_REF] Daumas | Modular Range Reduction: a New Algorithm for Fast and Accurate Computation of the Elementary Functions[END_REF][START_REF] Brisebarre | A new range-reduction algorithm[END_REF], [START_REF] Brisebarre | Efficient polynomial L ∞approximations[END_REF][START_REF] Muller | Elementary Functions: Algorithms and Implementation[END_REF][START_REF] De | Range reduction based on pythagorean triples for trigonometric function evaluation[END_REF], and [START_REF] Lefevre | Worst cases for correct rounding of the elementary functions in double precision[END_REF][START_REF] Defour | Cache-optimised methods for the evaluation of elementary functions[END_REF][START_REF] Sierpi | Pythagorean triangles[END_REF], and their symmetric counterparts [START_REF] Daumas | Modular Range Reduction: a New Algorithm for Fast and Accurate Computation of the Elementary Functions[END_REF][START_REF] Defour | A new scheme for table-based evaluation of functions[END_REF][START_REF] Brisebarre | A new range-reduction algorithm[END_REF], [START_REF] Muller | Elementary Functions: Algorithms and Implementation[END_REF][START_REF] Brisebarre | Efficient polynomial L ∞approximations[END_REF][START_REF] De | Range reduction based on pythagorean triples for trigonometric function evaluation[END_REF], and (20, 21, 29). In the following, note that we always consider the "degenerated" PPT (0, 1, 1), because it gives us an exact corrective term for the first table entry, without making the LCM k grow.

Selection of Primitive Pythagorean Triples

For each row i of the table indexed by x h • 2 p , we want to select exactly one PPT (a, b, c) with a corresponding value θ = arcsin(a/c) or ϕ = asinh(a/b) such that:

|x h -θ| < 2 -p-1 for trigonometric functions, or |x h -ϕ| < 2 -p-1 for hyperbolic functions.
The existence of such values θ and ϕ is a consequence of Theorem 1 and its Corollary 1.

In the following, we define our corrective terms, which we denote by corr, as: corr = θ -x h for trigonometric functions, or corr = ϕ -x h for hyperbolic functions.

Once one PPT has been selected for each row i, a naive solution would consist in storing exactly each a i , b i , c i in the lookup table T , plus an approximation of corr on as many bits as necessary. Instead, as presented in Section 3, we suggest to store two integers C h and S h of the form

C (i) h = b i c i • k and S (i) h = a i c i • k (5)
for the trigonometric functions, or

C (i) h = c i b i • k and S (i) h = a i b i • k (6)
for the hyperbolic functions, where k ∈ N * is the same for all table entries. In this article, in order to reduce the memory footprint of the table T , we look for small table entries C h and S h . This entails looking for a value k that is rather small, which can be simplified as a search for a small value in a set of least common multiples. It was shown in [START_REF] De | Range reduction based on pythagorean triples for trigonometric function evaluation[END_REF] that a straightforward approach, consisting in computing the set of all possible LCMs, was quickly out of reach with current technology, as soon as tables had more than a few rows.

To reduce the computational time, the solution proposed in [START_REF] De | Range reduction based on pythagorean triples for trigonometric function evaluation[END_REF] consists in looking for an LCM directly amongst generated values. We call this solution "exhaustive search" in the sequel of this article, as it always gives the smallest LCM. This claim may be counter-intuitive since one may ask why the smallest LCM would necessarily appear as the denominator side of some PPT. To prove it, we start by proving that for any table, there is a set of PPTs that fills it, for which the denominator sides (either hypotenuses or bigger legs) have the least LCM possible:

Lemma 1. If k ∈ N * is
the least element in the set of possible LCMs for a table of n rows, then there is a set of primitive Pythagorean triples {T i = (a i , b i , c i )} i∈{0,...,n-1} , with T i belonging to row i, such that: lcm(c 0 , . . . , c n-1 ) = k for the trigonometric functions, and lcm(b 0 , . . . , b n-1 ) = k for the hyperbolic functions.

Proof. By construction. For readability, and without loss of generality, we will only consider the LCM for the trigonometric functions. The set of possible LCMs for a table T of n rows is non-empty as a consequence of Theorem 1. Thus, by the Well-Ordering Principle, we know that k exists. Now, let T 0 , . . . , T n-1 be a set of primitive or non- primitive Pythagorean triples that fill the table T and such that lcm(c 0 , . . . , c n-1 ) = k. If there exists i ∈ {0, . . . , n -1} such that T i is non primitive, then there exists an integer α > 1 and T i ∈ PPT such that T i = α • T i . Thus, we have k = lcm(c 0 , . . . , α • c i , . . . , c n-1 ).

Hence α divides k. Since k is the least element of the set of possible LCMs, k/α < k cannot be a valid LCM. Therefore the prime factors of α must be shared with other c j . Hence, it is possible to write k = lcm(c 0 , . . . , c i , . . . , c n-1 ).

As mentioned in Section 4.1, T i and T i share the same angles θ and ϕ, and consequently they belong to the same subdivision of the table T . By repeating this process c i by c i as long as necessary, one can construct a set of primitive Pythagorean triples {T i } i∈{0,...,n-1} such that k = lcm(c 0 , . . . , c n-1 ), which concludes the proof.

Second, let us recall two important results about primitive Pythagorean triples.

Theorem 2.

Let n be a positive integer. First n is the hypotenuse of a primitive Pythagorean triple if and only if all of its prime factors are of the form 4k + 1. Second, when n > 2, n is a leg of a primitive Pythagorean triple if and only if n ≡ 2 mod 4.

Proof. For the first result, a proof is given by Sierpi ński in [START_REF] Sierpi | Elementary Theory of Numbers: Second English Edition[END_REF]ch. XI.3]. A more recent one can be found in [START_REF] Vella | When is n a member of a pythagorean triple?[END_REF]. For the second result, different proofs are given in [36, ch. II.3], and [38, p. 116] Now, for the trigonometric functions, we formulate Theorem 3, which states that the smallest LCM k is the hypothenuse of a PPT. Theorem 3. If k ∈ N * is the least element of the set of possible LCMs for a table of n rows for trigonometric functions, then k is the hypotenuse of a primitive Pythagorean triple.

Proof. Lemma 1 tells us that each T i can be made primitive. Thus, by Theorem 2, we have ∀i ∈ {0, . . . , n -1} , ∃ {β p,i } p∈Pπ ∈ N Pπ , c i = p∈Pπ p βp,i where P π is the set of Pythagorean primes, i.e. the set of prime numbers congruent to 1 modulo 4. By construction, we have

k = lcm(c 0 , . . . , c n-1 ) = p∈Pπ p maxi(βp,i) ,
which means that k is a product of Pythagorean primes. By Theorem 2, k is the hypotenuse of at least one primitive Pythagorean triple, which concludes the proof.

Finally, for the case of hyperbolic functions, Theorem 4 shows that the smallest LCM k is a leg of a PPT. Theorem 4. If k ∈ N * is the least element of the set of possible LCMs for a table of n rows for hyperbolic functions, then k is a leg of a primitive Pythagorean triple.

Proof. By Theorem 2, we only need to prove that k ≡ 2 mod 4.

Using Lemma 1, we know that each T i = (a i , b i , c i ) can be made primitive. Thus, using the aforementioned equivalence, we have ∀i ∈ {0, . . . , n -1} , b i ≡ 2 mod 4.

Hence, either every b i is odd, or there exists i ∈ {0, . . . , n -1} such that b i is even. In the first case, the LCM k of all b i 's is obviously odd too. In the latter case, b i is even implies that it is a multiple of 4 as a consequence of Equation [START_REF] Ziv | Fast evaluation of elementary mathematical functions with correctly rounded last bit[END_REF], which entails that k is also a multiple of 4. Therefore, by construction, k = lcm(b 0 , . . . , b n-1 ) ≡ 2 mod 4, which concludes the proof. Theorems 3 and 4 justify why we call our search amongst generated hypotenuses or legs "exhaustive", because by searching exhaustively amongst increasing PPT hypotenuses or legs, the smallest LCM will be found eventually.

IMPLEMENTATION AND NUMERIC RESULTS

In this section, we present how we implemented the proposed method to generate exact lookup tables that have been described in Section 4. We have designed two solutions to look for a small common multiple k. The first solution is based on an exhaustive search and allows us to build tables indexed by up to 7 bits in a reasonable amount of time. The second solution uses a heuristic approach, which reduces memory consumption during the execution and the search time, allowing us to build tables indexed by 10 bits much faster than the exhaustive search.

Exhaustive Search

This subsection first describes the design of our exhaustive algorithm, then discusses the different choices that we have when several PPTs are available inside one table row for the found LCM. Finally, numeric results are presented for various precisions p along with two toy exact lookup tables for the trigonometric and hyperbolic functions.

Algorithm

As seen in Section 4.3, we restrain our search to the set of generated hypotenuses c or to the set of generated legs b, for the trigonometric and hyperbolic functions, respectively. In other words, we require that the LCM k be the hypotenuse or a leg of one of the generated PPTs. Moreover, for the hyperbolic functions, one has sinh(ln(2)/2) < 1, so that the search can safely be limited to the set of bigger legs only.

To perform such an exhaustive search, we designed a C++ program that takes as input the number p of bits used to index the table T and tries to generate an exact lookup table. To achieve this, the algorithm looks for the smallest integer k, amongst the generated values, that is a multiple of at least one PPT hypotenuse or leg per entry. By Theorems 3 and 4, this search is guaranteed to find the smallest LCM. The algorithm is the following: Start with a maximum denominator size (in bits) of n = 4 and then follow these four steps:

1) Generate all PPTs (a, b, c) such that c ≤ 2 n or c ≤ 2 n / cos ((N -1/2) • 2 -p ),
where N is the number of table entries, for the trigonometric and hyperbolic functions, respectively. The latter inequality guarantees that every interesting leg for the search step will be generated. It is during this step that the Barning-Hall matrices from Equation ( 4) are used. 2) Store only the PPTs that belong to a table entry, i.e.

the potentially interesting ones. 3) Search for the LCM k among the PPT hypotenuses c or legs b that lie in [2 n-1 , 2 n ], for the trigonometric or the hyperbolic functions, respectively. 4) If such a k is found, build values (S h , C h , • j (corr i ))

for every row using Equation ( 5) or ( 6) and return an exact lookup table. Otherwise, set n ← n + 1 and go back to step 1.

Sometimes, several primitive Pythagorean triples are possible for a same table row. Then the selection of only one of them depends on which goal we want to achieve. In this article, we made the choice of selecting the one for which arcsin(a/c) or asinh(a/b) is the closest to x h , as this shifts the bits of the corrective term to the right compared to the reduced argument x . This virtually increases the precision of the "corrected" reduced argument, by having null bits in front of the mantissa of the corrective term.

But we see at least three alternatives to this solution. First, we could try to minimize the value max i (corr i )min j (corr j ). This could be interesting to store smaller corrective terms δ i = corr i -min j (corr j ), and incorporate min j (corr j ) into the polynomial approximations. But minimizing such a value might be much more expensive as its time complexity is n-1 i=0 m i , where m i is the number of possible triples for entry i. Second, we could choose the PPT for which the corrective term has as many identical bits (0's or 1's) as possible after its least significant bits. If there are enough, the precision of the corrective term is also virtually extended, which could allow for smaller tables. Finally, we could relax the constraints on the k i 's to allow greater values of their LCM k, so that the corrective terms may have even lower magnitudes or be even closer to machine numbers. This can be done in different fashions, but we will only describe one to explain the idea to the reader. Let us assume that we want a 10-bit-indexed table T . What we do is generate a 10 + m-bit-indexed table T instead: T has roughly 2 m times as many rows as the table that we want. Now let us make sets of about 2 m contiguous rows in this table. A subsequent step selects among each set of rows which of the available corrective terms is the best fit for the table T , and merges the rows into a single one for T . This technique could increase the precision of each corrective term at the cost of greater PPTs. As long as the exact stored values are still machine-representable, then this method stays interesting for software implementations. However, for hardware implementations, this might be less interesting since it involves more storage bits than a regular table.

Numeric Results

Tables 1 and2 show the results obtained for the trigonometric and hyperbolic functions, respectively. Timings were measured on a server with an Intel R Xeon R E5-2650 v2 @ 2.6 GHz processor (16 physical cores) with 125 GB of RAM running on GNU/Linux. For the number p of bits that is targeted, the tables describe the value k that was found, followed by the number n of bits used to represent k (that is, n = log 2 (k) ), the time (in seconds) taken by our program, and the numbers of PPTs and denominators considered during the LCM search.

As can be seen, it was possible to find k and to build tables indexed by up to p = 7 bits in a reasonable amount of time. However, it is clear that the number of dynamic memory allocations, which are mainly used to store the triples and the denominators, grows exponentially with p. Consequently, it was not possible to find k for p ≥ 8 with our hardware configuration.

Table 3 describes an exact lookup table for the trigonometric functions when p = 4, where k = 5525 and the absolute value of the corrective term is at most 0x1.d6513b89c7237p-6, that is, ≈ 0.0287 for input index i = 5. Table 4 presents an exact lookup table for the hyperbolic functions when p = 5, where k = 10,080 and the absolute value of the corrective term is at most 0x1.7c74108520aebp-7, that is, ≈ 0.0116 for input index i = 3. Figures 2 and3 give a visual representation of those tables in the Euclidean plane, depicting the stored triples by dots and the limits of each entry interval by dashed lines.

Heuristic Search

To build tables indexed by a larger number of bits, a more efficient solution must be found. In order to drastically reduce the search space, we have developed two heuristics, one for the trigonometric functions and one for the hyperbolic functions. These heuristics try to characterize the denominator sides (hypotenuses or bigger legs) to either keep or reject PPTs during the generation step.

For this purpose, let us observe the decomposition in prime factors of each k found using the exhaustive search. Such decompositions are given in Tables 5 and6 for both the trigonometric and the hyperbolic tables. These factorizations show that every k in the table is a composite number Prime factorization

144 2 4 • 3 2 840 2 3 • 3 • 5 • 7 10,080 2 5 • 3 2 • 5 • 7 180,180 2 2 • 3 2 • 5 • 7 • 11 • 13 1,081,080 2 3 • 3 3 • 5 • 7 • 11 • 13
divisible by relatively small primes. Furthermore, for the trigonometric functions, all those small primes are of the form 4n + 1, as per Theorem 2. Therefore, each heuristic that we propose follows a simple rule: for the trigonometric functions, only store primitive Pythagorean triples with a hypotenuse of the form:

c = p∈Pπ p rp with      r p ∈ N * if p = 5 r p ∈ {0, 1} if 13 ≤ p ≤ 73 r p = 0 if p > 73 , (8)
where P π is the set of Pythagorean primes: For the hyperbolic functions, the heuristic only stores primitive Pythagorean triples with a bigger leg of the form:

P π = {5,
b = p∈P ≤23 p rp with r p ∈ N * if p < 5 r p ∈ {0, 1} if 5 ≤ p ≤ 23 , (9)
where P ≤23 is the set of primes lower than or equal to 23:

P ≤23 = {2, 3 , 5, 7, 11, 13, 17, 19, 23} .
Results, timings, and numbers of considered triples and potential LCMs (hypotenuses or legs) for this heuristic are given in Tables 7 and8 for the trigonometric and the hyperbolic functions, respectively. As can be seen, this algorithm considers a number of potential LCMs several orders of magnitude lower than the exhaustive search solution. This reduces drastically the memory usage and execution times. For instance, for the trigonometric functions, when p = 7, only 3453 triples are stored, compared to the 1,347,954 triples for the exhaustive algorithm. In this first case, the execution time was reduced from 31 seconds to 0.4 seconds. And for the hyperbolic functions, when p = 7, only 1743 triples are stored, while there were 147,748 when using the exhaustive algorithm. In this second case, the execution time was reduced from 328 seconds to 0.3 seconds.

With this heuristic, the bottleneck is no longer the memory but the selection of PPTs during their generation.

Operation

Floating-point operations Without FMA With FMA

E 2 = E 1 + E 2 11 E 2 = E 2 + E 2 12 E 3 = E 1 + E 3 16 E 3 = E 3 + E 3 27 E 2 = E 1 × E 2 20 6 E 2 = E 2 × E 2 26 11 E 3 = E 1 × E 3 47 19 E 3 = E 3 × E 3 107 43
Indeed, this selection is carried out on the elements of an exponentially-growing set. And checking if a given integer satisfies either Equation [START_REF] Muller | Elementary Functions: Algorithms and Implementation[END_REF] or Equation ( 9) requires checking if it is multiple of certain prime numbers, which is a rather expensive test involving integer division.

COMPARISONS WITH OTHER METHODS

We have presented a range reduction for the trigonometric and hyperbolic functions based on exact lookup tables and a method to efficiently build such tables. In order to compare this solution with the other solutions presented in Section 2, we consider a two-phase evaluation scheme for the trigonometric and hyperbolic functions that targets correct rounding for the rounding to nearest in double precision.

The quick and the accurate phases target a relative error less than 2 -66 and 2 -150 , respectively [START_REF] Lefèvre | Hardest-to-round cases[END_REF], [START_REF] Muller | Handbook of Floating-Point Arithmetic[END_REF]. We choose p = 10 which corresponds to 805 rows in the trigonometric table and 356 rows in the hyperbolic table.

In order to ease comparisons, we consider only the number of memory accesses required by the second range reduction and the number of floating-point operations (FLOPs) involved in the reconstruction step, and table sizes. We will consider that expansion algorithms are used whenever high accuracy is required as it is the case in the correctly rounded library CR-Libm [START_REF]CR-Libm, a library of correctly rounded elementary functions in double-precision[END_REF]. Let us recall that an expansion of size n consists of an unevaluated sum of n floating-point numbers that represents a given number with a larger precision than what is available in hardware [START_REF] Shewchuk | Adaptive precision floating-point arithmetic and fast robust geometric predicates[END_REF]. We will take Table 9 extracted from [6, § 2.3], [START_REF] Lauter | Basic building blocks for a triple-double intermediate format[END_REF], and [41, § 3.2] as the reference costs for those algorithms. The notation E n stands for an expansion of size n in double precision, so that, with this notation, E 1 represents a regular binary64/double precision floating-point number.

Costs for the Trigonometric Functions

Tang's Solution

In order to reach an accuracy of 66 bits, Tang's solution requires access to tabulated values S h and C h that are stored as expansions of size 2. These values need to be multiplied by the two results of the polynomial evaluations, which can be considered stored as expansion of size 2 as well. The total cost of the quick phase becomes: 4 double-precision memory accesses, 2 multiplications E 2 × E 2 , and 1 addition E 2 + E 2 , that is, 34 FLOPs (64 without FMA). In case the quick phase failed to return correct rounding, the accurate phase is launched. This requires access to 2 extra tabulated values to represent S h and C h as expansions of size 3. Those values need to be multiplied by the two results of the polynomial evaluations, which can be considered stored as expansion of size 3 as well. The total cost of the accurate phase becomes: 2 extra memory accesses, 2 multiplications E 3 × E 3 , and 1 addition E 3 + E 3 . This corresponds to 6 memory accesses, 113 FLOPs (241 without FMA), and a 38,640 byte table.

Gal's Solution

Using Gal's method, the corrective terms allow around 63 bits of accuracy, and Stehlé and Zimmermann's improvement allows to reach 74 bits. By considering Stehlé's approach, only one double-precision number is required for S h , C h and the corrective term, which fits on about 20 bits, to reach an accuracy of 66 bits. Again, S h and C h need to be multiplied by the two results of the polynomial evaluations, which can be considered stored as expansions of size 2. Thus the quick phase requires 2 + 1 doubleprecision memory accesses, 1 addition E 2 = E 1 + E 2 for the corrective term, 2 multiplications E 1 × E 2 , and 1 addition E 2 + E 2 . The cost of the quick phase with this table becomes 3 memory accesses and 35 FLOPs (63 without FMA).

To reach the 150-bit accuracy required by the accurate phase, it is necessary to get 2 extra floating-point numbers for S h and C h , which are not exact. The corrective term is then incorporated in the final result using an addition E 3 = E 1 + E 3 . The remaining operations need to be done using size-3 expansions. The total cost for the accurate phase becomes: 4 extra memory accesses, 2 multiplications E 3 × E 3 , 1 addition E 1 + E 3 , and 1 addition E 3 + E 3 , that is, 7 memory accesses, 129 FLOPs (257 without FMA), and a 45,080-byte table.

Exact Lookup Tables Solution

With our solution, as shown in Table 7, at most 39 bits are required to store S h and C h , that is, only one floating-point number per entry. Our corrective terms are inexact, so that the cost of the quick phase is the same as Gal's approach in Section 6.1.2, except that the addition for the corrective term is an addition E 2 = E 2 + E 2 , which accounts for 1 extra FLOP and 1 extra memory access. Hence, the quick phase for the exact lookup table solution is 36 FLOPs (64 without FMA), which is as much as Tang's solution.

However, for the accurate phase, values S h and C h that were accessed during the quick phase are exact, and do not require any extra memory access. The corrective term is stored as an expansion of size 3 and it requires 1 extra memory access to reach the 150 bits of accuracy. The addition for the corrective term is performed using an addition with expansions of size 3. Multiplications correspond to E 3 = E 1 × E 3 as the results of the polynomial evaluations can be considered as expansions of size 3. The final addition is done using an E 3 = E 3 + E 3 operation. That is, the total cost of this step becomes 5 memory accesses and 92 FLOPs (148 without FMA), for a 32,200 byte table.

Comparison Results

Table 10 synthesizes the estimated costs for those three range reduction algorithms based on tabulated values. This table reports the number of memory accesses and FLOPs for the quick and accurate phases, together with the size in bytes of each row of the precomputed table.

First, it can be seen that the proposed table-based range reduction requires less memory per table row than the other solutions. Tang's method requires 48 bytes per row, Gal's method 56 bytes and our exact lookup table 40 bytes. This is an improvement in memory usage of ≈ 17% and ≈ 29% over Tang's and Gal's methods, respectively.

Second, regarding the number of operations, our solution requires two more FLOPs than Tang's solution for the quick phase (none without FMA), and one more FLOP than Gal's solution. This represents an overhead of ≈ 6% (0% without FMA) and ≈ 2%, respectively. This small downside comes with two advantages: First, as two out of the four terms in the reconstruction are exact, error bounds will be easier to determine. Second, the accurate phase is much faster. Indeed, for this phase, there is an improvement in favor of our approach of ≈ 19% (39% without FMA) and ≈ 29% (42% without FMA) over Tang and Gal, respectively.

Third, the solution we propose reduces the number of memory accesses during the accurate phase. For this phase, the number of accesses is reduced from 7 to 5 compared to Gal's approach, This is an improvement of ≈ 29%, which translates to ≈ 17% compared to Tang's solution.

Costs for the Hyperbolic Functions

The costs for memory accesses and table sizes for the hyperbolic functions are the same as the ones for the trigonometric functions. Indeed, although the exact lookup tables are smaller for the hyperbolic functions because ln(2)/2 ≈ 0.347 is lower than π/4 ≈ 0.785, they both contain as many words per row. We recall the reconstruction step for the hyperbolic functions:

sinh(y) = 2 n-1 -2 -n-1 • (C h • P C (x ) + S h • P S (x )) ± 2 n-1 + 2 -n-1 • (S h • P C (x ) + C h • P S (x )) . (10)
Note that if we do not count the computations of the terms 2 n-1 ± 2 -n-1 , there are 6 multiplications, and 3 additions. Indeed these terms can be neglected in the cost computations as they are easy to generate using shifts and adds in integer arithmetics.

2 = E 1 + E 2 , and 3 addi- tions E 2 = E 2 + E 2 .
Hence, the cost of the quick phase with this table is FLOPs (179 without FMA). For the accurate phase, all operations need to be done using size-3 expansions, except for the addition of the corrective term E 3 = E 1 +E 3 . The total cost for the accurate phase becomes: 355 floating-point operations (739 without FMA).

Exact Lookup Tables Solution

Again, as for the trigonometric functions, the cost of the quick phase is the same as Gal's approach plus one extra memory access and one extra FLOP for the addition of the inexact corrective term, which is 94 FLOPs (180 without FMA). However, the table is smaller as it takes only 14,240 bytes.

For the accurate phase, as it was the case for the trigonometric functions, values S h and C h are exact and do not require any extra memory access. Only the corrective term is stored as an expansion of size 3 and requires 1 extra memory accesses in order to reach an accuracy of 150 bits. Hence, the accurate phase corresponds to 4 multiplications E 1 × E 3 , 2 multiplications E 3 × E 3 , and 4 additions E 3 + E 3 . The total cost of this step is thus 270 floating-point operations (510 without FMA).

Comparison Results

As in Section 6.1.4, we compare the three methods using synthesized results from Table 10.

Regarding memory usage, we have quantitatively the same benefits as for the trigonometric functions.

Regarding the number of floating-point operations, our solution has the same small overhead of one FLOP for the quick phase compared to Gal's solution, but it requires 8% (6% without FMA) less FLOPs than Tang's solution. For the accurate phase, there is an improvement in favor of our approach of ≈ 20% (29% without FMA) and ≈ 24% (31% without FMA) over Tang and Gal, respectively. This is mainly due to the huge cost of multiplications E 3 × E 3 that our method avoids in 4 out of 6 multiplications.

EXAMPLE ON THE TRIGONOMETRIC SINE

We propose to illustrate the use of the proposed errorfree tabulated values compared to Tang's solution, which is probably the most pervasive, using Table 3 and Table 11. We consider the evaluation of the sine function for the input value y = 10 when targeting 8 bits of accuracy. When necessary, storage precision and intermediate computations will be done on 16 bits of precision in order to emulate 8bit expansions of size 2, like what is commonly used for the quick phase. For clarity, bit fields will be represented in radix 2 (suffix " 2 "). First, the reduced argument x is computed on 16 bits:

q = 10 • 2/π = 6 x = 10 -q • π/2 = 1.00100110100001 2 • 2 -1 • (1 + ε -16 ) ≈ 0.575225830078125.
As explained earlier, x is essentially inexact and has to be rounded (it is rounded to 16 bits in this case). By construction, x belongs to the interval [-π/4, π/4], but in practice, properties of symmetry are used so that it finally lies in [0, π/4]. We have q = 6, which entails q ≡ 2 mod 4. This means that an additional rotation by π radians was made during the first range reduction, so that the sign changed and we now have sin(10) = -sin(x). Now let us compute x h and x , such that x = x h + x with x h = i • 2 -4 , i ∈ [0, 13] and |x | ≤ 2 -5 . This leads to

x h = 9 • 2 -4 and x = x -x h = 1.10100001 2 • 2 -7 • (1 + ε -16 ).
For row index i = 9, Table 11 gives us

S h = 1.000100010000111 2 • 2 -1 • (1 + ε -16 ) C h = 1.101100010001111 2 • 2 -1 • (1 + ε -16 ),
while Table 3 gives us Now is when the different table-based methods diverge. They will be analyzed separately. Lookup Method. Using Tang's table, the evaluation of two polynomial approximations to sin(x) and cos(x) is performed with an output precision of 12 bits at input x = 1.10100001 2 • 2 -7 :

Tang's Table

sin(x ) ≈ 1.10100001 2 • 2 -7 • (1 + ε -12 ) cos(x ) ≈ 1.0 2 • (1 + ε -12 ). (11)
The final result is then computed on 8 bits using tabulated values S h and C h with the results from Equation ( 11

Finally, the result is reconstructed as follows, using the error-free tabulated values S h and C h from Table 3 and the approximations from Equation ( 12 Comparison Between Both Methods. One can see that for the exact lookup table method, the error is quickly concentrated into the reduced argument x , then into x -corr and finally into the polynomial approximations. The main advantage of this method relies on the fact that S h and C h embed no rounding error at all, which allows for an easier, faster, and more elegant reconstruction step.

CONCLUSIONS AND PERSPECTIVES

In this article, we have presented a new approach to address table-based range reductions in the evaluation process of elementary functions. We have shown that this method allows for simpler and faster evaluation of these functions. For the trigonometric and hyperbolic functions, we have made use of Pythagorean triples to build exact lookup tables, which concentrate the error into the polynomial evaluations. Compared to other solutions, exact lookup tables eliminate the rounding error on certain tabulated values, and transfer this error to the remaining reduced argument. We have focused on those functions, as they both benefit from the use of Pythagorean triples. However, the concept remains valid for other functions, provided that exact values can be found for the tables. Compared to the state of the art, we have shown that it was possible to reduce the table sizes, the number of memory accesses and the number of floating-point operations involved in the reconstruction process by up to 29%, 29% and 42%, respectively, when targeting correct rounding in double precision. This benefit would be even higher for larger targeted accuracies since the tabulated values are exactly stored and do not depend on the targeted accuracy.

As future work, it would be interesting to further characterize potential small LCMs in order to speed up the table precomputation process. The objective is to compute the interesting triples directly, possibly using one of Fässler's algorithms [START_REF] Fässler | Multiple Pythagorean number triples[END_REF], instead of generating huge sets of triples and then selecting the relevant ones. Finally, in the proposed algorithms we focused on looking for the smallest possible LCM so that tabulated values would be stored on the minimal number of bits. This property is essential for hardware implementations where each bit is important, but not for software implementations. In that case, it could be interesting to look for tabulated values that fit in a given format (e.g. 53 bits for double-precision floating-point numbers) but that would also increase the precision or reduce the magnitude of the corrective terms. This could save some extra memory accesses and associated floating-point operations.

•

  H. de Lassus Saint-Geniès, D. Defour, and G. Revy are with the DALI project-team, Université de Perpignan Via Domitia and LIRMM (CNRS: UMR 5506), Perpignan, France. Manuscript last updated March 2, 2017.

12 Fig. 1 .

 121 Fig. 1. Primitive Pythagorean triples with a hypotenuse c < 2 12 .

4 Fig. 2 .

 42 Fig. 2. Visual representation of the trigonometric exact lookup table fromTable3in the Euclidean plane.

Fig. 3 .

 3 Fig. 3. Visual representation of the hyperbolic exact lookup table fromTable 4 in the Euclidean plane.

  [START_REF] Brisebarre | A new range-reduction algorithm[END_REF][START_REF] De | Range reduction based on pythagorean triples for trigonometric function evaluation[END_REF][START_REF] Sierpi | Pythagorean triangles[END_REF][START_REF] Vella | When is n a member of a pythagorean triple?[END_REF][START_REF]Arrondi correct de fonctions mathématiques -fonctions univariées et bivariées, certification et automatisation[END_REF], 53, 61, 73, . . . } .

S

  h = 2880 C h = 4715 corr = -1.110100000010101 2 • 2 -7 • (1 + ε -16 )with k = 5525 as mentioned in Section 5.1.2.

  ):sin(10) = -sin(x) ≈ -S h • 1.0 2 -C h • 1.10100001 2 • 2 -7 ≈ -1.0001011 2 • 2 -1 • (1 + ε -8 ) ≈ -0.54296875.Exact TableLookupMethod. With the proposed solution, the evaluation of polynomial approximations to sin(x)/k and cos(x)/k are performed with an output precision of 12 bits at input x -corr ≈ 1.10111000100101 2 • 2 -6 :sin(x -corr)/k ≈ 1.01000110101 2 • 2 -18 cos(x -corr)/k ≈ 1.011110111 2 • 2 -13 .

  ):sin(10) = -sin(x) = -S h • cos(x -corr)/k -C h • sin(x -corr)/k ≈ -1.0001011 2 • 2 -1 ≈ -0.54296875.

  1) For the trigonometric functions, a) If arcsin(b/c) > π/4, one has immediately arcsin(a/c) < π/4, which falls into the range of the exact lookup table. b) Whenever arcsin(b/c) ≈ π/4, one also has arcsin(a/c) ≈ π/4, and both triples may either fall into two different subintervals of the exact lookup table, which can help reduce the value k (since they share a common hypotenuse), or fall into the same subinterval, which can help find a better corrective term. falls into the range of the lookup table.

	2) For the hyperbolic functions, since the reduced ar-
	gument x lies in [0, ln(2)/2], interesting PPTs must satisfy a/b ∈ sinh([0, (n + 1/2) • 2 -p ]) ⊃ [0, √ 2/4]. Therefore, if a ≥ 4b/ √ 2, only the symmetric PPT
	(b, a, c)

TABLE 1 Exhaustive

 1 Search Results for sin and cos.

	p	k	n	time (s)	PPTs Hypotenuses
	3	425	9	1	87	33
	4	5525	13	1	1405	428
	5	160,225	18	0.2	42,329	11,765
	6	1,698,385	21	7	335,345	87,633
	7	6,569,225	23	31	1,347,954	335,645
	8	> 2 27	> 27	> 6700 > 21,407,993	> 4,976,110

TABLE 2 Exhaustive

 2 Search Results for sinh and cosh.

	p	k	n	time (s)	PPTs Bigger legs
	3	144	8	1	23	12
	4	840	10	1	86	43
	5	10,080	14	1	1202	610
	6	171,360	18	9	18,674	9312
	7	1,081,080	21	328	147,748	72,476
	8	> 2 24	> 24 > 60,000 > 1,188,585	> 574,800

TABLE 3 A

 3 trigonometric exact lookup table computed for p = 4.

	Index	S h	C h	corr
	0	0	5525 +0x0.0000000000000p+0
	1	235	5520 -0x1.46e9e7603049fp-6
	2	612	5491 -0x1.cad996fe25a24p-7
	3 1036 5427 +0x1.27ac440de0a8cp-10
	4 1360 5355 -0x1.522b2a9e8491dp-10
	5 1547 5304 -0x1.d6513b89c7237p-6
	6 2044 5133 +0x1.038b12ae4eba1p-8
	7 2340 5005 -0x1.53f734851f48bp-13
	8 2600 4875 -0x1.49140da6fe454p-7
	9 2880 4715 -0x1.d02973d03a1f6p-7
	10 3315 4420 +0x1.2f1f464d3dc25p-6
	11 3500 4275 -0x1.7caa112f287aep-10
	12 3720 4085 -0x1.735972faced77p-7
	13 3952 3861 -0x1.fa6ed9240ab1ap-7

TABLE 4 A

 4 hyperbolic exact lookup table computed for p = 5.

	Index	S h	C h	corr
	0	0	10,080 +0x0.0000000000000p+0
	1	284	10,084 -0x1.93963974f0cb6p-9
	2	651	10,101 +0x1.0b316b3c740d1p-9
	3 1064 10,136 +0x1.7c74108520aebp-7
	4 1190 10,150 -0x1.d8f891d50d1a1p-8
	5 1560 10,200 -0x1.13297ef8b55bbp-9
	6 1848 10,248 -0x1.535fdc36d3139p-8
	7 2222 10,322 -0x1.fe04ef1053a97p-15
	8 2560 10,400 +0x1.5891c9eaef76ap-10
	9 2940 10,500 +0x1.a58844d36e49ep-8
	10 3237 10,587 +0x1.b77a5031ebc86p-9
	11 3456 10,656 -0x1.dcf49bb32dc17p-8

  Table 3 in the Euclidean plane.

Table 4

 4 in the Euclidean plane.

TABLE 5 Prime

 5 Factorization of Found Common Multiples for sin and cos.

	k	Prime factorization
	425	5 2 • 17
	5525	5 2 • 13 • 17
	160,225	5 2 • 13 • 17 • 29
	1,698,385	5 • 13 • 17 • 29 • 53
	6,569,225	5

2 • 13 • 17 • 29 • 41

TABLE 6 Prime

 6 Factorization of Found Common Multiples for sinh and cosh.

	k

TABLE 7 Heuristic

 7 Search Results for sin and cos.

	p	k	n time (s)	PPTs Hypotenuses
	3	425	9	1	42	8
	4	5525 13	1	211	17
	5	160,225 18	1	996	40
	6	1,698,385 21	0.1	2172	66
	7	6,569,225 23	0.4	3453	69
	8	314,201,225 29	9.5 10,468	100
	9	12,882,250,225 34	294 20,312	109
	10 279,827,610,985 39	9393 33,057	110

TABLE 8 Heuristic

 8 Search Results for sinh and cosh.

	p	k	n	time (s) PPTs Bigger legs
	3	144	8	1	23	12
	4	840 10	1	65	24
	5	10,080 14	1	247	79
	6	180,180 18	1	917	193
	7	1,081,080 21	0.3	1743	248
	8	17,907,120 25	3.2	3909	388
	9	147,026,880 28	23	5802	400
	10 2,793,510,720 32	350	9012	502

TABLE 9

 9 Costs of Addition and Multiplication of Expansions.

TABLE 10

 10 Computation and Memory Access Costs for Three Table-Based Range Reductions for Trigonometric and Hyperbolic Functions. The number of memory accesses and the number of floating-point operations (without FMA in parentheses) are reported.

		Memory Accesses	Floating-point Operations (without FMA)
	Solution	Quick Accurate	Trigonometric	Hyperbolic	Bytes per Row
				Quick	Accurate	Quick	Accurate
	Tang	4	6	34 (64)	113 (241)	102 (192)	339 (723)	48
	Gal	3	7	35 (63)	129 (257)	93 (179)	355 (739)	56
	Proposed	4	5	36 (64)	92 (148)	94 (180)	270 (510)	40

  6.2.1 Tang's and Gal's SolutionsOn the first hand, let us consider Tang's solution: Overall, the table requires 17,088 bytes. For the quick phase, we must use E 2 expansions for 6 multiplications and 3 additions, as we have seen in Equation[START_REF] Chevillard | Efficient and accurate computation of upper bounds of approximation errors[END_REF]. These extended-precisions operations account for 102 FLOPs (192 without FMA). During the accurate phase, all computations are done using E 3 expansions, which require a total of 339 FLOPs (723 without FMA).On the other hand, using Gal's method, the table takes 19,936 bytes and one addition E 2 = E 1 + E 2 for x -corr is added to the cost computations. The quick phase accounts now for 4 multiplications E 1 × E 2 between tabulated values and polynomial results, 2 multiplications E 2 × E 2 for the remaining terms, 1 addition E

TABLE 11 Tang

 11 's table for p = 4 with 16-bit precision

	Index	S h	C h
	0 0x0.0000p+0 0x1.0000p+0
	1 0x1.ffaap-5 0x1.ff00p-1
	2 0x1.feaap-4 0x1.fc02p-1
	3 0x1.7dc2p-3 0x1.f706p-1
	4 0x1.faaep-3 0x1.f016p-1
	5 0x1.3ad2p-2 0x1.e734p-1
	6 0x1.7710p-2 0x1.dc6cp-1
	7 0x1.b1d8p-2 0x1.cfc6p-1
	8 0x1.eaeep-2 0x1.c152p-1
	9 0x1.110ep-1 0x1.b11ep-1
	10 0x1.2b92p-1 0x1.9f36p-1
	11 0x1.44ecp-1 0x1.8bb2p-1
	12 0x1.5d00p-1 0x1.76a0p-1
	13 0x1.73b8p-1 0x1.6018p-1

ACKNOWLEDGMENTS

This work was supported by the MetaLibm 2 ANR project (Ingénierie Numérique et Sécurité 2013, ANR-13-INSE-0007). We thank the reviewers for their constructive comments on a previous version of this article.