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 "expm1l.s"
30
31#include "libm.h"
32	.weak __expm1l
33	.type __expm1l,@function
34
35	.data
36	.align	16
37ln2_hi:	.4byte	0xd1d00000, 0xb17217f7, 0x3ffe, 0x0
38ln2_lo:	.4byte	0x4c67fc0d, 0x8654361c, 0xbfce, 0x0
39
40	ENTRY(expm1l)
41	movl	16(%rsp),%ecx		# cx <--sign&bexp(x)
42	movl	%ecx,%eax		# ax <--sign&bexp(x)
43	andl	$0x7fff,%ecx		# ecx <-- zero_xtnd(bexp(x))
44	cmpl	$0x3ffe,%ecx		# Is |x| < ln(2)?
45	jb	.shortcut		# If so, take a shortcut.
46	je	.check_tail		# |x| may be only slightly < ln(2)
47.general_case:				# Here, |x| > ln(2) or x is NaN
48	cmpl	$0x7fff,%ecx		# bexp(|x|) = bexp(INF)?
49	je	.not_finite		# if so, x is not finite
50	andl	$0xffff,%eax		# eax <-- sign&bexp(x)
51	cmpl	$0xc006,%eax		# x <= -128?
52	jae	1f			# if so, simply return -1
53	cmpl	$0x400d,%ecx		# |x| < 16384 = 2^14?
54	jb	.finite_non_special	# if so, proceed with argument reduction
55	fldt	8(%rsp)			# x >= 16384; x
56	fld1				# 1, x
57	fscale				# +Inf, x
58	fstp	%st(1)			# +Inf
59	ret
60
61.finite_non_special:			# -128 < x < -ln(2) || ln(2) < x < 2^14
62	fldt	8(%rsp)			# x
63	fld	%st(0)			# x, x
64	fldl2e				# log2(e), x, x
65	fmulp				# z := x*log2(e), x
66	frndint				# [z], x
67	fst	%st(2)			# [z], x, [z]
68#ifdef PIC	/* PIC-L macro */
69	fldt	ln2_hi(%rip)		# ln2_hi, [z], x, [z]
70#else
71	fldt	ln2_hi			# ln2_hi, [z], x, [z]
72#endif
73	fmulp				# [z]*ln2_hi, x, [z]
74	fsubrp	%st,%st(1)		# x-[z]*ln2_hi, [z]
75#ifdef PIC	/* PIC-L macro */
76	fldt	ln2_lo(%rip)		# ln2_lo, x-[z]*ln2_hi, [z]
77#else
78	fldt	ln2_lo			# ln2_lo, x-[z]*ln2_hi, [z]
79#endif
80	fmulp	%st,%st(2)		# [z]*ln2_lo, x-[z]*ln2_hi, [z]
81	fsubrp	%st,%st(1)		# r := x-[z]*ln(2), [z]
82	fldl2e				# log2(e), r, [z]
83	fmulp				# f := r*log2(e), [z]
84	f2xm1				# 2^f-1,[z]
85	fld1				# 1, 2^f-1, [z]
86	faddp	%st,%st(1)		# 2^f, [z]
87	fscale				# e^x, [z]
88	fstp	%st(1)			# e^x
89	fld1				# 1, e^x
90	fsubrp	%st,%st(1)		# e^x-1
91	ret
92
93.check_tail:
94	movl	12(%rsp),%ecx		# ecx <-- hi_32(sgnfcnd(x))
95	cmpl	$0xb17217f7,%ecx	# Is |x| < ln(2)?
96	ja	.finite_non_special
97	jb	.shortcut
98	movl	8(%rsp),%edx		# edx <-- lo_32(x)
99	cmpl	$0xd1cf79ab,%edx	# Is |x| slightly < ln(2)?
100	ja	.finite_non_special	# branch if |x| slightly > ln(2)
101.shortcut:
102	# Here, |x| < ln(2), so |z| = |x/ln(2)| < 1,
103	# whence z is in f2xm1's domain.
104	fldt	8(%rsp)			# x
105	fldl2e				# log2(e), x
106	fmulp				# z := x*log2(e)
107	f2xm1				# 2^(x*log2(e))-1 = e^x-1
108	ret
109
110.not_finite:
111	movl	12(%rsp),%ecx		# ecx <-- hi_32(sgnfcnd(x))
112	cmpl	$0x80000000,%ecx	# hi_32(|x|) = hi_32(INF)?
113	jne	.NaN_or_pinf		# if not, x is NaN
114	movl	8(%rsp),%edx		# edx <-- lo_32(x)
115	cmpl	$0,%edx			# lo_32(x) = 0?
116	jne	.NaN_or_pinf		# if not, x is NaN
117	movl	16(%rsp),%eax		# ax <-- sign&bexp((x))
118	andl	$0x8000,%eax		# here, x is infinite, but +/-?
119	jz	.NaN_or_pinf		# branch if x = +INF
1201:
121	fld1				# Here, x = -inf, so return -1
122	fchs
123	ret
124
125.NaN_or_pinf:
126	# Here, x = NaN or +inf, so load x and return immediately.
127	fldt	8(%rsp)
128	ret
129	.align	16
130	SET_SIZE(expm1l)
131	.section .note.GNU-stack,"",%progbits
132