intervals 0.3.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. data/VERSION.txt +1 -0
  2. data/ext/crlibm/AUTHORS +2 -0
  3. data/ext/crlibm/COPYING +504 -0
  4. data/ext/crlibm/ChangeLog +80 -0
  5. data/ext/crlibm/INSTALL +182 -0
  6. data/ext/crlibm/Makefile.am +84 -0
  7. data/ext/crlibm/Makefile.in +530 -0
  8. data/ext/crlibm/NEWS +0 -0
  9. data/ext/crlibm/README +31 -0
  10. data/ext/crlibm/TODO +47 -0
  11. data/ext/crlibm/VERSION +1 -0
  12. data/ext/crlibm/aclocal.m4 +989 -0
  13. data/ext/crlibm/atan-itanium.c +846 -0
  14. data/ext/crlibm/atan-pentium.c +261 -0
  15. data/ext/crlibm/atan_accurate.c +244 -0
  16. data/ext/crlibm/atan_accurate.h +191 -0
  17. data/ext/crlibm/atan_fast.c +324 -0
  18. data/ext/crlibm/atan_fast.h +678 -0
  19. data/ext/crlibm/config.guess +1461 -0
  20. data/ext/crlibm/config.sub +1566 -0
  21. data/ext/crlibm/configure +7517 -0
  22. data/ext/crlibm/configure.ac +364 -0
  23. data/ext/crlibm/crlibm.h +125 -0
  24. data/ext/crlibm/crlibm_config.h +149 -0
  25. data/ext/crlibm/crlibm_config.h.in +148 -0
  26. data/ext/crlibm/crlibm_private.c +293 -0
  27. data/ext/crlibm/crlibm_private.h +658 -0
  28. data/ext/crlibm/csh_fast.c +631 -0
  29. data/ext/crlibm/csh_fast.h +771 -0
  30. data/ext/crlibm/double-extended.h +496 -0
  31. data/ext/crlibm/exp-td.c +962 -0
  32. data/ext/crlibm/exp-td.h +685 -0
  33. data/ext/crlibm/exp_accurate.c +197 -0
  34. data/ext/crlibm/exp_accurate.h +85 -0
  35. data/ext/crlibm/gappa/log-de-E0-logir0.gappa +106 -0
  36. data/ext/crlibm/gappa/log-de-E0.gappa +79 -0
  37. data/ext/crlibm/gappa/log-de.gappa +81 -0
  38. data/ext/crlibm/gappa/log-td-E0-logir0.gappa +126 -0
  39. data/ext/crlibm/gappa/log-td-E0.gappa +143 -0
  40. data/ext/crlibm/gappa/log-td-accurate-E0-logir0.gappa +230 -0
  41. data/ext/crlibm/gappa/log-td-accurate-E0.gappa +213 -0
  42. data/ext/crlibm/gappa/log-td-accurate.gappa +217 -0
  43. data/ext/crlibm/gappa/log-td.gappa +156 -0
  44. data/ext/crlibm/gappa/trigoSinCosCase3.gappa +204 -0
  45. data/ext/crlibm/gappa/trigoTanCase2.gappa +73 -0
  46. data/ext/crlibm/install-sh +269 -0
  47. data/ext/crlibm/log-de.c +431 -0
  48. data/ext/crlibm/log-de.h +732 -0
  49. data/ext/crlibm/log-td.c +852 -0
  50. data/ext/crlibm/log-td.h +819 -0
  51. data/ext/crlibm/log10-td.c +906 -0
  52. data/ext/crlibm/log10-td.h +823 -0
  53. data/ext/crlibm/log2-td.c +935 -0
  54. data/ext/crlibm/log2-td.h +821 -0
  55. data/ext/crlibm/maple/atan.mpl +359 -0
  56. data/ext/crlibm/maple/common-procedures.mpl +997 -0
  57. data/ext/crlibm/maple/csh.mpl +446 -0
  58. data/ext/crlibm/maple/double-extended.mpl +151 -0
  59. data/ext/crlibm/maple/exp-td.mpl +195 -0
  60. data/ext/crlibm/maple/log-de.mpl +243 -0
  61. data/ext/crlibm/maple/log-td.mpl +316 -0
  62. data/ext/crlibm/maple/log10-td.mpl +345 -0
  63. data/ext/crlibm/maple/log2-td.mpl +334 -0
  64. data/ext/crlibm/maple/trigo.mpl +728 -0
  65. data/ext/crlibm/maple/triple-double.mpl +58 -0
  66. data/ext/crlibm/missing +198 -0
  67. data/ext/crlibm/mkinstalldirs +40 -0
  68. data/ext/crlibm/rem_pio2_accurate.c +219 -0
  69. data/ext/crlibm/rem_pio2_accurate.h +53 -0
  70. data/ext/crlibm/scs_lib/AUTHORS +3 -0
  71. data/ext/crlibm/scs_lib/COPYING +504 -0
  72. data/ext/crlibm/scs_lib/ChangeLog +16 -0
  73. data/ext/crlibm/scs_lib/INSTALL +215 -0
  74. data/ext/crlibm/scs_lib/Makefile.am +18 -0
  75. data/ext/crlibm/scs_lib/Makefile.in +328 -0
  76. data/ext/crlibm/scs_lib/NEWS +0 -0
  77. data/ext/crlibm/scs_lib/README +9 -0
  78. data/ext/crlibm/scs_lib/TODO +4 -0
  79. data/ext/crlibm/scs_lib/addition_scs.c +623 -0
  80. data/ext/crlibm/scs_lib/config.guess +1461 -0
  81. data/ext/crlibm/scs_lib/config.sub +1566 -0
  82. data/ext/crlibm/scs_lib/configure +6226 -0
  83. data/ext/crlibm/scs_lib/division_scs.c +110 -0
  84. data/ext/crlibm/scs_lib/double2scs.c +174 -0
  85. data/ext/crlibm/scs_lib/install-sh +269 -0
  86. data/ext/crlibm/scs_lib/missing +198 -0
  87. data/ext/crlibm/scs_lib/mkinstalldirs +40 -0
  88. data/ext/crlibm/scs_lib/multiplication_scs.c +456 -0
  89. data/ext/crlibm/scs_lib/poly_fct.c +112 -0
  90. data/ext/crlibm/scs_lib/print_scs.c +73 -0
  91. data/ext/crlibm/scs_lib/rand_scs.c +63 -0
  92. data/ext/crlibm/scs_lib/scs.h +353 -0
  93. data/ext/crlibm/scs_lib/scs2double.c +391 -0
  94. data/ext/crlibm/scs_lib/scs2mpf.c +58 -0
  95. data/ext/crlibm/scs_lib/scs2mpfr.c +61 -0
  96. data/ext/crlibm/scs_lib/scs_private.c +23 -0
  97. data/ext/crlibm/scs_lib/scs_private.h +133 -0
  98. data/ext/crlibm/scs_lib/tests/tbx_timing.h +102 -0
  99. data/ext/crlibm/scs_lib/wrapper_scs.h +486 -0
  100. data/ext/crlibm/scs_lib/zero_scs.c +52 -0
  101. data/ext/crlibm/stamp-h.in +1 -0
  102. data/ext/crlibm/tests/Makefile.am +43 -0
  103. data/ext/crlibm/tests/Makefile.in +396 -0
  104. data/ext/crlibm/tests/blind_test.c +148 -0
  105. data/ext/crlibm/tests/generate_test_vectors.c +258 -0
  106. data/ext/crlibm/tests/soak_test.c +334 -0
  107. data/ext/crlibm/tests/test_common.c +627 -0
  108. data/ext/crlibm/tests/test_common.h +28 -0
  109. data/ext/crlibm/tests/test_perf.c +570 -0
  110. data/ext/crlibm/tests/test_val.c +249 -0
  111. data/ext/crlibm/trigo_accurate.c +500 -0
  112. data/ext/crlibm/trigo_accurate.h +331 -0
  113. data/ext/crlibm/trigo_fast.c +1219 -0
  114. data/ext/crlibm/trigo_fast.h +639 -0
  115. data/ext/crlibm/triple-double.h +878 -0
  116. data/ext/extconf.rb +31 -0
  117. data/ext/fpu.c +107 -0
  118. data/ext/jamis-mod.rb +591 -0
  119. data/lib/fpu.rb +287 -0
  120. data/lib/interval.rb +1170 -0
  121. data/lib/intervals.rb +212 -0
  122. data/lib/struct_float.rb +133 -0
  123. data/test/data_atan.txt +360 -0
  124. data/test/data_cos.txt +346 -0
  125. data/test/data_cosh.txt +3322 -0
  126. data/test/data_exp.txt +3322 -0
  127. data/test/data_log.txt +141 -0
  128. data/test/data_sin.txt +140 -0
  129. data/test/data_sinh.txt +3322 -0
  130. data/test/data_tan.txt +342 -0
  131. metadata +186 -0
