1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996, 1997, 1998, 1999, 2001, 2004, 2005, 2007
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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 /* Reactos modifications */
25 #define ALIGNARG(log2) log2
26 #define ASM_TYPE_DIRECTIVE(name,typearg)
27 #define ASM_SIZE_DIRECTIVE(name)
28 #define cfi_adjust_cfa_offset(x)
36 ASM_TYPE_DIRECTIVE(infinity,@object)
40 .byte 0, 0, 0, 0, 0, 0, HEX(f0), HEX(7f)
41 ASM_SIZE_DIRECTIVE(infinity)
42 ASM_TYPE_DIRECTIVE(zero,@object)
45 ASM_SIZE_DIRECTIVE(zero)
46 ASM_TYPE_DIRECTIVE(minf_mzero,@object)
50 .byte 0, 0, 0, 0, 0, 0, HEX(f0), HEX(ff)
53 .byte 0, 0, 0, 0, 0, 0, 0, HEX(80)
54 ASM_SIZE_DIRECTIVE(minf_mzero)
55 ASM_TYPE_DIRECTIVE(one,@object)
59 ASM_SIZE_DIRECTIVE(one)
60 ASM_TYPE_DIRECTIVE(limit,@object)
64 ASM_SIZE_DIRECTIVE(limit)
65 ASM_TYPE_DIRECTIVE(p63,@object)
68 .byte 0, 0, 0, 0, 0, 0, HEX(e0), HEX(43)
69 ASM_SIZE_DIRECTIVE(p63)
72 #define MO(op) op##@GOTOFF(%ecx)
73 #define MOX(op,x,f) op##@GOTOFF(%ecx,x,f)
76 #define MOX(op,x,f) op[x*f]
81 fld qword ptr [esp + 12] // y
91 cmp ah, HEX(040) // is y == 0 ?
94 cmp ah, 5 // is y == ±inf ?
97 cmp ah, 1 // is y == NaN ?
100 fld qword ptr [esp + 4] // x : y
103 cfi_adjust_cfa_offset (8)
117 /* fistpll raises invalid exception for |y| >= 1L<<63. */
120 fcomp qword ptr ds:MO(p63) // y : x
125 /* First see whether `y' is a natural number. In this case we
126 can use a more precise algorithm. */
128 fistp qword ptr [esp] // y : x
129 fild qword ptr [esp] // int(y) : y : x
130 fucomp st(1) // y : x
135 /* OK, we have an integer value for y. */
137 cfi_adjust_cfa_offset (-4)
139 cfi_adjust_cfa_offset (-4)
142 jns L4 // y >= 0, jump
143 fdivr qword ptr MO(one) // 1/x (now referred to as x)
147 L4: fld qword ptr MO(one) // 1 : x
153 fmul st, st(1) // x : ST*x
155 L5: fmul st, st // x*x : ST*x
165 fld qword ptr [esp + 4] // x : y
166 fld qword ptr MO(one) // 1.0 : x : y
167 fucomp st(1) // x : y
175 cfi_adjust_cfa_offset (8)
177 L2: /* y is a real number. */
179 fld qword ptr MO(one) // 1.0 : x : y
180 fld qword ptr MO(limit) // 0.29 : 1.0 : x : y
181 fld st(2) // x : 0.29 : 1.0 : x : y
182 fsub st, st(2) // x-1 : 0.29 : 1.0 : x : y
183 fabs // |x-1| : 0.29 : 1.0 : x : y
184 fucompp // 1.0 : x : y
186 fxch st(1) // x : 1.0 : y
189 fsub st, st(1) // x-1 : 1.0 : y
190 fyl2xp1 // log2(x) : y
193 L7: fyl2x // log2(x) : y
194 L8: fmul st, st(1) // y*log2(x) : y
195 fst st(1) // y*log2(x) : y*log2(x)
196 frndint // int(y*log2(x)) : y*log2(x)
197 fsub st(1), st // int(y*log2(x)) : fract(y*log2(x))
198 fxch // fract(y*log2(x)) : int(y*log2(x))
199 f2xm1 // 2^fract(y*log2(x))-1 : int(y*log2(x))
200 fadd qword ptr MO(one) // 2^fract(y*log2(x)) : int(y*log2(x))
201 fscale // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
203 cfi_adjust_cfa_offset (-8)
204 fstp st(1) // 2^fract(y*log2(x))*2^int(y*log2(x))
210 L11:fstp st(0) // pop y
211 fld qword ptr MO(one)
216 L12: fstp st(0) // pop y
217 fld qword ptr MO(one) // 1
218 fld qword ptr [esp + 4] // x : 1
220 fucompp // < 1, == 1, or > 1
224 je L13 // jump if x is NaN
227 je L14 // jump if |x| == 1
232 fld qword ptr MOX(inf_zero, edx, 4)
236 L14:fld qword ptr MO(one)
240 L13:fld qword ptr [esp + 4] // load x == NaN
243 cfi_adjust_cfa_offset (8)
248 jz L16 // jump if x == +inf
250 // We must find out whether y is an odd integer.
252 fistp qword ptr [esp] // y
253 fild qword ptr [esp] // int(y) : y
259 // OK, the value is an integer, but is the number of bits small
260 // enough so that all are coming from the mantissa?
262 cfi_adjust_cfa_offset (-4)
264 cfi_adjust_cfa_offset (-4)
266 jz L18 // jump if not odd
272 cmp eax, HEX(000200000)
273 ja L18 // does not fit in mantissa bits
274 // It's an odd integer.
276 fld qword ptr MOX(minf_mzero, edx, 8)
279 cfi_adjust_cfa_offset (8)
281 L16:fcomp qword ptr ds:MO(zero)
283 cfi_adjust_cfa_offset (-8)
287 fld qword ptr MOX(inf_zero, eax, 1)
290 cfi_adjust_cfa_offset (8)
292 L17: shl edx, 30 // sign bit for y in right position
294 cfi_adjust_cfa_offset (-8)
296 fld qword ptr MOX(inf_zero, edx, 8)
299 cfi_adjust_cfa_offset (8)
306 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
311 fistp qword ptr [esp] // y
312 fild qword ptr [esp] // int(y) : y
318 // OK, the value is an integer, but is the number of bits small
319 // enough so that all are coming from the mantissa?
321 cfi_adjust_cfa_offset (-4)
323 cfi_adjust_cfa_offset (-4)
325 jz L27 // jump if not odd
326 cmp edx, HEX(0ffe00000)
327 jbe L27 // does not fit in mantissa bits
328 // It's an odd integer.
329 // Raise divide-by-zero exception and get minus infinity value.
330 fld qword ptr MO(one)
331 fdiv qword ptr MO(zero)
335 cfi_adjust_cfa_offset (8)
338 cfi_adjust_cfa_offset (-8)
339 L27: // Raise divide-by-zero exception and get infinity value.
340 fld qword ptr MO(one)
341 fdiv qword ptr MO(zero)
344 cfi_adjust_cfa_offset (8)
346 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
351 fistp qword ptr [esp] // y
352 fild qword ptr [esp] // int(y) : y
358 // OK, the value is an integer, but is the number of bits small
359 // enough so that all are coming from the mantissa?
361 cfi_adjust_cfa_offset (-4)
363 cfi_adjust_cfa_offset (-4)
365 jz L24 // jump if not odd
366 cmp edx, HEX(0ffe00000)
367 jae L24 // does not fit in mantissa bits
368 // It's an odd integer.
369 fld qword ptr MO(mzero)
372 cfi_adjust_cfa_offset (8)
374 L23: add esp, 8 // Don't use 2 x pop
375 cfi_adjust_cfa_offset (-8)
376 L24: fld qword ptr MO(zero)