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