Update.
[glibc.git] / sysdeps / libm-i387 / e_powf.S
blobd7342bf56f6cd4bc7b0ed4d4d362fec704126a20
1 /* ix87 specific implementation of pow function.
2    Copyright (C) 1996, 1997 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 Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    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    Library General Public License for more details.
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
21 #include <machine/asm.h>
23 #ifdef __ELF__
24         .section .rodata
25 #else
26         .text
27 #endif
29         .align ALIGNARG(4)
30         ASM_TYPE_DIRECTIVE(infinity,@object)
31 inf_zero:
32 infinity:
33         .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
34         ASM_SIZE_DIRECTIVE(infinity)
35         ASM_TYPE_DIRECTIVE(zero,@object)
36 zero:   .double 0.0
37         ASM_SIZE_DIRECTIVE(zero)
38         ASM_TYPE_DIRECTIVE(minf_mzero,@object)
39 minf_mzero:
40 minfinity:
41         .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
42 mzero:
43         .byte 0, 0, 0, 0, 0, 0, 0, 0x80
44         ASM_SIZE_DIRECTIVE(minf_mzero)
45         ASM_TYPE_DIRECTIVE(one,@object)
46 one:    .double 1.0
47         ASM_SIZE_DIRECTIVE(one)
48         ASM_TYPE_DIRECTIVE(limit,@object)
49 limit:  .double 0.29
50         ASM_SIZE_DIRECTIVE(limit)
52 #ifdef PIC
53 #define MO(op) op##@GOTOFF(%ecx)
54 #define MOX(op,x,f) op##@GOTOFF(%ecx,x,f)
55 #else
56 #define MO(op) op
57 #define MOX(op,x,f) op(,x,f)
58 #endif
60         .text
61 ENTRY(__ieee754_powf)
62         flds    8(%esp) // y
63         fxam
65 #ifdef  PIC
66         call    1f
67 1:      popl    %ecx
68         addl    $_GLOBAL_OFFSET_TABLE_+[.-1b], %ecx
69 #endif
71         fnstsw
72         movb    %ah, %dl
73         andb    $0x45, %ah
74         cmpb    $0x40, %ah      // is y == 0 ?
75         je      11f
77         cmpb    $0x05, %ah      // is y == ±inf ?
78         je      12f
80         cmpb    $0x01, %ah      // is y == NaN ?
81         je      30f
83         flds    4(%esp)         // x : y
85         subl    $4, %esp
87         fxam
88         fnstsw
89         movb    %ah, %dh
90         andb    $0x45, %ah
91         cmpb    $0x40, %ah
92         je      20f             // x is ±0
94         cmpb    $0x05, %ah
95         je      15f             // x is ±inf
97         fxch                    // y : x
99         /* First see whether `y' is a natural number.  In this case we
100            can use a more precise algorithm.  */
101         fld     %st             // y : y : x
102         fistpl  (%esp)          // y : x
103         fildl   (%esp)          // int(y) : y : x
104         fucomp  %st(1)          // y : x
105         fnstsw
106         sahf
107         jne     2f
109         /* OK, we have an integer value for y.  */
110         popl    %edx
111         orl     $0, %edx
112         fstp    %st(0)          // x
113         jns     4f              // y >= 0, jump
114         fdivrl  MO(one)         // 1/x          (now referred to as x)
115         negl    %edx
116 4:      fldl    MO(one)         // 1 : x
117         fxch
119 6:      shrl    $1, %edx
120         jnc     5f
121         fxch
122         fmul    %st(1)          // x : ST*x
123         fxch
124 5:      fmul    %st(0), %st     // x*x : ST*x
125         testl   %edx, %edx
126         jnz     6b
127         fstp    %st(0)          // ST*x
128 30:     ret
130         .align ALIGNARG(4)
131 2:      /* y is a real number.  */
132         fxch                    // x : y
133         fldl    MO(one)         // 1.0 : x : y
134         fld     %st(1)          // x : 1.0 : x : y
135         fsub    %st(1)          // x-1 : 1.0 : x : y
136         fabs                    // |x-1| : 1.0 : x : y
137         fcompl  MO(limit)       // 1.0 : x : y
138         fnstsw
139         fxch                    // x : 1.0 : y
140         sahf
141         ja      7f
142         fsub    %st(1)          // x-1 : 1.0 : y
143         fyl2xp1                 // log2(x) : y
144         jmp     8f
146 7:      fyl2x                   // log2(x) : y
147 8:      fmul    %st(1)          // y*log2(x) : y
148         fst     %st(1)          // y*log2(x) : y*log2(x)
149         frndint                 // int(y*log2(x)) : y*log2(x)
150         fsubr   %st, %st(1)     // int(y*log2(x)) : fract(y*log2(x))
151         fxch                    // fract(y*log2(x)) : int(y*log2(x))
152         f2xm1                   // 2^fract(y*log2(x))-1 : int(y*log2(x))
153         faddl   MO(one)         // 2^fract(y*log2(x)) : int(y*log2(x))
154         fscale                  // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
155         addl    $4, %esp
156         fstp    %st(1)          // 2^fract(y*log2(x))*2^int(y*log2(x))
157         ret
160         // pow(x,±0) = 1
161         .align ALIGNARG(4)
162 11:     fstp    %st(0)          // pop y
163         fldl    MO(one)
164         ret
166         // y == ±inf
167         .align ALIGNARG(4)
168 12:     fstp    %st(0)          // pop y
169         flds    4(%esp)         // x
170         fabs
171         fcompl  MO(one)         // < 1, == 1, or > 1
172         fnstsw
173         andb    $0x45, %ah
174         cmpb    $0x45, %ah
175         je      13f             // jump if x is NaN
177         cmpb    $0x40, %ah
178         je      14f             // jump if |x| == 1
180         shlb    $1, %ah
181         xorb    %ah, %dl
182         andl    $2, %edx
183         fldl    MOX(inf_zero, %edx, 4)
184         ret
186         .align ALIGNARG(4)
187 14:     fldl    MO(infinity)
188         fmull   MO(zero)        // raise invalid exception
189         ret
191         .align ALIGNARG(4)
192 13:     flds    4(%esp)         // load x == NaN
193         ret
195         .align ALIGNARG(4)
196         // x is ±inf
197 15:     fstp    %st(0)          // y
198         testb   $2, %dh
199         jz      16f             // jump if x == +inf
201         // We must find out whether y is an odd integer.
202         fld     %st             // y : y
203         fistpl  (%esp)          // y
204         fildl   (%esp)          // int(y) : y
205         fucompp                 // <empty>
206         fnstsw
207         sahf
208         jne     17f
210         // OK, the value is an integer, but is the number of bits small
211         // enough so that all are coming from the mantissa?
212         popl    %edx
213         testb   $1, %dl
214         jz      18f             // jump if not odd
215         movl    %edx, %eax
216         orl     %edx, %edx
217         jns     155f
218         negl    %eax
219 155:    cmpl    $0x01000000, %eax
220         ja      18f             // does not fit in mantissa bits
221         // It's an odd integer.
222         shrl    $31, %edx
223         fldl    MOX(minf_mzero, %edx, 8)
224         ret
226         .align ALIGNARG(4)
227 16:     fcompl  MO(zero)
228         addl    $4, %esp
229         fnstsw
230         shrl    $5, %eax
231         andl    $8, %eax
232         fldl    MOX(inf_zero, %eax, 1)
233         ret
235         .align ALIGNARG(4)
236 17:     shll    $30, %edx       // sign bit for y in right position
237         addl    $4, %esp
238 18:     shrl    $31, %edx
239         fldl    MOX(inf_zero, %edx, 8)
240         ret
242         .align ALIGNARG(4)
243         // x is ±0
244 20:     fstp    %st(0)          // y
245         testb   $2, %dl
246         jz      21f             // y > 0
248         // x is ±0 and y is < 0.  We must find out whether y is an odd integer.
249         testb   $2, %dh
250         jz      25f
252         fld     %st             // y : y
253         fistpl  (%esp)          // y
254         fildl   (%esp)          // int(y) : y
255         fucompp                 // <empty>
256         fnstsw
257         sahf
258         jne     26f
260         // OK, the value is an integer, but is the number of bits small
261         // enough so that all are coming from the mantissa?
262         popl    %edx
263         testb   $1, %dl
264         jz      27f             // jump if not odd
265         cmpl    $0xff000000, %edx
266         jbe     27f             // does not fit in mantissa bits
267         // It's an odd integer.
268         // Raise divide-by-zero exception and get minus infinity value.
269         fldl    MO(one)
270         fdivl   MO(zero)
271         fchs
272         ret
274 25:     fstp    %st(0)
275 26:     popl    %eax
276 27:     // Raise divide-by-zero exception and get infinity value.
277         fldl    MO(one)
278         fdivl   MO(zero)
279         ret
281         .align ALIGNARG(4)
282         // x is ±0 and y is > 0.  We must find out whether y is an odd integer.
283 21:     testb   $2, %dh
284         jz      22f
286         fld     %st             // y : y
287         fistpl  (%esp)          // y
288         fildl   (%esp)          // int(y) : y
289         fucompp                 // <empty>
290         fnstsw
291         sahf
292         jne     23f
294         // OK, the value is an integer, but is the number of bits small
295         // enough so that all are coming from the mantissa?
296         popl    %edx
297         testb   $1, %dl
298         jz      24f             // jump if not odd
299         cmpl    $0xff000000, %edx
300         jae     24f             // does not fit in mantissa bits
301         // It's an odd integer.
302         fldl    MO(mzero)
303         ret
305 22:     fstp    %st(0)
306 23:     popl    %eax
307 24:     fldl    MO(zero)
308         ret
310 END(__ieee754_powf)