xref: /freebsd/sys/i386/include/floatingpoint.h (revision 50f44fa07d13ef9c4228caca160f3a5e15fac11a)
124a82630SAndrew Moore /*-
224a82630SAndrew Moore  * Copyright (c) 1993 Andrew Moore, Talke Studio
324a82630SAndrew Moore  * All rights reserved.
424a82630SAndrew Moore  *
524a82630SAndrew Moore  * Redistribution and use in source and binary forms, with or without
624a82630SAndrew Moore  * modification, are permitted provided that the following conditions
724a82630SAndrew Moore  * are met:
824a82630SAndrew Moore  * 1. Redistributions of source code must retain the above copyright
924a82630SAndrew Moore  *    notice, this list of conditions and the following disclaimer.
1024a82630SAndrew Moore  * 2. Redistributions in binary form must reproduce the above copyright
1124a82630SAndrew Moore  *    notice, this list of conditions and the following disclaimer in the
1224a82630SAndrew Moore  *    documentation and/or other materials provided with the distribution.
1324a82630SAndrew Moore  * 3. All advertising materials mentioning features or use of this software
1424a82630SAndrew Moore  *    must display the following acknowledgement:
1524a82630SAndrew Moore  *	This product includes software developed by the University of
1624a82630SAndrew Moore  *	California, Berkeley and its contributors.
1724a82630SAndrew Moore  * 4. Neither the name of the University nor the names of its contributors
1824a82630SAndrew Moore  *    may be used to endorse or promote products derived from this software
1924a82630SAndrew Moore  *    without specific prior written permission.
2024a82630SAndrew Moore  *
2124a82630SAndrew Moore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2224a82630SAndrew Moore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2324a82630SAndrew Moore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2424a82630SAndrew Moore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2524a82630SAndrew Moore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2624a82630SAndrew Moore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2724a82630SAndrew Moore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2824a82630SAndrew Moore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2924a82630SAndrew Moore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3024a82630SAndrew Moore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3124a82630SAndrew Moore  * SUCH DAMAGE.
3224a82630SAndrew Moore  *
3334a8ed1bSRodney W. Grimes  *	from: @(#) floatingpoint.h	1.0 (Berkeley) 9/23/93
3450f44fa0SGarrett Wollman  *	$Id: floatingpoint.h,v 1.4 1993/11/07 17:42:55 wollman Exp $
3524a82630SAndrew Moore  */
3624a82630SAndrew Moore 
3724a82630SAndrew Moore /*
3824a82630SAndrew Moore  *	IEEE floating point structure and function definitions
3924a82630SAndrew Moore  */
4024a82630SAndrew Moore 
4124a82630SAndrew Moore #ifndef _FLOATINGPOINT_H_
4224a82630SAndrew Moore #define _FLOATINGPOINT_H_
4324a82630SAndrew Moore 
4424a82630SAndrew Moore #include <sys/cdefs.h>
4550f44fa0SGarrett Wollman #include <machine/ieeefp.h>
4624a82630SAndrew Moore 
4724a82630SAndrew Moore #ifdef __GNUC__
4824a82630SAndrew Moore 
496e393973SGarrett Wollman #ifdef __i386__
506e393973SGarrett Wollman 
5124a82630SAndrew Moore #define fnstcw(addr)	__asm("fnstcw %0" : "=m" (*addr) : "0" (*addr))
5224a82630SAndrew Moore #define fnstsw(addr)	__asm("fnstsw %0" : "=m" (*addr) : "0" (*addr))
5324a82630SAndrew Moore #define fnstenv(addr)	__asm("fnstenv %0" : "=m" (*addr) : "0" (*addr))
5424a82630SAndrew Moore #define fldenv(addr)	__asm("fldenv %0" : : "m" (*addr))
5524a82630SAndrew Moore 
5624a82630SAndrew Moore 
5724a82630SAndrew Moore /*
5824a82630SAndrew Moore  * return the contents of a FP register
5924a82630SAndrew Moore  */
606bef4df5SAndrew Moore static __inline__ int
616bef4df5SAndrew Moore __fpgetreg(int _reg)
6224a82630SAndrew Moore {
636bef4df5SAndrew Moore 	unsigned short _mem;
6424a82630SAndrew Moore 
656bef4df5SAndrew Moore 	switch(_reg) {
6624a82630SAndrew Moore 	default:
676bef4df5SAndrew Moore 		fnstcw(&_mem);
6824a82630SAndrew Moore 		break;
6924a82630SAndrew Moore 	case FP_STKY_REG:
706bef4df5SAndrew Moore 		fnstsw(&_mem);
7124a82630SAndrew Moore 		break;
7224a82630SAndrew Moore 	}
736bef4df5SAndrew Moore 	return _mem;
7424a82630SAndrew Moore }
7524a82630SAndrew Moore 
7624a82630SAndrew Moore /*
7724a82630SAndrew Moore  * set a FP mode; return previous mode
7824a82630SAndrew Moore  */
796bef4df5SAndrew Moore static __inline__ int
806bef4df5SAndrew Moore __fpsetreg(int _m, int _reg, int _fld, int _off)
8124a82630SAndrew Moore {
826bef4df5SAndrew Moore 	unsigned _env[7];
836bef4df5SAndrew Moore 	unsigned _p;
8424a82630SAndrew Moore 
856bef4df5SAndrew Moore 	fnstenv(_env);
866bef4df5SAndrew Moore 	_p =  (_env[_reg] & _fld) >> _off;
876bef4df5SAndrew Moore 	_env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld);
886bef4df5SAndrew Moore 	fldenv(_env);
896bef4df5SAndrew Moore 	return _p;
9024a82630SAndrew Moore }
9124a82630SAndrew Moore 
9224a82630SAndrew Moore #endif /* __i386__ */
9324a82630SAndrew Moore 
9424a82630SAndrew Moore #endif /* __GNUC__ */
9524a82630SAndrew Moore 
9624a82630SAndrew Moore /*
9724a82630SAndrew Moore  * SysV/386 FP control interface
9824a82630SAndrew Moore  */
9924a82630SAndrew Moore #define fpgetround()	((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF)
10024a82630SAndrew Moore #define fpsetround(m)	__fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF)
10124a82630SAndrew Moore #define fpgetprec()	((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF)
10224a82630SAndrew Moore #define fpsetprec(m)	__fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF)
10324a82630SAndrew Moore #define fpgetmask()	((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF)
10424a82630SAndrew Moore #define fpsetmask(m)	__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)
10524a82630SAndrew Moore #define fpgetsticky()	((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF)
10624a82630SAndrew Moore #define fpresetsticky(m)	__fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF)
10724a82630SAndrew Moore #define fpsetsticky(m)	fpresetsticky(m)
10824a82630SAndrew Moore 
10924a82630SAndrew Moore #endif /* !_FLOATINGPOINT_H_ */
110