]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/fpu/e_expl.S
Fix x86/x86_64 expm1l inaccuracy and exceptions (bugs 13885, 13923).
[thirdparty/glibc.git] / sysdeps / i386 / fpu / e_expl.S
1 /*
2 * Written by J.T. Conklin <jtc@netbsd.org>.
3 * Public domain.
4 *
5 * Adapted for `long double' by Ulrich Drepper <drepper@cygnus.com>.
6 */
7
8 /*
9 * The 8087 method for the exponential function is to calculate
10 * exp(x) = 2^(x log2(e))
11 * after separating integer and fractional parts
12 * x log2(e) = i + f, |f| <= .5
13 * 2^i is immediate but f needs to be precise for long double accuracy.
14 * Suppress range reduction error in computing f by the following.
15 * Separate x into integer and fractional parts
16 * x = xi + xf, |xf| <= .5
17 * Separate log2(e) into the sum of an exact number c0 and small part c1.
18 * c0 + c1 = log2(e) to extra precision
19 * Then
20 * f = (c0 xi - i) + c0 xf + c1 x
21 * where c0 xi is exact and so also is (c0 xi - i).
22 * -- moshier@na-net.ornl.gov
23 */
24
25 #include <machine/asm.h>
26
27 #ifdef USE_AS_EXP10L
28 # define IEEE754_EXPL __ieee754_exp10l
29 # define EXPL_FINITE __exp10l_finite
30 # define FLDLOG fldl2t
31 #elif defined USE_AS_EXPM1L
32 # define IEEE754_EXPL __expm1l
33 # undef EXPL_FINITE
34 # define FLDLOG fldl2e
35 #else
36 # define IEEE754_EXPL __ieee754_expl
37 # define EXPL_FINITE __expl_finite
38 # define FLDLOG fldl2e
39 #endif
40
41 .section .rodata.cst16,"aM",@progbits,16
42
43 .p2align 4
44 #ifdef USE_AS_EXP10L
45 ASM_TYPE_DIRECTIVE(c0,@object)
46 c0: .byte 0, 0, 0, 0, 0, 0, 0x9a, 0xd4, 0x00, 0x40
47 .byte 0, 0, 0, 0, 0, 0
48 ASM_SIZE_DIRECTIVE(c0)
49 ASM_TYPE_DIRECTIVE(c1,@object)
50 c1: .byte 0x58, 0x92, 0xfc, 0x15, 0x37, 0x9a, 0x97, 0xf0, 0xef, 0x3f
51 .byte 0, 0, 0, 0, 0, 0
52 ASM_SIZE_DIRECTIVE(c1)
53 #else
54 ASM_TYPE_DIRECTIVE(c0,@object)
55 c0: .byte 0, 0, 0, 0, 0, 0, 0xaa, 0xb8, 0xff, 0x3f
56 .byte 0, 0, 0, 0, 0, 0
57 ASM_SIZE_DIRECTIVE(c0)
58 ASM_TYPE_DIRECTIVE(c1,@object)
59 c1: .byte 0x20, 0xfa, 0xee, 0xc2, 0x5f, 0x70, 0xa5, 0xec, 0xed, 0x3f
60 .byte 0, 0, 0, 0, 0, 0
61 ASM_SIZE_DIRECTIVE(c1)
62 #endif
63 ASM_TYPE_DIRECTIVE(csat,@object)
64 csat: .byte 0, 0, 0, 0, 0, 0, 0, 0x80, 0x0e, 0x40
65 .byte 0, 0, 0, 0, 0, 0
66 ASM_SIZE_DIRECTIVE(csat)
67
68 #ifdef PIC
69 # define MO(op) op##@GOTOFF(%ecx)
70 #else
71 # define MO(op) op
72 #endif
73
74 .text
75 ENTRY(IEEE754_EXPL)
76 #ifdef USE_AS_EXPM1L
77 movzwl 4+8(%esp), %eax
78 xorb $0x80, %ah // invert sign bit (now 1 is "positive")
79 cmpl $0xc006, %eax // is num positive and exp >= 6 (number is >= 128.0)?
80 jae HIDDEN_JUMPTARGET (__expl) // (if num is denormal, it is at least >= 64.0)
81 #endif
82 fldt 4(%esp)
83 /* I added the following ugly construct because expl(+-Inf) resulted
84 in NaN. The ugliness results from the bright minds at Intel.
85 For the i686 the code can be written better.
86 -- drepper@cygnus.com. */
87 fxam /* Is NaN or +-Inf? */
88 #ifdef PIC
89 LOAD_PIC_REG (cx)
90 #endif
91 #ifndef USE_AS_EXPM1L
92 movzwl 4+8(%esp), %eax
93 #endif
94 andl $0x7fff, %eax
95 cmpl $0x400d, %eax
96 jle 3f
97 /* Overflow, underflow or infinity or NaN as argument. */
98 fstsw %ax
99 movb $0x45, %dh
100 andb %ah, %dh
101 cmpb $0x05, %dh
102 je 1f /* Is +-Inf, jump. */
103 cmpb $0x01, %dh
104 je 2f /* Is +-NaN, jump. */
105 /* Overflow or underflow; saturate. */
106 fstp %st
107 fldt MO(csat)
108 andb $2, %ah
109 jz 3f
110 fchs
111 3:
112 #ifdef USE_AS_EXPM1L
113 /* Test for +-0 as argument. */
114 fstsw %ax
115 movb $0x45, %dh
116 andb %ah, %dh
117 cmpb $0x40, %dh
118 je 2f
119 #endif
120 FLDLOG /* 1 log2(base) */
121 fmul %st(1), %st /* 1 x log2(base) */
122 frndint /* 1 i */
123 fld %st(1) /* 2 x */
124 frndint /* 2 xi */
125 fld %st(1) /* 3 i */
126 fldt MO(c0) /* 4 c0 */
127 fld %st(2) /* 5 xi */
128 fmul %st(1), %st /* 5 c0 xi */
129 fsubp %st, %st(2) /* 4 f = c0 xi - i */
130 fld %st(4) /* 5 x */
131 fsub %st(3), %st /* 5 xf = x - xi */
132 fmulp %st, %st(1) /* 4 c0 xf */
133 faddp %st, %st(1) /* 3 f = f + c0 xf */
134 fldt MO(c1) /* 4 */
135 fmul %st(4), %st /* 4 c1 * x */
136 faddp %st, %st(1) /* 3 f = f + c1 * x */
137 f2xm1 /* 3 2^(fract(x * log2(base))) - 1 */
138 #ifdef USE_AS_EXPM1L
139 fstp %st(1) /* 2 */
140 fscale /* 2 scale factor is st(1); base^x - 2^i */
141 fxch /* 2 i */
142 fld1 /* 3 1.0 */
143 fscale /* 3 2^i */
144 fld1 /* 4 1.0 */
145 fsubrp %st, %st(1) /* 3 2^i - 1.0 */
146 fstp %st(1) /* 2 */
147 faddp %st, %st(1) /* 1 base^x - 1.0 */
148 #else
149 fld1 /* 4 1.0 */
150 faddp /* 3 2^(fract(x * log2(base))) */
151 fstp %st(1) /* 2 */
152 fscale /* 2 scale factor is st(1); base^x */
153 fstp %st(1) /* 1 */
154 #endif
155 fstp %st(1) /* 0 */
156 jmp 2f
157 1: testl $0x200, %eax /* Test sign. */
158 jz 2f /* If positive, jump. */
159 fstp %st
160 #ifdef USE_AS_EXPM1L
161 fld1
162 fchs
163 #else
164 fldz /* Set result to 0. */
165 #endif
166 2: ret
167 END(IEEE754_EXPL)
168 #ifdef USE_AS_EXPM1L
169 libm_hidden_def (__expm1l)
170 weak_alias (__expm1l, expm1l)
171 #else
172 strong_alias (IEEE754_EXPL, EXPL_FINITE)
173 #endif