1 /*---------------------------------------------------------------------------+
4 | Computation of an approximation of the sin function and the cosine |
5 | function by a polynomial. |
7 | Copyright (C) 1992,1993,1994,1997,1999 |
8 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
9 | E-mail billm@melbpc.org.au |
12 +---------------------------------------------------------------------------*/
15 #include "exception.h"
16 #include "reg_constant.h"
18 #include "fpu_system.h"
19 #include "control_w.h"
26 static const unsigned long long pos_terms_l
[N_COEFF_P
] =
34 static const unsigned long long neg_terms_l
[N_COEFF_N
] =
46 static const unsigned long long pos_terms_h
[N_COEFF_PH
] =
54 static const unsigned long long neg_terms_h
[N_COEFF_NH
] =
63 /*--- poly_sine() -----------------------------------------------------------+
65 +---------------------------------------------------------------------------*/
66 void poly_sine(FPU_REG
*st0_ptr
)
68 int exponent
, echange
;
69 Xsig accumulator
, argSqrd
, argTo4
;
70 unsigned long fix_up
, adj
;
71 unsigned long long fixed_arg
;
74 exponent
= exponent(st0_ptr
);
76 accumulator
.lsw
= accumulator
.midw
= accumulator
.msw
= 0;
78 /* Split into two ranges, for arguments below and above 1.0 */
79 /* The boundary between upper and lower is approx 0.88309101259 */
80 if ( (exponent
< -1) || ((exponent
== -1) && (st0_ptr
->sigh
<= 0xe21240aa)) )
82 /* The argument is <= 0.88309101259 */
84 argSqrd
.msw
= st0_ptr
->sigh
; argSqrd
.midw
= st0_ptr
->sigl
; argSqrd
.lsw
= 0;
85 mul64_Xsig(&argSqrd
, &significand(st0_ptr
));
86 shr_Xsig(&argSqrd
, 2*(-1-exponent
));
87 argTo4
.msw
= argSqrd
.msw
; argTo4
.midw
= argSqrd
.midw
;
88 argTo4
.lsw
= argSqrd
.lsw
;
89 mul_Xsig_Xsig(&argTo4
, &argTo4
);
91 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), neg_terms_l
,
93 mul_Xsig_Xsig(&accumulator
, &argSqrd
);
94 negate_Xsig(&accumulator
);
96 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), pos_terms_l
,
99 shr_Xsig(&accumulator
, 2); /* Divide by four */
100 accumulator
.msw
|= 0x80000000; /* Add 1.0 */
102 mul64_Xsig(&accumulator
, &significand(st0_ptr
));
103 mul64_Xsig(&accumulator
, &significand(st0_ptr
));
104 mul64_Xsig(&accumulator
, &significand(st0_ptr
));
106 /* Divide by four, FPU_REG compatible, etc */
107 exponent
= 3*exponent
;
109 /* The minimum exponent difference is 3 */
110 shr_Xsig(&accumulator
, exponent(st0_ptr
) - exponent
);
112 negate_Xsig(&accumulator
);
113 XSIG_LL(accumulator
) += significand(st0_ptr
);
115 echange
= round_Xsig(&accumulator
);
117 setexponentpos(&result
, exponent(st0_ptr
) + echange
);
121 /* The argument is > 0.88309101259 */
122 /* We use sin(st(0)) = cos(pi/2-st(0)) */
124 fixed_arg
= significand(st0_ptr
);
128 /* The argument is >= 1.0 */
130 /* Put the binary point at the left. */
133 /* pi/2 in hex is: 1.921fb54442d18469 898CC51701B839A2 52049C1 */
134 fixed_arg
= 0x921fb54442d18469LL
- fixed_arg
;
135 /* There is a special case which arises due to rounding, to fix here. */
136 if ( fixed_arg
== 0xffffffffffffffffLL
)
139 XSIG_LL(argSqrd
) = fixed_arg
; argSqrd
.lsw
= 0;
140 mul64_Xsig(&argSqrd
, &fixed_arg
);
142 XSIG_LL(argTo4
) = XSIG_LL(argSqrd
); argTo4
.lsw
= argSqrd
.lsw
;
143 mul_Xsig_Xsig(&argTo4
, &argTo4
);
145 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), neg_terms_h
,
147 mul_Xsig_Xsig(&accumulator
, &argSqrd
);
148 negate_Xsig(&accumulator
);
150 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), pos_terms_h
,
152 negate_Xsig(&accumulator
);
154 mul64_Xsig(&accumulator
, &fixed_arg
);
155 mul64_Xsig(&accumulator
, &fixed_arg
);
157 shr_Xsig(&accumulator
, 3);
158 negate_Xsig(&accumulator
);
160 add_Xsig_Xsig(&accumulator
, &argSqrd
);
162 shr_Xsig(&accumulator
, 1);
164 accumulator
.lsw
|= 1; /* A zero accumulator here would cause problems */
165 negate_Xsig(&accumulator
);
167 /* The basic computation is complete. Now fix the answer to
168 compensate for the error due to the approximation used for
172 /* This has an exponent of -65 */
174 /* The fix-up needs to be improved for larger args */
175 if ( argSqrd
.msw
& 0xffc00000 )
177 /* Get about 32 bit precision in these: */
178 fix_up
-= mul_32_32(0x898cc517, argSqrd
.msw
) / 6;
180 fix_up
= mul_32_32(fix_up
, LL_MSW(fixed_arg
));
182 adj
= accumulator
.lsw
; /* temp save */
183 accumulator
.lsw
-= fix_up
;
184 if ( accumulator
.lsw
> adj
)
185 XSIG_LL(accumulator
) --;
187 echange
= round_Xsig(&accumulator
);
189 setexponentpos(&result
, echange
- 1);
192 significand(&result
) = XSIG_LL(accumulator
);
193 setsign(&result
, getsign(st0_ptr
));
194 FPU_copy_to_reg0(&result
, TAG_Valid
);
197 if ( (exponent(&result
) >= 0)
198 && (significand(&result
) > 0x8000000000000000LL
) )
200 EXCEPTION(EX_INTERNAL
|0x150);
202 #endif /* PARANOID */
208 /*--- poly_cos() ------------------------------------------------------------+
210 +---------------------------------------------------------------------------*/
211 void poly_cos(FPU_REG
*st0_ptr
)
214 long int exponent
, exp2
, echange
;
215 Xsig accumulator
, argSqrd
, fix_up
, argTo4
;
216 unsigned long long fixed_arg
;
219 if ( (exponent(st0_ptr
) > 0)
220 || ((exponent(st0_ptr
) == 0)
221 && (significand(st0_ptr
) > 0xc90fdaa22168c234LL
)) )
223 EXCEPTION(EX_Invalid
);
224 FPU_copy_to_reg0(&CONST_QNaN
, TAG_Special
);
227 #endif /* PARANOID */
229 exponent
= exponent(st0_ptr
);
231 accumulator
.lsw
= accumulator
.midw
= accumulator
.msw
= 0;
233 if ( (exponent
< -1) || ((exponent
== -1) && (st0_ptr
->sigh
<= 0xb00d6f54)) )
235 /* arg is < 0.687705 */
237 argSqrd
.msw
= st0_ptr
->sigh
; argSqrd
.midw
= st0_ptr
->sigl
;
239 mul64_Xsig(&argSqrd
, &significand(st0_ptr
));
243 /* shift the argument right by the required places */
244 shr_Xsig(&argSqrd
, 2*(-1-exponent
));
247 argTo4
.msw
= argSqrd
.msw
; argTo4
.midw
= argSqrd
.midw
;
248 argTo4
.lsw
= argSqrd
.lsw
;
249 mul_Xsig_Xsig(&argTo4
, &argTo4
);
251 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), neg_terms_h
,
253 mul_Xsig_Xsig(&accumulator
, &argSqrd
);
254 negate_Xsig(&accumulator
);
256 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), pos_terms_h
,
258 negate_Xsig(&accumulator
);
260 mul64_Xsig(&accumulator
, &significand(st0_ptr
));
261 mul64_Xsig(&accumulator
, &significand(st0_ptr
));
262 shr_Xsig(&accumulator
, -2*(1+exponent
));
264 shr_Xsig(&accumulator
, 3);
265 negate_Xsig(&accumulator
);
267 add_Xsig_Xsig(&accumulator
, &argSqrd
);
269 shr_Xsig(&accumulator
, 1);
271 /* It doesn't matter if accumulator is all zero here, the
272 following code will work ok */
273 negate_Xsig(&accumulator
);
275 if ( accumulator
.lsw
& 0x80000000 )
276 XSIG_LL(accumulator
) ++;
277 if ( accumulator
.msw
== 0 )
279 /* The result is 1.0 */
280 FPU_copy_to_reg0(&CONST_1
, TAG_Valid
);
285 significand(&result
) = XSIG_LL(accumulator
);
287 /* will be a valid positive nr with expon = -1 */
288 setexponentpos(&result
, -1);
293 fixed_arg
= significand(st0_ptr
);
297 /* The argument is >= 1.0 */
299 /* Put the binary point at the left. */
302 /* pi/2 in hex is: 1.921fb54442d18469 898CC51701B839A2 52049C1 */
303 fixed_arg
= 0x921fb54442d18469LL
- fixed_arg
;
304 /* There is a special case which arises due to rounding, to fix here. */
305 if ( fixed_arg
== 0xffffffffffffffffLL
)
311 /* A shift is needed here only for a narrow range of arguments,
312 i.e. for fixed_arg approx 2^-32, but we pick up more... */
313 if ( !(LL_MSW(fixed_arg
) & 0xffff0000) )
320 XSIG_LL(argSqrd
) = fixed_arg
; argSqrd
.lsw
= 0;
321 mul64_Xsig(&argSqrd
, &fixed_arg
);
325 /* shift the argument right by the required places */
326 shr_Xsig(&argSqrd
, 2*(-1-exponent
));
329 argTo4
.msw
= argSqrd
.msw
; argTo4
.midw
= argSqrd
.midw
;
330 argTo4
.lsw
= argSqrd
.lsw
;
331 mul_Xsig_Xsig(&argTo4
, &argTo4
);
333 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), neg_terms_l
,
335 mul_Xsig_Xsig(&accumulator
, &argSqrd
);
336 negate_Xsig(&accumulator
);
338 polynomial_Xsig(&accumulator
, &XSIG_LL(argTo4
), pos_terms_l
,
341 shr_Xsig(&accumulator
, 2); /* Divide by four */
342 accumulator
.msw
|= 0x80000000; /* Add 1.0 */
344 mul64_Xsig(&accumulator
, &fixed_arg
);
345 mul64_Xsig(&accumulator
, &fixed_arg
);
346 mul64_Xsig(&accumulator
, &fixed_arg
);
348 /* Divide by four, FPU_REG compatible, etc */
349 exponent
= 3*exponent
;
351 /* The minimum exponent difference is 3 */
352 shr_Xsig(&accumulator
, exp2
- exponent
);
354 negate_Xsig(&accumulator
);
355 XSIG_LL(accumulator
) += fixed_arg
;
357 /* The basic computation is complete. Now fix the answer to
358 compensate for the error due to the approximation used for
362 /* This has an exponent of -65 */
363 XSIG_LL(fix_up
) = 0x898cc51701b839a2ll
;
366 /* The fix-up needs to be improved for larger args */
367 if ( argSqrd
.msw
& 0xffc00000 )
369 /* Get about 32 bit precision in these: */
370 fix_up
.msw
-= mul_32_32(0x898cc517, argSqrd
.msw
) / 2;
371 fix_up
.msw
+= mul_32_32(0x898cc517, argTo4
.msw
) / 24;
374 exp2
+= norm_Xsig(&accumulator
);
375 shr_Xsig(&accumulator
, 1); /* Prevent overflow */
377 shr_Xsig(&fix_up
, 65 + exp2
);
379 add_Xsig_Xsig(&accumulator
, &fix_up
);
381 echange
= round_Xsig(&accumulator
);
383 setexponentpos(&result
, exp2
+ echange
);
384 significand(&result
) = XSIG_LL(accumulator
);
387 FPU_copy_to_reg0(&result
, TAG_Valid
);
390 if ( (exponent(&result
) >= 0)
391 && (significand(&result
) > 0x8000000000000000LL
) )
393 EXCEPTION(EX_INTERNAL
|0x151);
395 #endif /* PARANOID */