xref: /freebsd/sys/i386/include/floatingpoint.h (revision 34a8ed1be138603eb7679d15429782f272bf2b7d)
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
3434a8ed1bSRodney W. Grimes  *	$Id$
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>
4524a82630SAndrew Moore #include <sys/ieeefp.h>
4624a82630SAndrew Moore 
4724a82630SAndrew Moore #ifdef __GNUC__
4824a82630SAndrew Moore 
4924a82630SAndrew Moore #define fnstcw(addr)	__asm("fnstcw %0" : "=m" (*addr) : "0" (*addr))
5024a82630SAndrew Moore #define fnstsw(addr)	__asm("fnstsw %0" : "=m" (*addr) : "0" (*addr))
5124a82630SAndrew Moore #define fnstenv(addr)	__asm("fnstenv %0" : "=m" (*addr) : "0" (*addr))
5224a82630SAndrew Moore #define fldenv(addr)	__asm("fldenv %0" : : "m" (*addr))
5324a82630SAndrew Moore 
5424a82630SAndrew Moore #ifdef __i386__
5524a82630SAndrew Moore 
5624a82630SAndrew Moore /*
5724a82630SAndrew Moore  * return the contents of a FP register
5824a82630SAndrew Moore  */
596bef4df5SAndrew Moore static __inline__ int
606bef4df5SAndrew Moore __fpgetreg(int _reg)
6124a82630SAndrew Moore {
626bef4df5SAndrew Moore 	unsigned short _mem;
6324a82630SAndrew Moore 
646bef4df5SAndrew Moore 	switch(_reg) {
6524a82630SAndrew Moore 	default:
666bef4df5SAndrew Moore 		fnstcw(&_mem);
6724a82630SAndrew Moore 		break;
6824a82630SAndrew Moore 	case FP_STKY_REG:
696bef4df5SAndrew Moore 		fnstsw(&_mem);
7024a82630SAndrew Moore 		break;
7124a82630SAndrew Moore 	}
726bef4df5SAndrew Moore 	return _mem;
7324a82630SAndrew Moore }
7424a82630SAndrew Moore 
7524a82630SAndrew Moore /*
7624a82630SAndrew Moore  * set a FP mode; return previous mode
7724a82630SAndrew Moore  */
786bef4df5SAndrew Moore static __inline__ int
796bef4df5SAndrew Moore __fpsetreg(int _m, int _reg, int _fld, int _off)
8024a82630SAndrew Moore {
816bef4df5SAndrew Moore 	unsigned _env[7];
826bef4df5SAndrew Moore 	unsigned _p;
8324a82630SAndrew Moore 
846bef4df5SAndrew Moore 	fnstenv(_env);
856bef4df5SAndrew Moore 	_p =  (_env[_reg] & _fld) >> _off;
866bef4df5SAndrew Moore 	_env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld);
876bef4df5SAndrew Moore 	fldenv(_env);
886bef4df5SAndrew Moore 	return _p;
8924a82630SAndrew Moore }
9024a82630SAndrew Moore 
9124a82630SAndrew Moore #endif /* __i386__ */
9224a82630SAndrew Moore 
9324a82630SAndrew Moore #endif /* __GNUC__ */
9424a82630SAndrew Moore 
9524a82630SAndrew Moore /*
9624a82630SAndrew Moore  * SysV/386 FP control interface
9724a82630SAndrew Moore  */
9824a82630SAndrew Moore #define fpgetround()	((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF)
9924a82630SAndrew Moore #define fpsetround(m)	__fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF)
10024a82630SAndrew Moore #define fpgetprec()	((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF)
10124a82630SAndrew Moore #define fpsetprec(m)	__fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF)
10224a82630SAndrew Moore #define fpgetmask()	((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF)
10324a82630SAndrew Moore #define fpsetmask(m)	__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)
10424a82630SAndrew Moore #define fpgetsticky()	((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF)
10524a82630SAndrew Moore #define fpresetsticky(m)	__fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF)
10624a82630SAndrew Moore #define fpsetsticky(m)	fpresetsticky(m)
10724a82630SAndrew Moore 
10824a82630SAndrew Moore #endif /* !_FLOATINGPOINT_H_ */
109