| 1 | /* ix87 specific implementation of exp(x)-1. |
| 2 | Copyright (C) 1996-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* Using: e^x - 1 = 2^(x * log2(e)) - 1 */ |
| 20 | |
| 21 | #include <sysdep.h> |
| 22 | #include <machine/asm.h> |
| 23 | #include <i386-math-asm.h> |
| 24 | #include <libm-alias-double.h> |
| 25 | |
| 26 | .section .rodata |
| 27 | |
| 28 | .align ALIGNARG(4) |
| 29 | .type minus1,@object |
| 30 | minus1: .double -1.0 |
| 31 | ASM_SIZE_DIRECTIVE(minus1) |
| 32 | .type one,@object |
| 33 | one: .double 1.0 |
| 34 | ASM_SIZE_DIRECTIVE(one) |
| 35 | .type l2e,@object |
| 36 | l2e: .quad 0xb8aa3b295c17f0bc /* 1.442695040888963407359924681002 */ |
| 37 | .short 0x3fff |
| 38 | ASM_SIZE_DIRECTIVE(l2e) |
| 39 | |
| 40 | DEFINE_DBL_MIN |
| 41 | |
| 42 | #ifdef PIC |
| 43 | #define MO(op) op##@GOTOFF(%edx) |
| 44 | #else |
| 45 | #define MO(op) op |
| 46 | #endif |
| 47 | |
| 48 | .text |
| 49 | ENTRY(__expm1) |
| 50 | movzwl 4+6(%esp), %eax |
| 51 | xorb $0x80, %ah // invert sign bit (now 1 is "positive") |
| 52 | cmpl $0xc086, %eax // is num >= 704? |
| 53 | jae HIDDEN_JUMPTARGET (__exp) |
| 54 | |
| 55 | fldl 4(%esp) // x |
| 56 | fxam // Is NaN, +-Inf or +-0? |
| 57 | xorb $0x80, %ah |
| 58 | cmpl $0xc043, %eax // is num <= -38.0? |
| 59 | fstsw %ax |
| 60 | movb $0x45, %ch |
| 61 | jb 4f |
| 62 | |
| 63 | // Below -38.0 (may be -NaN or -Inf). |
| 64 | andb %ah, %ch |
| 65 | #ifdef PIC |
| 66 | LOAD_PIC_REG (dx) |
| 67 | #endif |
| 68 | cmpb $0x01, %ch |
| 69 | je 5f // If -NaN, jump. |
| 70 | jmp 2f // -large, possibly -Inf. |
| 71 | |
| 72 | 4: // In range -38.0 to 704.0 (may be +-0 but not NaN or +-Inf). |
| 73 | andb %ah, %ch |
| 74 | cmpb $0x40, %ch |
| 75 | je 3f // If +-0, jump. |
| 76 | #ifdef PIC |
| 77 | LOAD_PIC_REG (dx) |
| 78 | #endif |
| 79 | |
| 80 | 5: fldt MO(l2e) // log2(e) : x |
| 81 | fmulp // log2(e)*x |
| 82 | fld %st // log2(e)*x : log2(e)*x |
| 83 | // Set round-to-nearest temporarily. |
| 84 | subl $8, %esp |
| 85 | cfi_adjust_cfa_offset (8) |
| 86 | fstcw 4(%esp) |
| 87 | movl $0xf3ff, %ecx |
| 88 | andl 4(%esp), %ecx |
| 89 | movl %ecx, (%esp) |
| 90 | fldcw (%esp) |
| 91 | frndint // int(log2(e)*x) : log2(e)*x |
| 92 | fldcw 4(%esp) |
| 93 | addl $8, %esp |
| 94 | cfi_adjust_cfa_offset (-8) |
| 95 | fsubr %st, %st(1) // int(log2(e)*x) : fract(log2(e)*x) |
| 96 | fxch // fract(log2(e)*x) : int(log2(e)*x) |
| 97 | f2xm1 // 2^fract(log2(e)*x)-1 : int(log2(e)*x) |
| 98 | fscale // 2^(log2(e)*x)-2^int(log2(e)*x) : int(log2(e)*x) |
| 99 | fxch // int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x) |
| 100 | fldl MO(one) // 1 : int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x) |
| 101 | fscale // 2^int(log2(e)*x) : int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x) |
| 102 | fsubrl MO(one) // 1-2^int(log2(e)*x) : int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x) |
| 103 | fstp %st(1) // 1-2^int(log2(e)*x) : 2^(log2(e)*x)-2^int(log2(e)*x) |
| 104 | fsubrp %st, %st(1) // 2^(log2(e)*x) |
| 105 | DBL_CHECK_FORCE_UFLOW |
| 106 | ret |
| 107 | |
| 108 | 2: fstp %st |
| 109 | fldl MO(minus1) // Set result to -1.0. |
| 110 | 3: ret |
| 111 | END(__expm1) |
| 112 | libm_alias_double (__expm1, expm1) |
| 113 | |