xref: /titanic_52/usr/src/lib/libc/i386/fp/fpstart.c (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
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 (c) 1990, 1991 UNIX System Laboratories, Inc.
24  *	Copyright (c) 1988 AT&T
25  *	  All Rights Reserved
26  */
27 
28 /*
29  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
31  */
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 /*
36  * Establish the default settings for the floating-point state for a C language
37  * program:
38  *	rounding mode		-- round to nearest default by OS,
39  *	exceptions enabled	-- all masked
40  *	sticky bits		-- all clear by default by OS.
41  *      precision control       -- double extended
42  * Set _fp_hw according to what floating-point hardware is available.
43  * Set _sse_hw according to what SSE hardware is available.
44  * Set __flt_rounds according to the rounding mode.
45  */
46 
47 #pragma weak _fpstart = __fpstart
48 
49 #include	"synonyms.h"
50 #include	<sys/types.h>
51 #include	<sys/sysi86.h>	/* for SI86FPHW/SI86FPSTART definitions */
52 #include	<sys/fp.h>	/* for FPU_CW_INIT and SSE_MXCSR_INIT */
53 
54 extern int	_private_sysi86();
55 extern int	__fltrounds();
56 
57 int	_fp_hw;			/* default: bss: 0 == no hardware */
58 int	_sse_hw;		/* default: bss: 0 == no sse */
59 int	__flt_rounds;		/* ANSI rounding mode */
60 
61 void
62 __fpstart()
63 {
64 	/*
65 	 * query OS for HW status and ensure the x87 and (optional)
66 	 * SSE control words are (will be) set correctly.
67 	 */
68 	if ((_sse_hw = _private_sysi86(SI86FPSTART,
69 	    &_fp_hw, FPU_CW_INIT, SSE_MXCSR_INIT)) == -1) {
70 		extern void _putcw();
71 
72 		/*
73 		 * (fallback to old syscall on old kernels)
74 		 */
75 		_sse_hw = 0;
76 		(void) _private_sysi86(SI86FPHW, &_fp_hw);
77 		_putcw(0x133f);
78 	}
79 
80 	/*
81 	 * At this point the x87 fp environment that has been (or more
82 	 * hopefully, will be) established by the kernel is:
83 	 *
84 	 * affine infinity	0x1000
85 	 * round to nearest	0x0000
86 	 * 64-bit doubles	0x0300
87 	 * precision, underflow, overflow, zero-divide, denorm, invalid masked
88 	 *			0x003f
89 	 *
90 	 * which conforms to the 4th edition i386 ABI definition.
91 	 *
92 	 * Additionally, if we have SSE hardware, we've also masked all
93 	 * the same traps, and have round to nearest.
94 	 */
95 
96 	__flt_rounds = 1;	/* ANSI way of saying round-to-nearest */
97 }
98