xref: /freebsd/lib/msun/i387/e_exp.S (revision 72d8d94d6fe43e71a78956d9b417a1a65d20e87e)
13a8617a8SJordan K. Hubbard/*
23a8617a8SJordan K. Hubbard * Copyright (c) 1993,94 Winning Strategies, Inc.
33a8617a8SJordan K. Hubbard * All rights reserved.
43a8617a8SJordan K. Hubbard *
53a8617a8SJordan K. Hubbard * Redistribution and use in source and binary forms, with or without
63a8617a8SJordan K. Hubbard * modification, are permitted provided that the following conditions
73a8617a8SJordan K. Hubbard * are met:
83a8617a8SJordan K. Hubbard * 1. Redistributions of source code must retain the above copyright
93a8617a8SJordan K. Hubbard *    notice, this list of conditions and the following disclaimer.
103a8617a8SJordan K. Hubbard * 2. Redistributions in binary form must reproduce the above copyright
113a8617a8SJordan K. Hubbard *    notice, this list of conditions and the following disclaimer in the
123a8617a8SJordan K. Hubbard *    documentation and/or other materials provided with the distribution.
133a8617a8SJordan K. Hubbard * 3. All advertising materials mentioning features or use of this software
143a8617a8SJordan K. Hubbard *    must display the following acknowledgement:
153a8617a8SJordan K. Hubbard *      This product includes software developed by Winning Strategies, Inc.
163a8617a8SJordan K. Hubbard * 4. The name of the author may not be used to endorse or promote products
173a8617a8SJordan K. Hubbard *    derived from this software without specific prior written permission.
183a8617a8SJordan K. Hubbard *
193a8617a8SJordan K. Hubbard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
203a8617a8SJordan K. Hubbard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
213a8617a8SJordan K. Hubbard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223a8617a8SJordan K. Hubbard * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
233a8617a8SJordan K. Hubbard * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
243a8617a8SJordan K. Hubbard * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
253a8617a8SJordan K. Hubbard * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
263a8617a8SJordan K. Hubbard * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
273a8617a8SJordan K. Hubbard * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
283a8617a8SJordan K. Hubbard * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293a8617a8SJordan K. Hubbard */
303a8617a8SJordan K. Hubbard
313a8617a8SJordan K. Hubbard/*
323a8617a8SJordan K. Hubbard * Written by:
333a8617a8SJordan K. Hubbard *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
343a8617a8SJordan K. Hubbard */
353a8617a8SJordan K. Hubbard
36ec8cc608SJordan K. Hubbard#include <machine/asmacros.h>
373a8617a8SJordan K. Hubbard
381130b656SJordan K. HubbardRCSID("$FreeBSD$")
393a8617a8SJordan K. Hubbard
403a8617a8SJordan K. Hubbard/* e^x = 2^(x * log2(e)) */
413a8617a8SJordan K. HubbardENTRY(__ieee754_exp)
4272d8d94dSBruce Evans	/*
4372d8d94dSBruce Evans	 * If x is +-Inf, then the subtraction would give Inf-Inf = NaN.
4472d8d94dSBruce Evans	 * Avoid this.  Also avoid it if x is NaN for convenience.
4572d8d94dSBruce Evans	 */
4672d8d94dSBruce Evans	movl	8(%esp),%eax
4772d8d94dSBruce Evans	andl	$0x7fffffff,%eax
4872d8d94dSBruce Evans	cmpl	$0x7ff00000,%eax
4972d8d94dSBruce Evans	jae	x_Inf_or_NaN
5072d8d94dSBruce Evans
513a8617a8SJordan K. Hubbard	fldl	4(%esp)
5272d8d94dSBruce Evans
5372d8d94dSBruce Evans	/*
5472d8d94dSBruce Evans	 * Ensure that the rounding mode is to nearest (to give the smallest
5572d8d94dSBruce Evans	 * possible fraction) and that the precision is as high as possible.
5672d8d94dSBruce Evans	 * We may as well mask interrupts if we switch the mode.
5772d8d94dSBruce Evans	 */
5872d8d94dSBruce Evans	fstcw	4(%esp)
5972d8d94dSBruce Evans	movl	4(%esp),%eax
6072d8d94dSBruce Evans	andl	$0x0300,%eax
6172d8d94dSBruce Evans	cmpl	$0x0300,%eax		/* RC == 0 && PC == 3? */
6272d8d94dSBruce Evans	je	1f			/* jump if mode is good */
6372d8d94dSBruce Evans	movl	$0x137f,8(%esp)
6472d8d94dSBruce Evans	fldcw	8(%esp)
6572d8d94dSBruce Evans1:
663a8617a8SJordan K. Hubbard	fldl2e
673a8617a8SJordan K. Hubbard	fmulp				/* x * log2(e) */
683a8617a8SJordan K. Hubbard	fstl	%st(1)
693a8617a8SJordan K. Hubbard	frndint				/* int(x * log2(e)) */
703a8617a8SJordan K. Hubbard	fstl	%st(2)
713a8617a8SJordan K. Hubbard	fsubrp				/* fract(x * log2(e)) */
723a8617a8SJordan K. Hubbard	f2xm1				/* 2^(fract(x * log2(e))) - 1 */
733a8617a8SJordan K. Hubbard	fld1
743a8617a8SJordan K. Hubbard	faddp				/* 2^(fract(x * log2(e))) */
753a8617a8SJordan K. Hubbard	fscale				/* e^x */
76f05bebb3SBruce Evans	fstpl	%st(1)
7772d8d94dSBruce Evans	je	1f
7872d8d94dSBruce Evans	fldcw	4(%esp)
7972d8d94dSBruce Evans1:
8072d8d94dSBruce Evans	ret
8172d8d94dSBruce Evans
8272d8d94dSBruce Evansx_Inf_or_NaN:
8372d8d94dSBruce Evans	/*
8472d8d94dSBruce Evans	 * Return 0 if x is -Inf.  Otherwise just return x, although the
8572d8d94dSBruce Evans	 * C version would return (x + x) (Real Indefinite) if x is a NaN.
8672d8d94dSBruce Evans	 */
8772d8d94dSBruce Evans	cmpl	$0xfff00000,8(%esp)
8872d8d94dSBruce Evans	jne	x_not_minus_Inf
8972d8d94dSBruce Evans	cmpl	$0,4(%esp)
9072d8d94dSBruce Evans	jne	x_not_minus_Inf
9172d8d94dSBruce Evans	fldz
9272d8d94dSBruce Evans	ret
9372d8d94dSBruce Evans
9472d8d94dSBruce Evansx_not_minus_Inf:
9572d8d94dSBruce Evans	fldl	4(%esp)
963a8617a8SJordan K. Hubbard	ret
97