Fix expm1 missing underflows (bug 16353).
[glibc.git] / sysdeps / i386 / fpu / s_expm1.S
blob05e5285d7bb560ffde57424c6ca6210676450cb3
1 /* ix87 specific implementation of exp(x)-1.
2    Copyright (C) 1996-2015 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5    Based on code by John C. Bowman <bowman@ipp-garching.mpg.de>.
6    Corrections by H.J. Lu (hjl@gnu.ai.mit.edu), 1997.
8    The GNU C Library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2.1 of the License, or (at your option) any later version.
13    The GNU C Library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
18    You should have received a copy of the GNU Lesser General Public
19    License along with the GNU C Library; if not, see
20    <http://www.gnu.org/licenses/>.  */
22         /* Using: e^x - 1 = 2^(x * log2(e)) - 1 */
24 #include <sysdep.h>
25 #include <machine/asm.h>
27         .section .rodata
29         .align ALIGNARG(4)
30         .type minus1,@object
31 minus1: .double -1.0
32         ASM_SIZE_DIRECTIVE(minus1)
33         .type one,@object
34 one:    .double 1.0
35         ASM_SIZE_DIRECTIVE(one)
36         .type l2e,@object
37 l2e:    .tfloat 1.442695040888963407359924681002
38         ASM_SIZE_DIRECTIVE(l2e)
40         .section .rodata.cst8,"aM",@progbits,8
42         .p2align 3
43         .type dbl_min,@object
44 dbl_min:        .byte 0, 0, 0, 0, 0, 0, 0x10, 0
45         ASM_SIZE_DIRECTIVE(dbl_min)
47 #ifdef PIC
48 #define MO(op) op##@GOTOFF(%edx)
49 #else
50 #define MO(op) op
51 #endif
53         .text
54 ENTRY(__expm1)
55         movzwl  4+6(%esp), %eax
56         xorb    $0x80, %ah      // invert sign bit (now 1 is "positive")
57         cmpl    $0xc086, %eax   // is num >= 704?
58         jae     HIDDEN_JUMPTARGET (__exp)
60         fldl    4(%esp)         // x
61         fxam                    // Is NaN, +-Inf or +-0?
62         xorb    $0x80, %ah
63         cmpl    $0xc043, %eax   // is num <= -38.0?
64         fstsw   %ax
65         movb    $0x45, %ch
66         jb      4f
68         // Below -38.0 (may be -NaN or -Inf).
69         andb    %ah, %ch
70 #ifdef  PIC
71         LOAD_PIC_REG (dx)
72 #endif
73         cmpb    $0x01, %ch
74         je      5f              // If -NaN, jump.
75         jmp     2f              // -large, possibly -Inf.
77 4:      // In range -38.0 to 704.0 (may be +-0 but not NaN or +-Inf).
78         andb    %ah, %ch
79         cmpb    $0x40, %ch
80         je      3f              // If +-0, jump.
81 #ifdef  PIC
82         LOAD_PIC_REG (dx)
83 #endif
84         fld     %st
85         fabs
86         fcoml   MO(dbl_min)
87         fstp    %st
88         fnstsw
89         sahf
90         jae     5f
91         subl    $8, %esp
92         cfi_adjust_cfa_offset (8)
93         fld     %st(0)
94         fmul    %st(0)
95         fstpl   (%esp)
96         addl    $8, %esp
97         cfi_adjust_cfa_offset (-8)
98         ret
100 5:      fldt    MO(l2e)         // log2(e) : x
101         fmulp                   // log2(e)*x
102         fld     %st             // log2(e)*x : log2(e)*x
103         // Set round-to-nearest temporarily.
104         subl    $8, %esp
105         cfi_adjust_cfa_offset (8)
106         fstcw   4(%esp)
107         movl    $0xf3ff, %ecx
108         andl    4(%esp), %ecx
109         movl    %ecx, (%esp)
110         fldcw   (%esp)
111         frndint                 // int(log2(e)*x) : log2(e)*x
112         fldcw   4(%esp)
113         addl    $8, %esp
114         cfi_adjust_cfa_offset (-8)
115         fsubr   %st, %st(1)     // int(log2(e)*x) : fract(log2(e)*x)
116         fxch                    // fract(log2(e)*x) : int(log2(e)*x)
117         f2xm1                   // 2^fract(log2(e)*x)-1 : int(log2(e)*x)
118         fscale                  // 2^(log2(e)*x)-2^int(log2(e)*x) : int(log2(e)*x)
119         fxch                    // int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x)
120         fldl    MO(one)         // 1 : int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x)
121         fscale                  // 2^int(log2(e)*x) : int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x)
122         fsubrl  MO(one)         // 1-2^int(log2(e)*x) : int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x)
123         fstp    %st(1)          // 1-2^int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x)
124         fsubrp  %st, %st(1)     // 2^(log2(e)*x)
125         ret
127 2:      fstp    %st
128         fldl    MO(minus1)      // Set result to -1.0.
129 3:      ret
130 END(__expm1)
131 weak_alias (__expm1, expm1)