xref: /freebsd/sys/i386/include/floatingpoint.h (revision 24a82630a20b64577bcc7454bec7288b1dbdb37a)
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  *
3324a82630SAndrew Moore  *	@(#) floatingpoint.h	1.0 (Berkeley) 9/23/93
3424a82630SAndrew Moore  */
3524a82630SAndrew Moore 
3624a82630SAndrew Moore /*
3724a82630SAndrew Moore  *	IEEE floating point structure and function definitions
3824a82630SAndrew Moore  */
3924a82630SAndrew Moore 
4024a82630SAndrew Moore #ifndef _FLOATINGPOINT_H_
4124a82630SAndrew Moore #define _FLOATINGPOINT_H_
4224a82630SAndrew Moore 
4324a82630SAndrew Moore #include <sys/cdefs.h>
4424a82630SAndrew Moore #include <sys/ieeefp.h>
4524a82630SAndrew Moore 
4624a82630SAndrew Moore #ifdef __GNUC__
4724a82630SAndrew Moore 
4824a82630SAndrew Moore #define fnstcw(addr)	__asm("fnstcw %0" : "=m" (*addr) : "0" (*addr))
4924a82630SAndrew Moore #define fnstsw(addr)	__asm("fnstsw %0" : "=m" (*addr) : "0" (*addr))
5024a82630SAndrew Moore #define fnstenv(addr)	__asm("fnstenv %0" : "=m" (*addr) : "0" (*addr))
5124a82630SAndrew Moore #define fldenv(addr)	__asm("fldenv %0" : : "m" (*addr))
5224a82630SAndrew Moore 
5324a82630SAndrew Moore #ifdef __i386__
5424a82630SAndrew Moore 
5524a82630SAndrew Moore /*
5624a82630SAndrew Moore  * return the contents of a FP register
5724a82630SAndrew Moore  */
5824a82630SAndrew Moore static __inline__
5924a82630SAndrew Moore __fpgetreg(reg)
6024a82630SAndrew Moore {
6124a82630SAndrew Moore 	unsigned short mem;
6224a82630SAndrew Moore 
6324a82630SAndrew Moore 	switch(reg) {
6424a82630SAndrew Moore 	default:
6524a82630SAndrew Moore 		fnstcw(&mem);
6624a82630SAndrew Moore 		break;
6724a82630SAndrew Moore 	case FP_STKY_REG:
6824a82630SAndrew Moore 		fnstsw(&mem);
6924a82630SAndrew Moore 		break;
7024a82630SAndrew Moore 	}
7124a82630SAndrew Moore 	return mem;
7224a82630SAndrew Moore }
7324a82630SAndrew Moore 
7424a82630SAndrew Moore /*
7524a82630SAndrew Moore  * set a FP mode; return previous mode
7624a82630SAndrew Moore  */
7724a82630SAndrew Moore static __inline__
7824a82630SAndrew Moore __fpsetreg(m, reg, fld, off)
7924a82630SAndrew Moore {
8024a82630SAndrew Moore 	unsigned env[7];
8124a82630SAndrew Moore 	unsigned p;
8224a82630SAndrew Moore 
8324a82630SAndrew Moore 	fnstenv(env);
8424a82630SAndrew Moore 	p =  (env[reg] & fld) >> off;
8524a82630SAndrew Moore 	env[reg] = (env[reg] & ~fld) | (m << off & fld);
8624a82630SAndrew Moore 	fldenv(env);
8724a82630SAndrew Moore 	return p;
8824a82630SAndrew Moore }
8924a82630SAndrew Moore 
9024a82630SAndrew Moore #endif /* __i386__ */
9124a82630SAndrew Moore 
9224a82630SAndrew Moore #endif /* __GNUC__ */
9324a82630SAndrew Moore 
9424a82630SAndrew Moore /*
9524a82630SAndrew Moore  * SysV/386 FP control interface
9624a82630SAndrew Moore  */
9724a82630SAndrew Moore #define fpgetround()	((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF)
9824a82630SAndrew Moore #define fpsetround(m)	__fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF)
9924a82630SAndrew Moore #define fpgetprec()	((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF)
10024a82630SAndrew Moore #define fpsetprec(m)	__fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF)
10124a82630SAndrew Moore #define fpgetmask()	((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF)
10224a82630SAndrew Moore #define fpsetmask(m)	__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)
10324a82630SAndrew Moore #define fpgetsticky()	((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF)
10424a82630SAndrew Moore #define fpresetsticky(m)	__fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF)
10524a82630SAndrew Moore #define fpsetsticky(m)	fpresetsticky(m)
10624a82630SAndrew Moore 
10724a82630SAndrew Moore #endif /* !_FLOATINGPOINT_H_ */
108