1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-2016 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 <http://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
193 2: // y is a large integer (absolute value at least 1L<<63).
194 // If y has absolute value at least 1L<<78, then any finite
195 // nonzero x will result in 0 (underflow), 1 or infinity (overflow).
196 // Saturate y to those bounds to avoid overflow in the calculation
198 fldl MO(p78) // 1L<<78 : y : x
199 fld %st(1) // y : 1L<<78 : y : x
200 fabs // |y| : 1L<<78 : y : x
201 fcomip %st(1), %st // 1L<<78 : y : x
205 fldl MO(p78) // 1L<<78 : x
208 fchs // -(1L<<78) : x
210 3: /* y is a real number. */
212 cfi_adjust_cfa_offset (40)
214 fstpt (%rsp) // <empty>
215 call HIDDEN_JUMPTARGET (__powl_helper) // <result>
217 cfi_adjust_cfa_offset (-40)
222 11: fstp %st(0) // pop y
228 12: fstp %st(0) // pop y
230 fldt 8(%rsp) // x : 1
232 fucompp // < 1, == 1, or > 1
236 je 13f // jump if x is NaN
239 je 14f // jump if |x| == 1
245 lea inf_zero(%rip),%rcx
248 fldl inf_zero(,%rdx, 4)
257 13: fldt 8(%rsp) // load x == NaN
264 jz 16f // jump if x == +inf
266 // fistpll raises invalid exception for |y| >= 1L<<63, but y
267 // may be odd unless we know |y| >= 1L<<64.
268 fldl MO(p64) // 1L<<64 : y
269 fld %st(1) // y : 1L<<64 : y
270 fabs // |y| : 1L<<64 : y
271 fcomip %st(1), %st // 1L<<64 : y
274 fldl MO(p63) // p63 : y
279 // We must find out whether y is an odd integer.
281 fistpll -8(%rsp) // y
282 fildll -8(%rsp) // int(y) : y
284 ffreep %st // <empty>
287 // OK, the value is an integer, but is it odd?
291 jz 18f // jump if not odd
292 // It's an odd integer.
295 lea minf_mzero(%rip),%rcx
298 fldl minf_mzero(,%rdx, 8)
308 lea inf_zero(%rip),%rcx
311 fldl inf_zero(,%rax, 1)
316 17: shll $30, %edx // sign bit for y in right position
319 lea inf_zero(%rip),%rcx
322 fldl inf_zero(,%rdx, 8)
332 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
336 // fistpll raises invalid exception for |y| >= 1L<<63, but y
337 // may be odd unless we know |y| >= 1L<<64.
338 fldl MO(p64) // 1L<<64 : y
339 fld %st(1) // y : 1L<<64 : y
340 fabs // |y| : 1L<<64 : y
341 fcomip %st(1), %st // 1L<<64 : y
344 fldl MO(p63) // p63 : y
350 fistpll -8(%rsp) // y
351 fildll -8(%rsp) // int(y) : y
353 ffreep %st // <empty>
356 // OK, the value is an integer, but is it odd?
360 jz 27f // jump if not odd
361 // It's an odd integer.
362 // Raise divide-by-zero exception and get minus infinity value.
370 27: // Raise divide-by-zero exception and get infinity value.
376 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
380 // fistpll raises invalid exception for |y| >= 1L<<63, but y
381 // may be odd unless we know |y| >= 1L<<64.
382 fldl MO(p64) // 1L<<64 : y
384 fcomi %st(1), %st // y : 1L<<64
387 fldl MO(p63) // p63 : y
393 fistpll -8(%rsp) // y
394 fildll -8(%rsp) // int(y) : y
396 ffreep %st // <empty>
399 // OK, the value is an integer, but is it odd?
403 jz 24f // jump if not odd
404 // It's an odd integer.
414 strong_alias (__ieee754_powl, __powl_finite)