1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-2018 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>
21 #include <i386-math-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 p10: .byte 0, 0, 0, 0, 0, 0, 0x90, 0x40
37 ASM_SIZE_DIRECTIVE(p10)
39 .section .rodata.cst16,"aM",@progbits,16
42 .type infinity,@object
45 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
46 ASM_SIZE_DIRECTIVE(infinity)
49 ASM_SIZE_DIRECTIVE(zero)
50 .type minf_mzero,@object
53 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
55 .byte 0, 0, 0, 0, 0, 0, 0, 0x80
56 ASM_SIZE_DIRECTIVE(minf_mzero)
60 # define MO(op) op##@GOTOFF(%ecx)
61 # define MOX(op,x,f) op##@GOTOFF(%ecx,x,f)
64 # define MOX(op,x,f) op(,x,f)
79 cmpb $0x40, %ah // is y == 0 ?
82 cmpb $0x05, %ah // is y == ±inf ?
85 cmpb $0x01, %ah // is y == NaN ?
91 cfi_adjust_cfa_offset (8)
108 /* fistpll raises invalid exception for |y| >= 1L<<63. */
111 fcompl MO(p63) // y : x
116 /* First see whether `y' is a natural number. In this case we
117 can use a more precise algorithm. */
119 fistpll (%esp) // y : x
120 fildll (%esp) // int(y) : y : x
121 fucomp %st(1) // y : x
126 /* OK, we have an integer value for y. If large enough that
127 errors may propagate out of the 11 bits excess precision, use
128 the algorithm for real exponent instead. */
131 fcompl MO(p10) // y : x
136 cfi_adjust_cfa_offset (-4)
138 cfi_adjust_cfa_offset (-4)
141 jns 4f // y >= 0, jump
142 fdivrl MO(one) // 1/x (now referred to as x)
146 4: fldl MO(one) // 1 : x
149 /* If y is even, take the absolute value of x. Otherwise,
150 ensure all intermediate values that might overflow have the
156 6: shrdl $1, %edx, %eax
160 fmul %st(1) // x : ST*x
162 5: fld %st // x : x : ST*x
163 fabs // |x| : x : ST*x
164 fmulp // |x|*x : ST*x
173 DBL_NARROW_EVAL_UFLOW_NONNAN
177 30: fldl 4(%esp) // x : y
178 fldl MO(one) // 1.0 : x : y
179 fucomp %st(1) // x : y
187 cfi_adjust_cfa_offset (8)
189 cfi_adjust_cfa_offset (-8)
193 cfi_adjust_cfa_offset (8)
195 2: // y is a large integer (absolute value at least 1L<<10), but
196 // may be odd unless at least 1L<<64. So it may be necessary
197 // to adjust the sign of a negative result afterwards.
202 3: /* y is a real number. */
204 fldl MO(one) // 1.0 : x : y
205 fldl MO(limit) // 0.29 : 1.0 : x : y
206 fld %st(2) // x : 0.29 : 1.0 : x : y
207 fsub %st(2) // x-1 : 0.29 : 1.0 : x : y
208 fabs // |x-1| : 0.29 : 1.0 : x : y
209 fucompp // 1.0 : x : y
214 fsub %st(1) // x-1 : 1.0 : y
215 fyl2xp1 // log2(x) : y
218 7: fyl2x // log2(x) : y
219 8: fmul %st(1) // y*log2(x) : y
220 fst %st(1) // y*log2(x) : y*log2(x)
221 frndint // int(y*log2(x)) : y*log2(x)
222 fsubr %st, %st(1) // int(y*log2(x)) : fract(y*log2(x))
223 fxch // fract(y*log2(x)) : int(y*log2(x))
224 f2xm1 // 2^fract(y*log2(x))-1 : int(y*log2(x))
225 faddl MO(one) // 2^fract(y*log2(x)) : int(y*log2(x))
227 // Before scaling, we must negate if x is negative and y is an
231 // x is negative. If y is an odd integer, negate the result.
232 fldl 20(%esp) // y : 2^fract(y*log2(x)) : int(y*log2(x))
233 fld %st // y : y : 2^fract(y*log2(x)) : int(y*log2(x))
234 fabs // |y| : y : 2^fract(y*log2(x)) : int(y*log2(x))
235 fcompl MO(p63) // y : 2^fract(y*log2(x)) : int(y*log2(x))
240 // We must find out whether y is an odd integer.
241 fld %st // y : y : 2^fract(y*log2(x)) : int(y*log2(x))
242 fistpll (%esp) // y : 2^fract(y*log2(x)) : int(y*log2(x))
243 fildll (%esp) // int(y) : y : 2^fract(y*log2(x)) : int(y*log2(x))
244 fucompp // 2^fract(y*log2(x)) : int(y*log2(x))
249 // OK, the value is an integer, but is it odd?
251 cfi_adjust_cfa_offset (-4)
253 cfi_adjust_cfa_offset (-4)
255 jz 292f // jump if not odd
256 // It's an odd integer.
260 cfi_adjust_cfa_offset (8)
261 290: fstp %st(0) // 2^fract(y*log2(x)) : int(y*log2(x))
263 cfi_adjust_cfa_offset (-8)
264 292: fscale // +/- 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
265 fstp %st(1) // +/- 2^fract(y*log2(x))*2^int(y*log2(x))
266 DBL_NARROW_EVAL_UFLOW_NONNAN
272 11: fstp %st(0) // pop y
278 12: fstp %st(0) // pop y
280 fldl 4(%esp) // x : 1
282 fucompp // < 1, == 1, or > 1
286 je 13f // jump if x is NaN
289 je 14f // jump if |x| == 1
294 fldl MOX(inf_zero, %edx, 4)
302 13: fldl 4(%esp) // load x == NaN
305 cfi_adjust_cfa_offset (8)
310 jz 16f // jump if x == +inf
312 // fistpll raises invalid exception for |y| >= 1L<<63, so test
313 // that (in which case y is certainly even) before testing
322 // We must find out whether y is an odd integer.
325 fildll (%esp) // int(y) : y
331 // OK, the value is an integer.
333 cfi_adjust_cfa_offset (-4)
335 cfi_adjust_cfa_offset (-4)
337 jz 18f // jump if not odd
338 // It's an odd integer.
340 fldl MOX(minf_mzero, %edx, 8)
343 cfi_adjust_cfa_offset (8)
347 cfi_adjust_cfa_offset (-8)
351 fldl MOX(inf_zero, %eax, 1)
354 cfi_adjust_cfa_offset (8)
356 17: shll $30, %edx // sign bit for y in right position
358 cfi_adjust_cfa_offset (-8)
360 fldl MOX(inf_zero, %edx, 8)
363 cfi_adjust_cfa_offset (8)
370 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
374 // fistpll raises invalid exception for |y| >= 1L<<63, so test
375 // that (in which case y is certainly even) before testing
386 fildll (%esp) // int(y) : y
392 // OK, the value is an integer.
394 cfi_adjust_cfa_offset (-4)
396 cfi_adjust_cfa_offset (-4)
398 jz 27f // jump if not odd
399 // It's an odd integer.
400 // Raise divide-by-zero exception and get minus infinity value.
406 cfi_adjust_cfa_offset (8)
409 cfi_adjust_cfa_offset (-8)
410 27: // Raise divide-by-zero exception and get infinity value.
415 cfi_adjust_cfa_offset (8)
417 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
421 // fistpll raises invalid exception for |y| >= 1L<<63, so test
422 // that (in which case y is certainly even) before testing
431 fildll (%esp) // int(y) : y
437 // OK, the value is an integer.
439 cfi_adjust_cfa_offset (-4)
441 cfi_adjust_cfa_offset (-4)
443 jz 24f // jump if not odd
444 // It's an odd integer.
448 cfi_adjust_cfa_offset (8)
450 23: addl $8, %esp // Don't use 2 x pop
451 cfi_adjust_cfa_offset (-8)
456 strong_alias (__ieee754_pow, __pow_finite)