xref: /titanic_50/usr/src/lib/libc/amd64/fp/_xtoll.s (revision 0e42dee69ed771bf604dd1789fca9d77b5bbe302)
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, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27	.ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31#include <SYS.h>
32
33	.set	cw,0
34	.set	cw_old,2
35	.set	two_words,4
36
37/* This function truncates the top of the 387 stack into a signed long. */
38
39	ENTRY(__xtol)	/* 387-stack to signed long */
40	subq	$8,%rsp
41	fstcw	cw_old(%rsp)
42	movw	cw_old(%rsp),%ax
43	movw	%ax,%cx
44	andw	$0x0c00,%cx		/* save RC */
45	orw	$0x0c00,%ax
46	movw	%ax,cw(%rsp)
47	fldcw	cw(%rsp)
48	fistpl	two_words(%rsp)
49					/* fwait implied here */
50	fstcw	cw(%rsp)		/* fetch CW in case masks changed */
51	movw	cw(%rsp),%ax
52	andw	$0xf3ff,%ax
53	orw	%cx,%ax			/* restore RC */
54	movw	%ax,cw(%rsp)
55	fldcw	cw(%rsp)
56	movl	two_words(%rsp),%eax
57	addq	$8,%rsp
58	ret
59	SET_SIZE(__xtol)
60
61/* This function truncates the top of the 387 stack into a signed long long. */
62
63	ENTRY(__xtoll)	/* 387-stack to signed long long */
64	subq	$16,%rsp
65	fstcw	cw_old(%rsp)
66	movw	cw_old(%rsp),%ax
67	movw	%ax,%cx
68	andw	$0x0c00,%cx		/* save RC */
69	orw	$0x0c00,%ax
70	movw	%ax,cw(%rsp)
71	fldcw	cw(%rsp)
72	fistpll	8(%rsp)
73					/* fwait implied here */
74	fstcw	cw(%rsp)		/* fetch CW in case masks changed */
75	movw	cw(%rsp),%ax
76	andw	$0xf3ff,%ax
77	orw	%cx,%ax			/* restore RC */
78	movw	%ax,cw(%rsp)
79	fldcw	cw(%rsp)
80	movq	8(%rsp),%rax
81	addq	$16,%rsp
82	ret
83	SET_SIZE(__xtoll)
84
85/* This function truncates the top of the 387 stack into a unsigned long. */
86
87	.align	16
88two_to_31: .4byte	0x4f000000
89
90	ENTRY(__xtoul)	/* 387-stack to unsigned */
91	subq	$8,%rsp
92	fstcw	cw_old(%rsp)
93	movw	cw_old(%rsp),%ax
94	movw	%ax,%cx
95	andw	$0x0c00,%cx		/* save RC */
96	orw	$0x0c00,%ax
97	movw	%ax,cw(%rsp)
98	fldcw	cw(%rsp)
99	flds	two_to_31(%rip)
100	fcomip	%st(1),%st		/* compare 2**31 to x */
101	jp	.donotsub		/* jump if x is NaN */
102	ja	.donotsub		/* jump if 2**31 > x */
103	fsubs	two_to_31(%rip)	/* subtract 2**31 */
104.donotsub:
105	fistpl	two_words(%rsp)
106	fwait				/* in case fistpl causes exception */
107	movl	two_words(%rsp),%eax
108	jp	.donotadd		/* flags did not change */
109	ja	.donotadd		/* flags did not change */
110	addl	$-2147483648,%eax	/* add back 2**31 */
111.donotadd:
112	fstcw	cw(%rsp)		/* fetch CW in case masks changed */
113	movw	cw(%rsp),%dx
114	andw	$0xf3ff,%dx
115	orw	%cx,%dx			/* restore RC */
116	movw	%dx,cw(%rsp)
117	fldcw	cw(%rsp)
118	addq	$8,%rsp
119	ret
120	SET_SIZE(__xtoul)
121