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