1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-2015 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)
30 ASM_SIZE_DIRECTIVE(limit)
32 p63: .byte 0, 0, 0, 0, 0, 0, 0xe0, 0x43
33 ASM_SIZE_DIRECTIVE(p63)
35 p10: .byte 0, 0, 0, 0, 0, 0, 0x90, 0x40
36 ASM_SIZE_DIRECTIVE(p10)
38 .section .rodata.cst16,"aM",@progbits,16
41 .type infinity,@object
44 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
45 ASM_SIZE_DIRECTIVE(infinity)
48 ASM_SIZE_DIRECTIVE(zero)
49 .type minf_mzero,@object
52 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
54 .byte 0, 0, 0, 0, 0, 0, 0, 0x80
55 ASM_SIZE_DIRECTIVE(minf_mzero)
58 # define MO(op) op##@GOTOFF(%ecx)
59 # define MOX(op,x,f) op##@GOTOFF(%ecx,x,f)
62 # define MOX(op,x,f) op(,x,f)
77 cmpb $0x40, %ah // is y == 0 ?
80 cmpb $0x05, %ah // is y == ±inf ?
83 cmpb $0x01, %ah // is y == NaN ?
89 cfi_adjust_cfa_offset (8)
106 /* fistpll raises invalid exception for |y| >= 1L<<63. */
109 fcompl MO(p63) // y : x
114 /* First see whether `y' is a natural number. In this case we
115 can use a more precise algorithm. */
117 fistpll (%esp) // y : x
118 fildll (%esp) // int(y) : y : x
119 fucomp %st(1) // y : x
124 /* OK, we have an integer value for y. If large enough that
125 errors may propagate out of the 11 bits excess precision, use
126 the algorithm for real exponent instead. */
129 fcompl MO(p10) // y : x
134 cfi_adjust_cfa_offset (-4)
136 cfi_adjust_cfa_offset (-4)
139 jns 4f // y >= 0, jump
140 fdivrl MO(one) // 1/x (now referred to as x)
144 4: fldl MO(one) // 1 : x
147 /* If y is even, take the absolute value of x. Otherwise,
148 ensure all intermediate values that might overflow have the
154 6: shrdl $1, %edx, %eax
158 fmul %st(1) // x : ST*x
160 5: fld %st // x : x : ST*x
161 fabs // |x| : x : ST*x
162 fmulp // |x|*x : ST*x
171 30: fldl 4(%esp) // x : y
172 fldl MO(one) // 1.0 : x : y
173 fucomp %st(1) // x : y
181 cfi_adjust_cfa_offset (8)
183 cfi_adjust_cfa_offset (-8)
187 cfi_adjust_cfa_offset (8)
189 2: // y is a large integer (absolute value at least 1L<<10), but
190 // may be odd unless at least 1L<<64. So it may be necessary
191 // to adjust the sign of a negative result afterwards.
196 3: /* y is a real number. */
198 fldl MO(one) // 1.0 : x : y
199 fldl MO(limit) // 0.29 : 1.0 : x : y
200 fld %st(2) // x : 0.29 : 1.0 : x : y
201 fsub %st(2) // x-1 : 0.29 : 1.0 : x : y
202 fabs // |x-1| : 0.29 : 1.0 : x : y
203 fucompp // 1.0 : x : y
208 fsub %st(1) // x-1 : 1.0 : y
209 fyl2xp1 // log2(x) : y
212 7: fyl2x // log2(x) : y
213 8: fmul %st(1) // y*log2(x) : y
214 fst %st(1) // y*log2(x) : y*log2(x)
215 frndint // int(y*log2(x)) : y*log2(x)
216 fsubr %st, %st(1) // int(y*log2(x)) : fract(y*log2(x))
217 fxch // fract(y*log2(x)) : int(y*log2(x))
218 f2xm1 // 2^fract(y*log2(x))-1 : int(y*log2(x))
219 faddl MO(one) // 2^fract(y*log2(x)) : int(y*log2(x))
221 // Before scaling, we must negate if x is negative and y is an
225 // x is negative. If y is an odd integer, negate the result.
226 fldl 20(%esp) // y : 2^fract(y*log2(x)) : int(y*log2(x))
227 fld %st // y : y : 2^fract(y*log2(x)) : int(y*log2(x))
228 fabs // |y| : y : 2^fract(y*log2(x)) : int(y*log2(x))
229 fcompl MO(p63) // y : 2^fract(y*log2(x)) : int(y*log2(x))
234 // We must find out whether y is an odd integer.
235 fld %st // y : y : 2^fract(y*log2(x)) : int(y*log2(x))
236 fistpll (%esp) // y : 2^fract(y*log2(x)) : int(y*log2(x))
237 fildll (%esp) // int(y) : y : 2^fract(y*log2(x)) : int(y*log2(x))
238 fucompp // 2^fract(y*log2(x)) : int(y*log2(x))
243 // OK, the value is an integer, but is it odd?
245 cfi_adjust_cfa_offset (-4)
247 cfi_adjust_cfa_offset (-4)
249 jz 292f // jump if not odd
250 // It's an odd integer.
254 cfi_adjust_cfa_offset (8)
255 290: fstp %st(0) // 2^fract(y*log2(x)) : int(y*log2(x))
257 cfi_adjust_cfa_offset (-8)
258 292: fscale // +/- 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
259 fstp %st(1) // +/- 2^fract(y*log2(x))*2^int(y*log2(x))
265 11: fstp %st(0) // pop y
271 12: fstp %st(0) // pop y
273 fldl 4(%esp) // x : 1
275 fucompp // < 1, == 1, or > 1
279 je 13f // jump if x is NaN
282 je 14f // jump if |x| == 1
287 fldl MOX(inf_zero, %edx, 4)
295 13: fldl 4(%esp) // load x == NaN
298 cfi_adjust_cfa_offset (8)
303 jz 16f // jump if x == +inf
305 // fistpll raises invalid exception for |y| >= 1L<<63, so test
306 // that (in which case y is certainly even) before testing
315 // We must find out whether y is an odd integer.
318 fildll (%esp) // int(y) : y
324 // OK, the value is an integer.
326 cfi_adjust_cfa_offset (-4)
328 cfi_adjust_cfa_offset (-4)
330 jz 18f // jump if not odd
331 // It's an odd integer.
333 fldl MOX(minf_mzero, %edx, 8)
336 cfi_adjust_cfa_offset (8)
340 cfi_adjust_cfa_offset (-8)
344 fldl MOX(inf_zero, %eax, 1)
347 cfi_adjust_cfa_offset (8)
349 17: shll $30, %edx // sign bit for y in right position
351 cfi_adjust_cfa_offset (-8)
353 fldl MOX(inf_zero, %edx, 8)
356 cfi_adjust_cfa_offset (8)
363 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
367 // fistpll raises invalid exception for |y| >= 1L<<63, so test
368 // that (in which case y is certainly even) before testing
379 fildll (%esp) // int(y) : y
385 // OK, the value is an integer.
387 cfi_adjust_cfa_offset (-4)
389 cfi_adjust_cfa_offset (-4)
391 jz 27f // jump if not odd
392 // It's an odd integer.
393 // Raise divide-by-zero exception and get minus infinity value.
399 cfi_adjust_cfa_offset (8)
402 cfi_adjust_cfa_offset (-8)
403 27: // Raise divide-by-zero exception and get infinity value.
408 cfi_adjust_cfa_offset (8)
410 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
414 // fistpll raises invalid exception for |y| >= 1L<<63, so test
415 // that (in which case y is certainly even) before testing
424 fildll (%esp) // int(y) : y
430 // OK, the value is an integer.
432 cfi_adjust_cfa_offset (-4)
434 cfi_adjust_cfa_offset (-4)
436 jz 24f // jump if not odd
437 // It's an odd integer.
441 cfi_adjust_cfa_offset (8)
443 23: addl $8, %esp // Don't use 2 x pop
444 cfi_adjust_cfa_offset (-8)
449 strong_alias (__ieee754_pow, __pow_finite)