1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-2013 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>
22 .section .rodata.cst8,"aM",@progbits,8
27 ASM_SIZE_DIRECTIVE(one)
29 p3: .byte 0, 0, 0, 0, 0, 0, 0x20, 0x40
30 ASM_SIZE_DIRECTIVE(p3)
32 p63: .byte 0, 0, 0, 0, 0, 0, 0xe0, 0x43
33 ASM_SIZE_DIRECTIVE(p63)
35 p64: .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x43
36 ASM_SIZE_DIRECTIVE(p64)
38 p78: .byte 0, 0, 0, 0, 0, 0, 0xd0, 0x44
39 ASM_SIZE_DIRECTIVE(p78)
41 pm79: .byte 0, 0, 0, 0, 0, 0, 0, 0x3b
42 ASM_SIZE_DIRECTIVE(pm79)
44 .section .rodata.cst16,"aM",@progbits,16
47 .type infinity,@object
50 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
51 ASM_SIZE_DIRECTIVE(infinity)
54 ASM_SIZE_DIRECTIVE(zero)
55 .type minf_mzero,@object
58 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
60 .byte 0, 0, 0, 0, 0, 0, 0, 0x80
61 ASM_SIZE_DIRECTIVE(minf_mzero)
64 # define MO(op) op##(%rip)
78 cmpb $0x40, %ah // is y == 0 ?
81 cmpb $0x05, %ah // is y == ±inf ?
84 cmpb $0x01, %ah // is y == NaN ?
101 /* fistpll raises invalid exception for |y| >= 1L<<63. */
102 fldl MO(p63) // 1L<<63 : y : x
103 fld %st(1) // y : 1L<<63 : y : x
104 fabs // |y| : 1L<<63 : y : x
105 fcomip %st(1), %st // 1L<<63 : y : x
109 /* First see whether `y' is a natural number. In this case we
110 can use a more precise algorithm. */
112 fistpll -8(%rsp) // y : x
113 fildll -8(%rsp) // int(y) : y : x
114 fucomip %st(1),%st // y : x
117 // If y has absolute value at most 0x1p-79, then any finite
118 // nonzero x will result in 1. Saturate y to those bounds to
119 // avoid underflow in the calculation of y*log2(x).
120 fldl MO(pm79) // 0x1p-79 : y : x
121 fld %st(1) // y : 0x1p-79 : y : x
122 fabs // |y| : 0x1p-79 : y : x
123 fcomip %st(1), %st // 0x1p-79 : y : x
127 fldl MO(pm79) // 0x1p-79 : x
133 9: /* OK, we have an integer value for y. Unless very small
134 (we use < 8), use the algorithm for real exponent to avoid
135 accumulation of errors. */
136 fldl MO(p3) // 8 : y : x
137 fld %st(1) // y : 8 : y : x
138 fabs // |y| : 8 : y : x
139 fcomip %st(1), %st // 8 : y : x
146 jns 4f // y >= 0, jump
147 fdivrl MO(one) // 1/x (now referred to as x)
151 4: fldl MO(one) // 1 : x
154 6: shrdl $1, %edx, %eax
157 fmul %st(1) // x : ST*x
159 5: fmul %st(0), %st // x*x : ST*x
168 30: fldt 8(%rsp) // x : y
169 fldl MO(one) // 1.0 : x : y
170 fucomip %st(1),%st // x : y
177 2: // y is a large integer (absolute value at least 8), but
178 // may be odd unless at least 1L<<64. So it may be necessary
179 // to adjust the sign of a negative result afterwards.
183 // If y has absolute value at least 1L<<78, then any finite
184 // nonzero x will result in 0 (underflow), 1 or infinity (overflow).
185 // Saturate y to those bounds to avoid overflow in the calculation
187 fldl MO(p78) // 1L<<78 : y : |x|
188 fld %st(1) // y : 1L<<78 : y : |x|
189 fabs // |y| : 1L<<78 : y : |x|
190 fcomip %st(1), %st // 1L<<78 : y : |x|
191 fstp %st(0) // y : |x|
194 fldl MO(p78) // 1L<<78 : |x|
197 fchs // -(1L<<78) : |x|
199 3: /* y is a real number. */
201 cfi_adjust_cfa_offset (40)
203 fstpt (%rsp) // <empty>
205 call HIDDEN_JUMPTARGET (__powl_helper) // <result>
208 cfi_adjust_cfa_offset (-40)
211 // x is negative. If y is an odd integer, negate the result.
212 fldt 24(%rsp) // y : abs(result)
213 fldl MO(p64) // 1L<<64 : y : abs(result)
214 fld %st(1) // y : 1L<<64 : y : abs(result)
215 fabs // |y| : 1L<<64 : y : abs(result)
216 fcomip %st(1), %st // 1L<<64 : y : abs(result)
217 fstp %st(0) // y : abs(result)
219 fldl MO(p63) // p63 : y : abs(result)
220 fxch // y : p63 : abs(result)
221 fprem // y%p63 : p63 : abs(result)
222 fstp %st(1) // y%p63 : abs(result)
224 // We must find out whether y is an odd integer.
225 fld %st // y : y : abs(result)
226 fistpll -8(%rsp) // y : abs(result)
227 fildll -8(%rsp) // int(y) : y : abs(result)
228 fucomip %st(1),%st // y : abs(result)
229 ffreep %st // abs(result)
232 // OK, the value is an integer, but is it odd?
236 jz 290f // jump if not odd
237 // It's an odd integer.
240 291: fstp %st(0) // abs(result)
245 11: fstp %st(0) // pop y
251 12: fstp %st(0) // pop y
253 fldt 8(%rsp) // x : 1
255 fucompp // < 1, == 1, or > 1
259 je 13f // jump if x is NaN
262 je 14f // jump if |x| == 1
268 lea inf_zero(%rip),%rcx
271 fldl inf_zero(,%rdx, 4)
280 13: fldt 8(%rsp) // load x == NaN
287 jz 16f // jump if x == +inf
289 // fistpll raises invalid exception for |y| >= 1L<<63, but y
290 // may be odd unless we know |y| >= 1L<<64.
291 fldl MO(p64) // 1L<<64 : y
292 fld %st(1) // y : 1L<<64 : y
293 fabs // |y| : 1L<<64 : y
294 fcomip %st(1), %st // 1L<<64 : y
297 fldl MO(p63) // p63 : y
302 // We must find out whether y is an odd integer.
304 fistpll -8(%rsp) // y
305 fildll -8(%rsp) // int(y) : y
307 ffreep %st // <empty>
310 // OK, the value is an integer, but is it odd?
314 jz 18f // jump if not odd
315 // It's an odd integer.
318 lea minf_mzero(%rip),%rcx
321 fldl minf_mzero(,%rdx, 8)
331 lea inf_zero(%rip),%rcx
334 fldl inf_zero(,%rax, 1)
339 17: shll $30, %edx // sign bit for y in right position
342 lea inf_zero(%rip),%rcx
345 fldl inf_zero(,%rdx, 8)
355 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
359 // fistpll raises invalid exception for |y| >= 1L<<63, but y
360 // may be odd unless we know |y| >= 1L<<64.
361 fldl MO(p64) // 1L<<64 : y
362 fld %st(1) // y : 1L<<64 : y
363 fabs // |y| : 1L<<64 : y
364 fcomip %st(1), %st // 1L<<64 : y
367 fldl MO(p63) // p63 : y
373 fistpll -8(%rsp) // y
374 fildll -8(%rsp) // int(y) : y
376 ffreep %st // <empty>
379 // OK, the value is an integer, but is it odd?
383 jz 27f // jump if not odd
384 // It's an odd integer.
385 // Raise divide-by-zero exception and get minus infinity value.
393 27: // Raise divide-by-zero exception and get infinity value.
399 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
403 // fistpll raises invalid exception for |y| >= 1L<<63, but y
404 // may be odd unless we know |y| >= 1L<<64.
405 fldl MO(p64) // 1L<<64 : y
407 fcomi %st(1), %st // y : 1L<<64
410 fldl MO(p63) // p63 : y
416 fistpll -8(%rsp) // y
417 fildll -8(%rsp) // int(y) : y
419 ffreep %st // <empty>
422 // OK, the value is an integer, but is it odd?
426 jz 24f // jump if not odd
427 // It's an odd integer.
437 strong_alias (__ieee754_powl, __powl_finite)