2.9
[glibc/nacl-glibc.git] / sysdeps / i386 / fpu / e_powf.S
blobc91545418d6142f4e327c54dec592bb953e5ccd6
1 /* ix87 specific implementation of pow function.
2    Copyright (C) 1996, 1997, 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
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         fldl    MO(limit)       // 0.29 : 1.0 : x : y
159         fld     %st(2)          // x : 0.29 : 1.0 : x : y
160         fsub    %st(2)          // x-1 : 0.29 : 1.0 : x : y
161         fabs                    // |x-1| : 0.29 : 1.0 : x : y
162         fucompp                 // 1.0 : x : y
163         fnstsw
164         fxch                    // x : 1.0 : y
165         sahf
166         ja      7f
167         fsub    %st(1)          // x-1 : 1.0 : y
168         fyl2xp1                 // log2(x) : y
169         jmp     8f
171 7:      fyl2x                   // log2(x) : y
172 8:      fmul    %st(1)          // y*log2(x) : y
173         fst     %st(1)          // y*log2(x) : y*log2(x)
174         frndint                 // int(y*log2(x)) : y*log2(x)
175         fsubr   %st, %st(1)     // int(y*log2(x)) : fract(y*log2(x))
176         fxch                    // fract(y*log2(x)) : int(y*log2(x))
177         f2xm1                   // 2^fract(y*log2(x))-1 : int(y*log2(x))
178         faddl   MO(one)         // 2^fract(y*log2(x)) : int(y*log2(x))
179         fscale                  // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
180         addl    $4, %esp
181         cfi_adjust_cfa_offset (-4)
182         fstp    %st(1)          // 2^fract(y*log2(x))*2^int(y*log2(x))
183         ret
186         // pow(x,±0) = 1
187         .align ALIGNARG(4)
188 11:     fstp    %st(0)          // pop y
189         fldl    MO(one)
190         ret
192         // y == ±inf
193         .align ALIGNARG(4)
194 12:     fstp    %st(0)          // pop y
195         fldl    MO(one)         // 1
196         flds    4(%esp)         // x : 1
197         fabs                    // abs(x) : 1
198         fucompp                 // < 1, == 1, or > 1
199         fnstsw
200         andb    $0x45, %ah
201         cmpb    $0x45, %ah
202         je      13f             // jump if x is NaN
204         cmpb    $0x40, %ah
205         je      14f             // jump if |x| == 1
207         shlb    $1, %ah
208         xorb    %ah, %dl
209         andl    $2, %edx
210         fldl    MOX(inf_zero, %edx, 4)
211         ret
213         .align ALIGNARG(4)
214 14:     fldl    MO(one)
215         ret
217         .align ALIGNARG(4)
218 13:     flds    4(%esp)         // load x == NaN
219         ret
221         cfi_adjust_cfa_offset (4)
222         .align ALIGNARG(4)
223         // x is ±inf
224 15:     fstp    %st(0)          // y
225         testb   $2, %dh
226         jz      16f             // jump if x == +inf
228         // We must find out whether y is an odd integer.
229         fld     %st             // y : y
230         fistpl  (%esp)          // y
231         fildl   (%esp)          // int(y) : y
232         fucompp                 // <empty>
233         fnstsw
234         sahf
235         jne     17f
237         // OK, the value is an integer, but is the number of bits small
238         // enough so that all are coming from the mantissa?
239         popl    %edx
240         cfi_adjust_cfa_offset (-4)
241         testb   $1, %dl
242         jz      18f             // jump if not odd
243         movl    %edx, %eax
244         orl     %edx, %edx
245         jns     155f
246         negl    %eax
247 155:    cmpl    $0x01000000, %eax
248         ja      18f             // does not fit in mantissa bits
249         // It's an odd integer.
250         shrl    $31, %edx
251         fldl    MOX(minf_mzero, %edx, 8)
252         ret
254         cfi_adjust_cfa_offset (4)
255         .align ALIGNARG(4)
256 16:     fcompl  MO(zero)
257         addl    $4, %esp
258         cfi_adjust_cfa_offset (-4)
259         fnstsw
260         shrl    $5, %eax
261         andl    $8, %eax
262         fldl    MOX(inf_zero, %eax, 1)
263         ret
265         cfi_adjust_cfa_offset (4)
266         .align ALIGNARG(4)
267 17:     shll    $30, %edx       // sign bit for y in right position
268         addl    $4, %esp
269         cfi_adjust_cfa_offset (-4)
270 18:     shrl    $31, %edx
271         fldl    MOX(inf_zero, %edx, 8)
272         ret
274         cfi_adjust_cfa_offset (4)
275         .align ALIGNARG(4)
276         // x is ±0
277 20:     fstp    %st(0)          // y
278         testb   $2, %dl
279         jz      21f             // y > 0
281         // x is ±0 and y is < 0.  We must find out whether y is an odd integer.
282         testb   $2, %dh
283         jz      25f
285         fld     %st             // y : y
286         fistpl  (%esp)          // y
287         fildl   (%esp)          // int(y) : y
288         fucompp                 // <empty>
289         fnstsw
290         sahf
291         jne     26f
293         // OK, the value is an integer, but is the number of bits small
294         // enough so that all are coming from the mantissa?
295         popl    %edx
296         cfi_adjust_cfa_offset (-4)
297         testb   $1, %dl
298         jz      27f             // jump if not odd
299         cmpl    $0xff000000, %edx
300         jbe     27f             // does not fit in mantissa bits
301         // It's an odd integer.
302         // Raise divide-by-zero exception and get minus infinity value.
303         fldl    MO(one)
304         fdivl   MO(zero)
305         fchs
306         ret
308         cfi_adjust_cfa_offset (4)
309 25:     fstp    %st(0)
310 26:     addl    $4, %esp
311         cfi_adjust_cfa_offset (-4)
312 27:     // Raise divide-by-zero exception and get infinity value.
313         fldl    MO(one)
314         fdivl   MO(zero)
315         ret
317         cfi_adjust_cfa_offset (4)
318         .align ALIGNARG(4)
319         // x is ±0 and y is > 0.  We must find out whether y is an odd integer.
320 21:     testb   $2, %dh
321         jz      22f
323         fld     %st             // y : y
324         fistpl  (%esp)          // y
325         fildl   (%esp)          // int(y) : y
326         fucompp                 // <empty>
327         fnstsw
328         sahf
329         jne     23f
331         // OK, the value is an integer, but is the number of bits small
332         // enough so that all are coming from the mantissa?
333         popl    %edx
334         cfi_adjust_cfa_offset (-4)
335         testb   $1, %dl
336         jz      24f             // jump if not odd
337         cmpl    $0xff000000, %edx
338         jae     24f             // does not fit in mantissa bits
339         // It's an odd integer.
340         fldl    MO(mzero)
341         ret
343         cfi_adjust_cfa_offset (4)
344 22:     fstp    %st(0)
345 23:     addl    $4, %esp        // Don't use pop.
346         cfi_adjust_cfa_offset (-4)
347 24:     fldl    MO(zero)
348         ret
350 END(__ieee754_powf)