1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <machine/asm.h>
21 #include <x86_64-math-asm.h>
23 .section .rodata.cst8,"aM",@progbits,8
28 ASM_SIZE_DIRECTIVE(one)
30 p2: .byte 0, 0, 0, 0, 0, 0, 0x10, 0x40
31 ASM_SIZE_DIRECTIVE(p2)
33 p63: .byte 0, 0, 0, 0, 0, 0, 0xe0, 0x43
34 ASM_SIZE_DIRECTIVE(p63)
36 p64: .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x43
37 ASM_SIZE_DIRECTIVE(p64)
39 p78: .byte 0, 0, 0, 0, 0, 0, 0xd0, 0x44
40 ASM_SIZE_DIRECTIVE(p78)
42 pm79: .byte 0, 0, 0, 0, 0, 0, 0, 0x3b
43 ASM_SIZE_DIRECTIVE(pm79)
45 .section .rodata.cst16,"aM",@progbits,16
48 .type infinity,@object
51 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
52 ASM_SIZE_DIRECTIVE(infinity)
55 ASM_SIZE_DIRECTIVE(zero)
56 .type minf_mzero,@object
59 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
61 .byte 0, 0, 0, 0, 0, 0, 0, 0x80
62 ASM_SIZE_DIRECTIVE(minf_mzero)
66 # define MO(op) op##(%rip)
80 cmpb $0x40, %ah // is y == 0 ?
83 cmpb $0x05, %ah // is y == ±inf ?
86 cmpb $0x01, %ah // is y == NaN ?
106 /* fistpll raises invalid exception for |y| >= 1L<<63. */
107 fldl MO(p63) // 1L<<63 : y : x
108 fld %st(1) // y : 1L<<63 : y : x
109 fabs // |y| : 1L<<63 : y : x
110 fcomip %st(1), %st // 1L<<63 : y : x
114 /* First see whether `y' is a natural number. In this case we
115 can use a more precise algorithm. */
117 fistpll -8(%rsp) // y : x
118 fildll -8(%rsp) // int(y) : y : x
119 fucomip %st(1),%st // y : x
122 // If y has absolute value at most 0x1p-79, then any finite
123 // nonzero x will result in 1. Saturate y to those bounds to
124 // avoid underflow in the calculation of y*log2(x).
125 fldl MO(pm79) // 0x1p-79 : y : x
126 fld %st(1) // y : 0x1p-79 : y : x
127 fabs // |y| : 0x1p-79 : y : x
128 fcomip %st(1), %st // 0x1p-79 : y : x
132 fldl MO(pm79) // 0x1p-79 : x
138 9: /* OK, we have an integer value for y. Unless very small
139 (we use < 4), use the algorithm for real exponent to avoid
140 accumulation of errors. */
141 fldl MO(p2) // 4 : y : x
142 fld %st(1) // y : 4 : y : x
143 fabs // |y| : 4 : y : x
144 fcomip %st(1), %st // 4 : y : x
151 jns 4f // y >= 0, jump
152 fdivrl MO(one) // 1/x (now referred to as x)
156 4: fldl MO(one) // 1 : x
159 /* If y is even, take the absolute value of x. Otherwise,
160 ensure all intermediate values that might overflow have the
166 6: shrdl $1, %edx, %eax
170 fmul %st(1) // x : ST*x
172 5: fld %st // x : x : ST*x
173 fabs // |x| : x : ST*x
174 fmulp // |x|*x : ST*x
180 LDBL_CHECK_FORCE_UFLOW_NONNAN
184 30: fldt 8(%rsp) // x : y
185 fldl MO(one) // 1.0 : x : y
186 fucomip %st(1),%st // x : y
188 31: /* At least one argument NaN, and result should be NaN. */
192 /* pow (1, NaN); check if the NaN signaling. */
193 testb $0x40, 31(%rsp)
199 2: // y is a large integer (absolute value at least 1L<<63).
200 // If y has absolute value at least 1L<<78, then any finite
201 // nonzero x will result in 0 (underflow), 1 or infinity (overflow).
202 // Saturate y to those bounds to avoid overflow in the calculation
204 fldl MO(p78) // 1L<<78 : y : x
205 fld %st(1) // y : 1L<<78 : y : x
206 fabs // |y| : 1L<<78 : y : x
207 fcomip %st(1), %st // 1L<<78 : y : x
211 fldl MO(p78) // 1L<<78 : x
214 fchs // -(1L<<78) : x
216 3: /* y is a real number. */
218 cfi_adjust_cfa_offset (40)
220 fstpt (%rsp) // <empty>
221 call HIDDEN_JUMPTARGET (__powl_helper) // <result>
223 cfi_adjust_cfa_offset (-40)
226 // pow(x,±0) = 1, unless x is sNaN
228 11: fstp %st(0) // pop y
239 112: testb $0x40, 15(%rsp)
246 12: fstp %st(0) // pop y
248 fldt 8(%rsp) // x : 1
250 fucompp // < 1, == 1, or > 1
254 je 13f // jump if x is NaN
257 je 14f // jump if |x| == 1
263 lea inf_zero(%rip),%rcx
266 fldl inf_zero(,%rdx, 4)
275 13: fldt 8(%rsp) // load x == NaN
283 jz 16f // jump if x == +inf
285 // fistpll raises invalid exception for |y| >= 1L<<63, but y
286 // may be odd unless we know |y| >= 1L<<64.
287 fldl MO(p64) // 1L<<64 : y
288 fld %st(1) // y : 1L<<64 : y
289 fabs // |y| : 1L<<64 : y
290 fcomip %st(1), %st // 1L<<64 : y
293 fldl MO(p63) // p63 : y
298 // We must find out whether y is an odd integer.
300 fistpll -8(%rsp) // y
301 fildll -8(%rsp) // int(y) : y
303 ffreep %st // <empty>
306 // OK, the value is an integer, but is it odd?
310 jz 18f // jump if not odd
311 // It's an odd integer.
314 lea minf_mzero(%rip),%rcx
317 fldl minf_mzero(,%rdx, 8)
327 lea inf_zero(%rip),%rcx
330 fldl inf_zero(,%rax, 1)
335 17: shll $30, %edx // sign bit for y in right position
338 lea inf_zero(%rip),%rcx
341 fldl inf_zero(,%rdx, 8)
351 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
355 // fistpll raises invalid exception for |y| >= 1L<<63, but y
356 // may be odd unless we know |y| >= 1L<<64.
357 fldl MO(p64) // 1L<<64 : y
358 fld %st(1) // y : 1L<<64 : y
359 fabs // |y| : 1L<<64 : y
360 fcomip %st(1), %st // 1L<<64 : y
363 fldl MO(p63) // p63 : y
369 fistpll -8(%rsp) // y
370 fildll -8(%rsp) // int(y) : y
372 ffreep %st // <empty>
375 // OK, the value is an integer, but is it odd?
379 jz 27f // jump if not odd
380 // It's an odd integer.
381 // Raise divide-by-zero exception and get minus infinity value.
389 27: // Raise divide-by-zero exception and get infinity value.
395 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
399 // fistpll raises invalid exception for |y| >= 1L<<63, but y
400 // may be odd unless we know |y| >= 1L<<64.
401 fldl MO(p64) // 1L<<64 : y
403 fcomi %st(1), %st // y : 1L<<64
406 fldl MO(p63) // p63 : y
412 fistpll -8(%rsp) // y
413 fildll -8(%rsp) // int(y) : y
415 ffreep %st // <empty>
418 // OK, the value is an integer, but is it odd?
422 jz 24f // jump if not odd
423 // It's an odd integer.
433 strong_alias (__ieee754_powl, __powl_finite)