Update.
[glibc.git] / sysdeps / libm-i387 / e_powl.S
blob0ea482982284269fd0e83bb6a0c9c8a0e9c59889
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_powl)
62         fldt    16(%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         fldt    4(%esp)         // x : y
85         subl    $8,%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         fistpll (%esp)          // y : x
103         fildll  (%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    %eax
111         popl    %edx
112         orl     $0, %edx
113         fstp    %st(0)          // x
114         jns     4f              // y >= 0, jump
115         fdivrl  MO(one)         // 1/x          (now referred to as x)
116         negl    %eax
117         adcl    $0, %edx
118         negl    %edx
119 4:      fldl    MO(one)         // 1 : x
120         fxch
122 6:      shrdl   $1, %edx, %eax
123         jnc     5f
124         fxch
125         fmul    %st(1)          // x : ST*x
126         fxch
127 5:      fmul    %st(0), %st     // x*x : ST*x
128         movl    %eax, %ecx
129         orl     %edx, %ecx
130         jnz     6b
131         fstp    %st(0)          // ST*x
132 30:     ret
134         .align ALIGNARG(4)
135 2:      /* y is a real number.  */
136         fxch                    // x : y
137         fldl    MO(one)         // 1.0 : x : y
138         fld     %st(1)          // x : 1.0 : x : y
139         fsub    %st(1)          // x-1 : 1.0 : x : y
140         fabs                    // |x-1| : 1.0 : x : y
141         fcompl  MO(limit)       // 1.0 : x : y
142         fnstsw
143         fxch                    // x : 1.0 : y
144         sahf
145         ja      7f
146         fsub    %st(1)          // x-1 : 1.0 : y
147         fyl2xp1                 // log2(x) : y
148         jmp     8f
150 7:      fyl2x                   // log2(x) : y
151 8:      fmul    %st(1)          // y*log2(x) : y
152         fst     %st(1)          // y*log2(x) : y*log2(x)
153         frndint                 // int(y*log2(x)) : y*log2(x)
154         fsubr   %st, %st(1)     // int(y*log2(x)) : fract(y*log2(x))
155         fxch                    // fract(y*log2(x)) : int(y*log2(x))
156         f2xm1                   // 2^fract(y*log2(x))-1 : int(y*log2(x))
157         faddl   MO(one)         // 2^fract(y*log2(x)) : int(y*log2(x))
158         fscale                  // 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
159         addl    $8, %esp
160         fstp    %st(1)          // 2^fract(y*log2(x))*2^int(y*log2(x))
161         ret
164         // pow(x,±0) = 1
165         .align ALIGNARG(4)
166 11:     fstp    %st(0)          // pop y
167         fldl    MO(one)
168         ret
170         // y == ±inf
171         .align ALIGNARG(4)
172 12:     fstp    %st(0)          // pop y
173         fldt    4(%esp)         // x
174         fabs
175         fcompl  MO(one)         // < 1, == 1, or > 1
176         fnstsw
177         andb    $0x45, %ah
178         cmpb    $0x45, %ah
179         je      13f             // jump if x is NaN
181         cmpb    $0x40, %ah
182         je      14f             // jump if |x| == 1
184         shlb    $1, %ah
185         xorb    %ah, %dl
186         andl    $2, %edx
187         fldl    MOX(inf_zero, %edx, 4)
188         ret
190         .align ALIGNARG(4)
191 14:     fldl    MO(infinity)
192         fmull   MO(zero)        // raise invalid exception
193         ret
195         .align ALIGNARG(4)
196 13:     fldt    4(%esp)         // load x == NaN
197         ret
199         .align ALIGNARG(4)
200         // x is ±inf
201 15:     fstp    %st(0)          // y
202         testb   $2, %dh
203         jz      16f             // jump if x == +inf
205         // We must find out whether y is an odd integer.
206         fld     %st             // y : y
207         fistpll (%esp)          // y
208         fildll  (%esp)          // int(y) : y
209         fucompp                 // <empty>
210         fnstsw
211         sahf
212         jne     17f
214         // OK, the value is an integer, but is it odd?
215         popl    %eax
216         popl    %edx
217         andb    $1, %al
218         jz      18f             // jump if not odd
219         // It's an odd integer.
220         shrl    $31, %edx
221         fldl    MOX(minf_mzero, %edx, 8)
222         ret
224         .align ALIGNARG(4)
225 16:     fcompl  MO(zero)
226         addl    $8, %esp
227         fnstsw
228         shrl    $5, %eax
229         andl    $8, %eax
230         fldl    MOX(inf_zero, %eax, 1)
231         ret
233         .align ALIGNARG(4)
234 17:     shll    $30, %edx       // sign bit for y in right position
235         addl    $8, %esp
236 18:     shrl    $31, %edx
237         fldl    MOX(inf_zero, %edx, 8)
238         ret
240         .align ALIGNARG(4)
241         // x is ±0
242 20:     fstp    %st(0)          // y
243         testb   $2, %dl
244         jz      21f             // y > 0
246         // x is ±0 and y is < 0.  We must find out whether y is an odd integer.
247         testb   $2, %dh
248         jz      25f
250         fld     %st             // y : y
251         fistpll (%esp)          // y
252         fildll  (%esp)          // int(y) : y
253         fucompp                 // <empty>
254         fnstsw
255         sahf
256         jne     26f
258         // OK, the value is an integer, but is it odd?
259         popl    %eax
260         popl    %edx
261         andb    $1, %al
262         jz      27f             // jump if not odd
263         // It's an odd integer.
264         // Raise divide-by-zero exception and get minus infinity value.
265         fldl    MO(one)
266         fdivl   MO(zero)
267         fchs
268         ret
270 25:     fstp    %st(0)
271 26:     popl    %eax
272         popl    %edx
273 27:     // Raise divide-by-zero exception and get infinity value.
274         fldl    MO(one)
275         fdivl   MO(zero)
276         ret
278         .align ALIGNARG(4)
279         // x is ±0 and y is > 0.  We must find out whether y is an odd integer.
280 21:     testb   $2, %dh
281         jz      22f
283         fld     %st             // y : y
284         fistpll (%esp)          // y
285         fildll  (%esp)          // int(y) : y
286         fucompp                 // <empty>
287         fnstsw
288         sahf
289         jne     23f
291         // OK, the value is an integer, but is it odd?
292         popl    %eax
293         popl    %edx
294         andb    $1, %al
295         jz      24f             // jump if not odd
296         // It's an odd integer.
297         fldl    MO(mzero)
298         ret
300 22:     fstp    %st(0)
301 23:     popl    %eax
302         popl    %edx
303 24:     fldl    MO(zero)
304         ret
306 END(__ieee754_powl)