]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/fpu/e_powl.S
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / i386 / fpu / e_powl.S
1 /* ix87 specific implementation of pow function.
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <machine/asm.h>
21
22 .section .rodata.cst8,"aM",@progbits,8
23
24 .p2align 3
25 .type one,@object
26 one: .double 1.0
27 ASM_SIZE_DIRECTIVE(one)
28 .type p3,@object
29 p3: .byte 0, 0, 0, 0, 0, 0, 0x20, 0x40
30 ASM_SIZE_DIRECTIVE(p3)
31 .type p63,@object
32 p63: .byte 0, 0, 0, 0, 0, 0, 0xe0, 0x43
33 ASM_SIZE_DIRECTIVE(p63)
34 .type p64,@object
35 p64: .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x43
36 ASM_SIZE_DIRECTIVE(p64)
37 .type p78,@object
38 p78: .byte 0, 0, 0, 0, 0, 0, 0xd0, 0x44
39 ASM_SIZE_DIRECTIVE(p78)
40 .type pm79,@object
41 pm79: .byte 0, 0, 0, 0, 0, 0, 0, 0x3b
42 ASM_SIZE_DIRECTIVE(pm79)
43
44 .section .rodata.cst16,"aM",@progbits,16
45
46 .p2align 3
47 .type infinity,@object
48 inf_zero:
49 infinity:
50 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0x7f
51 ASM_SIZE_DIRECTIVE(infinity)
52 .type zero,@object
53 zero: .double 0.0
54 ASM_SIZE_DIRECTIVE(zero)
55 .type minf_mzero,@object
56 minf_mzero:
57 minfinity:
58 .byte 0, 0, 0, 0, 0, 0, 0xf0, 0xff
59 mzero:
60 .byte 0, 0, 0, 0, 0, 0, 0, 0x80
61 ASM_SIZE_DIRECTIVE(minf_mzero)
62
63 #ifdef PIC
64 # define MO(op) op##@GOTOFF(%ecx)
65 # define MOX(op,x,f) op##@GOTOFF(%ecx,x,f)
66 #else
67 # define MO(op) op
68 # define MOX(op,x,f) op(,x,f)
69 #endif
70
71 .text
72 ENTRY(__ieee754_powl)
73 fldt 16(%esp) // y
74 fxam
75
76 #ifdef PIC
77 LOAD_PIC_REG (cx)
78 #endif
79
80 fnstsw
81 movb %ah, %dl
82 andb $0x45, %ah
83 cmpb $0x40, %ah // is y == 0 ?
84 je 11f
85
86 cmpb $0x05, %ah // is y == ±inf ?
87 je 12f
88
89 cmpb $0x01, %ah // is y == NaN ?
90 je 30f
91
92 fldt 4(%esp) // x : y
93
94 subl $8,%esp
95 cfi_adjust_cfa_offset (8)
96
97 fxam
98 fnstsw
99 movb %ah, %dh
100 andb $0x45, %ah
101 cmpb $0x40, %ah
102 je 20f // x is ±0
103
104 cmpb $0x05, %ah
105 je 15f // x is ±inf
106
107 cmpb $0x01, %ah
108 je 32f // x is NaN
109
110 fxch // y : x
111
112 /* fistpll raises invalid exception for |y| >= 1L<<63. */
113 fld %st // y : y : x
114 fabs // |y| : y : x
115 fcompl MO(p63) // y : x
116 fnstsw
117 sahf
118 jnc 2f
119
120 /* First see whether `y' is a natural number. In this case we
121 can use a more precise algorithm. */
122 fld %st // y : y : x
123 fistpll (%esp) // y : x
124 fildll (%esp) // int(y) : y : x
125 fucomp %st(1) // y : x
126 fnstsw
127 sahf
128 je 9f
129
130 // If y has absolute value at most 0x1p-79, then any finite
131 // nonzero x will result in 1. Saturate y to those bounds to
132 // avoid underflow in the calculation of y*log2(x).
133 fld %st // y : y : x
134 fabs // |y| : y : x
135 fcompl MO(pm79) // y : x
136 fnstsw
137 sahf
138 jnc 3f
139 fstp %st(0) // pop y
140 fldl MO(pm79) // 0x1p-79 : x
141 testb $2, %dl
142 jnz 3f // y > 0
143 fchs // -0x1p-79 : x
144 jmp 3f
145
146 9: /* OK, we have an integer value for y. Unless very small
147 (we use < 8), use the algorithm for real exponent to avoid
148 accumulation of errors. */
149 fld %st // y : y : x
150 fabs // |y| : y : x
151 fcompl MO(p3) // y : x
152 fnstsw
153 sahf
154 jnc 3f
155 popl %eax
156 cfi_adjust_cfa_offset (-4)
157 popl %edx
158 cfi_adjust_cfa_offset (-4)
159 orl $0, %edx
160 fstp %st(0) // x
161 jns 4f // y >= 0, jump
162 fdivrl MO(one) // 1/x (now referred to as x)
163 negl %eax
164 adcl $0, %edx
165 negl %edx
166 4: fldl MO(one) // 1 : x
167 fxch
168
169 /* If y is even, take the absolute value of x. Otherwise,
170 ensure all intermediate values that might overflow have the
171 sign of x. */
172 testb $1, %al
173 jnz 6f
174 fabs
175
176 6: shrdl $1, %edx, %eax
177 jnc 5f
178 fxch
179 fabs
180 fmul %st(1) // x : ST*x
181 fxch
182 5: fld %st // x : x : ST*x
183 fabs // |x| : x : ST*x
184 fmulp // |x|*x : ST*x
185 shrl $1, %edx
186 movl %eax, %ecx
187 orl %edx, %ecx
188 jnz 6b
189 fstp %st(0) // ST*x
190 ret
191
192 /* y is ±NAN */
193 30: fldt 4(%esp) // x : y
194 fldl MO(one) // 1.0 : x : y
195 fucomp %st(1) // x : y
196 fnstsw
197 sahf
198 je 31f
199 fxch // y : x
200 31: fstp %st(1)
201 ret
202
203 cfi_adjust_cfa_offset (8)
204 32: addl $8, %esp
205 cfi_adjust_cfa_offset (-8)
206 fstp %st(1)
207 ret
208
209 cfi_adjust_cfa_offset (8)
210 .align ALIGNARG(4)
211 2: // y is a large integer (absolute value at least 1L<<63).
212 // If y has absolute value at least 1L<<78, then any finite
213 // nonzero x will result in 0 (underflow), 1 or infinity (overflow).
214 // Saturate y to those bounds to avoid overflow in the calculation
215 // of y*log2(x).
216 fld %st // y : y : x
217 fabs // |y| : y : x
218 fcompl MO(p78) // y : x
219 fnstsw
220 sahf
221 jc 3f
222 fstp %st(0) // pop y
223 fldl MO(p78) // 1L<<78 : x
224 testb $2, %dl
225 jz 3f // y > 0
226 fchs // -(1L<<78) : x
227 .align ALIGNARG(4)
228 3: /* y is a real number. */
229 subl $28, %esp
230 cfi_adjust_cfa_offset (28)
231 fstpt 12(%esp) // x
232 fstpt (%esp) // <empty>
233 call HIDDEN_JUMPTARGET (__powl_helper) // <result>
234 addl $36, %esp
235 cfi_adjust_cfa_offset (-36)
236 ret
237
238 // pow(x,±0) = 1
239 .align ALIGNARG(4)
240 11: fstp %st(0) // pop y
241 fldl MO(one)
242 ret
243
244 // y == ±inf
245 .align ALIGNARG(4)
246 12: fstp %st(0) // pop y
247 fldl MO(one) // 1
248 fldt 4(%esp) // x : 1
249 fabs // abs(x) : 1
250 fucompp // < 1, == 1, or > 1
251 fnstsw
252 andb $0x45, %ah
253 cmpb $0x45, %ah
254 je 13f // jump if x is NaN
255
256 cmpb $0x40, %ah
257 je 14f // jump if |x| == 1
258
259 shlb $1, %ah
260 xorb %ah, %dl
261 andl $2, %edx
262 fldl MOX(inf_zero, %edx, 4)
263 ret
264
265 .align ALIGNARG(4)
266 14: fldl MO(one)
267 ret
268
269 .align ALIGNARG(4)
270 13: fldt 4(%esp) // load x == NaN
271 ret
272
273 cfi_adjust_cfa_offset (8)
274 .align ALIGNARG(4)
275 // x is ±inf
276 15: fstp %st(0) // y
277 testb $2, %dh
278 jz 16f // jump if x == +inf
279
280 // fistpll raises invalid exception for |y| >= 1L<<63, but y
281 // may be odd unless we know |y| >= 1L<<64.
282 fld %st // y : y
283 fabs // |y| : y
284 fcompl MO(p64) // y
285 fnstsw
286 sahf
287 jnc 16f
288 fldl MO(p63) // p63 : y
289 fxch // y : p63
290 fprem // y%p63 : p63
291 fstp %st(1) // y%p63
292
293 // We must find out whether y is an odd integer.
294 fld %st // y : y
295 fistpll (%esp) // y
296 fildll (%esp) // int(y) : y
297 fucompp // <empty>
298 fnstsw
299 sahf
300 jne 17f
301
302 // OK, the value is an integer, but is it odd?
303 popl %eax
304 cfi_adjust_cfa_offset (-4)
305 popl %edx
306 cfi_adjust_cfa_offset (-4)
307 andb $1, %al
308 jz 18f // jump if not odd
309 // It's an odd integer.
310 shrl $31, %edx
311 fldl MOX(minf_mzero, %edx, 8)
312 ret
313
314 cfi_adjust_cfa_offset (8)
315 .align ALIGNARG(4)
316 16: fcompl MO(zero)
317 addl $8, %esp
318 cfi_adjust_cfa_offset (-8)
319 fnstsw
320 shrl $5, %eax
321 andl $8, %eax
322 fldl MOX(inf_zero, %eax, 1)
323 ret
324
325 cfi_adjust_cfa_offset (8)
326 .align ALIGNARG(4)
327 17: shll $30, %edx // sign bit for y in right position
328 addl $8, %esp
329 cfi_adjust_cfa_offset (-8)
330 18: shrl $31, %edx
331 fldl MOX(inf_zero, %edx, 8)
332 ret
333
334 cfi_adjust_cfa_offset (8)
335 .align ALIGNARG(4)
336 // x is ±0
337 20: fstp %st(0) // y
338 testb $2, %dl
339 jz 21f // y > 0
340
341 // x is ±0 and y is < 0. We must find out whether y is an odd integer.
342 testb $2, %dh
343 jz 25f
344
345 // fistpll raises invalid exception for |y| >= 1L<<63, but y
346 // may be odd unless we know |y| >= 1L<<64.
347 fld %st // y : y
348 fabs // |y| : y
349 fcompl MO(p64) // y
350 fnstsw
351 sahf
352 jnc 25f
353 fldl MO(p63) // p63 : y
354 fxch // y : p63
355 fprem // y%p63 : p63
356 fstp %st(1) // y%p63
357
358 fld %st // y : y
359 fistpll (%esp) // y
360 fildll (%esp) // int(y) : y
361 fucompp // <empty>
362 fnstsw
363 sahf
364 jne 26f
365
366 // OK, the value is an integer, but is it odd?
367 popl %eax
368 cfi_adjust_cfa_offset (-4)
369 popl %edx
370 cfi_adjust_cfa_offset (-4)
371 andb $1, %al
372 jz 27f // jump if not odd
373 // It's an odd integer.
374 // Raise divide-by-zero exception and get minus infinity value.
375 fldl MO(one)
376 fdivl MO(zero)
377 fchs
378 ret
379
380 cfi_adjust_cfa_offset (8)
381 25: fstp %st(0)
382 26: addl $8, %esp
383 cfi_adjust_cfa_offset (-8)
384 27: // Raise divide-by-zero exception and get infinity value.
385 fldl MO(one)
386 fdivl MO(zero)
387 ret
388
389 cfi_adjust_cfa_offset (8)
390 .align ALIGNARG(4)
391 // x is ±0 and y is > 0. We must find out whether y is an odd integer.
392 21: testb $2, %dh
393 jz 22f
394
395 // fistpll raises invalid exception for |y| >= 1L<<63, but y
396 // may be odd unless we know |y| >= 1L<<64.
397 fld %st // y : y
398 fcompl MO(p64) // y
399 fnstsw
400 sahf
401 jnc 22f
402 fldl MO(p63) // p63 : y
403 fxch // y : p63
404 fprem // y%p63 : p63
405 fstp %st(1) // y%p63
406
407 fld %st // y : y
408 fistpll (%esp) // y
409 fildll (%esp) // int(y) : y
410 fucompp // <empty>
411 fnstsw
412 sahf
413 jne 23f
414
415 // OK, the value is an integer, but is it odd?
416 popl %eax
417 cfi_adjust_cfa_offset (-4)
418 popl %edx
419 cfi_adjust_cfa_offset (-4)
420 andb $1, %al
421 jz 24f // jump if not odd
422 // It's an odd integer.
423 fldl MO(mzero)
424 ret
425
426 cfi_adjust_cfa_offset (8)
427 22: fstp %st(0)
428 23: addl $8, %esp // Don't use 2 x pop
429 cfi_adjust_cfa_offset (-8)
430 24: fldl MO(zero)
431 ret
432
433 END(__ieee754_powl)
434 strong_alias (__ieee754_powl, __powl_finite)