xref: /titanic_52/usr/src/lib/libm/i386/src/roundl.s (revision 385cc6b4ad1792caef3f84eb61eed3f27085801f)
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 2006 Sun Microsystems, Inc.  All rights reserved.
26 * Use is subject to license terms.
27 */
28
29	.file	"roundl.s"
30
31#include "libm.h"
32LIBM_ANSI_PRAGMA_WEAK(roundl,function)
33
34	.section .rodata
35	.align	4
36.Lhalf:	.float	0.5
37
38	ENTRY(roundl)
39	movl	%esp,%ecx
40	subl	$8,%esp
41	fstcw	-8(%ecx)
42	fldt	4(%ecx)
43	movw	-8(%ecx),%dx
44	andw	$0xf3ff,%dx
45	movw	%dx,-4(%ecx)
46	fldcw	-4(%ecx)		/ set RD = to_nearest
47	fld	%st(0)
48	frndint				/ [x],x
49	fstcw	-4(%ecx)
50	movw	-4(%ecx),%dx
51	andw	$0xf3ff,%dx
52	movw	-8(%ecx),%ax
53	andw	$0x0c00,%ax
54	orw	%dx,%ax
55	movw	%ax,-8(%ecx)
56	fldcw	-8(%ecx)		/ restore RD
57	fucom				/ check if x is already an integer
58	fstsw	%ax
59	sahf
60	jp	0f
61	je	0f
62	fxch				/ x,[x]
63	fsub	%st(1),%st		/ x-[x],[x]
64	fabs				/ |x-[x]|,[x]
65	PIC_SETUP(1)
66	fcoms	PIC_L(.Lhalf)
67	PIC_WRAPUP
68	fnstsw	%ax
69	sahf
70	jae	2f			/ if |x-[x]| = 0.5 goto halfway,
71					/ most cases will not take branch.
720:
73	addl	$8,%esp
74	fstp	%st(0)
75	ret
762:
77    / x = n+0.5, recompute roundl(x) as x+sign(x)*0.5
78	fldt	4(%ecx)			/ x, 0.5, [x]
79	movw	12(%ecx),%ax		/ sign+exp of x
80	andw	$0x8000,%ax		/ look at sign bit
81	jnz	3f
82	faddp
83	addl	$8,%esp
84	fstp	%st(1)
85	ret
863:
87	/ here, x is negative, so return x-0.5
88	fsubp	%st,%st(1)		/ x-0.5,[x]
89	addl	$8,%esp
90	fstp	%st(1)
91	ret
92	.align	4
93	SET_SIZE(roundl)
94