crmf 0.1.1 → 0.1.3

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