Fix x86 / x86_64 expl, exp10l missing underflows (bug 16361).
[glibc.git] / sysdeps / x86_64 / fpu / e_expl.S
blob866bad2c6e9b17d9ca8ce6d7dfbfca7a7a6de4ee
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  */
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  */
25 #include <machine/asm.h>
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
41         .section .rodata.cst16,"aM",@progbits,16
43         .p2align 4
44 #ifdef USE_AS_EXP10L
45         .type 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         .type 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         .type 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         .type 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 #ifndef USE_AS_EXPM1L
64         .type csat,@object
65 csat:   .byte 0, 0, 0, 0, 0, 0, 0, 0x80, 0x0e, 0x40
66         .byte 0, 0, 0, 0, 0, 0
67         ASM_SIZE_DIRECTIVE(csat)
68         .type cmin,@object
69 cmin:   .byte 0, 0, 0, 0, 0, 0, 0, 0x80, 0x1, 0
70         .byte 0, 0, 0, 0, 0, 0
71         ASM_SIZE_DIRECTIVE(cmin)
72 #endif
74 #ifdef PIC
75 # define MO(op) op##(%rip)
76 #else
77 # define MO(op) op
78 #endif
80         .text
81 ENTRY(IEEE754_EXPL)
82 #ifdef USE_AS_EXPM1L
83         movzwl  8+8(%rsp), %eax
84         xorb    $0x80, %ah      // invert sign bit (now 1 is "positive")
85         cmpl    $0xc006, %eax   // is num positive and exp >= 6 (number is >= 128.0)?
86         jae     HIDDEN_JUMPTARGET (__expl) // (if num is denormal, it is at least >= 64.0)
87 #endif
88         fldt    8(%rsp)
89 /* I added the following ugly construct because expl(+-Inf) resulted
90    in NaN.  The ugliness results from the bright minds at Intel.
91    For the i686 the code can be written better.
92    -- drepper@cygnus.com.  */
93         fxam                    /* Is NaN or +-Inf?  */
94 #ifdef USE_AS_EXPM1L
95         xorb    $0x80, %ah
96         cmpl    $0xc006, %eax
97         fstsw   %ax
98         movb    $0x45, %dh
99         jb      4f
101         /* Below -64.0 (may be -NaN or -Inf). */
102         andb    %ah, %dh
103         cmpb    $0x01, %dh
104         je      2f              /* Is +-NaN, jump.  */
105         jmp     1f              /* -large, possibly -Inf.  */
107 4:      /* In range -64.0 to 64.0 (may be +-0 but not NaN or +-Inf).  */
108         /* Test for +-0 as argument.  */
109         andb    %ah, %dh
110         cmpb    $0x40, %dh
111         je      2f
113         /* Test for arguments that are small but not subnormal.  */
114         movzwl  8+8(%rsp), %eax
115         andl    $0x7fff, %eax
116         cmpl    $0x3fbf, %eax
117         jge     3f
118         /* Argument's exponent below -64; avoid spurious underflow if
119            normal.  */
120         cmpl    $0x0001, %eax
121         jge     2f
122 #else
123         movzwl  8+8(%rsp), %eax
124         andl    $0x7fff, %eax
125         cmpl    $0x400d, %eax
126         jg      5f
127         cmpl    $0x3fbc, %eax
128         jge     3f
129         /* Argument's exponent below -67, result rounds to 1.  */
130         fld1
131         faddp
132         jmp     2f
133 5:      /* Overflow, underflow or infinity or NaN as argument.  */
134         fstsw   %ax
135         movb    $0x45, %dh
136         andb    %ah, %dh
137         cmpb    $0x05, %dh
138         je      1f              /* Is +-Inf, jump.    */
139         cmpb    $0x01, %dh
140         je      2f              /* Is +-NaN, jump.    */
141         /* Overflow or underflow; saturate.  */
142         fstp    %st
143         fldt    MO(csat)
144         andb    $2, %ah
145         jz      3f
146         fchs
147 #endif
148 3:      FLDLOG                  /* 1  log2(base)      */
149         fmul    %st(1), %st     /* 1  x log2(base)    */
150         /* Set round-to-nearest temporarily.  */
151         fstcw   -4(%rsp)
152         movl    $0xf3ff, %edx
153         andl    -4(%rsp), %edx
154         movl    %edx, -8(%rsp)
155         fldcw   -8(%rsp)
156         frndint                 /* 1  i               */
157         fld     %st(1)          /* 2  x               */
158         frndint                 /* 2  xi              */
159         fldcw   -4(%rsp)
160         fld     %st(1)          /* 3  i               */
161         fldt    MO(c0)          /* 4  c0              */
162         fld     %st(2)          /* 5  xi              */
163         fmul    %st(1), %st     /* 5  c0 xi           */
164         fsubp   %st, %st(2)     /* 4  f = c0 xi  - i  */
165         fld     %st(4)          /* 5  x               */
166         fsub    %st(3), %st     /* 5  xf = x - xi     */
167         fmulp   %st, %st(1)     /* 4  c0 xf           */
168         faddp   %st, %st(1)     /* 3  f = f + c0 xf   */
169         fldt    MO(c1)          /* 4                  */
170         fmul    %st(4), %st     /* 4  c1 * x          */
171         faddp   %st, %st(1)     /* 3  f = f + c1 * x  */
172         f2xm1                   /* 3 2^(fract(x * log2(base))) - 1 */
173 #ifdef USE_AS_EXPM1L
174         fstp    %st(1)          /* 2                  */
175         fscale                  /* 2 scale factor is st(1); base^x - 2^i */
176         fxch                    /* 2 i                */
177         fld1                    /* 3 1.0              */
178         fscale                  /* 3 2^i              */
179         fld1                    /* 4 1.0              */
180         fsubrp  %st, %st(1)     /* 3 2^i - 1.0        */
181         fstp    %st(1)          /* 2                  */
182         faddp   %st, %st(1)     /* 1 base^x - 1.0     */
183 #else
184         fld1                    /* 4 1.0              */
185         faddp                   /* 3 2^(fract(x * log2(base))) */
186         fstp    %st(1)          /* 2  */
187         fscale                  /* 2 scale factor is st(1); base^x */
188         fstp    %st(1)          /* 1  */
189         /* Ensure underflow for tiny result.  */
190         fldt    MO(cmin)        /* 2 cmin  */
191         fld     %st(1)          /* 3  */
192         fcomip  %st(1), %st     /* 2  */
193         fstp    %st             /* 1  */
194         jnc     6f
195         fld     %st
196         fmul    %st
197         fstp    %st
198 #endif
199 6:      fstp    %st(1)          /* 0  */
200         jmp     2f
202 #ifdef USE_AS_EXPM1L
203         /* For expm1l, only negative sign gets here.  */
204         fstp    %st
205         fld1
206         fchs
207 #else
208         testl   $0x200, %eax    /* Test sign.  */
209         jz      2f              /* If positive, jump.  */
210         fstp    %st
211         fldz                    /* Set result to 0.  */
212 #endif
213 2:      ret
214 END(IEEE754_EXPL)
215 #ifdef USE_AS_EXPM1L
216 libm_hidden_def (__expm1l)
217 weak_alias (__expm1l, expm1l)
218 #else
219 strong_alias (IEEE754_EXPL, EXPL_FINITE)
220 #endif