1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-1999, 2001, 2004-2005, 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##@GOTOFF(%ecx)
63 # define MOX(op,x,f) op##@GOTOFF(%ecx,x,f)
66 # define MOX(op,x,f) op(,x,f)
81 cmpb $0x40, %ah // is y == 0 ?
84 cmpb $0x05, %ah // is y == ±inf ?
87 cmpb $0x01, %ah // is y == NaN ?
93 cfi_adjust_cfa_offset (8)
107 /* fistpll raises invalid exception for |y| >= 1L<<63. */
110 fcompl MO(p63) // y : x
115 /* First see whether `y' is a natural number. In this case we
116 can use a more precise algorithm. */
118 fistpll (%esp) // y : x
119 fildll (%esp) // int(y) : y : x
120 fucomp %st(1) // y : x
125 /* OK, we have an integer value for y. */
127 cfi_adjust_cfa_offset (-4)
129 cfi_adjust_cfa_offset (-4)
132 jns 4f // y >= 0, jump
133 fdivrl MO(one) // 1/x (now referred to as x)
137 4: fldl MO(one) // 1 : x
140 6: shrdl $1, %edx, %eax
143 fmul %st(1) // x : ST*x
145 5: fmul %st(0), %st // x*x : ST*x
154 30: fldt 4(%esp) // x : y
155 fldl MO(one) // 1.0 : x : y
156 fucomp %st(1) // x : y
164 cfi_adjust_cfa_offset (8)
166 2: // y is a large integer (absolute value at least 1L<<63), but
167 // may be odd unless at least 1L<<64. So it may be necessary
168 // to adjust the sign of a negative result afterwards.
172 // If y has absolute value at least 1L<<78, then any finite
173 // nonzero x will result in 0 (underflow), 1 or infinity (overflow).
174 // Saturate y to those bounds to avoid overflow in the calculation
176 fld %st // y : y : |x|
177 fabs // |y| : y : |x|
178 fcompl MO(p78) // y : |x|
183 fldl MO(p78) // 1L<<78 : |x|
186 fchs // -(1L<<78) : |x|
188 3: /* y is a real number. */
190 fldl MO(one) // 1.0 : x : y
191 fldl MO(limit) // 0.29 : 1.0 : x : y
192 fld %st(2) // x : 0.29 : 1.0 : x : y
193 fsub %st(2) // x-1 : 0.29 : 1.0 : x : y
194 fabs // |x-1| : 0.29 : 1.0 : x : y
195 fucompp // 1.0 : x : y
200 fsub %st(1) // x-1 : 1.0 : y
201 fyl2xp1 // log2(x) : y
204 7: fyl2x // log2(x) : y
205 8: fmul %st(1) // y*log2(x) : y
206 fst %st(1) // y*log2(x) : y*log2(x)
207 frndint // int(y*log2(x)) : y*log2(x)
208 fsubr %st, %st(1) // int(y*log2(x)) : fract(y*log2(x))
209 fxch // fract(y*log2(x)) : int(y*log2(x))
210 f2xm1 // 2^fract(y*log2(x))-1 : int(y*log2(x))
211 faddl MO(one) // 2^fract(y*log2(x)) : int(y*log2(x))
212 fscale // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
213 fstp %st(1) // 2^fract(y*log2(x))*2^int(y*log2(x))
216 // x is negative. If y is an odd integer, negate the result.
217 fldt 24(%esp) // y : abs(result)
218 fld %st // y : y : abs(result)
219 fabs // |y| : y : abs(result)
220 fcompl MO(p64) // y : abs(result)
224 fldl MO(p63) // p63 : y : abs(result)
225 fxch // y : p63 : abs(result)
226 fprem // y%p63 : p63 : abs(result)
227 fstp %st(1) // y%p63 : abs(result)
229 // We must find out whether y is an odd integer.
230 fld %st // y : y : abs(result)
231 fistpll (%esp) // y : abs(result)
232 fildll (%esp) // int(y) : y : abs(result)
233 fucompp // abs(result)
238 // OK, the value is an integer, but is it odd?
240 cfi_adjust_cfa_offset (-4)
242 cfi_adjust_cfa_offset (-4)
244 jz 290f // jump if not odd
245 // It's an odd integer.
248 cfi_adjust_cfa_offset (8)
249 291: fstp %st(0) // abs(result)
251 cfi_adjust_cfa_offset (-8)
256 11: fstp %st(0) // pop y
262 12: fstp %st(0) // pop y
264 fldt 4(%esp) // x : 1
266 fucompp // < 1, == 1, or > 1
270 je 13f // jump if x is NaN
273 je 14f // jump if |x| == 1
278 fldl MOX(inf_zero, %edx, 4)
286 13: fldt 4(%esp) // load x == NaN
289 cfi_adjust_cfa_offset (8)
294 jz 16f // jump if x == +inf
296 // fistpll raises invalid exception for |y| >= 1L<<63, but y
297 // may be odd unless we know |y| >= 1L<<64.
304 fldl MO(p63) // p63 : y
309 // We must find out whether y is an odd integer.
312 fildll (%esp) // int(y) : y
318 // OK, the value is an integer, but is it odd?
320 cfi_adjust_cfa_offset (-4)
322 cfi_adjust_cfa_offset (-4)
324 jz 18f // jump if not odd
325 // It's an odd integer.
327 fldl MOX(minf_mzero, %edx, 8)
330 cfi_adjust_cfa_offset (8)
334 cfi_adjust_cfa_offset (-8)
338 fldl MOX(inf_zero, %eax, 1)
341 cfi_adjust_cfa_offset (8)
343 17: shll $30, %edx // sign bit for y in right position
345 cfi_adjust_cfa_offset (-8)
347 fldl MOX(inf_zero, %edx, 8)
350 cfi_adjust_cfa_offset (8)
357 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
361 // fistpll raises invalid exception for |y| >= 1L<<63, but y
362 // may be odd unless we know |y| >= 1L<<64.
369 fldl MO(p63) // p63 : y
376 fildll (%esp) // int(y) : y
382 // OK, the value is an integer, but is it odd?
384 cfi_adjust_cfa_offset (-4)
386 cfi_adjust_cfa_offset (-4)
388 jz 27f // jump if not odd
389 // It's an odd integer.
390 // Raise divide-by-zero exception and get minus infinity value.
396 cfi_adjust_cfa_offset (8)
399 cfi_adjust_cfa_offset (-8)
400 27: // Raise divide-by-zero exception and get infinity value.
405 cfi_adjust_cfa_offset (8)
407 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
411 // fistpll raises invalid exception for |y| >= 1L<<63, but y
412 // may be odd unless we know |y| >= 1L<<64.
418 fldl MO(p63) // p63 : y
425 fildll (%esp) // int(y) : y
431 // OK, the value is an integer, but is it odd?
433 cfi_adjust_cfa_offset (-4)
435 cfi_adjust_cfa_offset (-4)
437 jz 24f // jump if not odd
438 // It's an odd integer.
442 cfi_adjust_cfa_offset (8)
444 23: addl $8, %esp // Don't use 2 x pop
445 cfi_adjust_cfa_offset (-8)
450 strong_alias (__ieee754_powl, __powl_finite)