2 * Written by J.T. Conklin <jtc@netbsd.org>.
5 * Adapted for `long double' by Ulrich Drepper <drepper@cygnus.com>.
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
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
25 #include <libm-alias-ldouble.h>
26 #include <machine/asm.h>
27 #include <i386-math-asm.h>
28 #include <libm-alias-finite.h>
31 # define IEEE754_EXPL __ieee754_exp10l
32 # define FLDLOG fldl2t
33 #elif defined USE_AS_EXPM1L
34 # define IEEE754_EXPL __expm1l
35 # define FLDLOG fldl2e
37 # define IEEE754_EXPL __ieee754_expl
38 # define FLDLOG fldl2e
41 .section .rodata.cst16,"aM",@progbits,16
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)
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)
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)
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)
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)
72 # define MO(op) op##@GOTOFF(%ecx)
80 movzwl 4+8(%esp), %eax
81 xorb $0x80, %ah // invert sign bit (now 1 is "positive")
82 cmpl $0xc006, %eax // is num positive and exp >= 6 (number is >= 128.0)?
83 jae HIDDEN_JUMPTARGET (__expl) // (if num is denormal, it is at least >= 64.0)
86 /* I added the following ugly construct because expl(+-Inf) resulted
87 in NaN. The ugliness results from the bright minds at Intel.
88 For the i686 the code can be written better.
89 -- drepper@cygnus.com. */
90 fxam /* Is NaN or +-Inf? */
101 /* Below -64.0 (may be -NaN or -Inf). */
104 je 6f /* 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. */
113 /* Test for arguments that are small but not subnormal. */
114 movzwl 4+8(%esp), %eax
118 /* Argument's exponent below -64; avoid spurious underflow if
122 /* Force underflow and return the argument, to avoid wrong signs
123 of zero results from the code below in some rounding modes. */
129 movzwl 4+8(%esp), %eax
135 /* Argument's exponent below -67, result rounds to 1. */
139 5: /* Overflow, underflow or infinity or NaN as argument. */
144 je 1f /* Is +-Inf, jump. */
146 je 6f /* Is +-NaN, jump. */
147 /* Overflow or underflow; saturate. */
154 3: FLDLOG /* 1 log2(base) */
155 fmul %st(1), %st /* 1 x log2(base) */
156 /* Set round-to-nearest temporarily. */
158 cfi_adjust_cfa_offset (8)
169 cfi_adjust_cfa_offset (-8)
171 fldt MO(c0) /* 4 c0 */
172 fld %st(2) /* 5 xi */
173 fmul %st(1), %st /* 5 c0 xi */
174 fsubp %st, %st(2) /* 4 f = c0 xi - i */
176 fsub %st(3), %st /* 5 xf = x - xi */
177 fmulp %st, %st(1) /* 4 c0 xf */
178 faddp %st, %st(1) /* 3 f = f + c0 xf */
180 fmul %st(4), %st /* 4 c1 * x */
181 faddp %st, %st(1) /* 3 f = f + c1 * x */
182 f2xm1 /* 3 2^(fract(x * log2(base))) - 1 */
185 fscale /* 2 scale factor is st(1); base^x - 2^i */
190 fsubrp %st, %st(1) /* 3 2^i - 1.0 */
192 faddp %st, %st(1) /* 1 base^x - 1.0 */
195 faddp /* 3 2^(fract(x * log2(base))) */
197 fscale /* 2 scale factor is st(1); base^x */
199 LDBL_CHECK_FORCE_UFLOW_NONNEG
205 /* For expm1l, only negative sign gets here. */
210 testl $0x200, %eax /* Test sign. */
211 jz 2f /* If positive, jump. */
213 fldz /* Set result to 0. */
216 6: /* NaN argument. */
222 libm_hidden_def (__expm1l)
223 libm_alias_ldouble (__expm1, expm1)
224 #elif defined USE_AS_EXP10L
225 libm_alias_finite (__ieee754_exp10l, __exp10l)
227 libm_alias_finite (__ieee754_expl, __expl)