2 * Ported from a work by Andreas Grabher for Previous, NeXT Computer Emulator,
3 * derived from NetBSD M68040 FPSP functions,
4 * derived from release 2a of the SoftFloat IEC/IEEE Floating-point Arithmetic
5 * Package. Those parts of the code (and some later contributions) are
6 * provided under that license, as detailed below.
7 * It has subsequently been modified by contributors to the QEMU Project,
8 * so some portions are provided under:
9 * the SoftFloat-2a license
13 * Any future contributions to this file will be taken to be licensed under
14 * the Softfloat-2a license unless specifically indicated otherwise.
18 * Portions of this work are licensed under the terms of the GNU GPL,
19 * version 2 or later. See the COPYING file in the top-level directory.
22 #include "qemu/osdep.h"
23 #include "softfloat.h"
24 #include "fpu/softfloat-macros.h"
25 #include "softfloat_fpsp_tables.h"
28 #define piby2_exp 0x3FFF
29 #define pi_sig LIT64(0xc90fdaa22168c235)
31 static floatx80
propagateFloatx80NaNOneArg(floatx80 a
, float_status
*status
)
33 if (floatx80_is_signaling_nan(a
, status
)) {
34 float_raise(float_flag_invalid
, status
);
35 a
= floatx80_silence_nan(a
, status
);
38 if (status
->default_nan_mode
) {
39 return floatx80_default_nan(status
);
46 * Returns the modulo remainder of the extended double-precision floating-point
47 * value `a' with respect to the corresponding value `b'.
50 floatx80
floatx80_mod(floatx80 a
, floatx80 b
, float_status
*status
)
53 int32_t aExp
, bExp
, expDiff
;
54 uint64_t aSig0
, aSig1
, bSig
;
55 uint64_t qTemp
, term0
, term1
;
57 aSig0
= extractFloatx80Frac(a
);
58 aExp
= extractFloatx80Exp(a
);
59 aSign
= extractFloatx80Sign(a
);
60 bSig
= extractFloatx80Frac(b
);
61 bExp
= extractFloatx80Exp(b
);
64 if ((uint64_t) (aSig0
<< 1)
65 || ((bExp
== 0x7FFF) && (uint64_t) (bSig
<< 1))) {
66 return propagateFloatx80NaN(a
, b
, status
);
71 if ((uint64_t) (bSig
<< 1)) {
72 return propagateFloatx80NaN(a
, b
, status
);
79 float_raise(float_flag_invalid
, status
);
80 return floatx80_default_nan(status
);
82 normalizeFloatx80Subnormal(bSig
, &bExp
, &bSig
);
85 if ((uint64_t) (aSig0
<< 1) == 0) {
88 normalizeFloatx80Subnormal(aSig0
, &aExp
, &aSig0
);
90 bSig
|= LIT64(0x8000000000000000);
92 expDiff
= aExp
- bExp
;
97 qTemp
= (bSig
<= aSig0
);
102 while (0 < expDiff
) {
103 qTemp
= estimateDiv128To64(aSig0
, aSig1
, bSig
);
104 qTemp
= (2 < qTemp
) ? qTemp
- 2 : 0;
105 mul64To128(bSig
, qTemp
, &term0
, &term1
);
106 sub128(aSig0
, aSig1
, term0
, term1
, &aSig0
, &aSig1
);
107 shortShift128Left(aSig0
, aSig1
, 62, &aSig0
, &aSig1
);
112 qTemp
= estimateDiv128To64(aSig0
, aSig1
, bSig
);
113 qTemp
= (2 < qTemp
) ? qTemp
- 2 : 0;
114 qTemp
>>= 64 - expDiff
;
115 mul64To128(bSig
, qTemp
<< (64 - expDiff
), &term0
, &term1
);
116 sub128(aSig0
, aSig1
, term0
, term1
, &aSig0
, &aSig1
);
117 shortShift128Left(0, bSig
, 64 - expDiff
, &term0
, &term1
);
118 while (le128(term0
, term1
, aSig0
, aSig1
)) {
120 sub128(aSig0
, aSig1
, term0
, term1
, &aSig0
, &aSig1
);
124 normalizeRoundAndPackFloatx80(
125 80, zSign
, bExp
+ expDiff
, aSig0
, aSig1
, status
);
129 * Returns the mantissa of the extended double-precision floating-point
133 floatx80
floatx80_getman(floatx80 a
, float_status
*status
)
139 aSig
= extractFloatx80Frac(a
);
140 aExp
= extractFloatx80Exp(a
);
141 aSign
= extractFloatx80Sign(a
);
143 if (aExp
== 0x7FFF) {
144 if ((uint64_t) (aSig
<< 1)) {
145 return propagateFloatx80NaNOneArg(a
, status
);
147 float_raise(float_flag_invalid
, status
);
148 return floatx80_default_nan(status
);
153 return packFloatx80(aSign
, 0, 0);
155 normalizeFloatx80Subnormal(aSig
, &aExp
, &aSig
);
158 return roundAndPackFloatx80(status
->floatx80_rounding_precision
, aSign
,
159 0x3FFF, aSig
, 0, status
);
163 * Returns the exponent of the extended double-precision floating-point
164 * value `a' as an extended double-precision value.
167 floatx80
floatx80_getexp(floatx80 a
, float_status
*status
)
173 aSig
= extractFloatx80Frac(a
);
174 aExp
= extractFloatx80Exp(a
);
175 aSign
= extractFloatx80Sign(a
);
177 if (aExp
== 0x7FFF) {
178 if ((uint64_t) (aSig
<< 1)) {
179 return propagateFloatx80NaNOneArg(a
, status
);
181 float_raise(float_flag_invalid
, status
);
182 return floatx80_default_nan(status
);
187 return packFloatx80(aSign
, 0, 0);
189 normalizeFloatx80Subnormal(aSig
, &aExp
, &aSig
);
192 return int32_to_floatx80(aExp
- 0x3FFF, status
);
196 * Scales extended double-precision floating-point value in operand `a' by
197 * value `b'. The function truncates the value in the second operand 'b' to
198 * an integral value and adds that value to the exponent of the operand 'a'.
199 * The operation performed according to the IEC/IEEE Standard for Binary
200 * Floating-Point Arithmetic.
203 floatx80
floatx80_scale(floatx80 a
, floatx80 b
, float_status
*status
)
206 int32_t aExp
, bExp
, shiftCount
;
209 aSig
= extractFloatx80Frac(a
);
210 aExp
= extractFloatx80Exp(a
);
211 aSign
= extractFloatx80Sign(a
);
212 bSig
= extractFloatx80Frac(b
);
213 bExp
= extractFloatx80Exp(b
);
214 bSign
= extractFloatx80Sign(b
);
216 if (bExp
== 0x7FFF) {
217 if ((uint64_t) (bSig
<< 1) ||
218 ((aExp
== 0x7FFF) && (uint64_t) (aSig
<< 1))) {
219 return propagateFloatx80NaN(a
, b
, status
);
221 float_raise(float_flag_invalid
, status
);
222 return floatx80_default_nan(status
);
224 if (aExp
== 0x7FFF) {
225 if ((uint64_t) (aSig
<< 1)) {
226 return propagateFloatx80NaN(a
, b
, status
);
228 return packFloatx80(aSign
, floatx80_infinity
.high
,
229 floatx80_infinity
.low
);
233 return packFloatx80(aSign
, 0, 0);
238 normalizeFloatx80Subnormal(aSig
, &aExp
, &aSig
);
246 aExp
= bSign
? -0x6001 : 0xE000;
247 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
248 aSign
, aExp
, aSig
, 0, status
);
251 shiftCount
= 0x403E - bExp
;
253 aExp
= bSign
? (aExp
- bSig
) : (aExp
+ bSig
);
255 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
256 aSign
, aExp
, aSig
, 0, status
);
259 floatx80
floatx80_move(floatx80 a
, float_status
*status
)
265 aSig
= extractFloatx80Frac(a
);
266 aExp
= extractFloatx80Exp(a
);
267 aSign
= extractFloatx80Sign(a
);
269 if (aExp
== 0x7FFF) {
270 if ((uint64_t)(aSig
<< 1)) {
271 return propagateFloatx80NaNOneArg(a
, status
);
279 normalizeRoundAndPackFloatx80(status
->floatx80_rounding_precision
,
280 aSign
, aExp
, aSig
, 0, status
);
282 return roundAndPackFloatx80(status
->floatx80_rounding_precision
, aSign
,
283 aExp
, aSig
, 0, status
);
287 * Algorithms for transcendental functions supported by MC68881 and MC68882
288 * mathematical coprocessors. The functions are derived from FPSP library.
291 #define one_exp 0x3FFF
292 #define one_sig LIT64(0x8000000000000000)
295 * Function for compactifying extended double-precision floating point values.
298 static int32_t floatx80_make_compact(int32_t aExp
, uint64_t aSig
)
300 return (aExp
<< 16) | (aSig
>> 48);
304 * Log base e of x plus 1
307 floatx80
floatx80_lognp1(floatx80 a
, float_status
*status
)
313 int8_t user_rnd_mode
, user_rnd_prec
;
315 int32_t compact
, j
, k
;
316 floatx80 fp0
, fp1
, fp2
, fp3
, f
, logof2
, klog2
, saveu
;
318 aSig
= extractFloatx80Frac(a
);
319 aExp
= extractFloatx80Exp(a
);
320 aSign
= extractFloatx80Sign(a
);
322 if (aExp
== 0x7FFF) {
323 if ((uint64_t) (aSig
<< 1)) {
324 propagateFloatx80NaNOneArg(a
, status
);
327 float_raise(float_flag_invalid
, status
);
328 return floatx80_default_nan(status
);
330 return packFloatx80(0, floatx80_infinity
.high
, floatx80_infinity
.low
);
333 if (aExp
== 0 && aSig
== 0) {
334 return packFloatx80(aSign
, 0, 0);
337 if (aSign
&& aExp
>= one_exp
) {
338 if (aExp
== one_exp
&& aSig
== one_sig
) {
339 float_raise(float_flag_divbyzero
, status
);
340 return packFloatx80(aSign
, floatx80_infinity
.high
,
341 floatx80_infinity
.low
);
343 float_raise(float_flag_invalid
, status
);
344 return floatx80_default_nan(status
);
347 if (aExp
< 0x3f99 || (aExp
== 0x3f99 && aSig
== one_sig
)) {
348 /* <= min threshold */
349 float_raise(float_flag_inexact
, status
);
350 return floatx80_move(a
, status
);
353 user_rnd_mode
= status
->float_rounding_mode
;
354 user_rnd_prec
= status
->floatx80_rounding_precision
;
355 status
->float_rounding_mode
= float_round_nearest_even
;
356 status
->floatx80_rounding_precision
= 80;
358 compact
= floatx80_make_compact(aExp
, aSig
);
363 fp0
= floatx80_add(fp0
, float32_to_floatx80(make_float32(0x3F800000),
364 status
), status
); /* X = (1+Z) */
366 aExp
= extractFloatx80Exp(fp0
);
367 aSig
= extractFloatx80Frac(fp0
);
369 compact
= floatx80_make_compact(aExp
, aSig
);
371 if (compact
< 0x3FFE8000 || compact
> 0x3FFFC000) {
372 /* |X| < 1/2 or |X| > 3/2 */
374 fp1
= int32_to_floatx80(k
, status
);
376 fSig
= (aSig
& LIT64(0xFE00000000000000)) | LIT64(0x0100000000000000);
377 j
= (fSig
>> 56) & 0x7E; /* DISPLACEMENT FOR 1/F */
379 f
= packFloatx80(0, 0x3FFF, fSig
); /* F */
380 fp0
= packFloatx80(0, 0x3FFF, aSig
); /* Y */
382 fp0
= floatx80_sub(fp0
, f
, status
); /* Y-F */
386 fp0
= floatx80_mul(fp0
, log_tbl
[j
], status
); /* FP0 IS U = (Y-F)/F */
387 logof2
= packFloatx80(0, 0x3FFE, LIT64(0xB17217F7D1CF79AC));
388 klog2
= floatx80_mul(fp1
, logof2
, status
); /* FP1 IS K*LOG2 */
389 fp2
= floatx80_mul(fp0
, fp0
, status
); /* FP2 IS V=U*U */
394 fp1
= floatx80_mul(fp1
, float64_to_floatx80(
395 make_float64(0x3FC2499AB5E4040B), status
),
397 fp2
= floatx80_mul(fp2
, float64_to_floatx80(
398 make_float64(0xBFC555B5848CB7DB), status
),
400 fp1
= floatx80_add(fp1
, float64_to_floatx80(
401 make_float64(0x3FC99999987D8730), status
),
402 status
); /* A4+V*A6 */
403 fp2
= floatx80_add(fp2
, float64_to_floatx80(
404 make_float64(0xBFCFFFFFFF6F7E97), status
),
405 status
); /* A3+V*A5 */
406 fp1
= floatx80_mul(fp1
, fp3
, status
); /* V*(A4+V*A6) */
407 fp2
= floatx80_mul(fp2
, fp3
, status
); /* V*(A3+V*A5) */
408 fp1
= floatx80_add(fp1
, float64_to_floatx80(
409 make_float64(0x3FD55555555555A4), status
),
410 status
); /* A2+V*(A4+V*A6) */
411 fp2
= floatx80_add(fp2
, float64_to_floatx80(
412 make_float64(0xBFE0000000000008), status
),
413 status
); /* A1+V*(A3+V*A5) */
414 fp1
= floatx80_mul(fp1
, fp3
, status
); /* V*(A2+V*(A4+V*A6)) */
415 fp2
= floatx80_mul(fp2
, fp3
, status
); /* V*(A1+V*(A3+V*A5)) */
416 fp1
= floatx80_mul(fp1
, fp0
, status
); /* U*V*(A2+V*(A4+V*A6)) */
417 fp0
= floatx80_add(fp0
, fp2
, status
); /* U+V*(A1+V*(A3+V*A5)) */
419 fp1
= floatx80_add(fp1
, log_tbl
[j
+ 1],
420 status
); /* LOG(F)+U*V*(A2+V*(A4+V*A6)) */
421 fp0
= floatx80_add(fp0
, fp1
, status
); /* FP0 IS LOG(F) + LOG(1+U) */
423 status
->float_rounding_mode
= user_rnd_mode
;
424 status
->floatx80_rounding_precision
= user_rnd_prec
;
426 a
= floatx80_add(fp0
, klog2
, status
);
428 float_raise(float_flag_inexact
, status
);
431 } else if (compact
< 0x3FFEF07D || compact
> 0x3FFF8841) {
432 /* |X| < 1/16 or |X| > -1/16 */
434 fSig
= (aSig
& LIT64(0xFE00000000000000)) | LIT64(0x0100000000000000);
435 f
= packFloatx80(0, 0x3FFF, fSig
); /* F */
436 j
= (fSig
>> 56) & 0x7E; /* DISPLACEMENT FOR 1/F */
438 if (compact
>= 0x3FFF8000) { /* 1+Z >= 1 */
440 fp0
= floatx80_sub(float32_to_floatx80(make_float32(0x3F800000),
441 status
), f
, status
); /* 1-F */
442 fp0
= floatx80_add(fp0
, fp1
, status
); /* FP0 IS Y-F = (1-F)+Z */
443 fp1
= packFloatx80(0, 0, 0); /* K = 0 */
446 fp0
= floatx80_sub(float32_to_floatx80(make_float32(0x40000000),
447 status
), f
, status
); /* 2-F */
448 fp1
= floatx80_add(fp1
, fp1
, status
); /* 2Z */
449 fp0
= floatx80_add(fp0
, fp1
, status
); /* FP0 IS Y-F = (2-F)+2Z */
450 fp1
= packFloatx80(1, one_exp
, one_sig
); /* K = -1 */
455 fp1
= floatx80_add(fp1
, fp1
, status
); /* FP1 IS 2Z */
456 fp0
= floatx80_add(fp0
, float32_to_floatx80(make_float32(0x3F800000),
457 status
), status
); /* FP0 IS 1+X */
460 fp1
= floatx80_div(fp1
, fp0
, status
); /* U */
462 fp0
= floatx80_mul(fp1
, fp1
, status
); /* FP0 IS V = U*U */
463 fp1
= floatx80_mul(fp0
, fp0
, status
); /* FP1 IS W = V*V */
465 fp3
= float64_to_floatx80(make_float64(0x3F175496ADD7DAD6),
467 fp2
= float64_to_floatx80(make_float64(0x3F3C71C2FE80C7E0),
469 fp3
= floatx80_mul(fp3
, fp1
, status
); /* W*B5 */
470 fp2
= floatx80_mul(fp2
, fp1
, status
); /* W*B4 */
471 fp3
= floatx80_add(fp3
, float64_to_floatx80(
472 make_float64(0x3F624924928BCCFF), status
),
473 status
); /* B3+W*B5 */
474 fp2
= floatx80_add(fp2
, float64_to_floatx80(
475 make_float64(0x3F899999999995EC), status
),
476 status
); /* B2+W*B4 */
477 fp1
= floatx80_mul(fp1
, fp3
, status
); /* W*(B3+W*B5) */
478 fp2
= floatx80_mul(fp2
, fp0
, status
); /* V*(B2+W*B4) */
479 fp1
= floatx80_add(fp1
, float64_to_floatx80(
480 make_float64(0x3FB5555555555555), status
),
481 status
); /* B1+W*(B3+W*B5) */
483 fp0
= floatx80_mul(fp0
, saveu
, status
); /* FP0 IS U*V */
484 fp1
= floatx80_add(fp1
, fp2
,
485 status
); /* B1+W*(B3+W*B5) + V*(B2+W*B4) */
486 fp0
= floatx80_mul(fp0
, fp1
,
487 status
); /* U*V*([B1+W*(B3+W*B5)] + [V*(B2+W*B4)]) */
489 status
->float_rounding_mode
= user_rnd_mode
;
490 status
->floatx80_rounding_precision
= user_rnd_prec
;
492 a
= floatx80_add(fp0
, saveu
, status
);
494 /*if (!floatx80_is_zero(a)) { */
495 float_raise(float_flag_inexact
, status
);
506 floatx80
floatx80_logn(floatx80 a
, float_status
*status
)
512 int8_t user_rnd_mode
, user_rnd_prec
;
514 int32_t compact
, j
, k
, adjk
;
515 floatx80 fp0
, fp1
, fp2
, fp3
, f
, logof2
, klog2
, saveu
;
517 aSig
= extractFloatx80Frac(a
);
518 aExp
= extractFloatx80Exp(a
);
519 aSign
= extractFloatx80Sign(a
);
521 if (aExp
== 0x7FFF) {
522 if ((uint64_t) (aSig
<< 1)) {
523 propagateFloatx80NaNOneArg(a
, status
);
526 return packFloatx80(0, floatx80_infinity
.high
,
527 floatx80_infinity
.low
);
534 if (aSig
== 0) { /* zero */
535 float_raise(float_flag_divbyzero
, status
);
536 return packFloatx80(1, floatx80_infinity
.high
,
537 floatx80_infinity
.low
);
539 if ((aSig
& one_sig
) == 0) { /* denormal */
540 normalizeFloatx80Subnormal(aSig
, &aExp
, &aSig
);
543 a
= packFloatx80(aSign
, aExp
, aSig
);
548 float_raise(float_flag_invalid
, status
);
549 return floatx80_default_nan(status
);
552 user_rnd_mode
= status
->float_rounding_mode
;
553 user_rnd_prec
= status
->floatx80_rounding_precision
;
554 status
->float_rounding_mode
= float_round_nearest_even
;
555 status
->floatx80_rounding_precision
= 80;
557 compact
= floatx80_make_compact(aExp
, aSig
);
559 if (compact
< 0x3FFEF07D || compact
> 0x3FFF8841) {
560 /* |X| < 15/16 or |X| > 17/16 */
563 fp1
= int32_to_floatx80(k
, status
);
565 fSig
= (aSig
& LIT64(0xFE00000000000000)) | LIT64(0x0100000000000000);
566 j
= (fSig
>> 56) & 0x7E; /* DISPLACEMENT FOR 1/F */
568 f
= packFloatx80(0, 0x3FFF, fSig
); /* F */
569 fp0
= packFloatx80(0, 0x3FFF, aSig
); /* Y */
571 fp0
= floatx80_sub(fp0
, f
, status
); /* Y-F */
574 fp0
= floatx80_mul(fp0
, log_tbl
[j
], status
); /* FP0 IS U = (Y-F)/F */
575 logof2
= packFloatx80(0, 0x3FFE, LIT64(0xB17217F7D1CF79AC));
576 klog2
= floatx80_mul(fp1
, logof2
, status
); /* FP1 IS K*LOG2 */
577 fp2
= floatx80_mul(fp0
, fp0
, status
); /* FP2 IS V=U*U */
582 fp1
= floatx80_mul(fp1
, float64_to_floatx80(
583 make_float64(0x3FC2499AB5E4040B), status
),
585 fp2
= floatx80_mul(fp2
, float64_to_floatx80(
586 make_float64(0xBFC555B5848CB7DB), status
),
588 fp1
= floatx80_add(fp1
, float64_to_floatx80(
589 make_float64(0x3FC99999987D8730), status
),
590 status
); /* A4+V*A6 */
591 fp2
= floatx80_add(fp2
, float64_to_floatx80(
592 make_float64(0xBFCFFFFFFF6F7E97), status
),
593 status
); /* A3+V*A5 */
594 fp1
= floatx80_mul(fp1
, fp3
, status
); /* V*(A4+V*A6) */
595 fp2
= floatx80_mul(fp2
, fp3
, status
); /* V*(A3+V*A5) */
596 fp1
= floatx80_add(fp1
, float64_to_floatx80(
597 make_float64(0x3FD55555555555A4), status
),
598 status
); /* A2+V*(A4+V*A6) */
599 fp2
= floatx80_add(fp2
, float64_to_floatx80(
600 make_float64(0xBFE0000000000008), status
),
601 status
); /* A1+V*(A3+V*A5) */
602 fp1
= floatx80_mul(fp1
, fp3
, status
); /* V*(A2+V*(A4+V*A6)) */
603 fp2
= floatx80_mul(fp2
, fp3
, status
); /* V*(A1+V*(A3+V*A5)) */
604 fp1
= floatx80_mul(fp1
, fp0
, status
); /* U*V*(A2+V*(A4+V*A6)) */
605 fp0
= floatx80_add(fp0
, fp2
, status
); /* U+V*(A1+V*(A3+V*A5)) */
607 fp1
= floatx80_add(fp1
, log_tbl
[j
+ 1],
608 status
); /* LOG(F)+U*V*(A2+V*(A4+V*A6)) */
609 fp0
= floatx80_add(fp0
, fp1
, status
); /* FP0 IS LOG(F) + LOG(1+U) */
611 status
->float_rounding_mode
= user_rnd_mode
;
612 status
->floatx80_rounding_precision
= user_rnd_prec
;
614 a
= floatx80_add(fp0
, klog2
, status
);
616 float_raise(float_flag_inexact
, status
);
619 } else { /* |X-1| >= 1/16 */
622 fp1
= floatx80_sub(fp1
, float32_to_floatx80(make_float32(0x3F800000),
623 status
), status
); /* FP1 IS X-1 */
624 fp0
= floatx80_add(fp0
, float32_to_floatx80(make_float32(0x3F800000),
625 status
), status
); /* FP0 IS X+1 */
626 fp1
= floatx80_add(fp1
, fp1
, status
); /* FP1 IS 2(X-1) */
629 fp1
= floatx80_div(fp1
, fp0
, status
); /* U */
631 fp0
= floatx80_mul(fp1
, fp1
, status
); /* FP0 IS V = U*U */
632 fp1
= floatx80_mul(fp0
, fp0
, status
); /* FP1 IS W = V*V */
634 fp3
= float64_to_floatx80(make_float64(0x3F175496ADD7DAD6),
636 fp2
= float64_to_floatx80(make_float64(0x3F3C71C2FE80C7E0),
638 fp3
= floatx80_mul(fp3
, fp1
, status
); /* W*B5 */
639 fp2
= floatx80_mul(fp2
, fp1
, status
); /* W*B4 */
640 fp3
= floatx80_add(fp3
, float64_to_floatx80(
641 make_float64(0x3F624924928BCCFF), status
),
642 status
); /* B3+W*B5 */
643 fp2
= floatx80_add(fp2
, float64_to_floatx80(
644 make_float64(0x3F899999999995EC), status
),
645 status
); /* B2+W*B4 */
646 fp1
= floatx80_mul(fp1
, fp3
, status
); /* W*(B3+W*B5) */
647 fp2
= floatx80_mul(fp2
, fp0
, status
); /* V*(B2+W*B4) */
648 fp1
= floatx80_add(fp1
, float64_to_floatx80(
649 make_float64(0x3FB5555555555555), status
),
650 status
); /* B1+W*(B3+W*B5) */
652 fp0
= floatx80_mul(fp0
, saveu
, status
); /* FP0 IS U*V */
653 fp1
= floatx80_add(fp1
, fp2
, status
); /* B1+W*(B3+W*B5) + V*(B2+W*B4) */
654 fp0
= floatx80_mul(fp0
, fp1
,
655 status
); /* U*V*([B1+W*(B3+W*B5)] + [V*(B2+W*B4)]) */
657 status
->float_rounding_mode
= user_rnd_mode
;
658 status
->floatx80_rounding_precision
= user_rnd_prec
;
660 a
= floatx80_add(fp0
, saveu
, status
);
662 /*if (!floatx80_is_zero(a)) { */
663 float_raise(float_flag_inexact
, status
);
674 floatx80
floatx80_log10(floatx80 a
, float_status
*status
)
680 int8_t user_rnd_mode
, user_rnd_prec
;
684 aSig
= extractFloatx80Frac(a
);
685 aExp
= extractFloatx80Exp(a
);
686 aSign
= extractFloatx80Sign(a
);
688 if (aExp
== 0x7FFF) {
689 if ((uint64_t) (aSig
<< 1)) {
690 propagateFloatx80NaNOneArg(a
, status
);
693 return packFloatx80(0, floatx80_infinity
.high
,
694 floatx80_infinity
.low
);
698 if (aExp
== 0 && aSig
== 0) {
699 float_raise(float_flag_divbyzero
, status
);
700 return packFloatx80(1, floatx80_infinity
.high
,
701 floatx80_infinity
.low
);
705 float_raise(float_flag_invalid
, status
);
706 return floatx80_default_nan(status
);
709 user_rnd_mode
= status
->float_rounding_mode
;
710 user_rnd_prec
= status
->floatx80_rounding_precision
;
711 status
->float_rounding_mode
= float_round_nearest_even
;
712 status
->floatx80_rounding_precision
= 80;
714 fp0
= floatx80_logn(a
, status
);
715 fp1
= packFloatx80(0, 0x3FFD, LIT64(0xDE5BD8A937287195)); /* INV_L10 */
717 status
->float_rounding_mode
= user_rnd_mode
;
718 status
->floatx80_rounding_precision
= user_rnd_prec
;
720 a
= floatx80_mul(fp0
, fp1
, status
); /* LOGN(X)*INV_L10 */
722 float_raise(float_flag_inexact
, status
);
731 floatx80
floatx80_log2(floatx80 a
, float_status
*status
)
737 int8_t user_rnd_mode
, user_rnd_prec
;
741 aSig
= extractFloatx80Frac(a
);
742 aExp
= extractFloatx80Exp(a
);
743 aSign
= extractFloatx80Sign(a
);
745 if (aExp
== 0x7FFF) {
746 if ((uint64_t) (aSig
<< 1)) {
747 propagateFloatx80NaNOneArg(a
, status
);
750 return packFloatx80(0, floatx80_infinity
.high
,
751 floatx80_infinity
.low
);
757 float_raise(float_flag_divbyzero
, status
);
758 return packFloatx80(1, floatx80_infinity
.high
,
759 floatx80_infinity
.low
);
761 normalizeFloatx80Subnormal(aSig
, &aExp
, &aSig
);
765 float_raise(float_flag_invalid
, status
);
766 return floatx80_default_nan(status
);
769 user_rnd_mode
= status
->float_rounding_mode
;
770 user_rnd_prec
= status
->floatx80_rounding_precision
;
771 status
->float_rounding_mode
= float_round_nearest_even
;
772 status
->floatx80_rounding_precision
= 80;
774 if (aSig
== one_sig
) { /* X is 2^k */
775 status
->float_rounding_mode
= user_rnd_mode
;
776 status
->floatx80_rounding_precision
= user_rnd_prec
;
778 a
= int32_to_floatx80(aExp
- 0x3FFF, status
);
780 fp0
= floatx80_logn(a
, status
);
781 fp1
= packFloatx80(0, 0x3FFF, LIT64(0xB8AA3B295C17F0BC)); /* INV_L2 */
783 status
->float_rounding_mode
= user_rnd_mode
;
784 status
->floatx80_rounding_precision
= user_rnd_prec
;
786 a
= floatx80_mul(fp0
, fp1
, status
); /* LOGN(X)*INV_L2 */
789 float_raise(float_flag_inexact
, status
);
798 floatx80
floatx80_etox(floatx80 a
, float_status
*status
)
804 int8_t user_rnd_mode
, user_rnd_prec
;
806 int32_t compact
, n
, j
, k
, m
, m1
;
807 floatx80 fp0
, fp1
, fp2
, fp3
, l2
, scale
, adjscale
;
810 aSig
= extractFloatx80Frac(a
);
811 aExp
= extractFloatx80Exp(a
);
812 aSign
= extractFloatx80Sign(a
);
814 if (aExp
== 0x7FFF) {
815 if ((uint64_t) (aSig
<< 1)) {
816 return propagateFloatx80NaNOneArg(a
, status
);
819 return packFloatx80(0, 0, 0);
821 return packFloatx80(0, floatx80_infinity
.high
,
822 floatx80_infinity
.low
);
825 if (aExp
== 0 && aSig
== 0) {
826 return packFloatx80(0, one_exp
, one_sig
);
829 user_rnd_mode
= status
->float_rounding_mode
;
830 user_rnd_prec
= status
->floatx80_rounding_precision
;
831 status
->float_rounding_mode
= float_round_nearest_even
;
832 status
->floatx80_rounding_precision
= 80;
836 if (aExp
>= 0x3FBE) { /* |X| >= 2^(-65) */
837 compact
= floatx80_make_compact(aExp
, aSig
);
839 if (compact
< 0x400CB167) { /* |X| < 16380 log2 */
842 fp0
= floatx80_mul(fp0
, float32_to_floatx80(
843 make_float32(0x42B8AA3B), status
),
844 status
); /* 64/log2 * X */
846 n
= floatx80_to_int32(fp0
, status
); /* int(64/log2*X) */
847 fp0
= int32_to_floatx80(n
, status
);
849 j
= n
& 0x3F; /* J = N mod 64 */
850 m
= n
/ 64; /* NOTE: this is really arithmetic right shift by 6 */
853 * arithmetic right shift is division and
854 * round towards minus infinity
858 m
+= 0x3FFF; /* biased exponent of 2^(M) */
862 fp0
= floatx80_mul(fp0
, float32_to_floatx80(
863 make_float32(0xBC317218), status
),
864 status
); /* N * L1, L1 = lead(-log2/64) */
865 l2
= packFloatx80(0, 0x3FDC, LIT64(0x82E308654361C4C6));
866 fp2
= floatx80_mul(fp2
, l2
, status
); /* N * L2, L1+L2 = -log2/64 */
867 fp0
= floatx80_add(fp0
, fp1
, status
); /* X + N*L1 */
868 fp0
= floatx80_add(fp0
, fp2
, status
); /* R */
870 fp1
= floatx80_mul(fp0
, fp0
, status
); /* S = R*R */
871 fp2
= float32_to_floatx80(make_float32(0x3AB60B70),
873 fp2
= floatx80_mul(fp2
, fp1
, status
); /* fp2 is S*A5 */
874 fp3
= floatx80_mul(float32_to_floatx80(make_float32(0x3C088895),
876 status
); /* fp3 is S*A4 */
877 fp2
= floatx80_add(fp2
, float64_to_floatx80(make_float64(
878 0x3FA5555555554431), status
),
879 status
); /* fp2 is A3+S*A5 */
880 fp3
= floatx80_add(fp3
, float64_to_floatx80(make_float64(
881 0x3FC5555555554018), status
),
882 status
); /* fp3 is A2+S*A4 */
883 fp2
= floatx80_mul(fp2
, fp1
, status
); /* fp2 is S*(A3+S*A5) */
884 fp3
= floatx80_mul(fp3
, fp1
, status
); /* fp3 is S*(A2+S*A4) */
885 fp2
= floatx80_add(fp2
, float32_to_floatx80(
886 make_float32(0x3F000000), status
),
887 status
); /* fp2 is A1+S*(A3+S*A5) */
888 fp3
= floatx80_mul(fp3
, fp0
, status
); /* fp3 IS R*S*(A2+S*A4) */
889 fp2
= floatx80_mul(fp2
, fp1
,
890 status
); /* fp2 IS S*(A1+S*(A3+S*A5)) */
891 fp0
= floatx80_add(fp0
, fp3
, status
); /* fp0 IS R+R*S*(A2+S*A4) */
892 fp0
= floatx80_add(fp0
, fp2
, status
); /* fp0 IS EXP(R) - 1 */
895 fp0
= floatx80_mul(fp0
, fp1
, status
); /* 2^(J/64)*(Exp(R)-1) */
896 fp0
= floatx80_add(fp0
, float32_to_floatx80(exp_tbl2
[j
], status
),
897 status
); /* accurate 2^(J/64) */
898 fp0
= floatx80_add(fp0
, fp1
,
899 status
); /* 2^(J/64) + 2^(J/64)*(Exp(R)-1) */
901 scale
= packFloatx80(0, m
, one_sig
);
903 adjscale
= packFloatx80(0, m1
, one_sig
);
904 fp0
= floatx80_mul(fp0
, adjscale
, status
);
907 status
->float_rounding_mode
= user_rnd_mode
;
908 status
->floatx80_rounding_precision
= user_rnd_prec
;
910 a
= floatx80_mul(fp0
, scale
, status
);
912 float_raise(float_flag_inexact
, status
);
915 } else { /* |X| >= 16380 log2 */
916 if (compact
> 0x400CB27C) { /* |X| >= 16480 log2 */
917 status
->float_rounding_mode
= user_rnd_mode
;
918 status
->floatx80_rounding_precision
= user_rnd_prec
;
920 a
= roundAndPackFloatx80(
921 status
->floatx80_rounding_precision
,
922 0, -0x1000, aSig
, 0, status
);
924 a
= roundAndPackFloatx80(
925 status
->floatx80_rounding_precision
,
926 0, 0x8000, aSig
, 0, status
);
928 float_raise(float_flag_inexact
, status
);
934 fp0
= floatx80_mul(fp0
, float32_to_floatx80(
935 make_float32(0x42B8AA3B), status
),
936 status
); /* 64/log2 * X */
938 n
= floatx80_to_int32(fp0
, status
); /* int(64/log2*X) */
939 fp0
= int32_to_floatx80(n
, status
);
941 j
= n
& 0x3F; /* J = N mod 64 */
942 /* NOTE: this is really arithmetic right shift by 6 */
945 /* arithmetic right shift is division and
946 * round towards minus infinity
950 /* NOTE: this is really arithmetic right shift by 1 */
952 if (k
< 0 && (k
& 1)) {
953 /* arithmetic right shift is division and
954 * round towards minus infinity
959 m1
+= 0x3FFF; /* biased exponent of 2^(M1) */
960 m
+= 0x3FFF; /* biased exponent of 2^(M) */
965 } else { /* |X| < 2^(-65) */
966 status
->float_rounding_mode
= user_rnd_mode
;
967 status
->floatx80_rounding_precision
= user_rnd_prec
;
969 a
= floatx80_add(a
, float32_to_floatx80(make_float32(0x3F800000),
970 status
), status
); /* 1 + X */
972 float_raise(float_flag_inexact
, status
);
982 floatx80
floatx80_twotox(floatx80 a
, float_status
*status
)
988 int8_t user_rnd_mode
, user_rnd_prec
;
990 int32_t compact
, n
, j
, l
, m
, m1
;
991 floatx80 fp0
, fp1
, fp2
, fp3
, adjfact
, fact1
, fact2
;
993 aSig
= extractFloatx80Frac(a
);
994 aExp
= extractFloatx80Exp(a
);
995 aSign
= extractFloatx80Sign(a
);
997 if (aExp
== 0x7FFF) {
998 if ((uint64_t) (aSig
<< 1)) {
999 return propagateFloatx80NaNOneArg(a
, status
);
1002 return packFloatx80(0, 0, 0);
1004 return packFloatx80(0, floatx80_infinity
.high
,
1005 floatx80_infinity
.low
);
1008 if (aExp
== 0 && aSig
== 0) {
1009 return packFloatx80(0, one_exp
, one_sig
);
1012 user_rnd_mode
= status
->float_rounding_mode
;
1013 user_rnd_prec
= status
->floatx80_rounding_precision
;
1014 status
->float_rounding_mode
= float_round_nearest_even
;
1015 status
->floatx80_rounding_precision
= 80;
1019 compact
= floatx80_make_compact(aExp
, aSig
);
1021 if (compact
< 0x3FB98000 || compact
> 0x400D80C0) {
1022 /* |X| > 16480 or |X| < 2^(-70) */
1023 if (compact
> 0x3FFF8000) { /* |X| > 16480 */
1024 status
->float_rounding_mode
= user_rnd_mode
;
1025 status
->floatx80_rounding_precision
= user_rnd_prec
;
1028 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
1029 0, -0x1000, aSig
, 0, status
);
1031 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
1032 0, 0x8000, aSig
, 0, status
);
1034 } else { /* |X| < 2^(-70) */
1035 status
->float_rounding_mode
= user_rnd_mode
;
1036 status
->floatx80_rounding_precision
= user_rnd_prec
;
1038 a
= floatx80_add(fp0
, float32_to_floatx80(
1039 make_float32(0x3F800000), status
),
1040 status
); /* 1 + X */
1042 float_raise(float_flag_inexact
, status
);
1046 } else { /* 2^(-70) <= |X| <= 16480 */
1048 fp1
= floatx80_mul(fp1
, float32_to_floatx80(
1049 make_float32(0x42800000), status
),
1050 status
); /* X * 64 */
1051 n
= floatx80_to_int32(fp1
, status
);
1052 fp1
= int32_to_floatx80(n
, status
);
1054 l
= n
/ 64; /* NOTE: this is really arithmetic right shift by 6 */
1057 * arithmetic right shift is division and
1058 * round towards minus infinity
1062 m
= l
/ 2; /* NOTE: this is really arithmetic right shift by 1 */
1063 if (l
< 0 && (l
& 1)) {
1065 * arithmetic right shift is division and
1066 * round towards minus infinity
1071 m1
+= 0x3FFF; /* ADJFACT IS 2^(M') */
1073 adjfact
= packFloatx80(0, m1
, one_sig
);
1074 fact1
= exp2_tbl
[j
];
1076 fact2
.high
= exp2_tbl2
[j
] >> 16;
1078 fact2
.low
= (uint64_t)(exp2_tbl2
[j
] & 0xFFFF);
1081 fp1
= floatx80_mul(fp1
, float32_to_floatx80(
1082 make_float32(0x3C800000), status
),
1083 status
); /* (1/64)*N */
1084 fp0
= floatx80_sub(fp0
, fp1
, status
); /* X - (1/64)*INT(64 X) */
1085 fp2
= packFloatx80(0, 0x3FFE, LIT64(0xB17217F7D1CF79AC)); /* LOG2 */
1086 fp0
= floatx80_mul(fp0
, fp2
, status
); /* R */
1089 fp1
= floatx80_mul(fp0
, fp0
, status
); /* S = R*R */
1090 fp2
= float64_to_floatx80(make_float64(0x3F56C16D6F7BD0B2),
1092 fp3
= float64_to_floatx80(make_float64(0x3F811112302C712C),
1094 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S*A5 */
1095 fp3
= floatx80_mul(fp3
, fp1
, status
); /* S*A4 */
1096 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1097 make_float64(0x3FA5555555554CC1), status
),
1098 status
); /* A3+S*A5 */
1099 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1100 make_float64(0x3FC5555555554A54), status
),
1101 status
); /* A2+S*A4 */
1102 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S*(A3+S*A5) */
1103 fp3
= floatx80_mul(fp3
, fp1
, status
); /* S*(A2+S*A4) */
1104 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1105 make_float64(0x3FE0000000000000), status
),
1106 status
); /* A1+S*(A3+S*A5) */
1107 fp3
= floatx80_mul(fp3
, fp0
, status
); /* R*S*(A2+S*A4) */
1109 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S*(A1+S*(A3+S*A5)) */
1110 fp0
= floatx80_add(fp0
, fp3
, status
); /* R+R*S*(A2+S*A4) */
1111 fp0
= floatx80_add(fp0
, fp2
, status
); /* EXP(R) - 1 */
1113 fp0
= floatx80_mul(fp0
, fact1
, status
);
1114 fp0
= floatx80_add(fp0
, fact2
, status
);
1115 fp0
= floatx80_add(fp0
, fact1
, status
);
1117 status
->float_rounding_mode
= user_rnd_mode
;
1118 status
->floatx80_rounding_precision
= user_rnd_prec
;
1120 a
= floatx80_mul(fp0
, adjfact
, status
);
1122 float_raise(float_flag_inexact
, status
);
1132 floatx80
floatx80_tentox(floatx80 a
, float_status
*status
)
1138 int8_t user_rnd_mode
, user_rnd_prec
;
1140 int32_t compact
, n
, j
, l
, m
, m1
;
1141 floatx80 fp0
, fp1
, fp2
, fp3
, adjfact
, fact1
, fact2
;
1143 aSig
= extractFloatx80Frac(a
);
1144 aExp
= extractFloatx80Exp(a
);
1145 aSign
= extractFloatx80Sign(a
);
1147 if (aExp
== 0x7FFF) {
1148 if ((uint64_t) (aSig
<< 1)) {
1149 return propagateFloatx80NaNOneArg(a
, status
);
1152 return packFloatx80(0, 0, 0);
1154 return packFloatx80(0, floatx80_infinity
.high
,
1155 floatx80_infinity
.low
);
1158 if (aExp
== 0 && aSig
== 0) {
1159 return packFloatx80(0, one_exp
, one_sig
);
1162 user_rnd_mode
= status
->float_rounding_mode
;
1163 user_rnd_prec
= status
->floatx80_rounding_precision
;
1164 status
->float_rounding_mode
= float_round_nearest_even
;
1165 status
->floatx80_rounding_precision
= 80;
1169 compact
= floatx80_make_compact(aExp
, aSig
);
1171 if (compact
< 0x3FB98000 || compact
> 0x400B9B07) {
1172 /* |X| > 16480 LOG2/LOG10 or |X| < 2^(-70) */
1173 if (compact
> 0x3FFF8000) { /* |X| > 16480 */
1174 status
->float_rounding_mode
= user_rnd_mode
;
1175 status
->floatx80_rounding_precision
= user_rnd_prec
;
1178 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
1179 0, -0x1000, aSig
, 0, status
);
1181 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
1182 0, 0x8000, aSig
, 0, status
);
1184 } else { /* |X| < 2^(-70) */
1185 status
->float_rounding_mode
= user_rnd_mode
;
1186 status
->floatx80_rounding_precision
= user_rnd_prec
;
1188 a
= floatx80_add(fp0
, float32_to_floatx80(
1189 make_float32(0x3F800000), status
),
1190 status
); /* 1 + X */
1192 float_raise(float_flag_inexact
, status
);
1196 } else { /* 2^(-70) <= |X| <= 16480 LOG 2 / LOG 10 */
1198 fp1
= floatx80_mul(fp1
, float64_to_floatx80(
1199 make_float64(0x406A934F0979A371),
1200 status
), status
); /* X*64*LOG10/LOG2 */
1201 n
= floatx80_to_int32(fp1
, status
); /* N=INT(X*64*LOG10/LOG2) */
1202 fp1
= int32_to_floatx80(n
, status
);
1205 l
= n
/ 64; /* NOTE: this is really arithmetic right shift by 6 */
1208 * arithmetic right shift is division and
1209 * round towards minus infinity
1213 m
= l
/ 2; /* NOTE: this is really arithmetic right shift by 1 */
1214 if (l
< 0 && (l
& 1)) {
1216 * arithmetic right shift is division and
1217 * round towards minus infinity
1222 m1
+= 0x3FFF; /* ADJFACT IS 2^(M') */
1224 adjfact
= packFloatx80(0, m1
, one_sig
);
1225 fact1
= exp2_tbl
[j
];
1227 fact2
.high
= exp2_tbl2
[j
] >> 16;
1229 fact2
.low
= (uint64_t)(exp2_tbl2
[j
] & 0xFFFF);
1233 fp1
= floatx80_mul(fp1
, float64_to_floatx80(
1234 make_float64(0x3F734413509F8000), status
),
1235 status
); /* N*(LOG2/64LOG10)_LEAD */
1236 fp3
= packFloatx80(1, 0x3FCD, LIT64(0xC0219DC1DA994FD2));
1237 fp2
= floatx80_mul(fp2
, fp3
, status
); /* N*(LOG2/64LOG10)_TRAIL */
1238 fp0
= floatx80_sub(fp0
, fp1
, status
); /* X - N L_LEAD */
1239 fp0
= floatx80_sub(fp0
, fp2
, status
); /* X - N L_TRAIL */
1240 fp2
= packFloatx80(0, 0x4000, LIT64(0x935D8DDDAAA8AC17)); /* LOG10 */
1241 fp0
= floatx80_mul(fp0
, fp2
, status
); /* R */
1244 fp1
= floatx80_mul(fp0
, fp0
, status
); /* S = R*R */
1245 fp2
= float64_to_floatx80(make_float64(0x3F56C16D6F7BD0B2),
1247 fp3
= float64_to_floatx80(make_float64(0x3F811112302C712C),
1249 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S*A5 */
1250 fp3
= floatx80_mul(fp3
, fp1
, status
); /* S*A4 */
1251 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1252 make_float64(0x3FA5555555554CC1), status
),
1253 status
); /* A3+S*A5 */
1254 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1255 make_float64(0x3FC5555555554A54), status
),
1256 status
); /* A2+S*A4 */
1257 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S*(A3+S*A5) */
1258 fp3
= floatx80_mul(fp3
, fp1
, status
); /* S*(A2+S*A4) */
1259 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1260 make_float64(0x3FE0000000000000), status
),
1261 status
); /* A1+S*(A3+S*A5) */
1262 fp3
= floatx80_mul(fp3
, fp0
, status
); /* R*S*(A2+S*A4) */
1264 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S*(A1+S*(A3+S*A5)) */
1265 fp0
= floatx80_add(fp0
, fp3
, status
); /* R+R*S*(A2+S*A4) */
1266 fp0
= floatx80_add(fp0
, fp2
, status
); /* EXP(R) - 1 */
1268 fp0
= floatx80_mul(fp0
, fact1
, status
);
1269 fp0
= floatx80_add(fp0
, fact2
, status
);
1270 fp0
= floatx80_add(fp0
, fact1
, status
);
1272 status
->float_rounding_mode
= user_rnd_mode
;
1273 status
->floatx80_rounding_precision
= user_rnd_prec
;
1275 a
= floatx80_mul(fp0
, adjfact
, status
);
1277 float_raise(float_flag_inexact
, status
);
1287 floatx80
floatx80_tan(floatx80 a
, float_status
*status
)
1291 uint64_t aSig
, xSig
;
1293 int8_t user_rnd_mode
, user_rnd_prec
;
1295 int32_t compact
, l
, n
, j
;
1296 floatx80 fp0
, fp1
, fp2
, fp3
, fp4
, fp5
, invtwopi
, twopi1
, twopi2
;
1300 aSig
= extractFloatx80Frac(a
);
1301 aExp
= extractFloatx80Exp(a
);
1302 aSign
= extractFloatx80Sign(a
);
1304 if (aExp
== 0x7FFF) {
1305 if ((uint64_t) (aSig
<< 1)) {
1306 return propagateFloatx80NaNOneArg(a
, status
);
1308 float_raise(float_flag_invalid
, status
);
1309 return floatx80_default_nan(status
);
1312 if (aExp
== 0 && aSig
== 0) {
1313 return packFloatx80(aSign
, 0, 0);
1316 user_rnd_mode
= status
->float_rounding_mode
;
1317 user_rnd_prec
= status
->floatx80_rounding_precision
;
1318 status
->float_rounding_mode
= float_round_nearest_even
;
1319 status
->floatx80_rounding_precision
= 80;
1321 compact
= floatx80_make_compact(aExp
, aSig
);
1325 if (compact
< 0x3FD78000 || compact
> 0x4004BC7E) {
1326 /* 2^(-40) > |X| > 15 PI */
1327 if (compact
> 0x3FFF8000) { /* |X| >= 15 PI */
1329 fp1
= packFloatx80(0, 0, 0);
1330 if (compact
== 0x7FFEFFFF) {
1331 twopi1
= packFloatx80(aSign
^ 1, 0x7FFE,
1332 LIT64(0xC90FDAA200000000));
1333 twopi2
= packFloatx80(aSign
^ 1, 0x7FDC,
1334 LIT64(0x85A308D300000000));
1335 fp0
= floatx80_add(fp0
, twopi1
, status
);
1337 fp0
= floatx80_add(fp0
, twopi2
, status
);
1338 fp1
= floatx80_sub(fp1
, fp0
, status
);
1339 fp1
= floatx80_add(fp1
, twopi2
, status
);
1342 xSign
= extractFloatx80Sign(fp0
);
1343 xExp
= extractFloatx80Exp(fp0
);
1352 invtwopi
= packFloatx80(0, 0x3FFE - l
,
1353 LIT64(0xA2F9836E4E44152A)); /* INVTWOPI */
1354 twopi1
= packFloatx80(0, 0x3FFF + l
, LIT64(0xC90FDAA200000000));
1355 twopi2
= packFloatx80(0, 0x3FDD + l
, LIT64(0x85A308D300000000));
1357 /* SIGN(INARG)*2^63 IN SGL */
1358 twoto63
= packFloat32(xSign
, 0xBE, 0);
1360 fp2
= floatx80_mul(fp0
, invtwopi
, status
);
1361 fp2
= floatx80_add(fp2
, float32_to_floatx80(twoto63
, status
),
1362 status
); /* THE FRACT PART OF FP2 IS ROUNDED */
1363 fp2
= floatx80_sub(fp2
, float32_to_floatx80(twoto63
, status
),
1364 status
); /* FP2 is N */
1365 fp4
= floatx80_mul(twopi1
, fp2
, status
); /* W = N*P1 */
1366 fp5
= floatx80_mul(twopi2
, fp2
, status
); /* w = N*P2 */
1367 fp3
= floatx80_add(fp4
, fp5
, status
); /* FP3 is P */
1368 fp4
= floatx80_sub(fp4
, fp3
, status
); /* W-P */
1369 fp0
= floatx80_sub(fp0
, fp3
, status
); /* FP0 is A := R - P */
1370 fp4
= floatx80_add(fp4
, fp5
, status
); /* FP4 is p = (W-P)+w */
1371 fp3
= fp0
; /* FP3 is A */
1372 fp1
= floatx80_sub(fp1
, fp4
, status
); /* FP1 is a := r - p */
1373 fp0
= floatx80_add(fp0
, fp1
, status
); /* FP0 is R := A+a */
1376 n
= floatx80_to_int32(fp2
, status
);
1379 fp3
= floatx80_sub(fp3
, fp0
, status
); /* A-R */
1380 fp1
= floatx80_add(fp1
, fp3
, status
); /* FP1 is r := (A-R)+a */
1383 status
->float_rounding_mode
= user_rnd_mode
;
1384 status
->floatx80_rounding_precision
= user_rnd_prec
;
1386 a
= floatx80_move(a
, status
);
1388 float_raise(float_flag_inexact
, status
);
1393 fp1
= floatx80_mul(fp0
, float64_to_floatx80(
1394 make_float64(0x3FE45F306DC9C883), status
),
1395 status
); /* X*2/PI */
1397 n
= floatx80_to_int32(fp1
, status
);
1400 fp0
= floatx80_sub(fp0
, pi_tbl
[j
], status
); /* X-Y1 */
1401 fp0
= floatx80_sub(fp0
, float32_to_floatx80(pi_tbl2
[j
], status
),
1402 status
); /* FP0 IS R = (X-Y1)-Y2 */
1408 fp0
= floatx80_mul(fp0
, fp0
, status
); /* S = R*R */
1409 fp3
= float64_to_floatx80(make_float64(0x3EA0B759F50F8688),
1411 fp2
= float64_to_floatx80(make_float64(0xBEF2BAA5A8924F04),
1413 fp3
= floatx80_mul(fp3
, fp0
, status
); /* SQ4 */
1414 fp2
= floatx80_mul(fp2
, fp0
, status
); /* SP3 */
1415 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1416 make_float64(0xBF346F59B39BA65F), status
),
1417 status
); /* Q3+SQ4 */
1418 fp4
= packFloatx80(0, 0x3FF6, LIT64(0xE073D3FC199C4A00));
1419 fp2
= floatx80_add(fp2
, fp4
, status
); /* P2+SP3 */
1420 fp3
= floatx80_mul(fp3
, fp0
, status
); /* S(Q3+SQ4) */
1421 fp2
= floatx80_mul(fp2
, fp0
, status
); /* S(P2+SP3) */
1422 fp4
= packFloatx80(0, 0x3FF9, LIT64(0xD23CD68415D95FA1));
1423 fp3
= floatx80_add(fp3
, fp4
, status
); /* Q2+S(Q3+SQ4) */
1424 fp4
= packFloatx80(1, 0x3FFC, LIT64(0x8895A6C5FB423BCA));
1425 fp2
= floatx80_add(fp2
, fp4
, status
); /* P1+S(P2+SP3) */
1426 fp3
= floatx80_mul(fp3
, fp0
, status
); /* S(Q2+S(Q3+SQ4)) */
1427 fp2
= floatx80_mul(fp2
, fp0
, status
); /* S(P1+S(P2+SP3)) */
1428 fp4
= packFloatx80(1, 0x3FFD, LIT64(0xEEF57E0DA84BC8CE));
1429 fp3
= floatx80_add(fp3
, fp4
, status
); /* Q1+S(Q2+S(Q3+SQ4)) */
1430 fp2
= floatx80_mul(fp2
, fp1
, status
); /* RS(P1+S(P2+SP3)) */
1431 fp0
= floatx80_mul(fp0
, fp3
, status
); /* S(Q1+S(Q2+S(Q3+SQ4))) */
1432 fp1
= floatx80_add(fp1
, fp2
, status
); /* R+RS(P1+S(P2+SP3)) */
1433 fp0
= floatx80_add(fp0
, float32_to_floatx80(
1434 make_float32(0x3F800000), status
),
1435 status
); /* 1+S(Q1+S(Q2+S(Q3+SQ4))) */
1437 xSign
= extractFloatx80Sign(fp1
);
1438 xExp
= extractFloatx80Exp(fp1
);
1439 xSig
= extractFloatx80Frac(fp1
);
1441 fp1
= packFloatx80(xSign
, xExp
, xSig
);
1443 status
->float_rounding_mode
= user_rnd_mode
;
1444 status
->floatx80_rounding_precision
= user_rnd_prec
;
1446 a
= floatx80_div(fp0
, fp1
, status
);
1448 float_raise(float_flag_inexact
, status
);
1452 fp1
= floatx80_mul(fp0
, fp0
, status
); /* S = R*R */
1453 fp3
= float64_to_floatx80(make_float64(0x3EA0B759F50F8688),
1455 fp2
= float64_to_floatx80(make_float64(0xBEF2BAA5A8924F04),
1457 fp3
= floatx80_mul(fp3
, fp1
, status
); /* SQ4 */
1458 fp2
= floatx80_mul(fp2
, fp1
, status
); /* SP3 */
1459 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1460 make_float64(0xBF346F59B39BA65F), status
),
1461 status
); /* Q3+SQ4 */
1462 fp4
= packFloatx80(0, 0x3FF6, LIT64(0xE073D3FC199C4A00));
1463 fp2
= floatx80_add(fp2
, fp4
, status
); /* P2+SP3 */
1464 fp3
= floatx80_mul(fp3
, fp1
, status
); /* S(Q3+SQ4) */
1465 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S(P2+SP3) */
1466 fp4
= packFloatx80(0, 0x3FF9, LIT64(0xD23CD68415D95FA1));
1467 fp3
= floatx80_add(fp3
, fp4
, status
); /* Q2+S(Q3+SQ4) */
1468 fp4
= packFloatx80(1, 0x3FFC, LIT64(0x8895A6C5FB423BCA));
1469 fp2
= floatx80_add(fp2
, fp4
, status
); /* P1+S(P2+SP3) */
1470 fp3
= floatx80_mul(fp3
, fp1
, status
); /* S(Q2+S(Q3+SQ4)) */
1471 fp2
= floatx80_mul(fp2
, fp1
, status
); /* S(P1+S(P2+SP3)) */
1472 fp4
= packFloatx80(1, 0x3FFD, LIT64(0xEEF57E0DA84BC8CE));
1473 fp3
= floatx80_add(fp3
, fp4
, status
); /* Q1+S(Q2+S(Q3+SQ4)) */
1474 fp2
= floatx80_mul(fp2
, fp0
, status
); /* RS(P1+S(P2+SP3)) */
1475 fp1
= floatx80_mul(fp1
, fp3
, status
); /* S(Q1+S(Q2+S(Q3+SQ4))) */
1476 fp0
= floatx80_add(fp0
, fp2
, status
); /* R+RS(P1+S(P2+SP3)) */
1477 fp1
= floatx80_add(fp1
, float32_to_floatx80(
1478 make_float32(0x3F800000), status
),
1479 status
); /* 1+S(Q1+S(Q2+S(Q3+SQ4))) */
1481 status
->float_rounding_mode
= user_rnd_mode
;
1482 status
->floatx80_rounding_precision
= user_rnd_prec
;
1484 a
= floatx80_div(fp0
, fp1
, status
);
1486 float_raise(float_flag_inexact
, status
);
1497 floatx80
floatx80_sin(floatx80 a
, float_status
*status
)
1501 uint64_t aSig
, xSig
;
1503 int8_t user_rnd_mode
, user_rnd_prec
;
1505 int32_t compact
, l
, n
, j
;
1506 floatx80 fp0
, fp1
, fp2
, fp3
, fp4
, fp5
, x
, invtwopi
, twopi1
, twopi2
;
1507 float32 posneg1
, twoto63
;
1510 aSig
= extractFloatx80Frac(a
);
1511 aExp
= extractFloatx80Exp(a
);
1512 aSign
= extractFloatx80Sign(a
);
1514 if (aExp
== 0x7FFF) {
1515 if ((uint64_t) (aSig
<< 1)) {
1516 return propagateFloatx80NaNOneArg(a
, status
);
1518 float_raise(float_flag_invalid
, status
);
1519 return floatx80_default_nan(status
);
1522 if (aExp
== 0 && aSig
== 0) {
1523 return packFloatx80(aSign
, 0, 0);
1526 user_rnd_mode
= status
->float_rounding_mode
;
1527 user_rnd_prec
= status
->floatx80_rounding_precision
;
1528 status
->float_rounding_mode
= float_round_nearest_even
;
1529 status
->floatx80_rounding_precision
= 80;
1531 compact
= floatx80_make_compact(aExp
, aSig
);
1535 if (compact
< 0x3FD78000 || compact
> 0x4004BC7E) {
1536 /* 2^(-40) > |X| > 15 PI */
1537 if (compact
> 0x3FFF8000) { /* |X| >= 15 PI */
1539 fp1
= packFloatx80(0, 0, 0);
1540 if (compact
== 0x7FFEFFFF) {
1541 twopi1
= packFloatx80(aSign
^ 1, 0x7FFE,
1542 LIT64(0xC90FDAA200000000));
1543 twopi2
= packFloatx80(aSign
^ 1, 0x7FDC,
1544 LIT64(0x85A308D300000000));
1545 fp0
= floatx80_add(fp0
, twopi1
, status
);
1547 fp0
= floatx80_add(fp0
, twopi2
, status
);
1548 fp1
= floatx80_sub(fp1
, fp0
, status
);
1549 fp1
= floatx80_add(fp1
, twopi2
, status
);
1552 xSign
= extractFloatx80Sign(fp0
);
1553 xExp
= extractFloatx80Exp(fp0
);
1562 invtwopi
= packFloatx80(0, 0x3FFE - l
,
1563 LIT64(0xA2F9836E4E44152A)); /* INVTWOPI */
1564 twopi1
= packFloatx80(0, 0x3FFF + l
, LIT64(0xC90FDAA200000000));
1565 twopi2
= packFloatx80(0, 0x3FDD + l
, LIT64(0x85A308D300000000));
1567 /* SIGN(INARG)*2^63 IN SGL */
1568 twoto63
= packFloat32(xSign
, 0xBE, 0);
1570 fp2
= floatx80_mul(fp0
, invtwopi
, status
);
1571 fp2
= floatx80_add(fp2
, float32_to_floatx80(twoto63
, status
),
1572 status
); /* THE FRACT PART OF FP2 IS ROUNDED */
1573 fp2
= floatx80_sub(fp2
, float32_to_floatx80(twoto63
, status
),
1574 status
); /* FP2 is N */
1575 fp4
= floatx80_mul(twopi1
, fp2
, status
); /* W = N*P1 */
1576 fp5
= floatx80_mul(twopi2
, fp2
, status
); /* w = N*P2 */
1577 fp3
= floatx80_add(fp4
, fp5
, status
); /* FP3 is P */
1578 fp4
= floatx80_sub(fp4
, fp3
, status
); /* W-P */
1579 fp0
= floatx80_sub(fp0
, fp3
, status
); /* FP0 is A := R - P */
1580 fp4
= floatx80_add(fp4
, fp5
, status
); /* FP4 is p = (W-P)+w */
1581 fp3
= fp0
; /* FP3 is A */
1582 fp1
= floatx80_sub(fp1
, fp4
, status
); /* FP1 is a := r - p */
1583 fp0
= floatx80_add(fp0
, fp1
, status
); /* FP0 is R := A+a */
1586 n
= floatx80_to_int32(fp2
, status
);
1589 fp3
= floatx80_sub(fp3
, fp0
, status
); /* A-R */
1590 fp1
= floatx80_add(fp1
, fp3
, status
); /* FP1 is r := (A-R)+a */
1594 fp0
= float32_to_floatx80(make_float32(0x3F800000),
1597 status
->float_rounding_mode
= user_rnd_mode
;
1598 status
->floatx80_rounding_precision
= user_rnd_prec
;
1601 a
= floatx80_move(a
, status
);
1602 float_raise(float_flag_inexact
, status
);
1607 fp1
= floatx80_mul(fp0
, float64_to_floatx80(
1608 make_float64(0x3FE45F306DC9C883), status
),
1609 status
); /* X*2/PI */
1611 n
= floatx80_to_int32(fp1
, status
);
1614 fp0
= floatx80_sub(fp0
, pi_tbl
[j
], status
); /* X-Y1 */
1615 fp0
= floatx80_sub(fp0
, float32_to_floatx80(pi_tbl2
[j
], status
),
1616 status
); /* FP0 IS R = (X-Y1)-Y2 */
1621 fp0
= floatx80_mul(fp0
, fp0
, status
); /* FP0 IS S */
1622 fp1
= floatx80_mul(fp0
, fp0
, status
); /* FP1 IS T */
1623 fp2
= float64_to_floatx80(make_float64(0x3D2AC4D0D6011EE3),
1625 fp3
= float64_to_floatx80(make_float64(0xBDA9396F9F45AC19),
1628 xSign
= extractFloatx80Sign(fp0
); /* X IS S */
1629 xExp
= extractFloatx80Exp(fp0
);
1630 xSig
= extractFloatx80Frac(fp0
);
1634 posneg1
= make_float32(0xBF800000); /* -1 */
1637 posneg1
= make_float32(0x3F800000); /* 1 */
1638 } /* X IS NOW R'= SGN*R */
1640 fp2
= floatx80_mul(fp2
, fp1
, status
); /* TB8 */
1641 fp3
= floatx80_mul(fp3
, fp1
, status
); /* TB7 */
1642 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1643 make_float64(0x3E21EED90612C972), status
),
1644 status
); /* B6+TB8 */
1645 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1646 make_float64(0xBE927E4FB79D9FCF), status
),
1647 status
); /* B5+TB7 */
1648 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T(B6+TB8) */
1649 fp3
= floatx80_mul(fp3
, fp1
, status
); /* T(B5+TB7) */
1650 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1651 make_float64(0x3EFA01A01A01D423), status
),
1652 status
); /* B4+T(B6+TB8) */
1653 fp4
= packFloatx80(1, 0x3FF5, LIT64(0xB60B60B60B61D438));
1654 fp3
= floatx80_add(fp3
, fp4
, status
); /* B3+T(B5+TB7) */
1655 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T(B4+T(B6+TB8)) */
1656 fp1
= floatx80_mul(fp1
, fp3
, status
); /* T(B3+T(B5+TB7)) */
1657 fp4
= packFloatx80(0, 0x3FFA, LIT64(0xAAAAAAAAAAAAAB5E));
1658 fp2
= floatx80_add(fp2
, fp4
, status
); /* B2+T(B4+T(B6+TB8)) */
1659 fp1
= floatx80_add(fp1
, float32_to_floatx80(
1660 make_float32(0xBF000000), status
),
1661 status
); /* B1+T(B3+T(B5+TB7)) */
1662 fp0
= floatx80_mul(fp0
, fp2
, status
); /* S(B2+T(B4+T(B6+TB8))) */
1663 fp0
= floatx80_add(fp0
, fp1
, status
); /* [B1+T(B3+T(B5+TB7))]+
1664 * [S(B2+T(B4+T(B6+TB8)))]
1667 x
= packFloatx80(xSign
, xExp
, xSig
);
1668 fp0
= floatx80_mul(fp0
, x
, status
);
1670 status
->float_rounding_mode
= user_rnd_mode
;
1671 status
->floatx80_rounding_precision
= user_rnd_prec
;
1673 a
= floatx80_add(fp0
, float32_to_floatx80(posneg1
, status
), status
);
1675 float_raise(float_flag_inexact
, status
);
1680 xSign
= extractFloatx80Sign(fp0
); /* X IS R */
1681 xExp
= extractFloatx80Exp(fp0
);
1682 xSig
= extractFloatx80Frac(fp0
);
1684 xSign
^= (n
>> 1) & 1; /* X IS NOW R'= SGN*R */
1686 fp0
= floatx80_mul(fp0
, fp0
, status
); /* FP0 IS S */
1687 fp1
= floatx80_mul(fp0
, fp0
, status
); /* FP1 IS T */
1688 fp3
= float64_to_floatx80(make_float64(0xBD6AAA77CCC994F5),
1690 fp2
= float64_to_floatx80(make_float64(0x3DE612097AAE8DA1),
1692 fp3
= floatx80_mul(fp3
, fp1
, status
); /* T*A7 */
1693 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T*A6 */
1694 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1695 make_float64(0xBE5AE6452A118AE4), status
),
1696 status
); /* A5+T*A7 */
1697 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1698 make_float64(0x3EC71DE3A5341531), status
),
1699 status
); /* A4+T*A6 */
1700 fp3
= floatx80_mul(fp3
, fp1
, status
); /* T(A5+TA7) */
1701 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T(A4+TA6) */
1702 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1703 make_float64(0xBF2A01A01A018B59), status
),
1704 status
); /* A3+T(A5+TA7) */
1705 fp4
= packFloatx80(0, 0x3FF8, LIT64(0x88888888888859AF));
1706 fp2
= floatx80_add(fp2
, fp4
, status
); /* A2+T(A4+TA6) */
1707 fp1
= floatx80_mul(fp1
, fp3
, status
); /* T(A3+T(A5+TA7)) */
1708 fp2
= floatx80_mul(fp2
, fp0
, status
); /* S(A2+T(A4+TA6)) */
1709 fp4
= packFloatx80(1, 0x3FFC, LIT64(0xAAAAAAAAAAAAAA99));
1710 fp1
= floatx80_add(fp1
, fp4
, status
); /* A1+T(A3+T(A5+TA7)) */
1711 fp1
= floatx80_add(fp1
, fp2
,
1712 status
); /* [A1+T(A3+T(A5+TA7))]+
1716 x
= packFloatx80(xSign
, xExp
, xSig
);
1717 fp0
= floatx80_mul(fp0
, x
, status
); /* R'*S */
1718 fp0
= floatx80_mul(fp0
, fp1
, status
); /* SIN(R')-R' */
1720 status
->float_rounding_mode
= user_rnd_mode
;
1721 status
->floatx80_rounding_precision
= user_rnd_prec
;
1723 a
= floatx80_add(fp0
, x
, status
);
1725 float_raise(float_flag_inexact
, status
);
1736 floatx80
floatx80_cos(floatx80 a
, float_status
*status
)
1740 uint64_t aSig
, xSig
;
1742 int8_t user_rnd_mode
, user_rnd_prec
;
1744 int32_t compact
, l
, n
, j
;
1745 floatx80 fp0
, fp1
, fp2
, fp3
, fp4
, fp5
, x
, invtwopi
, twopi1
, twopi2
;
1746 float32 posneg1
, twoto63
;
1749 aSig
= extractFloatx80Frac(a
);
1750 aExp
= extractFloatx80Exp(a
);
1751 aSign
= extractFloatx80Sign(a
);
1753 if (aExp
== 0x7FFF) {
1754 if ((uint64_t) (aSig
<< 1)) {
1755 return propagateFloatx80NaNOneArg(a
, status
);
1757 float_raise(float_flag_invalid
, status
);
1758 return floatx80_default_nan(status
);
1761 if (aExp
== 0 && aSig
== 0) {
1762 return packFloatx80(0, one_exp
, one_sig
);
1765 user_rnd_mode
= status
->float_rounding_mode
;
1766 user_rnd_prec
= status
->floatx80_rounding_precision
;
1767 status
->float_rounding_mode
= float_round_nearest_even
;
1768 status
->floatx80_rounding_precision
= 80;
1770 compact
= floatx80_make_compact(aExp
, aSig
);
1774 if (compact
< 0x3FD78000 || compact
> 0x4004BC7E) {
1775 /* 2^(-40) > |X| > 15 PI */
1776 if (compact
> 0x3FFF8000) { /* |X| >= 15 PI */
1778 fp1
= packFloatx80(0, 0, 0);
1779 if (compact
== 0x7FFEFFFF) {
1780 twopi1
= packFloatx80(aSign
^ 1, 0x7FFE,
1781 LIT64(0xC90FDAA200000000));
1782 twopi2
= packFloatx80(aSign
^ 1, 0x7FDC,
1783 LIT64(0x85A308D300000000));
1784 fp0
= floatx80_add(fp0
, twopi1
, status
);
1786 fp0
= floatx80_add(fp0
, twopi2
, status
);
1787 fp1
= floatx80_sub(fp1
, fp0
, status
);
1788 fp1
= floatx80_add(fp1
, twopi2
, status
);
1791 xSign
= extractFloatx80Sign(fp0
);
1792 xExp
= extractFloatx80Exp(fp0
);
1801 invtwopi
= packFloatx80(0, 0x3FFE - l
,
1802 LIT64(0xA2F9836E4E44152A)); /* INVTWOPI */
1803 twopi1
= packFloatx80(0, 0x3FFF + l
, LIT64(0xC90FDAA200000000));
1804 twopi2
= packFloatx80(0, 0x3FDD + l
, LIT64(0x85A308D300000000));
1806 /* SIGN(INARG)*2^63 IN SGL */
1807 twoto63
= packFloat32(xSign
, 0xBE, 0);
1809 fp2
= floatx80_mul(fp0
, invtwopi
, status
);
1810 fp2
= floatx80_add(fp2
, float32_to_floatx80(twoto63
, status
),
1811 status
); /* THE FRACT PART OF FP2 IS ROUNDED */
1812 fp2
= floatx80_sub(fp2
, float32_to_floatx80(twoto63
, status
),
1813 status
); /* FP2 is N */
1814 fp4
= floatx80_mul(twopi1
, fp2
, status
); /* W = N*P1 */
1815 fp5
= floatx80_mul(twopi2
, fp2
, status
); /* w = N*P2 */
1816 fp3
= floatx80_add(fp4
, fp5
, status
); /* FP3 is P */
1817 fp4
= floatx80_sub(fp4
, fp3
, status
); /* W-P */
1818 fp0
= floatx80_sub(fp0
, fp3
, status
); /* FP0 is A := R - P */
1819 fp4
= floatx80_add(fp4
, fp5
, status
); /* FP4 is p = (W-P)+w */
1820 fp3
= fp0
; /* FP3 is A */
1821 fp1
= floatx80_sub(fp1
, fp4
, status
); /* FP1 is a := r - p */
1822 fp0
= floatx80_add(fp0
, fp1
, status
); /* FP0 is R := A+a */
1825 n
= floatx80_to_int32(fp2
, status
);
1828 fp3
= floatx80_sub(fp3
, fp0
, status
); /* A-R */
1829 fp1
= floatx80_add(fp1
, fp3
, status
); /* FP1 is r := (A-R)+a */
1833 fp0
= float32_to_floatx80(make_float32(0x3F800000), status
); /* 1 */
1835 status
->float_rounding_mode
= user_rnd_mode
;
1836 status
->floatx80_rounding_precision
= user_rnd_prec
;
1839 a
= floatx80_sub(fp0
, float32_to_floatx80(
1840 make_float32(0x00800000), status
),
1842 float_raise(float_flag_inexact
, status
);
1847 fp1
= floatx80_mul(fp0
, float64_to_floatx80(
1848 make_float64(0x3FE45F306DC9C883), status
),
1849 status
); /* X*2/PI */
1851 n
= floatx80_to_int32(fp1
, status
);
1854 fp0
= floatx80_sub(fp0
, pi_tbl
[j
], status
); /* X-Y1 */
1855 fp0
= floatx80_sub(fp0
, float32_to_floatx80(pi_tbl2
[j
], status
),
1856 status
); /* FP0 IS R = (X-Y1)-Y2 */
1861 fp0
= floatx80_mul(fp0
, fp0
, status
); /* FP0 IS S */
1862 fp1
= floatx80_mul(fp0
, fp0
, status
); /* FP1 IS T */
1863 fp2
= float64_to_floatx80(make_float64(0x3D2AC4D0D6011EE3),
1865 fp3
= float64_to_floatx80(make_float64(0xBDA9396F9F45AC19),
1868 xSign
= extractFloatx80Sign(fp0
); /* X IS S */
1869 xExp
= extractFloatx80Exp(fp0
);
1870 xSig
= extractFloatx80Frac(fp0
);
1872 if (((n
+ 1) >> 1) & 1) {
1874 posneg1
= make_float32(0xBF800000); /* -1 */
1877 posneg1
= make_float32(0x3F800000); /* 1 */
1878 } /* X IS NOW R'= SGN*R */
1880 fp2
= floatx80_mul(fp2
, fp1
, status
); /* TB8 */
1881 fp3
= floatx80_mul(fp3
, fp1
, status
); /* TB7 */
1882 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1883 make_float64(0x3E21EED90612C972), status
),
1884 status
); /* B6+TB8 */
1885 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1886 make_float64(0xBE927E4FB79D9FCF), status
),
1887 status
); /* B5+TB7 */
1888 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T(B6+TB8) */
1889 fp3
= floatx80_mul(fp3
, fp1
, status
); /* T(B5+TB7) */
1890 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1891 make_float64(0x3EFA01A01A01D423), status
),
1892 status
); /* B4+T(B6+TB8) */
1893 fp4
= packFloatx80(1, 0x3FF5, LIT64(0xB60B60B60B61D438));
1894 fp3
= floatx80_add(fp3
, fp4
, status
); /* B3+T(B5+TB7) */
1895 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T(B4+T(B6+TB8)) */
1896 fp1
= floatx80_mul(fp1
, fp3
, status
); /* T(B3+T(B5+TB7)) */
1897 fp4
= packFloatx80(0, 0x3FFA, LIT64(0xAAAAAAAAAAAAAB5E));
1898 fp2
= floatx80_add(fp2
, fp4
, status
); /* B2+T(B4+T(B6+TB8)) */
1899 fp1
= floatx80_add(fp1
, float32_to_floatx80(
1900 make_float32(0xBF000000), status
),
1901 status
); /* B1+T(B3+T(B5+TB7)) */
1902 fp0
= floatx80_mul(fp0
, fp2
, status
); /* S(B2+T(B4+T(B6+TB8))) */
1903 fp0
= floatx80_add(fp0
, fp1
, status
);
1904 /* [B1+T(B3+T(B5+TB7))]+[S(B2+T(B4+T(B6+TB8)))] */
1906 x
= packFloatx80(xSign
, xExp
, xSig
);
1907 fp0
= floatx80_mul(fp0
, x
, status
);
1909 status
->float_rounding_mode
= user_rnd_mode
;
1910 status
->floatx80_rounding_precision
= user_rnd_prec
;
1912 a
= floatx80_add(fp0
, float32_to_floatx80(posneg1
, status
), status
);
1914 float_raise(float_flag_inexact
, status
);
1919 xSign
= extractFloatx80Sign(fp0
); /* X IS R */
1920 xExp
= extractFloatx80Exp(fp0
);
1921 xSig
= extractFloatx80Frac(fp0
);
1923 xSign
^= ((n
+ 1) >> 1) & 1; /* X IS NOW R'= SGN*R */
1925 fp0
= floatx80_mul(fp0
, fp0
, status
); /* FP0 IS S */
1926 fp1
= floatx80_mul(fp0
, fp0
, status
); /* FP1 IS T */
1927 fp3
= float64_to_floatx80(make_float64(0xBD6AAA77CCC994F5),
1929 fp2
= float64_to_floatx80(make_float64(0x3DE612097AAE8DA1),
1931 fp3
= floatx80_mul(fp3
, fp1
, status
); /* T*A7 */
1932 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T*A6 */
1933 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1934 make_float64(0xBE5AE6452A118AE4), status
),
1935 status
); /* A5+T*A7 */
1936 fp2
= floatx80_add(fp2
, float64_to_floatx80(
1937 make_float64(0x3EC71DE3A5341531), status
),
1938 status
); /* A4+T*A6 */
1939 fp3
= floatx80_mul(fp3
, fp1
, status
); /* T(A5+TA7) */
1940 fp2
= floatx80_mul(fp2
, fp1
, status
); /* T(A4+TA6) */
1941 fp3
= floatx80_add(fp3
, float64_to_floatx80(
1942 make_float64(0xBF2A01A01A018B59), status
),
1943 status
); /* A3+T(A5+TA7) */
1944 fp4
= packFloatx80(0, 0x3FF8, LIT64(0x88888888888859AF));
1945 fp2
= floatx80_add(fp2
, fp4
, status
); /* A2+T(A4+TA6) */
1946 fp1
= floatx80_mul(fp1
, fp3
, status
); /* T(A3+T(A5+TA7)) */
1947 fp2
= floatx80_mul(fp2
, fp0
, status
); /* S(A2+T(A4+TA6)) */
1948 fp4
= packFloatx80(1, 0x3FFC, LIT64(0xAAAAAAAAAAAAAA99));
1949 fp1
= floatx80_add(fp1
, fp4
, status
); /* A1+T(A3+T(A5+TA7)) */
1950 fp1
= floatx80_add(fp1
, fp2
, status
);
1951 /* [A1+T(A3+T(A5+TA7))]+[S(A2+T(A4+TA6))] */
1953 x
= packFloatx80(xSign
, xExp
, xSig
);
1954 fp0
= floatx80_mul(fp0
, x
, status
); /* R'*S */
1955 fp0
= floatx80_mul(fp0
, fp1
, status
); /* SIN(R')-R' */
1957 status
->float_rounding_mode
= user_rnd_mode
;
1958 status
->floatx80_rounding_precision
= user_rnd_prec
;
1960 a
= floatx80_add(fp0
, x
, status
);
1962 float_raise(float_flag_inexact
, status
);
1973 floatx80
floatx80_atan(floatx80 a
, float_status
*status
)
1979 int8_t user_rnd_mode
, user_rnd_prec
;
1981 int32_t compact
, tbl_index
;
1982 floatx80 fp0
, fp1
, fp2
, fp3
, xsave
;
1984 aSig
= extractFloatx80Frac(a
);
1985 aExp
= extractFloatx80Exp(a
);
1986 aSign
= extractFloatx80Sign(a
);
1988 if (aExp
== 0x7FFF) {
1989 if ((uint64_t) (aSig
<< 1)) {
1990 return propagateFloatx80NaNOneArg(a
, status
);
1992 a
= packFloatx80(aSign
, piby2_exp
, pi_sig
);
1993 float_raise(float_flag_inexact
, status
);
1994 return floatx80_move(a
, status
);
1997 if (aExp
== 0 && aSig
== 0) {
1998 return packFloatx80(aSign
, 0, 0);
2001 compact
= floatx80_make_compact(aExp
, aSig
);
2003 user_rnd_mode
= status
->float_rounding_mode
;
2004 user_rnd_prec
= status
->floatx80_rounding_precision
;
2005 status
->float_rounding_mode
= float_round_nearest_even
;
2006 status
->floatx80_rounding_precision
= 80;
2008 if (compact
< 0x3FFB8000 || compact
> 0x4002FFFF) {
2009 /* |X| >= 16 or |X| < 1/16 */
2010 if (compact
> 0x3FFF8000) { /* |X| >= 16 */
2011 if (compact
> 0x40638000) { /* |X| > 2^(100) */
2012 fp0
= packFloatx80(aSign
, piby2_exp
, pi_sig
);
2013 fp1
= packFloatx80(aSign
, 0x0001, one_sig
);
2015 status
->float_rounding_mode
= user_rnd_mode
;
2016 status
->floatx80_rounding_precision
= user_rnd_prec
;
2018 a
= floatx80_sub(fp0
, fp1
, status
);
2020 float_raise(float_flag_inexact
, status
);
2025 fp1
= packFloatx80(1, one_exp
, one_sig
); /* -1 */
2026 fp1
= floatx80_div(fp1
, fp0
, status
); /* X' = -1/X */
2028 fp0
= floatx80_mul(fp1
, fp1
, status
); /* Y = X'*X' */
2029 fp1
= floatx80_mul(fp0
, fp0
, status
); /* Z = Y*Y */
2030 fp3
= float64_to_floatx80(make_float64(0xBFB70BF398539E6A),
2032 fp2
= float64_to_floatx80(make_float64(0x3FBC7187962D1D7D),
2034 fp3
= floatx80_mul(fp3
, fp1
, status
); /* Z*C5 */
2035 fp2
= floatx80_mul(fp2
, fp1
, status
); /* Z*C4 */
2036 fp3
= floatx80_add(fp3
, float64_to_floatx80(
2037 make_float64(0xBFC24924827107B8), status
),
2038 status
); /* C3+Z*C5 */
2039 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2040 make_float64(0x3FC999999996263E), status
),
2041 status
); /* C2+Z*C4 */
2042 fp1
= floatx80_mul(fp1
, fp3
, status
); /* Z*(C3+Z*C5) */
2043 fp2
= floatx80_mul(fp2
, fp0
, status
); /* Y*(C2+Z*C4) */
2044 fp1
= floatx80_add(fp1
, float64_to_floatx80(
2045 make_float64(0xBFD5555555555536), status
),
2046 status
); /* C1+Z*(C3+Z*C5) */
2047 fp0
= floatx80_mul(fp0
, xsave
, status
); /* X'*Y */
2048 /* [Y*(C2+Z*C4)]+[C1+Z*(C3+Z*C5)] */
2049 fp1
= floatx80_add(fp1
, fp2
, status
);
2050 /* X'*Y*([B1+Z*(B3+Z*B5)]+[Y*(B2+Z*(B4+Z*B6))]) ?? */
2051 fp0
= floatx80_mul(fp0
, fp1
, status
);
2052 fp0
= floatx80_add(fp0
, xsave
, status
);
2053 fp1
= packFloatx80(aSign
, piby2_exp
, pi_sig
);
2055 status
->float_rounding_mode
= user_rnd_mode
;
2056 status
->floatx80_rounding_precision
= user_rnd_prec
;
2058 a
= floatx80_add(fp0
, fp1
, status
);
2060 float_raise(float_flag_inexact
, status
);
2064 } else { /* |X| < 1/16 */
2065 if (compact
< 0x3FD78000) { /* |X| < 2^(-40) */
2066 status
->float_rounding_mode
= user_rnd_mode
;
2067 status
->floatx80_rounding_precision
= user_rnd_prec
;
2069 a
= floatx80_move(a
, status
);
2071 float_raise(float_flag_inexact
, status
);
2077 fp0
= floatx80_mul(fp0
, fp0
, status
); /* Y = X*X */
2078 fp1
= floatx80_mul(fp0
, fp0
, status
); /* Z = Y*Y */
2079 fp2
= float64_to_floatx80(make_float64(0x3FB344447F876989),
2081 fp3
= float64_to_floatx80(make_float64(0xBFB744EE7FAF45DB),
2083 fp2
= floatx80_mul(fp2
, fp1
, status
); /* Z*B6 */
2084 fp3
= floatx80_mul(fp3
, fp1
, status
); /* Z*B5 */
2085 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2086 make_float64(0x3FBC71C646940220), status
),
2087 status
); /* B4+Z*B6 */
2088 fp3
= floatx80_add(fp3
, float64_to_floatx80(
2089 make_float64(0xBFC24924921872F9),
2090 status
), status
); /* B3+Z*B5 */
2091 fp2
= floatx80_mul(fp2
, fp1
, status
); /* Z*(B4+Z*B6) */
2092 fp1
= floatx80_mul(fp1
, fp3
, status
); /* Z*(B3+Z*B5) */
2093 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2094 make_float64(0x3FC9999999998FA9), status
),
2095 status
); /* B2+Z*(B4+Z*B6) */
2096 fp1
= floatx80_add(fp1
, float64_to_floatx80(
2097 make_float64(0xBFD5555555555555), status
),
2098 status
); /* B1+Z*(B3+Z*B5) */
2099 fp2
= floatx80_mul(fp2
, fp0
, status
); /* Y*(B2+Z*(B4+Z*B6)) */
2100 fp0
= floatx80_mul(fp0
, xsave
, status
); /* X*Y */
2101 /* [B1+Z*(B3+Z*B5)]+[Y*(B2+Z*(B4+Z*B6))] */
2102 fp1
= floatx80_add(fp1
, fp2
, status
);
2103 /* X*Y*([B1+Z*(B3+Z*B5)]+[Y*(B2+Z*(B4+Z*B6))]) */
2104 fp0
= floatx80_mul(fp0
, fp1
, status
);
2106 status
->float_rounding_mode
= user_rnd_mode
;
2107 status
->floatx80_rounding_precision
= user_rnd_prec
;
2109 a
= floatx80_add(fp0
, xsave
, status
);
2111 float_raise(float_flag_inexact
, status
);
2117 aSig
&= LIT64(0xF800000000000000);
2118 aSig
|= LIT64(0x0400000000000000);
2119 xsave
= packFloatx80(aSign
, aExp
, aSig
); /* F */
2122 fp2
= packFloatx80(0, one_exp
, one_sig
); /* 1 */
2123 fp1
= floatx80_mul(fp1
, xsave
, status
); /* X*F */
2124 fp0
= floatx80_sub(fp0
, xsave
, status
); /* X-F */
2125 fp1
= floatx80_add(fp1
, fp2
, status
); /* 1 + X*F */
2126 fp0
= floatx80_div(fp0
, fp1
, status
); /* U = (X-F)/(1+X*F) */
2128 tbl_index
= compact
;
2130 tbl_index
&= 0x7FFF0000;
2131 tbl_index
-= 0x3FFB0000;
2133 tbl_index
+= compact
& 0x00007800;
2136 fp3
= atan_tbl
[tbl_index
];
2138 fp3
.high
|= aSign
? 0x8000 : 0; /* ATAN(F) */
2140 fp1
= floatx80_mul(fp0
, fp0
, status
); /* V = U*U */
2141 fp2
= float64_to_floatx80(make_float64(0xBFF6687E314987D8),
2143 fp2
= floatx80_add(fp2
, fp1
, status
); /* A3+V */
2144 fp2
= floatx80_mul(fp2
, fp1
, status
); /* V*(A3+V) */
2145 fp1
= floatx80_mul(fp1
, fp0
, status
); /* U*V */
2146 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2147 make_float64(0x4002AC6934A26DB3), status
),
2148 status
); /* A2+V*(A3+V) */
2149 fp1
= floatx80_mul(fp1
, float64_to_floatx80(
2150 make_float64(0xBFC2476F4E1DA28E), status
),
2151 status
); /* A1+U*V */
2152 fp1
= floatx80_mul(fp1
, fp2
, status
); /* A1*U*V*(A2+V*(A3+V)) */
2153 fp0
= floatx80_add(fp0
, fp1
, status
); /* ATAN(U) */
2155 status
->float_rounding_mode
= user_rnd_mode
;
2156 status
->floatx80_rounding_precision
= user_rnd_prec
;
2158 a
= floatx80_add(fp0
, fp3
, status
); /* ATAN(X) */
2160 float_raise(float_flag_inexact
, status
);
2170 floatx80
floatx80_asin(floatx80 a
, float_status
*status
)
2176 int8_t user_rnd_mode
, user_rnd_prec
;
2179 floatx80 fp0
, fp1
, fp2
, one
;
2181 aSig
= extractFloatx80Frac(a
);
2182 aExp
= extractFloatx80Exp(a
);
2183 aSign
= extractFloatx80Sign(a
);
2185 if (aExp
== 0x7FFF && (uint64_t) (aSig
<< 1)) {
2186 return propagateFloatx80NaNOneArg(a
, status
);
2189 if (aExp
== 0 && aSig
== 0) {
2190 return packFloatx80(aSign
, 0, 0);
2193 compact
= floatx80_make_compact(aExp
, aSig
);
2195 if (compact
>= 0x3FFF8000) { /* |X| >= 1 */
2196 if (aExp
== one_exp
&& aSig
== one_sig
) { /* |X| == 1 */
2197 float_raise(float_flag_inexact
, status
);
2198 a
= packFloatx80(aSign
, piby2_exp
, pi_sig
);
2199 return floatx80_move(a
, status
);
2200 } else { /* |X| > 1 */
2201 float_raise(float_flag_invalid
, status
);
2202 return floatx80_default_nan(status
);
2207 user_rnd_mode
= status
->float_rounding_mode
;
2208 user_rnd_prec
= status
->floatx80_rounding_precision
;
2209 status
->float_rounding_mode
= float_round_nearest_even
;
2210 status
->floatx80_rounding_precision
= 80;
2212 one
= packFloatx80(0, one_exp
, one_sig
);
2215 fp1
= floatx80_sub(one
, fp0
, status
); /* 1 - X */
2216 fp2
= floatx80_add(one
, fp0
, status
); /* 1 + X */
2217 fp1
= floatx80_mul(fp2
, fp1
, status
); /* (1+X)*(1-X) */
2218 fp1
= floatx80_sqrt(fp1
, status
); /* SQRT((1+X)*(1-X)) */
2219 fp0
= floatx80_div(fp0
, fp1
, status
); /* X/SQRT((1+X)*(1-X)) */
2221 status
->float_rounding_mode
= user_rnd_mode
;
2222 status
->floatx80_rounding_precision
= user_rnd_prec
;
2224 a
= floatx80_atan(fp0
, status
); /* ATAN(X/SQRT((1+X)*(1-X))) */
2226 float_raise(float_flag_inexact
, status
);
2235 floatx80
floatx80_acos(floatx80 a
, float_status
*status
)
2241 int8_t user_rnd_mode
, user_rnd_prec
;
2244 floatx80 fp0
, fp1
, one
;
2246 aSig
= extractFloatx80Frac(a
);
2247 aExp
= extractFloatx80Exp(a
);
2248 aSign
= extractFloatx80Sign(a
);
2250 if (aExp
== 0x7FFF && (uint64_t) (aSig
<< 1)) {
2251 return propagateFloatx80NaNOneArg(a
, status
);
2253 if (aExp
== 0 && aSig
== 0) {
2254 float_raise(float_flag_inexact
, status
);
2255 return roundAndPackFloatx80(status
->floatx80_rounding_precision
, 0,
2256 piby2_exp
, pi_sig
, 0, status
);
2259 compact
= floatx80_make_compact(aExp
, aSig
);
2261 if (compact
>= 0x3FFF8000) { /* |X| >= 1 */
2262 if (aExp
== one_exp
&& aSig
== one_sig
) { /* |X| == 1 */
2263 if (aSign
) { /* X == -1 */
2264 a
= packFloatx80(0, pi_exp
, pi_sig
);
2265 float_raise(float_flag_inexact
, status
);
2266 return floatx80_move(a
, status
);
2267 } else { /* X == +1 */
2268 return packFloatx80(0, 0, 0);
2270 } else { /* |X| > 1 */
2271 float_raise(float_flag_invalid
, status
);
2272 return floatx80_default_nan(status
);
2276 user_rnd_mode
= status
->float_rounding_mode
;
2277 user_rnd_prec
= status
->floatx80_rounding_precision
;
2278 status
->float_rounding_mode
= float_round_nearest_even
;
2279 status
->floatx80_rounding_precision
= 80;
2281 one
= packFloatx80(0, one_exp
, one_sig
);
2284 fp1
= floatx80_add(one
, fp0
, status
); /* 1 + X */
2285 fp0
= floatx80_sub(one
, fp0
, status
); /* 1 - X */
2286 fp0
= floatx80_div(fp0
, fp1
, status
); /* (1-X)/(1+X) */
2287 fp0
= floatx80_sqrt(fp0
, status
); /* SQRT((1-X)/(1+X)) */
2288 fp0
= floatx80_atan(fp0
, status
); /* ATAN(SQRT((1-X)/(1+X))) */
2290 status
->float_rounding_mode
= user_rnd_mode
;
2291 status
->floatx80_rounding_precision
= user_rnd_prec
;
2293 a
= floatx80_add(fp0
, fp0
, status
); /* 2 * ATAN(SQRT((1-X)/(1+X))) */
2295 float_raise(float_flag_inexact
, status
);
2301 * Hyperbolic arc tangent
2304 floatx80
floatx80_atanh(floatx80 a
, float_status
*status
)
2310 int8_t user_rnd_mode
, user_rnd_prec
;
2313 floatx80 fp0
, fp1
, fp2
, one
;
2315 aSig
= extractFloatx80Frac(a
);
2316 aExp
= extractFloatx80Exp(a
);
2317 aSign
= extractFloatx80Sign(a
);
2319 if (aExp
== 0x7FFF && (uint64_t) (aSig
<< 1)) {
2320 return propagateFloatx80NaNOneArg(a
, status
);
2323 if (aExp
== 0 && aSig
== 0) {
2324 return packFloatx80(aSign
, 0, 0);
2327 compact
= floatx80_make_compact(aExp
, aSig
);
2329 if (compact
>= 0x3FFF8000) { /* |X| >= 1 */
2330 if (aExp
== one_exp
&& aSig
== one_sig
) { /* |X| == 1 */
2331 float_raise(float_flag_divbyzero
, status
);
2332 return packFloatx80(aSign
, floatx80_infinity
.high
,
2333 floatx80_infinity
.low
);
2334 } else { /* |X| > 1 */
2335 float_raise(float_flag_invalid
, status
);
2336 return floatx80_default_nan(status
);
2340 user_rnd_mode
= status
->float_rounding_mode
;
2341 user_rnd_prec
= status
->floatx80_rounding_precision
;
2342 status
->float_rounding_mode
= float_round_nearest_even
;
2343 status
->floatx80_rounding_precision
= 80;
2345 one
= packFloatx80(0, one_exp
, one_sig
);
2346 fp2
= packFloatx80(aSign
, 0x3FFE, one_sig
); /* SIGN(X) * (1/2) */
2347 fp0
= packFloatx80(0, aExp
, aSig
); /* Y = |X| */
2348 fp1
= packFloatx80(1, aExp
, aSig
); /* -Y */
2349 fp0
= floatx80_add(fp0
, fp0
, status
); /* 2Y */
2350 fp1
= floatx80_add(fp1
, one
, status
); /* 1-Y */
2351 fp0
= floatx80_div(fp0
, fp1
, status
); /* Z = 2Y/(1-Y) */
2352 fp0
= floatx80_lognp1(fp0
, status
); /* LOG1P(Z) */
2354 status
->float_rounding_mode
= user_rnd_mode
;
2355 status
->floatx80_rounding_precision
= user_rnd_prec
;
2357 a
= floatx80_mul(fp0
, fp2
,
2358 status
); /* ATANH(X) = SIGN(X) * (1/2) * LOG1P(Z) */
2360 float_raise(float_flag_inexact
, status
);
2369 floatx80
floatx80_etoxm1(floatx80 a
, float_status
*status
)
2375 int8_t user_rnd_mode
, user_rnd_prec
;
2377 int32_t compact
, n
, j
, m
, m1
;
2378 floatx80 fp0
, fp1
, fp2
, fp3
, l2
, sc
, onebysc
;
2380 aSig
= extractFloatx80Frac(a
);
2381 aExp
= extractFloatx80Exp(a
);
2382 aSign
= extractFloatx80Sign(a
);
2384 if (aExp
== 0x7FFF) {
2385 if ((uint64_t) (aSig
<< 1)) {
2386 return propagateFloatx80NaNOneArg(a
, status
);
2389 return packFloatx80(aSign
, one_exp
, one_sig
);
2391 return packFloatx80(0, floatx80_infinity
.high
,
2392 floatx80_infinity
.low
);
2395 if (aExp
== 0 && aSig
== 0) {
2396 return packFloatx80(aSign
, 0, 0);
2399 user_rnd_mode
= status
->float_rounding_mode
;
2400 user_rnd_prec
= status
->floatx80_rounding_precision
;
2401 status
->float_rounding_mode
= float_round_nearest_even
;
2402 status
->floatx80_rounding_precision
= 80;
2404 if (aExp
>= 0x3FFD) { /* |X| >= 1/4 */
2405 compact
= floatx80_make_compact(aExp
, aSig
);
2407 if (compact
<= 0x4004C215) { /* |X| <= 70 log2 */
2410 fp0
= floatx80_mul(fp0
, float32_to_floatx80(
2411 make_float32(0x42B8AA3B), status
),
2412 status
); /* 64/log2 * X */
2413 n
= floatx80_to_int32(fp0
, status
); /* int(64/log2*X) */
2414 fp0
= int32_to_floatx80(n
, status
);
2416 j
= n
& 0x3F; /* J = N mod 64 */
2417 m
= n
/ 64; /* NOTE: this is really arithmetic right shift by 6 */
2420 * arithmetic right shift is division and
2421 * round towards minus infinity
2426 /*m += 0x3FFF; // biased exponent of 2^(M) */
2427 /*m1 += 0x3FFF; // biased exponent of -2^(-M) */
2430 fp0
= floatx80_mul(fp0
, float32_to_floatx80(
2431 make_float32(0xBC317218), status
),
2432 status
); /* N * L1, L1 = lead(-log2/64) */
2433 l2
= packFloatx80(0, 0x3FDC, LIT64(0x82E308654361C4C6));
2434 fp2
= floatx80_mul(fp2
, l2
, status
); /* N * L2, L1+L2 = -log2/64 */
2435 fp0
= floatx80_add(fp0
, fp1
, status
); /* X + N*L1 */
2436 fp0
= floatx80_add(fp0
, fp2
, status
); /* R */
2438 fp1
= floatx80_mul(fp0
, fp0
, status
); /* S = R*R */
2439 fp2
= float32_to_floatx80(make_float32(0x3950097B),
2441 fp2
= floatx80_mul(fp2
, fp1
, status
); /* fp2 is S*A6 */
2442 fp3
= floatx80_mul(float32_to_floatx80(make_float32(0x3AB60B6A),
2443 status
), fp1
, status
); /* fp3 is S*A5 */
2444 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2445 make_float64(0x3F81111111174385), status
),
2446 status
); /* fp2 IS A4+S*A6 */
2447 fp3
= floatx80_add(fp3
, float64_to_floatx80(
2448 make_float64(0x3FA5555555554F5A), status
),
2449 status
); /* fp3 is A3+S*A5 */
2450 fp2
= floatx80_mul(fp2
, fp1
, status
); /* fp2 IS S*(A4+S*A6) */
2451 fp3
= floatx80_mul(fp3
, fp1
, status
); /* fp3 IS S*(A3+S*A5) */
2452 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2453 make_float64(0x3FC5555555555555), status
),
2454 status
); /* fp2 IS A2+S*(A4+S*A6) */
2455 fp3
= floatx80_add(fp3
, float32_to_floatx80(
2456 make_float32(0x3F000000), status
),
2457 status
); /* fp3 IS A1+S*(A3+S*A5) */
2458 fp2
= floatx80_mul(fp2
, fp1
,
2459 status
); /* fp2 IS S*(A2+S*(A4+S*A6)) */
2460 fp1
= floatx80_mul(fp1
, fp3
,
2461 status
); /* fp1 IS S*(A1+S*(A3+S*A5)) */
2462 fp2
= floatx80_mul(fp2
, fp0
,
2463 status
); /* fp2 IS R*S*(A2+S*(A4+S*A6)) */
2464 fp0
= floatx80_add(fp0
, fp1
,
2465 status
); /* fp0 IS R+S*(A1+S*(A3+S*A5)) */
2466 fp0
= floatx80_add(fp0
, fp2
, status
); /* fp0 IS EXP(R) - 1 */
2468 fp0
= floatx80_mul(fp0
, exp_tbl
[j
],
2469 status
); /* 2^(J/64)*(Exp(R)-1) */
2472 fp1
= float32_to_floatx80(exp_tbl2
[j
], status
);
2473 onebysc
= packFloatx80(1, m1
+ 0x3FFF, one_sig
); /* -2^(-M) */
2474 fp1
= floatx80_add(fp1
, onebysc
, status
);
2475 fp0
= floatx80_add(fp0
, fp1
, status
);
2476 fp0
= floatx80_add(fp0
, exp_tbl
[j
], status
);
2477 } else if (m
< -3) {
2478 fp0
= floatx80_add(fp0
, float32_to_floatx80(exp_tbl2
[j
],
2480 fp0
= floatx80_add(fp0
, exp_tbl
[j
], status
);
2481 onebysc
= packFloatx80(1, m1
+ 0x3FFF, one_sig
); /* -2^(-M) */
2482 fp0
= floatx80_add(fp0
, onebysc
, status
);
2483 } else { /* -3 <= m <= 63 */
2485 fp0
= floatx80_add(fp0
, float32_to_floatx80(exp_tbl2
[j
],
2487 onebysc
= packFloatx80(1, m1
+ 0x3FFF, one_sig
); /* -2^(-M) */
2488 fp1
= floatx80_add(fp1
, onebysc
, status
);
2489 fp0
= floatx80_add(fp0
, fp1
, status
);
2492 sc
= packFloatx80(0, m
+ 0x3FFF, one_sig
);
2494 status
->float_rounding_mode
= user_rnd_mode
;
2495 status
->floatx80_rounding_precision
= user_rnd_prec
;
2497 a
= floatx80_mul(fp0
, sc
, status
);
2499 float_raise(float_flag_inexact
, status
);
2502 } else { /* |X| > 70 log2 */
2504 fp0
= float32_to_floatx80(make_float32(0xBF800000),
2507 status
->float_rounding_mode
= user_rnd_mode
;
2508 status
->floatx80_rounding_precision
= user_rnd_prec
;
2510 a
= floatx80_add(fp0
, float32_to_floatx80(
2511 make_float32(0x00800000), status
),
2512 status
); /* -1 + 2^(-126) */
2514 float_raise(float_flag_inexact
, status
);
2518 status
->float_rounding_mode
= user_rnd_mode
;
2519 status
->floatx80_rounding_precision
= user_rnd_prec
;
2521 return floatx80_etox(a
, status
);
2524 } else { /* |X| < 1/4 */
2525 if (aExp
>= 0x3FBE) {
2527 fp0
= floatx80_mul(fp0
, fp0
, status
); /* S = X*X */
2528 fp1
= float32_to_floatx80(make_float32(0x2F30CAA8),
2530 fp1
= floatx80_mul(fp1
, fp0
, status
); /* S * B12 */
2531 fp2
= float32_to_floatx80(make_float32(0x310F8290),
2533 fp1
= floatx80_add(fp1
, float32_to_floatx80(
2534 make_float32(0x32D73220), status
),
2536 fp2
= floatx80_mul(fp2
, fp0
, status
);
2537 fp1
= floatx80_mul(fp1
, fp0
, status
);
2538 fp2
= floatx80_add(fp2
, float32_to_floatx80(
2539 make_float32(0x3493F281), status
),
2541 fp1
= floatx80_add(fp1
, float64_to_floatx80(
2542 make_float64(0x3EC71DE3A5774682), status
),
2544 fp2
= floatx80_mul(fp2
, fp0
, status
);
2545 fp1
= floatx80_mul(fp1
, fp0
, status
);
2546 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2547 make_float64(0x3EFA01A019D7CB68), status
),
2549 fp1
= floatx80_add(fp1
, float64_to_floatx80(
2550 make_float64(0x3F2A01A01A019DF3), status
),
2552 fp2
= floatx80_mul(fp2
, fp0
, status
);
2553 fp1
= floatx80_mul(fp1
, fp0
, status
);
2554 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2555 make_float64(0x3F56C16C16C170E2), status
),
2557 fp1
= floatx80_add(fp1
, float64_to_floatx80(
2558 make_float64(0x3F81111111111111), status
),
2560 fp2
= floatx80_mul(fp2
, fp0
, status
);
2561 fp1
= floatx80_mul(fp1
, fp0
, status
);
2562 fp2
= floatx80_add(fp2
, float64_to_floatx80(
2563 make_float64(0x3FA5555555555555), status
),
2565 fp3
= packFloatx80(0, 0x3FFC, LIT64(0xAAAAAAAAAAAAAAAB));
2566 fp1
= floatx80_add(fp1
, fp3
, status
); /* B2 */
2567 fp2
= floatx80_mul(fp2
, fp0
, status
);
2568 fp1
= floatx80_mul(fp1
, fp0
, status
);
2570 fp2
= floatx80_mul(fp2
, fp0
, status
);
2571 fp1
= floatx80_mul(fp1
, a
, status
);
2573 fp0
= floatx80_mul(fp0
, float32_to_floatx80(
2574 make_float32(0x3F000000), status
),
2576 fp1
= floatx80_add(fp1
, fp2
, status
); /* Q */
2577 fp0
= floatx80_add(fp0
, fp1
, status
); /* S*B1+Q */
2579 status
->float_rounding_mode
= user_rnd_mode
;
2580 status
->floatx80_rounding_precision
= user_rnd_prec
;
2582 a
= floatx80_add(fp0
, a
, status
);
2584 float_raise(float_flag_inexact
, status
);
2587 } else { /* |X| < 2^(-65) */
2588 sc
= packFloatx80(1, 1, one_sig
);
2591 if (aExp
< 0x0033) { /* |X| < 2^(-16382) */
2592 fp0
= floatx80_mul(fp0
, float64_to_floatx80(
2593 make_float64(0x48B0000000000000), status
),
2595 fp0
= floatx80_add(fp0
, sc
, status
);
2597 status
->float_rounding_mode
= user_rnd_mode
;
2598 status
->floatx80_rounding_precision
= user_rnd_prec
;
2600 a
= floatx80_mul(fp0
, float64_to_floatx80(
2601 make_float64(0x3730000000000000), status
),
2604 status
->float_rounding_mode
= user_rnd_mode
;
2605 status
->floatx80_rounding_precision
= user_rnd_prec
;
2607 a
= floatx80_add(fp0
, sc
, status
);
2610 float_raise(float_flag_inexact
, status
);
2618 * Hyperbolic tangent
2621 floatx80
floatx80_tanh(floatx80 a
, float_status
*status
)
2625 uint64_t aSig
, vSig
;
2627 int8_t user_rnd_mode
, user_rnd_prec
;
2633 aSig
= extractFloatx80Frac(a
);
2634 aExp
= extractFloatx80Exp(a
);
2635 aSign
= extractFloatx80Sign(a
);
2637 if (aExp
== 0x7FFF) {
2638 if ((uint64_t) (aSig
<< 1)) {
2639 return propagateFloatx80NaNOneArg(a
, status
);
2641 return packFloatx80(aSign
, one_exp
, one_sig
);
2644 if (aExp
== 0 && aSig
== 0) {
2645 return packFloatx80(aSign
, 0, 0);
2648 user_rnd_mode
= status
->float_rounding_mode
;
2649 user_rnd_prec
= status
->floatx80_rounding_precision
;
2650 status
->float_rounding_mode
= float_round_nearest_even
;
2651 status
->floatx80_rounding_precision
= 80;
2653 compact
= floatx80_make_compact(aExp
, aSig
);
2655 if (compact
< 0x3FD78000 || compact
> 0x3FFFDDCE) {
2657 if (compact
< 0x3FFF8000) {
2659 status
->float_rounding_mode
= user_rnd_mode
;
2660 status
->floatx80_rounding_precision
= user_rnd_prec
;
2662 a
= floatx80_move(a
, status
);
2664 float_raise(float_flag_inexact
, status
);
2668 if (compact
> 0x40048AA1) {
2671 sign
|= aSign
? 0x80000000 : 0x00000000;
2672 fp0
= float32_to_floatx80(make_float32(sign
), status
);
2674 sign
^= 0x80800000; /* -SIGN(X)*EPS */
2676 status
->float_rounding_mode
= user_rnd_mode
;
2677 status
->floatx80_rounding_precision
= user_rnd_prec
;
2679 a
= floatx80_add(fp0
, float32_to_floatx80(make_float32(sign
),
2682 float_raise(float_flag_inexact
, status
);
2686 fp0
= packFloatx80(0, aExp
+ 1, aSig
); /* Y = 2|X| */
2687 fp0
= floatx80_etox(fp0
, status
); /* FP0 IS EXP(Y) */
2688 fp0
= floatx80_add(fp0
, float32_to_floatx80(
2689 make_float32(0x3F800000),
2690 status
), status
); /* EXP(Y)+1 */
2691 sign
= aSign
? 0x80000000 : 0x00000000;
2692 fp1
= floatx80_div(float32_to_floatx80(make_float32(
2693 sign
^ 0xC0000000), status
), fp0
,
2694 status
); /* -SIGN(X)*2 / [EXP(Y)+1] */
2695 fp0
= float32_to_floatx80(make_float32(sign
| 0x3F800000),
2698 status
->float_rounding_mode
= user_rnd_mode
;
2699 status
->floatx80_rounding_precision
= user_rnd_prec
;
2701 a
= floatx80_add(fp1
, fp0
, status
);
2703 float_raise(float_flag_inexact
, status
);
2708 } else { /* 2**(-40) < |X| < (5/2)LOG2 */
2709 fp0
= packFloatx80(0, aExp
+ 1, aSig
); /* Y = 2|X| */
2710 fp0
= floatx80_etoxm1(fp0
, status
); /* FP0 IS Z = EXPM1(Y) */
2711 fp1
= floatx80_add(fp0
, float32_to_floatx80(make_float32(0x40000000),
2715 vSign
= extractFloatx80Sign(fp1
);
2716 vExp
= extractFloatx80Exp(fp1
);
2717 vSig
= extractFloatx80Frac(fp1
);
2719 fp1
= packFloatx80(vSign
^ aSign
, vExp
, vSig
);
2721 status
->float_rounding_mode
= user_rnd_mode
;
2722 status
->floatx80_rounding_precision
= user_rnd_prec
;
2724 a
= floatx80_div(fp0
, fp1
, status
);
2726 float_raise(float_flag_inexact
, status
);
2736 floatx80
floatx80_sinh(floatx80 a
, float_status
*status
)
2742 int8_t user_rnd_mode
, user_rnd_prec
;
2745 floatx80 fp0
, fp1
, fp2
;
2748 aSig
= extractFloatx80Frac(a
);
2749 aExp
= extractFloatx80Exp(a
);
2750 aSign
= extractFloatx80Sign(a
);
2752 if (aExp
== 0x7FFF) {
2753 if ((uint64_t) (aSig
<< 1)) {
2754 return propagateFloatx80NaNOneArg(a
, status
);
2756 return packFloatx80(aSign
, floatx80_infinity
.high
,
2757 floatx80_infinity
.low
);
2760 if (aExp
== 0 && aSig
== 0) {
2761 return packFloatx80(aSign
, 0, 0);
2764 user_rnd_mode
= status
->float_rounding_mode
;
2765 user_rnd_prec
= status
->floatx80_rounding_precision
;
2766 status
->float_rounding_mode
= float_round_nearest_even
;
2767 status
->floatx80_rounding_precision
= 80;
2769 compact
= floatx80_make_compact(aExp
, aSig
);
2771 if (compact
> 0x400CB167) {
2773 if (compact
> 0x400CB2B3) {
2774 status
->float_rounding_mode
= user_rnd_mode
;
2775 status
->floatx80_rounding_precision
= user_rnd_prec
;
2777 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
2778 aSign
, 0x8000, aSig
, 0, status
);
2780 fp0
= floatx80_abs(a
); /* Y = |X| */
2781 fp0
= floatx80_sub(fp0
, float64_to_floatx80(
2782 make_float64(0x40C62D38D3D64634), status
),
2783 status
); /* (|X|-16381LOG2_LEAD) */
2784 fp0
= floatx80_sub(fp0
, float64_to_floatx80(
2785 make_float64(0x3D6F90AEB1E75CC7), status
),
2786 status
); /* |X| - 16381 LOG2, ACCURATE */
2787 fp0
= floatx80_etox(fp0
, status
);
2788 fp2
= packFloatx80(aSign
, 0x7FFB, one_sig
);
2790 status
->float_rounding_mode
= user_rnd_mode
;
2791 status
->floatx80_rounding_precision
= user_rnd_prec
;
2793 a
= floatx80_mul(fp0
, fp2
, status
);
2795 float_raise(float_flag_inexact
, status
);
2799 } else { /* |X| < 16380 LOG2 */
2800 fp0
= floatx80_abs(a
); /* Y = |X| */
2801 fp0
= floatx80_etoxm1(fp0
, status
); /* FP0 IS Z = EXPM1(Y) */
2802 fp1
= floatx80_add(fp0
, float32_to_floatx80(make_float32(0x3F800000),
2803 status
), status
); /* 1+Z */
2805 fp0
= floatx80_div(fp0
, fp1
, status
); /* Z/(1+Z) */
2806 fp0
= floatx80_add(fp0
, fp2
, status
);
2808 fact
= packFloat32(aSign
, 0x7E, 0);
2810 status
->float_rounding_mode
= user_rnd_mode
;
2811 status
->floatx80_rounding_precision
= user_rnd_prec
;
2813 a
= floatx80_mul(fp0
, float32_to_floatx80(fact
, status
), status
);
2815 float_raise(float_flag_inexact
, status
);
2825 floatx80
floatx80_cosh(floatx80 a
, float_status
*status
)
2830 int8_t user_rnd_mode
, user_rnd_prec
;
2835 aSig
= extractFloatx80Frac(a
);
2836 aExp
= extractFloatx80Exp(a
);
2838 if (aExp
== 0x7FFF) {
2839 if ((uint64_t) (aSig
<< 1)) {
2840 return propagateFloatx80NaNOneArg(a
, status
);
2842 return packFloatx80(0, floatx80_infinity
.high
,
2843 floatx80_infinity
.low
);
2846 if (aExp
== 0 && aSig
== 0) {
2847 return packFloatx80(0, one_exp
, one_sig
);
2850 user_rnd_mode
= status
->float_rounding_mode
;
2851 user_rnd_prec
= status
->floatx80_rounding_precision
;
2852 status
->float_rounding_mode
= float_round_nearest_even
;
2853 status
->floatx80_rounding_precision
= 80;
2855 compact
= floatx80_make_compact(aExp
, aSig
);
2857 if (compact
> 0x400CB167) {
2858 if (compact
> 0x400CB2B3) {
2859 status
->float_rounding_mode
= user_rnd_mode
;
2860 status
->floatx80_rounding_precision
= user_rnd_prec
;
2861 return roundAndPackFloatx80(status
->floatx80_rounding_precision
, 0,
2862 0x8000, one_sig
, 0, status
);
2864 fp0
= packFloatx80(0, aExp
, aSig
);
2865 fp0
= floatx80_sub(fp0
, float64_to_floatx80(
2866 make_float64(0x40C62D38D3D64634), status
),
2868 fp0
= floatx80_sub(fp0
, float64_to_floatx80(
2869 make_float64(0x3D6F90AEB1E75CC7), status
),
2871 fp0
= floatx80_etox(fp0
, status
);
2872 fp1
= packFloatx80(0, 0x7FFB, one_sig
);
2874 status
->float_rounding_mode
= user_rnd_mode
;
2875 status
->floatx80_rounding_precision
= user_rnd_prec
;
2877 a
= floatx80_mul(fp0
, fp1
, status
);
2879 float_raise(float_flag_inexact
, status
);
2885 fp0
= packFloatx80(0, aExp
, aSig
); /* |X| */
2886 fp0
= floatx80_etox(fp0
, status
); /* EXP(|X|) */
2887 fp0
= floatx80_mul(fp0
, float32_to_floatx80(make_float32(0x3F000000),
2888 status
), status
); /* (1/2)*EXP(|X|) */
2889 fp1
= float32_to_floatx80(make_float32(0x3E800000), status
); /* 1/4 */
2890 fp1
= floatx80_div(fp1
, fp0
, status
); /* 1/(2*EXP(|X|)) */
2892 status
->float_rounding_mode
= user_rnd_mode
;
2893 status
->floatx80_rounding_precision
= user_rnd_prec
;
2895 a
= floatx80_add(fp0
, fp1
, status
);
2897 float_raise(float_flag_inexact
, status
);