[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / sysdeps / i386 / fpu / e_powf.S
blobc835b978b9fce6fc5c95511f523f88331ccd016f
1 /* ix87 specific implementation of pow function.
2    Copyright (C) 1996, 1997, 1999, 2001, 2004, 2005
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
20    02111-1307 USA.  */
22 #include <machine/asm.h>
24 #ifdef __ELF__
25         .section .rodata
26 #else
27         .text
28 #endif
30         .align ALIGNARG(4)
31         ASM_TYPE_DIRECTIVE(infinity,@object)
32 inf_zero:
33 infinity:
34         .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
35         ASM_SIZE_DIRECTIVE(infinity)
36         ASM_TYPE_DIRECTIVE(zero,@object)
37 zero:   .double 0.0
38         ASM_SIZE_DIRECTIVE(zero)
39         ASM_TYPE_DIRECTIVE(minf_mzero,@object)
40 minf_mzero:
41 minfinity:
42         .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
43 mzero:
44         .byte 0, 0, 0, 0, 0, 0, 0, 0x80
45         ASM_SIZE_DIRECTIVE(minf_mzero)
46         ASM_TYPE_DIRECTIVE(one,@object)
47 one:    .double 1.0
48         ASM_SIZE_DIRECTIVE(one)
49         ASM_TYPE_DIRECTIVE(limit,@object)
50 limit:  .double 0.29
51         ASM_SIZE_DIRECTIVE(limit)
52         ASM_TYPE_DIRECTIVE(p31,@object)
53 p31:    .byte 0, 0, 0, 0, 0, 0, 0xe0, 0x41
54         ASM_SIZE_DIRECTIVE(p31)
56 #ifdef PIC
57 #define MO(op) op##@GOTOFF(%ecx)
58 #define MOX(op,x,f) op##@GOTOFF(%ecx,x,f)
59 #else
60 #define MO(op) op
61 #define MOX(op,x,f) op(,x,f)
62 #endif
64         .text
65 ENTRY(__ieee754_powf)
66         flds    8(%esp) // y
67         fxam
69 #ifdef  PIC
70         LOAD_PIC_REG (cx)
71 #endif
73         fnstsw
74         movb    %ah, %dl
75         andb    $0x45, %ah
76         cmpb    $0x40, %ah      // is y == 0 ?
77         je      11f
79         cmpb    $0x05, %ah      // is y == ±inf ?
80         je      12f
82         cmpb    $0x01, %ah      // is y == NaN ?
83         je      30f
85         flds    4(%esp)         // x : y
87         subl    $4, %esp
88         cfi_adjust_cfa_offset (4)
90         fxam
91         fnstsw
92         movb    %ah, %dh
93         andb    $0x45, %ah
94         cmpb    $0x40, %ah
95         je      20f             // x is ±0
97         cmpb    $0x05, %ah
98         je      15f             // x is ±inf
100         fxch                    // y : x
102         /* fistpl raises invalid exception for |y| >= 1L<<31.  */
103         fld     %st             // y : y : x
104         fabs                    // |y| : y : x
105         fcompl  MO(p31)         // y : x
106         fnstsw
107         sahf
108         jnc     2f
110         /* First see whether `y' is a natural number.  In this case we
111            can use a more precise algorithm.  */
112         fld     %st             // y : y : x
113         fistpl  (%esp)          // y : x
114         fildl   (%esp)          // int(y) : y : x
115         fucomp  %st(1)          // y : x
116         fnstsw
117         sahf
118         jne     2f
120         /* OK, we have an integer value for y.  */
121         popl    %edx
122         cfi_adjust_cfa_offset (-4)
123         orl     $0, %edx
124         fstp    %st(0)          // x
125         jns     4f              // y >= 0, jump
126         fdivrl  MO(one)         // 1/x          (now referred to as x)
127         negl    %edx
128 4:      fldl    MO(one)         // 1 : x
129         fxch
131 6:      shrl    $1, %edx
132         jnc     5f
133         fxch
134         fmul    %st(1)          // x : ST*x
135         fxch
136 5:      fmul    %st(0), %st     // x*x : ST*x
137         testl   %edx, %edx
138         jnz     6b
139         fstp    %st(0)          // ST*x
140         ret
142         /* y is ±NAN */
143 30:     flds    4(%esp)         // x : y
144         fldl    MO(one)         // 1.0 : x : y
145         fucomp  %st(1)          // x : y
146         fnstsw
147         sahf
148         je      31f
149         fxch                    // y : x
150 31:     fstp    %st(1)
151         ret
153         cfi_adjust_cfa_offset (4)
154         .align ALIGNARG(4)
155 2:      /* y is a real number.  */
156         fxch                    // x : y
157         fldl    MO(one)         // 1.0 : x : y
158         fld     %st(1)          // x : 1.0 : x : y
159         fsub    %st(1)          // x-1 : 1.0 : x : y
160         fabs                    // |x-1| : 1.0 : x : y
161         fcompl  MO(limit)       // 1.0 : x : y
162         fnstsw
163         fxch                    // x : 1.0 : y
164         sahf
165         ja      7f
166         fsub    %st(1)          // x-1 : 1.0 : y
167         fyl2xp1                 // log2(x) : y
168         jmp     8f
170 7:      fyl2x                   // log2(x) : y
171 8:      fmul    %st(1)          // y*log2(x) : y
172         fst     %st(1)          // y*log2(x) : y*log2(x)
173         frndint                 // int(y*log2(x)) : y*log2(x)
174         fsubr   %st, %st(1)     // int(y*log2(x)) : fract(y*log2(x))
175         fxch                    // fract(y*log2(x)) : int(y*log2(x))
176         f2xm1                   // 2^fract(y*log2(x))-1 : int(y*log2(x))
177         faddl   MO(one)         // 2^fract(y*log2(x)) : int(y*log2(x))
178         fscale                  // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
179         addl    $4, %esp
180         cfi_adjust_cfa_offset (-4)
181         fstp    %st(1)          // 2^fract(y*log2(x))*2^int(y*log2(x))
182         ret
185         // pow(x,±0) = 1
186         .align ALIGNARG(4)
187 11:     fstp    %st(0)          // pop y
188         fldl    MO(one)
189         ret
191         // y == ±inf
192         .align ALIGNARG(4)
193 12:     fstp    %st(0)          // pop y
194         flds    4(%esp)         // x
195         fabs
196         fcompl  MO(one)         // < 1, == 1, or > 1
197         fnstsw
198         andb    $0x45, %ah
199         cmpb    $0x45, %ah
200         je      13f             // jump if x is NaN
202         cmpb    $0x40, %ah
203         je      14f             // jump if |x| == 1
205         shlb    $1, %ah
206         xorb    %ah, %dl
207         andl    $2, %edx
208         fldl    MOX(inf_zero, %edx, 4)
209         ret
211         .align ALIGNARG(4)
212 14:     fldl    MO(one)
213         ret
215         .align ALIGNARG(4)
216 13:     flds    4(%esp)         // load x == NaN
217         ret
219         cfi_adjust_cfa_offset (4)
220         .align ALIGNARG(4)
221         // x is ±inf
222 15:     fstp    %st(0)          // y
223         testb   $2, %dh
224         jz      16f             // jump if x == +inf
226         // We must find out whether y is an odd integer.
227         fld     %st             // y : y
228         fistpl  (%esp)          // y
229         fildl   (%esp)          // int(y) : y
230         fucompp                 // <empty>
231         fnstsw
232         sahf
233         jne     17f
235         // OK, the value is an integer, but is the number of bits small
236         // enough so that all are coming from the mantissa?
237         popl    %edx
238         cfi_adjust_cfa_offset (-4)
239         testb   $1, %dl
240         jz      18f             // jump if not odd
241         movl    %edx, %eax
242         orl     %edx, %edx
243         jns     155f
244         negl    %eax
245 155:    cmpl    $0x01000000, %eax
246         ja      18f             // does not fit in mantissa bits
247         // It's an odd integer.
248         shrl    $31, %edx
249         fldl    MOX(minf_mzero, %edx, 8)
250         ret
252         cfi_adjust_cfa_offset (4)
253         .align ALIGNARG(4)
254 16:     fcompl  MO(zero)
255         addl    $4, %esp
256         cfi_adjust_cfa_offset (-4)
257         fnstsw
258         shrl    $5, %eax
259         andl    $8, %eax
260         fldl    MOX(inf_zero, %eax, 1)
261         ret
263         cfi_adjust_cfa_offset (4)
264         .align ALIGNARG(4)
265 17:     shll    $30, %edx       // sign bit for y in right position
266         addl    $4, %esp
267         cfi_adjust_cfa_offset (-4)
268 18:     shrl    $31, %edx
269         fldl    MOX(inf_zero, %edx, 8)
270         ret
272         cfi_adjust_cfa_offset (4)
273         .align ALIGNARG(4)
274         // x is ±0
275 20:     fstp    %st(0)          // y
276         testb   $2, %dl
277         jz      21f             // y > 0
279         // x is ±0 and y is < 0.  We must find out whether y is an odd integer.
280         testb   $2, %dh
281         jz      25f
283         fld     %st             // y : y
284         fistpl  (%esp)          // y
285         fildl   (%esp)          // int(y) : y
286         fucompp                 // <empty>
287         fnstsw
288         sahf
289         jne     26f
291         // OK, the value is an integer, but is the number of bits small
292         // enough so that all are coming from the mantissa?
293         popl    %edx
294         cfi_adjust_cfa_offset (-4)
295         testb   $1, %dl
296         jz      27f             // jump if not odd
297         cmpl    $0xff000000, %edx
298         jbe     27f             // does not fit in mantissa bits
299         // It's an odd integer.
300         // Raise divide-by-zero exception and get minus infinity value.
301         fldl    MO(one)
302         fdivl   MO(zero)
303         fchs
304         ret
306         cfi_adjust_cfa_offset (4)
307 25:     fstp    %st(0)
308 26:     addl    $4, %esp
309         cfi_adjust_cfa_offset (-4)
310 27:     // Raise divide-by-zero exception and get infinity value.
311         fldl    MO(one)
312         fdivl   MO(zero)
313         ret
315         cfi_adjust_cfa_offset (4)
316         .align ALIGNARG(4)
317         // x is ±0 and y is > 0.  We must find out whether y is an odd integer.
318 21:     testb   $2, %dh
319         jz      22f
321         fld     %st             // y : y
322         fistpl  (%esp)          // y
323         fildl   (%esp)          // int(y) : y
324         fucompp                 // <empty>
325         fnstsw
326         sahf
327         jne     23f
329         // OK, the value is an integer, but is the number of bits small
330         // enough so that all are coming from the mantissa?
331         popl    %edx
332         cfi_adjust_cfa_offset (-4)
333         testb   $1, %dl
334         jz      24f             // jump if not odd
335         cmpl    $0xff000000, %edx
336         jae     24f             // does not fit in mantissa bits
337         // It's an odd integer.
338         fldl    MO(mzero)
339         ret
341         cfi_adjust_cfa_offset (4)
342 22:     fstp    %st(0)
343 23:     addl    $4, %esp        // Don't use pop.
344         cfi_adjust_cfa_offset (-4)
345 24:     fldl    MO(zero)
346         ret
348 END(__ieee754_powf)