xref: /titanic_44/usr/src/lib/libm/amd64/src/expl.s (revision 6b35cb3cf158584a9408d44b9b6796564e8e1882)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
23 */
24/*
25 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26 * Use is subject to license terms.
27 */
28
29        .file "expl.s"
30
31#include "libm.h"
32LIBM_ANSI_PRAGMA_WEAK(expl,function)
33
34	.data
35	.align	16
36ln2_hi:	.4byte	0xd1d00000, 0xb17217f7, 0x3ffe, 0x0
37ln2_lo:	.4byte	0x4c67fc0d, 0x8654361c, 0xbfce, 0x0
38
39	ENTRY(expl)
40	movl	16(%rsp),%ecx		/ cx <--sign&bexp(x)
41	andl	$0x7fff,%ecx		/ ecx <-- zero_xtnd(bexp(x))
42	cmpl	$0x3ffe,%ecx		/ Is |x| < 0.5?
43	jb	2f			/ If so, see which shortcut to take
44	je	.check_tail		/ More checking if 0.5 <= |x| < 1
45.general_case:				/ Here, |x| >= 1 or x is NaN
46	cmpl	$0x7fff,%ecx		/ bexp(|x|) = bexp(INF)?
47	je	.not_finite		/ if so, x is not finite
48	cmpl	$0x400e,%ecx		/ |x| < 32768 = 2^15?
49	jb	.finite_non_special	/ if so, proceed with argument reduction
50	fldt	8(%rsp)			/ x
51	fld1				/ 1, x
52	jmp	1f
53.finite_non_special:			/ Here, ln(2) < |x| < 2^15
54	fldt	8(%rsp)			/ x
55	fld	%st(0)			/ x, x
56	fldl2e				/ log2(e), x, x
57	fmulp				/ z := x*log2(e), x
58	frndint				/ [z], x
59	fst	%st(2)			/ [z], x, [z]
60	PIC_SETUP(1)
61	fldt	PIC_L(ln2_hi)		/ ln2_hi, [z], x, [z]
62	fmulp				/ [z]*ln2_hi, x, [z]
63	fsubrp	%st,%st(1)		/ x-[z]*ln2_hi, [z]
64	fldt	PIC_L(ln2_lo)		/ ln2_lo, x-[z]*ln2_hi, [z]
65	PIC_WRAPUP
66	fmul	%st(2),%st		/ [z]*ln2_lo, x-[z]*ln2_hi, [z]
67	fsubrp	%st,%st(1)		/ r := x-[z]*ln(2), [z]
68	fldl2e				/ log2(e), r, [z]
69	fmulp				/ f := r*log2(e), [z]
70	f2xm1				/ 2^f-1,[z]
71	fld1				/ 1, 2^f-1, [z]
72	faddp	%st,%st(1)		/ 2^f, [z]
731:
74	fscale				/ e^x, [z]
75	fstp	%st(1)
76	ret
77
782:					/ Here, |x| < 0.5
79	cmpl	$0x3fbe,%ecx		/ Is |x| >= 2^-65?
80	jae	.shortcut		/ If so, take a shortcut
81	fldt	8(%rsp)			/ x
82	fld1				/ 1, x
83	faddp	%st,%st(1)		/ 1+x (for inexact & directed rounding)
84	ret
85
86.check_tail:
87	movl	12(%rsp),%ecx		/ ecx <-- hi_32(sgnfcnd(x))
88	cmpl	$0xb17217f7,%ecx	/ Is |x| < ln(2)?
89	ja	.finite_non_special
90	jb	.shortcut
91	movl	8(%rsp),%edx		/ edx <-- lo_32(x)
92	cmpl	$0xd1cf79ab,%edx	/ Is |x| slightly < ln(2)?
93	ja	.finite_non_special	/ branch if |x| slightly > ln(2)
94.shortcut:
95	/ Here, |x| < ln(2), so |z| = |x/ln(2)| < 1,
96	/ whence z is in f2xm1's domain.
97	fldt	8(%rsp)			/ x
98	fldl2e				/ log2(e), x
99	fmulp				/ x*log2(e)
100	f2xm1				/ 2^(x*log2(e))-1 = e^x-1
101	fld1				/ 1, e^x-1
102	faddp	%st,%st(1)		/ e^x
103	ret
104
105.not_finite:
106	movl	12(%rsp),%ecx		/ ecx <-- hi_32(sgnfcnd(x))
107	cmpl	$0x80000000,%ecx	/ hi_32(|x|) = hi_32(INF)?
108	jne	.NaN_or_pinf		/ if not, x is NaN
109	movl	8(%rsp),%edx		/ edx <-- lo_32(x)
110	cmpl	$0,%edx			/ lo_32(x) = 0?
111	jne	.NaN_or_pinf		/ if not, x is NaN
112	movl	16(%rsp),%eax		/ ax <-- sign&bexp((x))
113	andl	$0x8000,%eax		/ here, x is infinite, but +/-?
114	jz	.NaN_or_pinf		/ branch if x = +INF
115	fldz				/ Here, x = -inf, so return 0
116	ret
117
118.NaN_or_pinf:
119	/ Here, x = NaN or +inf, so load x and return immediately.
120	fldt	8(%rsp)
121	fadd	%st(0),%st		/ quiet SNaN
122	ret
123	.align	16
124	SET_SIZE(expl)
125