@@ -0,0 +1,1219 @@
1
+ /*
2
+ * Correctly rounded trigonometric functions
3
+ *
4
+ * Author : Catherine Daramy, Florent de Dinechin, David Defour
5
+ *
6
+ * This file is part of the crlibm library developed by the Arenaire
7
+ * project at Ecole Normale Superieure de Lyon
8
+ *
9
+ * This program is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU Lesser General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public License
20
+ * along with this program; if not, write to the Free Software
21
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
+ */
23
+
24
+ #include <stdio.h>
25
+ #include <stdlib.h>
26
+ #include "crlibm.h"
27
+ #include "crlibm_private.h"
28
+ #include "trigo_fast.h"
29
+
30
+
31
+
32
+ /*
33
+
34
+ How these functions work:
35
+
36
+ The trig range reduction in crlibm computes an integer k and a reduced
37
+ argument y such that
38
+
39
+ x = k.Pi/256 + y
40
+
41
+ with the reduced argument y directly in -Pi/512, Pi/512.
42
+ (Pi/512 < 4/512 = 2^-7)
43
+ y is computed as a double-double yh+yl
44
+
45
+ Then we read off a table
46
+
47
+ sah+sal ~ sin(kPi/256)
48
+ cah+cal ~ cos(kPi/256)
49
+
50
+ and we use the reconstruction
51
+
52
+ sin(kPi/256 + y) = sin(kPi/256)cos(y) + cos(kPi/256)sin(y)
53
+ cos(kPi/256 + y) = cos(kPi/256)cos(y) - sin(kPi/256)sin(y)
54
+
55
+ where cos(y) and sin(y) are computed as unevaluated 1+tc and (yh+yl)(1+ts)
56
+ respectively, where tc and ts are doubles resulting from a small
57
+ polynomial approximation.
58
+ This gives 14 extra bits of accuracy, so this first step is very accurate.
59
+
60
+
61
+ Why not use accurate tables as defined by Gal ?
62
+
63
+ In short, because Gal's fast approach only gives as many additiona bits
64
+ as you've got to address the table (so we are limited to 7 if we limit
65
+ the table size to 4KB), and we need more to have a good average performance.
66
+ From a performance point of view we probably lose a few cycles: There
67
+ is 4 values to read in our scheme compared to 3 in Gal's method. The
68
+ reconstruction costs a few floating-point operations more (not that
69
+ many, if you look in details and want to ensure more than 7 extra
70
+ bits).
71
+
72
+ Now for the advantages:
73
+ 1/ The whole thing is simpler
74
+ 2/ We have much more accuracy in the table, which simplifies the proof.
75
+ 3/ We will be able to reuse the same table values to speed up the
76
+ second step (just tabulating a third double such that the three-double
77
+ approx of sin/cos(kPi/256) will be summed exactly into an SCS number)
78
+
79
+
80
+
81
+ Now a word on range reduction:
82
+
83
+ We have 4 possible range reductions:
84
+
85
+ Cody and Waite with 2 constants (the fastest)
86
+ Cody and Waite with 3 constants (almost as fast)
87
+ Cody and Waite with 3 constants in double-double and k a long-long int
88
+ Payne and Hanek, implemented in SCS (the slowest).
89
+
90
+ Each of these range reductions except Payne and Hanek is valid for x
91
+ smaller than some bound.
92
+
93
+ This range reduction may cancel up to 62 bits according to a program
94
+ by Kahan/Douglas available in Muller's book and implemented as
95
+ function WorstCaseForAdditiveRangeReduction in common-procedures.mpl
96
+ However this is not a concern unless x is close to a multiple of Pi/2
97
+ (that is k&127==0): in the general case the reconstruction will add a
98
+ tabulated non-zero value, so the error to consider in the range
99
+ reduction is the absolute error. Only in the cases when k&127==0 do we
100
+ need to have 62 extra bits to compute with. This is ensured by using a
101
+ slower, more accurate range reduction. This test for k&127==0 actually
102
+ speeds up even these cases, because in these cases there is no table
103
+ to read and no reconstruction to do : a simple approximation to the
104
+ function suffices.
105
+
106
+
107
+ Why not use Payne and Hanek only as in Markstein's book ? Because
108
+ our scheme, in the absence of FMA, is much faster for small values
109
+ which are the most used.
110
+
111
+ Markstein takes as reduced argument the fractional part of x*256/Pi,
112
+ (or maybe it's 512 in his case), so he's got the same tables as we have,
113
+ but different polynomials (which compute sin(2Pi*y) and cos(2Pi*y).
114
+
115
+
116
+
117
+
118
+
119
+ */
120
+
121
+
122
+
123
+
124
+ #define DEBUG 0
125
+ /* TODO:
126
+
127
+
128
+ - In some Cody and Waite there are Mul12 involving k, CH and CM. They
129
+ can be improved by pre-splitting CH, CM (tabulated values)
130
+ and k (as an int) Then you can improve the precision by
131
+ taking kmax into account
132
+
133
+ - The first coefficient of the cosine polynomial is equal exactly
134
+ to 1/2 and this should be modified in order to increase to accuracy
135
+ of the approximation.
136
+
137
+ - The second step should get the reduced argument from the first step
138
+ (and use the same argument reduction). This should lead to 5x
139
+ improvement of the worst case.
140
+
141
+ - in the tangent there are three steps. This could be studied for the
142
+ other functions
143
+ */
144
+
145
+
146
+ static int rem_pio256_scs(scs_ptr result, const scs_ptr x){
147
+ uint64_t r[SCS_NB_WORDS+3], tmp;
148
+ unsigned int N;
149
+
150
+ /* result r[0],...,r[10] could store till 300 bits of precision */
151
+
152
+ /* that is really enough for computing the reduced argument */
153
+ int sign, i, j, ind;
154
+ int *digits_256_over_pi_pt;
155
+
156
+ if ((X_EXP != 1)||(X_IND < -2)){
157
+ scs_set(result, x);
158
+ return 0;
159
+ }
160
+
161
+ /* Compute the product |x| * 256/Pi */
162
+ if ((X_IND == -2)){
163
+ r[0] = 0; r[1] = 0;
164
+ r[2] = (uint64_t)(digits_256_over_pi[0]) * X_HW[0];
165
+ r[3] = ((uint64_t)(digits_256_over_pi[0]) * X_HW[1]
166
+ +(uint64_t)(digits_256_over_pi[1]) * X_HW[0]);
167
+ if(X_HW[2] == 0){
168
+ for(i=4; i<(SCS_NB_WORDS+3); i++){
169
+ r[i] = ((uint64_t)(digits_256_over_pi[i-3]) * X_HW[1]
170
+ +(uint64_t)(digits_256_over_pi[i-2]) * X_HW[0]);
171
+ }}else {
172
+ for(i=4; i<(SCS_NB_WORDS+3); i++){
173
+ r[i] = ((uint64_t)(digits_256_over_pi[i-4]) * X_HW[2]
174
+ +(uint64_t)(digits_256_over_pi[i-3]) * X_HW[1]
175
+ +(uint64_t)(digits_256_over_pi[i-2]) * X_HW[0]);
176
+ }
177
+ }
178
+ }else {
179
+ if (X_IND == -1){
180
+ r[0] = 0;
181
+ r[1] = (uint64_t)(digits_256_over_pi[0]) * X_HW[0];
182
+ r[2] = ((uint64_t)(digits_256_over_pi[0]) * X_HW[1]
183
+ +(uint64_t)(digits_256_over_pi[1]) * X_HW[0]);
184
+ if(X_HW[2] == 0){
185
+ for(i=3; i<(SCS_NB_WORDS+3); i++){
186
+ r[i] = ((uint64_t)(digits_256_over_pi[i-2]) * X_HW[1]
187
+ +(uint64_t)(digits_256_over_pi[i-1]) * X_HW[0]);
188
+ }}else {
189
+ for(i=3; i<(SCS_NB_WORDS+3); i++){
190
+ r[i] = ((uint64_t)(digits_256_over_pi[i-3]) * X_HW[2]
191
+ +(uint64_t)(digits_256_over_pi[i-2]) * X_HW[1]
192
+ +(uint64_t)(digits_256_over_pi[i-1]) * X_HW[0]);
193
+ }}
194
+ }else {
195
+ if (X_IND == 0){
196
+ r[0] = (uint64_t)(digits_256_over_pi[0]) * X_HW[0];
197
+ r[1] = ((uint64_t)(digits_256_over_pi[0]) * X_HW[1]
198
+ +(uint64_t)(digits_256_over_pi[1]) * X_HW[0]);
199
+ if(X_HW[2] == 0){
200
+ for(i=2; i<(SCS_NB_WORDS+3); i++){
201
+ r[i] = ((uint64_t)(digits_256_over_pi[i-1]) * X_HW[1]
202
+ +(uint64_t)(digits_256_over_pi[ i ]) * X_HW[0]);
203
+ }}else {
204
+ for(i=2; i<(SCS_NB_WORDS+3); i++){
205
+ r[i] = ((uint64_t)(digits_256_over_pi[i-2]) * X_HW[2]
206
+ +(uint64_t)(digits_256_over_pi[i-1]) * X_HW[1]
207
+ +(uint64_t)(digits_256_over_pi[ i ]) * X_HW[0]);
208
+ }}
209
+ }else {
210
+ if (X_IND == 1){
211
+ r[0] = ((uint64_t)(digits_256_over_pi[0]) * X_HW[1]
212
+ +(uint64_t)(digits_256_over_pi[1]) * X_HW[0]);
213
+ if(X_HW[2] == 0){
214
+ for(i=1; i<(SCS_NB_WORDS+3); i++){
215
+ r[i] = ((uint64_t)(digits_256_over_pi[ i ]) * X_HW[1]
216
+ +(uint64_t)(digits_256_over_pi[i+1]) * X_HW[0]);
217
+ }}else {
218
+ for(i=1; i<(SCS_NB_WORDS+3); i++){
219
+ r[i] = ((uint64_t)(digits_256_over_pi[i-1]) * X_HW[2]
220
+ +(uint64_t)(digits_256_over_pi[ i ]) * X_HW[1]
221
+ +(uint64_t)(digits_256_over_pi[i+1]) * X_HW[0]);
222
+ }}
223
+ }else {
224
+ ind = (X_IND - 2);
225
+ digits_256_over_pi_pt = (int*)&(digits_256_over_pi[ind]);
226
+ if(X_HW[2] == 0){
227
+ for(i=0; i<(SCS_NB_WORDS+3); i++){
228
+ r[i] = ((uint64_t)(digits_256_over_pi_pt[i+1]) * X_HW[1]
229
+ +(uint64_t)(digits_256_over_pi_pt[i+2]) * X_HW[0]);
230
+ }}else {
231
+ for(i=0; i<(SCS_NB_WORDS+3); i++){
232
+ r[i] = ((uint64_t)(digits_256_over_pi_pt[ i ]) * X_HW[2]
233
+ +(uint64_t)(digits_256_over_pi_pt[i+1]) * X_HW[1]
234
+ +(uint64_t)(digits_256_over_pi_pt[i+2]) * X_HW[0]);
235
+ }
236
+ }
237
+ }
238
+ }
239
+ }
240
+ }
241
+
242
+ /* Carry propagate */
243
+ r[SCS_NB_WORDS+1] += r[SCS_NB_WORDS+2]>>30;
244
+ for(i=(SCS_NB_WORDS+1); i>0; i--) {tmp=r[i]>>30; r[i-1] += tmp; r[i] -= (tmp<<30);}
245
+
246
+ /* The integer part is in r[0] */
247
+ N = r[0];
248
+ #if 0
249
+ printf("r[0] = %d\n", N);
250
+ #endif
251
+
252
+
253
+
254
+ if (r[1] > (SCS_RADIX)/2){ /* test if the reduced part is bigger than Pi/4 */
255
+ N += 1;
256
+ sign = -1;
257
+ for(i=1; i<(SCS_NB_WORDS+3); i++) { r[i]=((~(unsigned int)(r[i])) & 0x3fffffff);}
258
+ }
259
+ else
260
+ sign = 1;
261
+
262
+
263
+ /* Now we get the reduce argument and check for possible
264
+ * cancellation By Kahan algorithm we will have at most 2 digits
265
+ * of cancellations r[1] and r[2] in the worst case.
266
+ */
267
+ if (r[1] == 0)
268
+ if (r[2] == 0) i = 3;
269
+ else i = 2;
270
+ else i = 1;
271
+
272
+ for(j=0; j<SCS_NB_WORDS; j++) { R_HW[j] = r[i+j];}
273
+
274
+
275
+ R_EXP = 1;
276
+ R_IND = -i;
277
+ R_SGN = sign*X_SGN;
278
+
279
+ /* Last step :
280
+ * Multiplication by pi/2
281
+ */
282
+ scs_mul(result, Pio256_ptr, result);
283
+ return N*X_SGN;
284
+ }
285
+
286
+
287
+
288
+
289
+ #define DoSinZero(psh,psl) \
290
+ do{ \
291
+ yh2 = yh*yh ; \
292
+ ts = yh2 * (s3.d + yh2*(s5.d + yh2*s7.d)); \
293
+ /* (1+ts)*(yh+yl) is an approx to sin(yh+yl) */ \
294
+ /* Now compute (1+ts)*(yh+yl) */ \
295
+ Add12(*psh,*psl, yh, yl+ts*yh); \
296
+ } while(0)
297
+
298
+ #define DoCosZero(pch,pcl) \
299
+ do { \
300
+ yh2 = yh*yh ; \
301
+ tc = yh2 * (c2.d + yh2*(c4.d + yh2*c6.d )); \
302
+ /* 1+ tc is an approx to cos(yh+yl) */ \
303
+ /* Now compute 1+tc */ \
304
+ Add12(*pch,*pcl, 1., tc); \
305
+ } while(0)
306
+
307
+ /* See the documentation for explanations on DoSinNotZero */
308
+ #define DoSinNotZero(psh,psl) \
309
+ do { \
310
+ double thi, tlo, cahyh_h, cahyh_l ; \
311
+ Mul12(&cahyh_h,&cahyh_l, cah, yh); \
312
+ Add12(thi, tlo, sah,cahyh_h); \
313
+ tlo = tc*sah+(ts*cahyh_h+(sal+(tlo+(cahyh_l+(cal*yh + cah*yl))))) ; \
314
+ Add12(*psh,*psl, thi, tlo); \
315
+ } while(0)
316
+
317
+ /* See the documentation for explanations on DoCosNotZero */
318
+ #define DoCosNotZero(pch,pcl) \
319
+ do { \
320
+ double thi, tlo, sahyh_h,sahyh_l; \
321
+ Mul12(&sahyh_h,&sahyh_l, sah, yh); \
322
+ Add12(thi, tlo, cah, -sahyh_h); \
323
+ tlo = tc*cah-(ts*sahyh_h-(cal+(tlo-(sahyh_l+(sal*yh+sah*yl))))) ; \
324
+ Add12(*pch, *pcl, thi, tlo); \
325
+ } while(0)
326
+
327
+
328
+
329
+
330
+
331
+ /************************************************************************/
332
+ /* */
333
+ /* Argument Reduction */
334
+ /* */
335
+ /************************************************************************/
336
+
337
+
338
+ #define SIN 0
339
+ #define COS 1
340
+ #define TAN 2
341
+
342
+
343
+ #define RangeReductionSCS() \
344
+ do { \
345
+ scs_t X, Y,Yh,Yl; \
346
+ scs_set_d(X, rri->x); \
347
+ k= rem_pio256_scs(Y, X); \
348
+ index=(k&127)<<2; \
349
+ quadrant = (k>>7)&3; \
350
+ /* TODO an optimized procedure for the following */ \
351
+ scs_get_d(&yh, Y); \
352
+ scs_set_d(Yh, yh); \
353
+ scs_sub(Yl, Y,Yh); \
354
+ scs_get_d(&yl, Yl); \
355
+ }while(0)
356
+
357
+
358
+
359
+ /* A structure that holds all the information to be exchanged between
360
+ ComputeTrigWithArgred and the 12 functions sin_rn etc
361
+
362
+ It is purely for performance (almost 100 cycles out of 300 on a P4
363
+ when compared to passing a list of arguments). In addition to
364
+ saving a few memory accesses, it also allows other small
365
+ optimizations like deferring the possible change of sign of the
366
+ result to the the last moment using rri->changesign.
367
+
368
+ All this is not very elegant, but it is safe.
369
+ */
370
+
371
+ struct rrinfo_s {double rh; double rl; double x; int absxhi; int function;} ;
372
+ typedef struct rrinfo_s rrinfo;
373
+ #define changesign function /* saves one int in the rrinfo structure */
374
+
375
+ static void ComputeTrigWithArgred(rrinfo *rri){
376
+ double sah,sal,cah,cal, yh, yl, yh2, ts,tc, kd;
377
+ double kch_h,kch_l, kcm_h,kcm_l, th, tl,sh,sl,ch,cl;
378
+ int k, quadrant, index;
379
+ int64_t kl;
380
+
381
+ if (rri->absxhi < XMAX_CODY_WAITE_3) {
382
+ /* Compute k, deduce the table index and the quadrant */
383
+ DOUBLE2INT(k, rri->x * INV_PIO256);
384
+ kd = (double) k;
385
+ quadrant = (k>>7)&3;
386
+ index=(k&127)<<2;
387
+ if((index == 0)) {
388
+ /* Here a large cancellation on yh+yl would be a problem, so use double-double RR */
389
+ /* all this is exact */
390
+ Mul12(&kch_h, &kch_l, kd, RR_DD_MCH);
391
+ Mul12(&kcm_h, &kcm_l, kd, RR_DD_MCM);
392
+ Add12 (th,tl, kch_l, kcm_h) ;
393
+ /* only rounding error in the last multiplication and addition */
394
+ Add22 (&yh, &yl, (rri->x + kch_h) , (kcm_l - kd*RR_DD_CL), th, tl) ;
395
+ goto computeZero;
396
+ }
397
+ else {
398
+ /* index <> 0, don't worry about cancellations on yh+yl */
399
+ if (rri->absxhi < XMAX_CODY_WAITE_2) {
400
+ /* CW 2: all this is exact but the rightmost multiplication */
401
+ Add12 (yh,yl, (rri->x - kd*RR_CW2_CH), (kd*RR_CW2_MCL) ) ;
402
+ }
403
+ else {
404
+ /* CW 3: all this is exact but the rightmost multiplication */
405
+ Add12Cond(yh,yl, (rri->x - kd*RR_CW3_CH) - kd*RR_CW3_CM, kd*RR_CW3_MCL);
406
+ }
407
+ }
408
+ goto computeNotZero;
409
+ }
410
+
411
+ else if ( rri->absxhi < XMAX_DDRR ) {
412
+ /* x sufficiently small for a Cody and Waite in double-double */
413
+ DOUBLE2LONGINT(kl, rri->x*INV_PIO256);
414
+ kd=(double)kl;
415
+ quadrant = (kl>>7)&3;
416
+ index=(kl&127)<<2;
417
+ if(index == 0) {
418
+ /* Here again a large cancellation on yh+yl would be a problem,
419
+ so we do the accurate range reduction */
420
+ RangeReductionSCS(); /*recomputes k, index, quadrant, and yh and yl*/
421
+ /* Now it may happen that the new k differs by 1 of kl, so check that */
422
+ if(index==0) /* no surprise */
423
+ goto computeZero;
424
+ else
425
+ goto computeNotZero;
426
+ }
427
+ else { /* index<>0 : double-double argument reduction*/
428
+ /* all this is exact */
429
+ Mul12(&kch_h, &kch_l, kd, RR_DD_MCH);
430
+ Mul12(&kcm_h, &kcm_l, kd, RR_DD_MCM);
431
+ Add12 (th,tl, kch_l, kcm_h) ;
432
+ /* only rounding error in the last multiplication and addition */
433
+ Add22 (&yh, &yl, (rri->x + kch_h) , (kcm_l - kd*RR_DD_CL), th, tl) ;
434
+ // printf("%f\n", yh);
435
+ goto computeNotZero;
436
+ }
437
+ } /* closes if ( absxhi < XMAX_DDRR ) */
438
+
439
+ else {
440
+ /* Worst case : x very large, sin(x) probably meaningless, we return
441
+ correct rounding but do't mind taking time for it */
442
+ RangeReductionSCS();
443
+ quadrant = (k>>7)&3;
444
+ if(index == 0)
445
+ goto computeZero;
446
+ else
447
+ goto computeNotZero;
448
+ }
449
+
450
+
451
+ computeZero:
452
+ switch(rri->function) {
453
+
454
+ case SIN:
455
+ if (quadrant&1)
456
+ DoCosZero(&rri->rh, &rri->rl);
457
+ else
458
+ DoSinZero(&rri->rh, &rri->rl);
459
+ rri->changesign=(quadrant==2)||(quadrant==3);
460
+ return;
461
+
462
+ case COS:
463
+ if (quadrant&1)
464
+ DoSinZero(&rri->rh, &rri->rl);
465
+ else
466
+ DoCosZero(&rri->rh, &rri->rl);
467
+ rri->changesign= (quadrant==1)||(quadrant==2);
468
+ return;
469
+
470
+ case TAN:
471
+ rri->changesign = quadrant&1;
472
+ if (quadrant&1) {
473
+ DoSinZero(&ch, &cl);
474
+ DoCosZero(&sh, &sl);
475
+ } else {
476
+ DoSinZero(&sh, &sl);
477
+ DoCosZero(&ch, &cl);
478
+ }
479
+ Div22(&rri->rh, &rri->rl, sh, sl, ch, cl);
480
+ return;
481
+ }
482
+
483
+ computeNotZero:
484
+ if(index<=(64<<2)) {
485
+ sah=sincosTable[index+0].d; /* sin(a), high part */
486
+ sal=sincosTable[index+1].d; /* sin(a), low part */
487
+ cah=sincosTable[index+2].d; /* cos(a), high part */
488
+ cal=sincosTable[index+3].d; /* cos(a), low part */
489
+ }else { /* cah <= sah */
490
+ index=(128<<2) - index;
491
+ cah=sincosTable[index+0].d; /* cos(a), high part */
492
+ cal=sincosTable[index+1].d; /* cos(a), low part */
493
+ sah=sincosTable[index+2].d; /* sin(a), high part */
494
+ sal=sincosTable[index+3].d; /* sin(a), low part */
495
+ }
496
+ yh2 = yh*yh ;
497
+ ts = yh2 * (s3.d + yh2*(s5.d + yh2*s7.d));
498
+ tc = yh2 * (c2.d + yh2*(c4.d + yh2*c6.d ));
499
+ switch(rri->function) {
500
+
501
+ case SIN:
502
+ if (quadrant&1)
503
+ DoCosNotZero(&rri->rh, &rri->rl);
504
+ else
505
+ DoSinNotZero(&rri->rh, &rri->rl);
506
+ rri->changesign=(quadrant==2)||(quadrant==3);
507
+ return;
508
+
509
+ case COS:
510
+ if (quadrant&1)
511
+ DoSinNotZero(&rri->rh, &rri->rl);
512
+ else
513
+ DoCosNotZero(&rri->rh, &rri->rl);
514
+ rri->changesign=(quadrant==1)||(quadrant==2);
515
+ return;
516
+
517
+ case TAN:
518
+ rri->changesign = quadrant&1;
519
+ if (quadrant&1) {
520
+ DoSinNotZero(&ch, &cl);
521
+ DoCosNotZero(&sh, &sl);
522
+ } else {
523
+ DoSinNotZero(&sh, &sl);
524
+ DoCosNotZero(&ch, &cl);
525
+ }
526
+ Div22(&rri->rh, &rri->rl, sh, sl, ch, cl);
527
+ return;
528
+ }
529
+ }
530
+
531
+
532
+ /*************************************************************
533
+ *************************************************************
534
+ * SIN ROUNDED TO NEAREST *
535
+ *************************************************************
536
+ *************************************************************/
537
+
538
+ double sin_rn(double x){
539
+ double ts,x2,rncst;
540
+ rrinfo rri;
541
+ db_number x_split;
542
+
543
+ x_split.d=x;
544
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
545
+
546
+ /* SPECIAL CASES: x=(Nan, Inf) sin(x)=Nan */
547
+ if (rri.absxhi>=0x7ff00000) {
548
+ x_split.l=0xfff8000000000000LL;
549
+ return x_split.d - x_split.d;
550
+ }
551
+
552
+ else if (rri.absxhi < XMAX_SIN_CASE2){
553
+ /* CASE 1 : x small enough sin(x)=x */
554
+ if (rri.absxhi <XMAX_RETURN_X_FOR_SIN)
555
+ return x;
556
+
557
+ /* CASE 2 :XMAX_RETURN_X_FOR_SIN x < XMAX_SIN_CASE2
558
+ Fast polynomial evaluation as in DoSinZero */
559
+ x2 = x*x ;
560
+ ts = x2 * (s3.d + x2*(s5.d + x2*s7.d));
561
+ Add12(rri.rh,rri.rl, x, ts*x);
562
+ if(rri.rh == (rri.rh + (rri.rl * RN_CST_SIN_CASE2)))
563
+ return rri.rh;
564
+ else
565
+ return scs_sin_rn(x);
566
+ }
567
+
568
+ /* CASE 3 : Need argument reduction */
569
+ else {
570
+ rri.x=x;
571
+ rri.function=SIN;
572
+ ComputeTrigWithArgred(&rri);
573
+
574
+ #if 0
575
+ {
576
+ db_number t;
577
+ t.d = rri.rh ;
578
+ printf("\nrh = %08x %08x\n", t.i[HI], t.i[LO]);
579
+ t.d = rri.rl;
580
+ printf("rl = %08x %08x\n", t.i[HI], t.i[LO]);
581
+ }
582
+ #endif
583
+
584
+
585
+ rncst= RN_CST_SINCOS_CASE3;
586
+ if(rri.rh == (rri.rh + (rri.rl * rncst)))
587
+ if(rri.changesign) return -rri.rh; else return rri.rh;
588
+ else
589
+ return scs_sin_rn(x);
590
+ }
591
+ }
592
+
593
+
594
+
595
+
596
+
597
+
598
+ /*************************************************************
599
+ *************************************************************
600
+ * SIN ROUNDED TOWARD +INFINITY *
601
+ *************************************************************
602
+ *************************************************************/
603
+
604
+
605
+ double sin_ru(double x){
606
+ double xx, ts, epsilon;
607
+ rrinfo rri;
608
+ db_number x_split;
609
+
610
+ x_split.d=x;
611
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
612
+
613
+ /* SPECIAL CASES: x=(Nan, Inf) sin(x)=Nan */
614
+ if (rri.absxhi>=0x7ff00000) {
615
+ x_split.l=0xfff8000000000000LL;
616
+ return x_split.d - x_split.d;
617
+ }
618
+
619
+ if (rri.absxhi < XMAX_SIN_CASE2){
620
+
621
+ /* CASE 1 : x small enough, return x suitably rounded */
622
+ if (rri.absxhi <XMAX_RETURN_X_FOR_SIN) {
623
+ if(x>=0.)
624
+ return x;
625
+ else {
626
+ x_split.l --;
627
+ return x_split.d;
628
+ }
629
+ }
630
+ else {
631
+ /* CASE 2 : x < Pi/512
632
+ Fast polynomial evaluation */
633
+ xx = x*x;
634
+ ts = x * xx * (s3.d + xx*(s5.d + xx*s7.d ));
635
+ Add12(rri.rh,rri.rl, x, ts);
636
+ epsilon=EPS_SIN_CASE2;
637
+ }
638
+ }
639
+ else {
640
+ /* CASE 3 : Need argument reduction */
641
+ rri.x=x;
642
+ rri.function=SIN;
643
+ ComputeTrigWithArgred(&rri);
644
+ epsilon=EPS_SINCOS_CASE3;
645
+ if(rri.changesign) {
646
+ rri.rh = -rri.rh;
647
+ rri.rl = -rri.rl;
648
+ }
649
+ }
650
+
651
+ TEST_AND_RETURN_RU(rri.rh, rri.rl, epsilon);
652
+
653
+ /* if the previous block didn't return a value, launch accurate phase */
654
+ return scs_sin_ru(x);
655
+ }
656
+
657
+
658
+
659
+
660
+
661
+ /*************************************************************
662
+ *************************************************************
663
+ * SIN ROUNDED TOWARD -INFINITY *
664
+ *************************************************************
665
+ *************************************************************/
666
+ double sin_rd(double x){
667
+ double xx, ts, epsilon;
668
+ db_number x_split;
669
+ rrinfo rri;
670
+
671
+ x_split.d=x;
672
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
673
+
674
+ /* SPECIAL CASES: x=(Nan, Inf) sin(x)=Nan */
675
+ if (rri.absxhi>=0x7ff00000) {
676
+ x_split.l=0xfff8000000000000LL;
677
+ return x_split.d - x_split.d;
678
+ }
679
+
680
+ if (rri.absxhi < XMAX_SIN_CASE2){
681
+
682
+ /* CASE 1 : x small enough, return x suitably rounded */
683
+ if (rri.absxhi <XMAX_RETURN_X_FOR_SIN) {
684
+ if(x<=0.)
685
+ return x;
686
+ else {
687
+ x_split.l --;
688
+ return x_split.d;
689
+ }
690
+ }
691
+
692
+ else{
693
+ /* CASE 2 : x < Pi/512
694
+ Fast polynomial evaluation */
695
+ xx = x*x;
696
+ ts = x * xx * (s3.d + xx*(s5.d + xx*s7.d ));
697
+ Add12(rri.rh,rri.rl, x, ts);
698
+ epsilon=EPS_SIN_CASE2;
699
+ }
700
+ }
701
+ else {
702
+ /* CASE 3 : Need argument reduction */
703
+ rri.x=x;
704
+ rri.function=SIN;
705
+ ComputeTrigWithArgred(&rri);
706
+ epsilon=EPS_SINCOS_CASE3;
707
+ if(rri.changesign) {
708
+ rri.rh = -rri.rh;
709
+ rri.rl = -rri.rl;
710
+ }
711
+ }
712
+
713
+ TEST_AND_RETURN_RD(rri.rh, rri.rl, epsilon);
714
+
715
+ /* if the previous block didn't return a value, launch accurate phase */
716
+ return scs_sin_rd(x);
717
+ }
718
+
719
+
720
+
721
+
722
+
723
+ /*************************************************************
724
+ *************************************************************
725
+ * SIN ROUNDED TOWARD ZERO *
726
+ *************************************************************
727
+ *************************************************************/
728
+ double sin_rz(double x){
729
+ double xx, ts, epsilon;
730
+ db_number x_split;
731
+ rrinfo rri;
732
+ x_split.d=x;
733
+
734
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
735
+
736
+ /* SPECIAL CASES: x=(Nan, Inf) sin(x)=Nan */
737
+ if (rri.absxhi>=0x7ff00000) {
738
+ x_split.l=0xfff8000000000000LL;
739
+ return x_split.d - x_split.d;
740
+ }
741
+
742
+ if (rri.absxhi < XMAX_SIN_CASE2){
743
+
744
+ /* CASE 1 : x small enough, return x suitably rounded */
745
+ if (rri.absxhi <XMAX_RETURN_X_FOR_SIN) {
746
+ if(x==0) return x;
747
+ else {
748
+ x_split.l --;
749
+ return x_split.d;
750
+ }
751
+ }
752
+ else {
753
+ /* CASE 2 : x < Pi/512
754
+ Fast polynomial evaluation */
755
+ xx = x*x;
756
+ ts = x * xx * (s3.d + xx*(s5.d + xx*s7.d ));
757
+ Add12(rri.rh,rri.rl, x, ts);
758
+ epsilon=EPS_SIN_CASE2;
759
+ }
760
+ }
761
+ else {
762
+ /* CASE 3 : Need argument reduction */
763
+ rri.x=x;
764
+ rri.function=SIN;
765
+ ComputeTrigWithArgred(&rri);
766
+ epsilon=EPS_SINCOS_CASE3;
767
+ if(rri.changesign) {
768
+ rri.rh = -rri.rh;
769
+ rri.rl = -rri.rl;
770
+ }
771
+ }
772
+
773
+ TEST_AND_RETURN_RZ(rri.rh, rri.rl, epsilon);
774
+
775
+ /* if the previous block didn't return a value, launch accurate phase */
776
+ return scs_sin_rz(x);
777
+ }
778
+
779
+
780
+
781
+
782
+ /*************************************************************
783
+ *************************************************************
784
+ * COS ROUNDED TO NEAREST *
785
+ *************************************************************
786
+ *************************************************************/
787
+ double cos_rn(double x){
788
+ double tc, x2;
789
+ rrinfo rri;
790
+ db_number x_split;
791
+
792
+ x_split.d=x;
793
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
794
+
795
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
796
+ if (rri.absxhi>=0x7ff00000) {
797
+ /* was : return x-x;
798
+ but it's optimized out by Intel compiler (bug reported).
799
+ Who cares to be slow in this case anyway... */
800
+ x_split.l=0xfff8000000000000LL;
801
+ return x_split.d-x_split.d;
802
+ }
803
+
804
+ if (rri.absxhi < XMAX_COS_CASE2){
805
+ /* CASE 1 : x small enough cos(x)=1. */
806
+ if (rri.absxhi <XMAX_RETURN_1_FOR_COS_RN)
807
+ return 1.;
808
+ else {
809
+ /* CASE 2 : Fast polynomial evaluation */
810
+ x2 = x*x;
811
+ tc = x2 * (c2.d + x2*(c4.d + x2*c6.d ));
812
+ Add12(rri.rh,rri.rl, 1.0, tc);
813
+ if(rri.rh == (rri.rh + (rri.rl * RN_CST_COS_CASE2)))
814
+ return rri.rh;
815
+ else
816
+ return scs_cos_rn(x);
817
+ }
818
+ }
819
+ else {
820
+ /* CASE 3 : Need argument reduction */
821
+ rri.x=x;
822
+ rri.function=COS;
823
+ ComputeTrigWithArgred(&rri);
824
+ if(rri.rh == (rri.rh + (rri.rl * RN_CST_SINCOS_CASE3)))
825
+ if(rri.changesign) return -rri.rh; else return rri.rh;
826
+ else
827
+ return scs_cos_rn(x);
828
+ }
829
+ }
830
+
831
+
832
+
833
+ /*************************************************************
834
+ *************************************************************
835
+ * COS ROUNDED TO +INFINITY *
836
+ *************************************************************
837
+ *************************************************************/
838
+ double cos_ru(double x){
839
+ double x2, tc, epsilon;
840
+ rrinfo rri;
841
+ db_number x_split;
842
+
843
+ x_split.d=x;
844
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
845
+
846
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
847
+ if (rri.absxhi>=0x7ff00000) {
848
+ x_split.l=0xfff8000000000000LL;
849
+ return x_split.d - x_split.d;
850
+ }
851
+
852
+ if (rri.absxhi < XMAX_COS_CASE2){
853
+ /* CASE 1 : x small enough cos(x)=1. */
854
+ if (rri.absxhi <XMAX_RETURN_1_FOR_COS_RDIR)
855
+ return 1.;
856
+ else{
857
+ /* CASE 2 : Fast polynomial evaluation */
858
+ x2 = x*x;
859
+ tc = x2 * (c2.d + x2*(c4.d + x2*c6.d ));
860
+ Add12(rri.rh,rri.rl, 1, tc);
861
+ epsilon=EPS_COS_CASE2;
862
+ }
863
+ }
864
+
865
+ else {
866
+ /* CASE 3 : Need argument reduction */
867
+ rri.x=x;
868
+ rri.function=COS;
869
+ ComputeTrigWithArgred(&rri);
870
+ epsilon=EPS_SINCOS_CASE3;
871
+ if(rri.changesign) {
872
+ rri.rh = -rri.rh;
873
+ rri.rl = -rri.rl;
874
+ }
875
+ }
876
+
877
+ TEST_AND_RETURN_RU(rri.rh, rri.rl, epsilon);
878
+
879
+ /* if the previous block didn't return a value, launch accurate phase */
880
+ return scs_cos_ru(x);
881
+ }
882
+
883
+
884
+ /*************************************************************
885
+ *************************************************************
886
+ * COS ROUNDED TO -INFINITY *
887
+ *************************************************************
888
+ *************************************************************/
889
+ double cos_rd(double x){
890
+ double x2, tc, epsilon;
891
+ rrinfo rri;
892
+ db_number x_split;
893
+
894
+ x_split.d=x;
895
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
896
+
897
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
898
+ if (rri.absxhi>=0x7ff00000) {
899
+ x_split.l=0xfff8000000000000LL;
900
+ return x_split.d - x_split.d;
901
+ }
902
+
903
+ if (rri.absxhi < XMAX_COS_CASE2){
904
+ if (x==0) return 1;
905
+ /* CASE 1 : x small enough cos(x)=1. */
906
+ if (rri.absxhi <XMAX_RETURN_1_FOR_COS_RDIR)
907
+ return ONE_ROUNDED_DOWN;
908
+ else {
909
+ /* CASE 2 : Fast polynomial evaluation */
910
+ x2 = x*x;
911
+ tc = x2 * (c2.d + x2*(c4.d + x2*c6.d ));
912
+ Add12(rri.rh,rri.rl, 1, tc);
913
+ epsilon=EPS_COS_CASE2;
914
+ }
915
+ }
916
+ else {
917
+ /* CASE 3 : Need argument reduction */
918
+ rri.x=x;
919
+ rri.function=COS;
920
+ ComputeTrigWithArgred(&rri);
921
+ epsilon=EPS_SINCOS_CASE3;
922
+ if(rri.changesign) {
923
+ rri.rh = -rri.rh;
924
+ rri.rl = -rri.rl;
925
+ }
926
+ }
927
+
928
+ TEST_AND_RETURN_RD(rri.rh, rri.rl, epsilon);
929
+
930
+ /* if the previous block didn't return a value, launch accurate phase */
931
+ return scs_cos_rd(x);
932
+ }
933
+
934
+
935
+
936
+
937
+ /*************************************************************
938
+ *************************************************************
939
+ * COS ROUNDED TO ZERO *
940
+ *************************************************************
941
+ *************************************************************/
942
+ double cos_rz(double x){
943
+ double x2, tc, epsilon;
944
+ rrinfo rri;
945
+ db_number x_split;
946
+
947
+ x_split.d=x;
948
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
949
+
950
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
951
+ if (rri.absxhi>=0x7ff00000) {
952
+ x_split.l=0xfff8000000000000LL;
953
+ return x_split.d - x_split.d;
954
+ }
955
+
956
+ if (rri.absxhi < XMAX_COS_CASE2){
957
+ if (x==0) return 1;
958
+ /* CASE 1 : x small enough cos(x)=1. */
959
+ if (rri.absxhi <XMAX_RETURN_1_FOR_COS_RDIR)
960
+ return ONE_ROUNDED_DOWN;
961
+ else {
962
+ /* CASE 2 : Fast polynomial evaluation */
963
+ x2 = x*x;
964
+ tc = x2 * (c2.d + x2*(c4.d + x2*c6.d ));
965
+ Add12(rri.rh,rri.rl, 1, tc);
966
+ epsilon=EPS_COS_CASE2;
967
+ }
968
+ }
969
+ else {
970
+ /* CASE 3 : Need argument reduction */
971
+ rri.x=x;
972
+ rri.function=COS;
973
+ ComputeTrigWithArgred(&rri);
974
+ epsilon=EPS_SINCOS_CASE3;
975
+ if(rri.changesign) {
976
+ rri.rh = -rri.rh;
977
+ rri.rl = -rri.rl;
978
+ }
979
+ }
980
+
981
+ TEST_AND_RETURN_RZ(rri.rh, rri.rl, epsilon);
982
+
983
+ /* if the previous block didn't return a value, launch accurate phase */
984
+ return scs_cos_rz(x);
985
+ }
986
+
987
+
988
+
989
+
990
+
991
+ /*************************************************************
992
+ *************************************************************
993
+ * TAN ROUNDED TO NEAREST *
994
+ *************************************************************
995
+ *************************************************************/
996
+ double tan_rn(double x){
997
+ double x2, p5, tt;
998
+ rrinfo rri;
999
+ db_number x_split, rndcst;
1000
+
1001
+ x_split.d=x;
1002
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
1003
+
1004
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
1005
+ if (rri.absxhi>=0x7ff00000) {
1006
+ x_split.l=0xfff8000000000000LL;
1007
+ return x_split.d - x_split.d;
1008
+ }
1009
+
1010
+ if (rri.absxhi < XMAX_TAN_CASE2){
1011
+ if (rri.absxhi < XMAX_RETURN_X_FOR_TAN)
1012
+ return x;
1013
+ /* Dynamic computation of the rounding constant */
1014
+ rndcst.i[HI] = 0x3ff00000 + (((rri.absxhi & 0x000fffff)+0x00100000) >> (0x3ff+2 - (rri.absxhi>>20))) ;
1015
+ rndcst.i[LO] =0xffffffff;
1016
+ /* Fast Taylor series */
1017
+ x2 = x*x;
1018
+ p5 = t5.d + x2*(t7.d + x2*(t9.d + x2*t11.d));
1019
+ tt = x2*(t3h.d + (t3l.d + x2*p5));
1020
+ Add12(rri.rh, rri.rl, x, x*tt);
1021
+ /* Test if round to nearest achieved */
1022
+ if(rri.rh == (rri.rh + (rri.rl * rndcst.d)))
1023
+ return rri.rh;
1024
+ else
1025
+ return scs_tan_rn(x);
1026
+ }
1027
+ else {
1028
+ /* Otherwise : Range reduction then standard evaluation */
1029
+ rri.x=x;
1030
+ rri.function=TAN;
1031
+ ComputeTrigWithArgred(&rri);
1032
+
1033
+ /* Test if round to nearest achieved */
1034
+ if(rri.rh == (rri.rh + (rri.rl * RN_CST_TAN_CASE3)))
1035
+ if(rri.changesign) return -rri.rh; else return rri.rh;
1036
+ else
1037
+ return scs_tan_rn(x);
1038
+ }
1039
+ }
1040
+
1041
+
1042
+
1043
+ /*************************************************************
1044
+ *************************************************************
1045
+ * ROUNDED TOWARD +INFINITY
1046
+ *************************************************************
1047
+ *************************************************************/
1048
+ double tan_ru(double x){
1049
+ double epsilon, p5, tt, x2;
1050
+ db_number x_split;
1051
+ rrinfo rri;
1052
+
1053
+ x_split.d=x;
1054
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
1055
+
1056
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
1057
+ if (rri.absxhi>=0x7ff00000) {
1058
+ x_split.l=0xfff8000000000000LL;
1059
+ return x_split.d - x_split.d;
1060
+ }
1061
+
1062
+ if (rri.absxhi < XMAX_TAN_CASE2){
1063
+ if (rri.absxhi < XMAX_RETURN_X_FOR_TAN) {
1064
+ if(x<=0.)
1065
+ return x;
1066
+ else {
1067
+ x_split.l ++;
1068
+ return x_split.d;
1069
+ }
1070
+ }
1071
+ else {
1072
+ /* Fast Taylor series */
1073
+ x2 = x*x;
1074
+ p5 = t5.d + x2*(t7.d + x2*(t9.d + x2*t11.d));
1075
+ tt = x2*(t3h.d + (t3l.d +x2*p5));
1076
+ Add12(rri.rh, rri.rl, x, x*tt);
1077
+
1078
+ /* TODO dynamic computation of error constant */
1079
+ TEST_AND_RETURN_RU(rri.rh, rri.rl, EPS_TAN_CASE2);
1080
+
1081
+ /* if the previous block didn't return a value, launch accurate phase */
1082
+ return scs_tan_ru(x);
1083
+ }
1084
+ }
1085
+ else {
1086
+ /* Normal case: Range reduction then standard evaluation */
1087
+ rri.x=x;
1088
+ rri.function=TAN;
1089
+ ComputeTrigWithArgred(&rri);
1090
+ epsilon=EPS_TAN_CASE3;
1091
+ if(rri.changesign) {
1092
+ rri.rh= -rri.rh;
1093
+ rri.rl=-rri.rl;
1094
+ }
1095
+ }
1096
+
1097
+ TEST_AND_RETURN_RU(rri.rh, rri.rl, epsilon);
1098
+
1099
+ /* if the previous block didn't return a value, launch accurate phase */
1100
+ return scs_tan_ru(x);
1101
+ }
1102
+
1103
+
1104
+ /*************************************************************
1105
+ *************************************************************
1106
+ * ROUNDED TOWARD -INFINITY
1107
+ *************************************************************
1108
+ *************************************************************/
1109
+ double tan_rd(double x){
1110
+ double epsilon, p5, tt, x2;
1111
+ rrinfo rri;
1112
+ db_number x_split;
1113
+
1114
+
1115
+ x_split.d=x;
1116
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
1117
+
1118
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
1119
+ if (rri.absxhi>=0x7ff00000){
1120
+ x_split.l=0xfff8000000000000LL;
1121
+ return x_split.d - x_split.d;
1122
+
1123
+ }
1124
+
1125
+ if (rri.absxhi < XMAX_TAN_CASE2){
1126
+ if (rri.absxhi < XMAX_RETURN_X_FOR_TAN) {
1127
+ if(x>=0.)
1128
+ return x;
1129
+ else {
1130
+ x_split.l ++;
1131
+ return x_split.d;
1132
+ }
1133
+ }
1134
+
1135
+ /* Fast Taylor series */
1136
+ x2 = x*x;
1137
+ p5 = t5.d + x2*(t7.d + x2*(t9.d + x2*t11.d));
1138
+ tt = x2*(t3h.d + (t3l.d +x2*p5));
1139
+ Add12(rri.rh, rri.rl, x, x*tt);
1140
+
1141
+ TEST_AND_RETURN_RD(rri.rh, rri.rl, EPS_TAN_CASE2);
1142
+
1143
+ /* if the previous block didn't return a value, launch accurate phase */
1144
+ return scs_tan_rd(x);
1145
+ }
1146
+
1147
+ else {
1148
+ /* normal case: Range reduction then standard evaluation */
1149
+ rri.x=x;
1150
+ rri.function=TAN;
1151
+ ComputeTrigWithArgred(&rri);
1152
+ epsilon=EPS_TAN_CASE3;
1153
+ if(rri.changesign) {
1154
+ rri.rh= -rri.rh;
1155
+ rri.rl=-rri.rl;
1156
+ }
1157
+ }
1158
+
1159
+ TEST_AND_RETURN_RD(rri.rh, rri.rl, epsilon);
1160
+
1161
+ /* if the previous block didn't return a value, launch accurate phase */
1162
+ return scs_tan_rd(x);
1163
+ }
1164
+
1165
+
1166
+ /*************************************************************
1167
+ *************************************************************
1168
+ * ROUNDED TOWARD ZERO
1169
+ *************************************************************
1170
+ *************************************************************/
1171
+ double tan_rz(double x){
1172
+ double epsilon, p5, tt, x2;
1173
+ rrinfo rri;
1174
+ db_number x_split;
1175
+
1176
+ x_split.d=x;
1177
+ rri.absxhi = x_split.i[HI] & 0x7fffffff;
1178
+
1179
+ /* SPECIAL CASES: x=(Nan, Inf) cos(x)=Nan */
1180
+ if (rri.absxhi>=0x7ff00000) {
1181
+ x_split.l=0xfff8000000000000LL;
1182
+ return x_split.d - x_split.d;
1183
+ }
1184
+
1185
+ if (rri.absxhi < XMAX_TAN_CASE2){
1186
+ if (rri.absxhi < XMAX_RETURN_X_FOR_TAN) {
1187
+ return x;
1188
+ }
1189
+ else{
1190
+ /* Fast Taylor series */
1191
+ x2 = x*x;
1192
+ p5 = t5.d + x2*(t7.d + x2*(t9.d + x2*t11.d));
1193
+ tt = x2*(t3h.d + (t3l.d +x2*p5));
1194
+ Add12(rri.rh, rri.rl, x, x*tt);
1195
+
1196
+ TEST_AND_RETURN_RZ(rri.rh, rri.rl, EPS_TAN_CASE2);
1197
+
1198
+ /* if the TEST_AND_RETURN block didn't return a value, launch accurate phase */
1199
+ return scs_tan_rz(x);
1200
+ }
1201
+ }
1202
+ else {
1203
+ /* Normal case: Range reduction then standard evaluation */
1204
+ rri.x=x;
1205
+ rri.function=TAN;
1206
+ ComputeTrigWithArgred(&rri);
1207
+ epsilon=EPS_TAN_CASE3;
1208
+ if(rri.changesign) {
1209
+ rri.rh = -rri.rh;
1210
+ rri.rl = -rri.rl;
1211
+ }
1212
+ }
1213
+
1214
+ TEST_AND_RETURN_RZ(rri.rh, rri.rl, epsilon);
1215
+
1216
+ /* if the previous block didn't return a value, launch accurate phase */
1217
+ return scs_tan_rz(x);
1218
+ }
1219
+