xref: /titanic_52/usr/src/lib/libc/i386/fp/fpstart.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Establish the default settings for the floating-point state for a C language
377c478bd9Sstevel@tonic-gate  * program:
387c478bd9Sstevel@tonic-gate  *	rounding mode		-- round to nearest default by OS,
397c478bd9Sstevel@tonic-gate  *	exceptions enabled	-- all masked
407c478bd9Sstevel@tonic-gate  *	sticky bits		-- all clear by default by OS.
417c478bd9Sstevel@tonic-gate  *      precision control       -- double extended
427c478bd9Sstevel@tonic-gate  * Set _fp_hw according to what floating-point hardware is available.
437c478bd9Sstevel@tonic-gate  * Set _sse_hw according to what SSE hardware is available.
447c478bd9Sstevel@tonic-gate  * Set __flt_rounds according to the rounding mode.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #pragma weak _fpstart = __fpstart
487c478bd9Sstevel@tonic-gate 
49*7257d1b4Sraf #include	"lint.h"
507c478bd9Sstevel@tonic-gate #include	<sys/types.h>
517c478bd9Sstevel@tonic-gate #include	<sys/sysi86.h>	/* for SI86FPHW/SI86FPSTART definitions */
527c478bd9Sstevel@tonic-gate #include	<sys/fp.h>	/* for FPU_CW_INIT and SSE_MXCSR_INIT */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate extern int	__fltrounds();
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate int	_fp_hw;			/* default: bss: 0 == no hardware */
577c478bd9Sstevel@tonic-gate int	_sse_hw;		/* default: bss: 0 == no sse */
587c478bd9Sstevel@tonic-gate int	__flt_rounds;		/* ANSI rounding mode */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
617c478bd9Sstevel@tonic-gate __fpstart()
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * query OS for HW status and ensure the x87 and (optional)
657c478bd9Sstevel@tonic-gate 	 * SSE control words are (will be) set correctly.
667c478bd9Sstevel@tonic-gate 	 */
678cd45542Sraf 	if ((_sse_hw = sysi86(SI86FPSTART,
687c478bd9Sstevel@tonic-gate 	    &_fp_hw, FPU_CW_INIT, SSE_MXCSR_INIT)) == -1) {
697c478bd9Sstevel@tonic-gate 		extern void _putcw();
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		/*
727c478bd9Sstevel@tonic-gate 		 * (fallback to old syscall on old kernels)
737c478bd9Sstevel@tonic-gate 		 */
747c478bd9Sstevel@tonic-gate 		_sse_hw = 0;
758cd45542Sraf 		(void) sysi86(SI86FPHW, &_fp_hw);
767c478bd9Sstevel@tonic-gate 		_putcw(0x133f);
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * At this point the x87 fp environment that has been (or more
817c478bd9Sstevel@tonic-gate 	 * hopefully, will be) established by the kernel is:
827c478bd9Sstevel@tonic-gate 	 *
837c478bd9Sstevel@tonic-gate 	 * affine infinity	0x1000
847c478bd9Sstevel@tonic-gate 	 * round to nearest	0x0000
857c478bd9Sstevel@tonic-gate 	 * 64-bit doubles	0x0300
867c478bd9Sstevel@tonic-gate 	 * precision, underflow, overflow, zero-divide, denorm, invalid masked
877c478bd9Sstevel@tonic-gate 	 *			0x003f
887c478bd9Sstevel@tonic-gate 	 *
897c478bd9Sstevel@tonic-gate 	 * which conforms to the 4th edition i386 ABI definition.
907c478bd9Sstevel@tonic-gate 	 *
917c478bd9Sstevel@tonic-gate 	 * Additionally, if we have SSE hardware, we've also masked all
927c478bd9Sstevel@tonic-gate 	 * the same traps, and have round to nearest.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	__flt_rounds = 1;	/* ANSI way of saying round-to-nearest */
967c478bd9Sstevel@tonic-gate }
97