1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-1999, 2001, 2004, 2007, 2011-2012
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #include <machine/asm.h>
23 .section .rodata.cst8,"aM",@progbits,8
28 ASM_SIZE_DIRECTIVE(one)
31 ASM_SIZE_DIRECTIVE(limit)
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 .section .rodata.cst16,"aM",@progbits,16
45 .type infinity,@object
48 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
49 ASM_SIZE_DIRECTIVE(infinity)
52 ASM_SIZE_DIRECTIVE(zero)
53 .type minf_mzero,@object
56 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
58 .byte 0, 0, 0, 0, 0, 0, 0, 0x80
59 ASM_SIZE_DIRECTIVE(minf_mzero)
62 # define MO(op) op##(%rip)
76 cmpb $0x40, %ah // is y == 0 ?
79 cmpb $0x05, %ah // is y == ±inf ?
82 cmpb $0x01, %ah // is y == NaN ?
99 /* fistpll raises invalid exception for |y| >= 1L<<63. */
100 fldl MO(p63) // 1L<<63 : y : x
101 fld %st(1) // y : 1L<<63 : y : x
102 fabs // |y| : 1L<<63 : y : x
103 fcomip %st(1), %st // 1L<<63 : y : x
107 /* First see whether `y' is a natural number. In this case we
108 can use a more precise algorithm. */
110 fistpll -8(%rsp) // y : x
111 fildll -8(%rsp) // int(y) : y : x
112 fucomip %st(1),%st // y : x
115 /* OK, we have an integer value for y. */
120 jns 4f // y >= 0, jump
121 fdivrl MO(one) // 1/x (now referred to as x)
125 4: fldl MO(one) // 1 : x
128 6: shrdl $1, %edx, %eax
131 fmul %st(1) // x : ST*x
133 5: fmul %st(0), %st // x*x : ST*x
142 30: fldt 8(%rsp) // x : y
143 fldl MO(one) // 1.0 : x : y
144 fucomip %st(1),%st // x : y
151 2: // y is a large integer (absolute value at least 1L<<63), but
152 // may be odd unless at least 1L<<64. So it may be necessary
153 // to adjust the sign of a negative result afterwards.
157 // If y has absolute value at least 1L<<78, then any finite
158 // nonzero x will result in 0 (underflow), 1 or infinity (overflow).
159 // Saturate y to those bounds to avoid overflow in the calculation
161 fldl MO(p78) // 1L<<78 : y : |x|
162 fld %st(1) // y : 1L<<78 : y : |x|
163 fabs // |y| : 1L<<78 : y : |x|
164 fcomip %st(1), %st // 1L<<78 : y : |x|
165 fstp %st(0) // y : |x|
168 fldl MO(p78) // 1L<<78 : |x|
171 fchs // -(1L<<78) : |x|
173 3: /* y is a real number. */
175 fldl MO(one) // 1.0 : x : y
176 fldl MO(limit) // 0.29 : 1.0 : x : y
177 fld %st(2) // x : 0.29 : 1.0 : x : y
178 fsub %st(2) // x-1 : 0.29 : 1.0 : x : y
179 fabs // |x-1| : 0.29 : 1.0 : x : y
180 fucompp // 1.0 : x : y
185 fsub %st(1) // x-1 : 1.0 : y
186 fyl2xp1 // log2(x) : y
189 7: fyl2x // log2(x) : y
190 8: fmul %st(1) // y*log2(x) : y
191 fst %st(1) // y*log2(x) : y*log2(x)
192 frndint // int(y*log2(x)) : y*log2(x)
193 fsubr %st, %st(1) // int(y*log2(x)) : fract(y*log2(x))
194 fxch // fract(y*log2(x)) : int(y*log2(x))
195 f2xm1 // 2^fract(y*log2(x))-1 : int(y*log2(x))
196 faddl MO(one) // 2^fract(y*log2(x)) : int(y*log2(x))
197 fscale // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
198 fstp %st(1) // 2^fract(y*log2(x))*2^int(y*log2(x))
201 // x is negative. If y is an odd integer, negate the result.
202 fldt 24(%rsp) // y : abs(result)
203 fldl MO(p64) // 1L<<64 : y : abs(result)
204 fld %st(1) // y : 1L<<64 : y : abs(result)
205 fabs // |y| : 1L<<64 : y : abs(result)
206 fcomip %st(1), %st // 1L<<64 : y : abs(result)
207 fstp %st(0) // y : abs(result)
209 fldl MO(p63) // p63 : y : abs(result)
210 fxch // y : p63 : abs(result)
211 fprem // y%p63 : p63 : abs(result)
212 fstp %st(1) // y%p63 : abs(result)
214 // We must find out whether y is an odd integer.
215 fld %st // y : y : abs(result)
216 fistpll -8(%rsp) // y : abs(result)
217 fildll -8(%rsp) // int(y) : y : abs(result)
218 fucomip %st(1),%st // y : abs(result)
219 ffreep %st // abs(result)
222 // OK, the value is an integer, but is it odd?
226 jz 290f // jump if not odd
227 // It's an odd integer.
230 291: fstp %st(0) // abs(result)
235 11: fstp %st(0) // pop y
241 12: fstp %st(0) // pop y
243 fldt 8(%rsp) // x : 1
245 fucompp // < 1, == 1, or > 1
249 je 13f // jump if x is NaN
252 je 14f // jump if |x| == 1
258 lea inf_zero(%rip),%rcx
261 fldl inf_zero(,%rdx, 4)
270 13: fldt 8(%rsp) // load x == NaN
277 jz 16f // jump if x == +inf
279 // fistpll raises invalid exception for |y| >= 1L<<63, but y
280 // may be odd unless we know |y| >= 1L<<64.
281 fldl MO(p64) // 1L<<64 : y
282 fld %st(1) // y : 1L<<64 : y
283 fabs // |y| : 1L<<64 : y
284 fcomip %st(1), %st // 1L<<64 : y
287 fldl MO(p63) // p63 : y
292 // We must find out whether y is an odd integer.
294 fistpll -8(%rsp) // y
295 fildll -8(%rsp) // int(y) : y
297 ffreep %st // <empty>
300 // OK, the value is an integer, but is it odd?
304 jz 18f // jump if not odd
305 // It's an odd integer.
308 lea minf_mzero(%rip),%rcx
311 fldl minf_mzero(,%rdx, 8)
321 lea inf_zero(%rip),%rcx
324 fldl inf_zero(,%rax, 1)
329 17: shll $30, %edx // sign bit for y in right position
332 lea inf_zero(%rip),%rcx
335 fldl inf_zero(,%rdx, 8)
345 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
349 // fistpll raises invalid exception for |y| >= 1L<<63, but y
350 // may be odd unless we know |y| >= 1L<<64.
351 fldl MO(p64) // 1L<<64 : y
352 fld %st(1) // y : 1L<<64 : y
353 fabs // |y| : 1L<<64 : y
354 fcomip %st(1), %st // 1L<<64 : y
357 fldl MO(p63) // p63 : y
363 fistpll -8(%rsp) // y
364 fildll -8(%rsp) // int(y) : y
366 ffreep %st // <empty>
369 // OK, the value is an integer, but is it odd?
373 jz 27f // jump if not odd
374 // It's an odd integer.
375 // Raise divide-by-zero exception and get minus infinity value.
383 27: // Raise divide-by-zero exception and get infinity value.
389 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
393 // fistpll raises invalid exception for |y| >= 1L<<63, but y
394 // may be odd unless we know |y| >= 1L<<64.
395 fldl MO(p64) // 1L<<64 : y
397 fcomi %st(1), %st // y : 1L<<64
400 fldl MO(p63) // p63 : y
406 fistpll -8(%rsp) // y
407 fildll -8(%rsp) // int(y) : y
409 ffreep %st // <empty>
412 // OK, the value is an integer, but is it odd?
416 jz 24f // jump if not odd
417 // It's an odd integer.
427 strong_alias (__ieee754_powl, __powl_finite)