Merge branch 'vendor/MPFR'
[dragonfly.git] / lib / libm / arch / i386 / e_exp.S
1 /*
2  * Copyright (c) 1993,94 Winning Strategies, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Winning Strategies, Inc.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $NetBSD: e_exp.S,v 1.12 2002/02/27 16:32:46 christos Exp $
31  * $FreeBSD: src/lib/msun/i387/e_exp.S,v 1.8.2.1 2000/07/10 09:16:28 obrien Exp $
32  */
33
34 /*
35  * Written by:
36  *      J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
37  */
38
39 #include <machine/asm.h>
40
41 #include "abi.h"
42
43
44 /* e^x = 2^(x * log2(e)) */
45 ENTRY(exp)
46 #ifndef __i386__
47         /*
48          * XXX: This code is broken and needs to be merged with the i386 case.
49          */
50         fstcw   -12(%rsp)
51         movw    -12(%rsp),%dx
52         orw     $0x0180,%dx
53         movw    %dx,-16(%rsp)
54         fldcw   -16(%rsp)
55         movsd   %xmm0,-8(%rsp)
56         fldl    -8(%rsp)
57
58         fldl2e
59         fmulp                           /* x * log2(e) */
60         fld     %st(0)
61         frndint                         /* int(x * log2(e)) */
62         fxch    %st(1)
63         fsub    %st(1),%st              /* fract(x * log2(e)) */
64         f2xm1                           /* 2^(fract(x * log2(e))) - 1 */
65         fld1
66         faddp                           /* 2^(fract(x * log2(e))) */
67         fscale                          /* e^x */
68         fstp    %st(1)
69
70         fstpl   -8(%rsp)
71         movsd   -8(%rsp),%xmm0
72         fldcw   -12(%rsp)
73         ret
74 #else
75         /*
76          * If x is +-Inf, then the subtraction would give Inf-Inf = NaN.
77          * Avoid this.  Also avoid it if x is NaN for convenience.
78          */
79         movl    8(%esp),%eax
80         andl    $0x7fffffff,%eax
81         cmpl    $0x7ff00000,%eax
82         jae     x_Inf_or_NaN
83
84         fldl    4(%esp)
85
86         /*
87          * Ensure that the rounding mode is to nearest (to give the smallest
88          * possible fraction) and that the precision is as high as possible.
89          * We may as well mask interrupts if we switch the mode.
90          */
91         fstcw   4(%esp)
92         movl    4(%esp),%eax
93         andl    $0x0300,%eax
94         cmpl    $0x0300,%eax            /* RC == 0 && PC == 3? */
95         je      1f                      /* jump if mode is good */
96         movl    $0x137f,8(%esp)
97         fldcw   8(%esp)
98 1:
99         fldl2e
100         fmulp                           /* x * log2(e) */
101         fst     %st(1)
102         frndint                         /* int(x * log2(e)) */
103         fst     %st(2)
104         fsubrp                          /* fract(x * log2(e)) */
105         f2xm1                           /* 2^(fract(x * log2(e))) - 1 */ 
106         fld1
107         faddp                           /* 2^(fract(x * log2(e))) */
108         fscale                          /* e^x */
109         fstp    %st(1)
110         je      1f
111         fldcw   4(%esp)
112 1:
113         ret
114
115 x_Inf_or_NaN:
116         /*
117          * Return 0 if x is -Inf.  Otherwise just return x, although the
118          * C version would return (x + x) (Real Indefinite) if x is a NaN.
119          */
120         cmpl    $0xfff00000,8(%esp)
121         jne     x_not_minus_Inf
122         cmpl    $0,4(%esp)
123         jne     x_not_minus_Inf
124         fldz
125         ret
126
127 x_not_minus_Inf:
128         fldl    4(%esp)
129         ret
130 #endif
131 END(exp)