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 ?
104 /* fistpll raises invalid exception for |y| >= 1L<<63. */
105 fldl MO(p63) // 1L<<63 : y : x
106 fld %st(1) // y : 1L<<63 : y : x
107 fabs // |y| : 1L<<63 : y : x
108 fcomip %st(1), %st // 1L<<63 : y : x
112 /* First see whether `y' is a natural number. In this case we
113 can use a more precise algorithm. */
115 fistpll -8(%rsp) // y : x
116 fildll -8(%rsp) // int(y) : y : x
117 fucomip %st(1),%st // y : x
120 // If y has absolute value at most 0x1p-79, then any finite
121 // nonzero x will result in 1. Saturate y to those bounds to
122 // avoid underflow in the calculation of y*log2(x).
123 fldl MO(pm79) // 0x1p-79 : y : x
124 fld %st(1) // y : 0x1p-79 : y : x
125 fabs // |y| : 0x1p-79 : y : x
126 fcomip %st(1), %st // 0x1p-79 : y : x
130 fldl MO(pm79) // 0x1p-79 : x
136 9: /* OK, we have an integer value for y. Unless very small
137 (we use < 8), use the algorithm for real exponent to avoid
138 accumulation of errors. */
139 fldl MO(p3) // 8 : y : x
140 fld %st(1) // y : 8 : y : x
141 fabs // |y| : 8 : y : x
142 fcomip %st(1), %st // 8 : y : x
149 jns 4f // y >= 0, jump
150 fdivrl MO(one) // 1/x (now referred to as x)
154 4: fldl MO(one) // 1 : x
157 6: shrdl $1, %edx, %eax
160 fmul %st(1) // x : ST*x
162 5: fmul %st(0), %st // x*x : ST*x
171 30: fldt 8(%rsp) // x : y
172 fldl MO(one) // 1.0 : x : y
173 fucomip %st(1),%st // x : y
180 2: // y is a large integer (absolute value at least 8), but
181 // may be odd unless at least 1L<<64. So it may be necessary
182 // to adjust the sign of a negative result afterwards.
186 // If y has absolute value at least 1L<<78, then any finite
187 // nonzero x will result in 0 (underflow), 1 or infinity (overflow).
188 // Saturate y to those bounds to avoid overflow in the calculation
190 fldl MO(p78) // 1L<<78 : y : |x|
191 fld %st(1) // y : 1L<<78 : y : |x|
192 fabs // |y| : 1L<<78 : y : |x|
193 fcomip %st(1), %st // 1L<<78 : y : |x|
194 fstp %st(0) // y : |x|
197 fldl MO(p78) // 1L<<78 : |x|
200 fchs // -(1L<<78) : |x|
202 3: /* y is a real number. */
204 cfi_adjust_cfa_offset (40)
206 fstpt (%rsp) // <empty>
208 call HIDDEN_JUMPTARGET (__powl_helper) // <result>
211 cfi_adjust_cfa_offset (-40)
214 // x is negative. If y is an odd integer, negate the result.
215 fldt 24(%rsp) // y : abs(result)
216 fldl MO(p64) // 1L<<64 : y : abs(result)
217 fld %st(1) // y : 1L<<64 : y : abs(result)
218 fabs // |y| : 1L<<64 : y : abs(result)
219 fcomip %st(1), %st // 1L<<64 : y : abs(result)
220 fstp %st(0) // y : abs(result)
222 fldl MO(p63) // p63 : y : abs(result)
223 fxch // y : p63 : abs(result)
224 fprem // y%p63 : p63 : abs(result)
225 fstp %st(1) // y%p63 : abs(result)
227 // We must find out whether y is an odd integer.
228 fld %st // y : y : abs(result)
229 fistpll -8(%rsp) // y : abs(result)
230 fildll -8(%rsp) // int(y) : y : abs(result)
231 fucomip %st(1),%st // y : abs(result)
232 ffreep %st // abs(result)
235 // OK, the value is an integer, but is it odd?
239 jz 290f // jump if not odd
240 // It's an odd integer.
243 291: fstp %st(0) // abs(result)
248 11: fstp %st(0) // pop y
254 12: fstp %st(0) // pop y
256 fldt 8(%rsp) // x : 1
258 fucompp // < 1, == 1, or > 1
262 je 13f // jump if x is NaN
265 je 14f // jump if |x| == 1
271 lea inf_zero(%rip),%rcx
274 fldl inf_zero(,%rdx, 4)
283 13: fldt 8(%rsp) // load x == NaN
290 jz 16f // jump if x == +inf
292 // fistpll raises invalid exception for |y| >= 1L<<63, but y
293 // may be odd unless we know |y| >= 1L<<64.
294 fldl MO(p64) // 1L<<64 : y
295 fld %st(1) // y : 1L<<64 : y
296 fabs // |y| : 1L<<64 : y
297 fcomip %st(1), %st // 1L<<64 : y
300 fldl MO(p63) // p63 : y
305 // We must find out whether y is an odd integer.
307 fistpll -8(%rsp) // y
308 fildll -8(%rsp) // int(y) : y
310 ffreep %st // <empty>
313 // OK, the value is an integer, but is it odd?
317 jz 18f // jump if not odd
318 // It's an odd integer.
321 lea minf_mzero(%rip),%rcx
324 fldl minf_mzero(,%rdx, 8)
334 lea inf_zero(%rip),%rcx
337 fldl inf_zero(,%rax, 1)
342 17: shll $30, %edx // sign bit for y in right position
345 lea inf_zero(%rip),%rcx
348 fldl inf_zero(,%rdx, 8)
358 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
362 // fistpll raises invalid exception for |y| >= 1L<<63, but y
363 // may be odd unless we know |y| >= 1L<<64.
364 fldl MO(p64) // 1L<<64 : y
365 fld %st(1) // y : 1L<<64 : y
366 fabs // |y| : 1L<<64 : y
367 fcomip %st(1), %st // 1L<<64 : y
370 fldl MO(p63) // p63 : y
376 fistpll -8(%rsp) // y
377 fildll -8(%rsp) // int(y) : y
379 ffreep %st // <empty>
382 // OK, the value is an integer, but is it odd?
386 jz 27f // jump if not odd
387 // It's an odd integer.
388 // Raise divide-by-zero exception and get minus infinity value.
396 27: // Raise divide-by-zero exception and get infinity value.
402 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
406 // fistpll raises invalid exception for |y| >= 1L<<63, but y
407 // may be odd unless we know |y| >= 1L<<64.
408 fldl MO(p64) // 1L<<64 : y
410 fcomi %st(1), %st // y : 1L<<64
413 fldl MO(p63) // p63 : y
419 fistpll -8(%rsp) // y
420 fildll -8(%rsp) // int(y) : y
422 ffreep %st // <empty>
425 // OK, the value is an integer, but is it odd?
429 jz 24f // jump if not odd
430 // It's an odd integer.
440 strong_alias (__ieee754_powl, __powl_finite)