xref: /illumos-gate/usr/src/lib/libc/i386/fp/fpstart.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58cd45542Sraf  * Common Development and Distribution License (the "License").
68cd45542Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
218cd45542Sraf 
228cd45542Sraf /*
238cd45542Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
248cd45542Sraf  * Use is subject to license terms.
258cd45542Sraf  */
268cd45542Sraf 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
297c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
307c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
34*ed093b41SRobert Mustacchi  * Copyright 2023 Oxide Computer Company
35*ed093b41SRobert Mustacchi  */
36*ed093b41SRobert Mustacchi 
37*ed093b41SRobert Mustacchi /*
387c478bd9Sstevel@tonic-gate  * Establish the default settings for the floating-point state for a C language
397c478bd9Sstevel@tonic-gate  * program:
407c478bd9Sstevel@tonic-gate  *	rounding mode		-- round to nearest default by OS,
417c478bd9Sstevel@tonic-gate  *	exceptions enabled	-- all masked
427c478bd9Sstevel@tonic-gate  *	sticky bits		-- all clear by default by OS.
437c478bd9Sstevel@tonic-gate  *      precision control       -- double extended
447c478bd9Sstevel@tonic-gate  * Set _fp_hw according to what floating-point hardware is available.
457c478bd9Sstevel@tonic-gate  * Set _sse_hw according to what SSE hardware is available.
467c478bd9Sstevel@tonic-gate  * Set __flt_rounds according to the rounding mode.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #pragma weak _fpstart = __fpstart
507c478bd9Sstevel@tonic-gate 
517257d1b4Sraf #include	"lint.h"
527c478bd9Sstevel@tonic-gate #include	<sys/types.h>
537c478bd9Sstevel@tonic-gate #include	<sys/sysi86.h>	/* for SI86FPHW/SI86FPSTART definitions */
547c478bd9Sstevel@tonic-gate #include	<sys/fp.h>	/* for FPU_CW_INIT and SSE_MXCSR_INIT */
55*ed093b41SRobert Mustacchi #include	<upanic.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate extern int	__fltrounds();
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int	_fp_hw;			/* default: bss: 0 == no hardware */
607c478bd9Sstevel@tonic-gate int	_sse_hw;		/* default: bss: 0 == no sse */
617c478bd9Sstevel@tonic-gate int	__flt_rounds;		/* ANSI rounding mode */
627c478bd9Sstevel@tonic-gate 
63*ed093b41SRobert Mustacchi #define	UPANIC_MSG	"32-bit FPU init failed!"
64*ed093b41SRobert Mustacchi 
657c478bd9Sstevel@tonic-gate void
__fpstart()667c478bd9Sstevel@tonic-gate __fpstart()
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	/*
697c478bd9Sstevel@tonic-gate 	 * query OS for HW status and ensure the x87 and (optional)
707c478bd9Sstevel@tonic-gate 	 * SSE control words are (will be) set correctly.
717c478bd9Sstevel@tonic-gate 	 */
728cd45542Sraf 	if ((_sse_hw = sysi86(SI86FPSTART,
737c478bd9Sstevel@tonic-gate 	    &_fp_hw, FPU_CW_INIT, SSE_MXCSR_INIT)) == -1) {
747c478bd9Sstevel@tonic-gate 		/*
75*ed093b41SRobert Mustacchi 		 * Because the system only supports a 64-bit kernel, the above
76*ed093b41SRobert Mustacchi 		 * should always work (we assume it does in 64-bit libc). If it
77*ed093b41SRobert Mustacchi 		 * should fail, we upanic. Note, because the FPU state is
78*ed093b41SRobert Mustacchi 		 * unknown, we use a static message below and don't rely upon
79*ed093b41SRobert Mustacchi 		 * strlen(), but rather compile time lengths.
807c478bd9Sstevel@tonic-gate 		 */
81*ed093b41SRobert Mustacchi 		upanic(UPANIC_MSG, sizeof (UPANIC_MSG));
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * At this point the x87 fp environment that has been (or more
867c478bd9Sstevel@tonic-gate 	 * hopefully, will be) established by the kernel is:
877c478bd9Sstevel@tonic-gate 	 *
887c478bd9Sstevel@tonic-gate 	 * affine infinity	0x1000
897c478bd9Sstevel@tonic-gate 	 * round to nearest	0x0000
907c478bd9Sstevel@tonic-gate 	 * 64-bit doubles	0x0300
917c478bd9Sstevel@tonic-gate 	 * precision, underflow, overflow, zero-divide, denorm, invalid masked
927c478bd9Sstevel@tonic-gate 	 *			0x003f
937c478bd9Sstevel@tonic-gate 	 *
947c478bd9Sstevel@tonic-gate 	 * which conforms to the 4th edition i386 ABI definition.
957c478bd9Sstevel@tonic-gate 	 *
967c478bd9Sstevel@tonic-gate 	 * Additionally, if we have SSE hardware, we've also masked all
977c478bd9Sstevel@tonic-gate 	 * the same traps, and have round to nearest.
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	__flt_rounds = 1;	/* ANSI way of saying round-to-nearest */
1017c478bd9Sstevel@tonic-gate }
102