floatingpoint.h revision 50477
1326Salm/*-
2326Salm * Copyright (c) 1993 Andrew Moore, Talke Studio
3326Salm * All rights reserved.
4326Salm *
5326Salm * Redistribution and use in source and binary forms, with or without
6326Salm * modification, are permitted provided that the following conditions
7326Salm * are met:
8326Salm * 1. Redistributions of source code must retain the above copyright
9326Salm *    notice, this list of conditions and the following disclaimer.
10326Salm * 2. Redistributions in binary form must reproduce the above copyright
11326Salm *    notice, this list of conditions and the following disclaimer in the
12326Salm *    documentation and/or other materials provided with the distribution.
13326Salm * 3. All advertising materials mentioning features or use of this software
14326Salm *    must display the following acknowledgement:
15326Salm *	This product includes software developed by the University of
16326Salm *	California, Berkeley and its contributors.
17326Salm * 4. Neither the name of the University nor the names of its contributors
18326Salm *    may be used to endorse or promote products derived from this software
19326Salm *    without specific prior written permission.
20326Salm *
21326Salm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22326Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23326Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24326Salm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25326Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26326Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27326Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28326Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29326Salm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30326Salm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31326Salm * SUCH DAMAGE.
32326Salm *
33621Srgrimes *	from: @(#) floatingpoint.h	1.0 (Berkeley) 9/23/93
3450477Speter * $FreeBSD: head/sys/i386/include/floatingpoint.h 50477 1999-08-28 01:08:13Z peter $
35326Salm */
36326Salm
372941Sbde#ifndef _FLOATINGPOINT_H_
382941Sbde#define _FLOATINGPOINT_H_
392941Sbde
40326Salm/*
412941Sbde * IEEE floating point structure and function definitions
42326Salm */
43326Salm
442941Sbde/*-
452941Sbde * XXX the following undocumented pollution is exported:
462941Sbde *	fpsetsticky().
472941Sbde *	FP*FLD, FP*OFF and FP*REG from <machine/ieeefp.h>
482941Sbde */
49326Salm
50326Salm#include <sys/cdefs.h>
511834Swollman#include <machine/ieeefp.h>
52326Salm
53326Salm#ifdef __GNUC__
54326Salm
552941Sbde#define __fldenv(addr)	__asm __volatile("fldenv %0" : : "m" (*(addr)))
562941Sbde#define __fnstenv(addr)	__asm __volatile("fnstenv %0" : "=m" (*(addr)))
572941Sbde#define __fnstcw(addr)	__asm __volatile("fnstcw %0" : "=m" (*(addr)))
582941Sbde#define __fnstsw(addr)	__asm __volatile("fnstsw %0" : "=m" (*(addr)))
59719Swollman
60326Salm/*
61326Salm * return the contents of a FP register
62326Salm */
63338Salmstatic __inline__ int
64338Salm__fpgetreg(int _reg)
65326Salm{
66338Salm	unsigned short _mem;
67326Salm
682941Sbde	/*-
692941Sbde	 * This is more efficient than it looks.  The switch gets optimized
702941Sbde	 * away if _reg is constant.
712941Sbde	 *
722941Sbde	 * The default case only supports _reg == 0.  We could handle more
732941Sbde	 * registers (e.g., tags) using fnstenv, but the interface doesn't
742941Sbde	 * support more.
752941Sbde	 */
76338Salm	switch(_reg) {
77326Salm	default:
782941Sbde		__fnstcw(&_mem);
79326Salm		break;
80326Salm	case FP_STKY_REG:
812941Sbde		__fnstsw(&_mem);
82326Salm		break;
83326Salm	}
84338Salm	return _mem;
85326Salm}
86326Salm
87326Salm/*
88326Salm * set a FP mode; return previous mode
89326Salm */
90338Salmstatic __inline__ int
91338Salm__fpsetreg(int _m, int _reg, int _fld, int _off)
92326Salm{
93338Salm	unsigned _env[7];
94338Salm	unsigned _p;
95326Salm
962941Sbde	/*
972941Sbde	 * _reg == 0 could be handled better using fnstcw/fldcw.
982941Sbde	 */
992941Sbde	__fnstenv(_env);
100338Salm	_p =  (_env[_reg] & _fld) >> _off;
101338Salm	_env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld);
1022941Sbde	__fldenv(_env);
103338Salm	return _p;
104326Salm}
105326Salm
106326Salm#endif /* __GNUC__ */
107326Salm
108326Salm/*
109326Salm * SysV/386 FP control interface
110326Salm */
11135477Sdes#define fpgetround()  ((fp_rnd_t) ((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF))
11235477Sdes#define fpsetround(m) ((fp_rnd_t) __fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF))
11335477Sdes#define fpgetprec()   ((fp_prec_t) ((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF))
11435477Sdes#define fpsetprec(m)  ((fp_prec_t) __fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF))
11535477Sdes#define fpgetmask()   ((fp_except_t) ((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF))
11635477Sdes#define fpsetmask(m)  ((fp_except_t) (__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)))
11735477Sdes#define fpgetsticky() ((fp_except_t) ((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF))
11835477Sdes#define fpresetsticky(m)      ((fp_except_t) __fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF))
119326Salm#define fpsetsticky(m)	fpresetsticky(m)
120326Salm
121326Salm#endif /* !_FLOATINGPOINT_H_ */
122