11834Swollman/*-
2175232Sbde * Copyright (c) 2003 Peter Wemm.
31834Swollman * Copyright (c) 1990 Andrew Moore, Talke Studio
41834Swollman * All rights reserved.
51834Swollman *
61834Swollman * Redistribution and use in source and binary forms, with or without
71834Swollman * modification, are permitted provided that the following conditions
81834Swollman * are met:
91834Swollman * 1. Redistributions of source code must retain the above copyright
101834Swollman *    notice, this list of conditions and the following disclaimer.
111834Swollman * 2. Redistributions in binary form must reproduce the above copyright
121834Swollman *    notice, this list of conditions and the following disclaimer in the
131834Swollman *    documentation and/or other materials provided with the distribution.
141834Swollman * 3. All advertising materials mentioning features or use of this software
151834Swollman *    must display the following acknowledgement:
161834Swollman *	This product includes software developed by the University of
171834Swollman *	California, Berkeley and its contributors.
181834Swollman * 4. Neither the name of the University nor the names of its contributors
191834Swollman *    may be used to endorse or promote products derived from this software
201834Swollman *    without specific prior written permission.
211834Swollman *
221834Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231834Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241834Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251834Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261834Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271834Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281834Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291834Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301834Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311834Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321834Swollman * SUCH DAMAGE.
331834Swollman *
341834Swollman * 	from: @(#) ieeefp.h 	1.0 (Berkeley) 9/23/93
3550477Speter * $FreeBSD$
361834Swollman */
371834Swollman
38175232Sbde#ifndef _MACHINE_IEEEFP_H_
39175232Sbde#define _MACHINE_IEEEFP_H_
40175232Sbde
411834Swollman/*
42226607Sdas * Deprecated historical FPU control interface
43226607Sdas *
44175232Sbde * IEEE floating point type, constant and function definitions.
45175234Sbde * XXX: FP*FLD and FP*OFF are undocumented pollution.
461834Swollman */
471834Swollman
48143063Sjoerg#ifndef _SYS_CDEFS_H_
49143063Sjoerg#error this file needs sys/cdefs.h as a prerequisite
50143063Sjoerg#endif
51143063Sjoerg
521834Swollman/*
53175232Sbde * Rounding modes.
541834Swollman */
551834Swollmantypedef enum {
561834Swollman	FP_RN=0,	/* round to nearest */
57175232Sbde	FP_RM,		/* round down towards minus infinity */
58175232Sbde	FP_RP,		/* round up towards plus infinity */
591834Swollman	FP_RZ		/* truncate */
601834Swollman} fp_rnd_t;
611834Swollman
621834Swollman/*
63175232Sbde * Precision (i.e., rounding precision) modes.
641834Swollman */
651834Swollmantypedef enum {
6613765Smpp	FP_PS=0,	/* 24 bit (single-precision) */
671834Swollman	FP_PRS,		/* reserved */
681834Swollman	FP_PD,		/* 53 bit (double-precision) */
6913765Smpp	FP_PE		/* 64 bit (extended-precision) */
701834Swollman} fp_prec_t;
711834Swollman
721834Swollman#define fp_except_t	int
731834Swollman
741834Swollman/*
75175232Sbde * Exception bit masks.
761834Swollman */
771834Swollman#define FP_X_INV	0x01	/* invalid operation */
781834Swollman#define FP_X_DNML	0x02	/* denormal */
791834Swollman#define FP_X_DZ		0x04	/* zero divide */
801834Swollman#define FP_X_OFL	0x08	/* overflow */
811834Swollman#define FP_X_UFL	0x10	/* underflow */
821834Swollman#define FP_X_IMP	0x20	/* (im)precision */
8349081Scracauer#define FP_X_STK	0x40	/* stack fault */
841834Swollman
851834Swollman/*
86175232Sbde * FPU control word bit-field masks.
871834Swollman */
881834Swollman#define FP_MSKS_FLD	0x3f	/* exception masks field */
891834Swollman#define FP_PRC_FLD	0x300	/* precision control field */
90175232Sbde#define	FP_RND_FLD	0xc00	/* rounding control field */
91175232Sbde
92175232Sbde/*
93175232Sbde * FPU status word bit-field masks.
94175232Sbde */
951834Swollman#define FP_STKY_FLD	0x3f	/* sticky flags field */
961834Swollman
971834Swollman/*
98175232Sbde * FPU control word bit-field offsets (shift counts).
991834Swollman */
1001834Swollman#define FP_MSKS_OFF	0	/* exception masks offset */
1011834Swollman#define FP_PRC_OFF	8	/* precision control offset */
102175232Sbde#define	FP_RND_OFF	10	/* rounding control offset */
103175232Sbde
104175232Sbde/*
105175232Sbde * FPU status word bit-field offsets (shift counts).
106175232Sbde */
1071834Swollman#define FP_STKY_OFF	0	/* sticky flags offset */
1081834Swollman
109143063Sjoerg#ifdef __GNUCLIKE_ASM
110109520Smarcel
111175234Sbde#define	__fldcw(addr)	__asm __volatile("fldcw %0" : : "m" (*(addr)))
112109520Smarcel#define	__fldenv(addr)	__asm __volatile("fldenv %0" : : "m" (*(addr)))
113175233Sbde#define	__fnclex()	__asm __volatile("fnclex")
114175232Sbde#define	__fnstcw(addr)	__asm __volatile("fnstcw %0" : "=m" (*(addr)))
115109520Smarcel#define	__fnstenv(addr)	__asm __volatile("fnstenv %0" : "=m" (*(addr)))
116109520Smarcel#define	__fnstsw(addr)	__asm __volatile("fnstsw %0" : "=m" (*(addr)))
117109520Smarcel
118109520Smarcel/*
119175234Sbde * Load the control word.  Be careful not to trap if there is a currently
120175234Sbde * unmasked exception (ones that will become freshly unmasked are not a
121175234Sbde * problem).  This case must be handled by a save/restore of the
122175234Sbde * environment or even of the full x87 state.  Accessing the environment
123175234Sbde * is very inefficient, so only do it when necessary.
124109520Smarcel */
125175234Sbdestatic __inline void
126175234Sbde__fnldcw(unsigned short _cw, unsigned short _newcw)
127109520Smarcel{
128175234Sbde	struct {
129175234Sbde		unsigned _cw;
130175234Sbde		unsigned _other[6];
131175234Sbde	} _env;
132175234Sbde	unsigned short _sw;
133109520Smarcel
134175234Sbde	if ((_cw & FP_MSKS_FLD) != FP_MSKS_FLD) {
135175234Sbde		__fnstsw(&_sw);
136175234Sbde		if (((_sw & ~_cw) & FP_STKY_FLD) != 0) {
137175234Sbde			__fnstenv(&_env);
138175234Sbde			_env._cw = _newcw;
139175234Sbde			__fldenv(&_env);
140175234Sbde			return;
141175234Sbde		}
142109520Smarcel	}
143175234Sbde	__fldcw(&_newcw);
144109520Smarcel}
145109520Smarcel
146175234Sbdestatic __inline fp_rnd_t
147175234Sbdefpgetround(void)
148109520Smarcel{
149175234Sbde	unsigned short _cw;
150109520Smarcel
151175234Sbde	__fnstcw(&_cw);
152175234Sbde	return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF));
153109520Smarcel}
154109520Smarcel
155175234Sbdestatic __inline fp_rnd_t
156175234Sbdefpsetround(fp_rnd_t _m)
157175234Sbde{
158175234Sbde	fp_rnd_t _p;
159175234Sbde	unsigned short _cw, _newcw;
160175234Sbde
161175234Sbde	__fnstcw(&_cw);
162175234Sbde	_p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF);
163175234Sbde	_newcw = _cw & ~FP_RND_FLD;
164175234Sbde	_newcw |= (_m << FP_RND_OFF) & FP_RND_FLD;
165175234Sbde	__fnldcw(_cw, _newcw);
166175234Sbde	return (_p);
167175234Sbde}
168175234Sbde
169175234Sbdestatic __inline fp_prec_t
170175234Sbdefpgetprec(void)
171175234Sbde{
172175234Sbde	unsigned short _cw;
173175234Sbde
174175234Sbde	__fnstcw(&_cw);
175175234Sbde	return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF));
176175234Sbde}
177175234Sbde
178175234Sbdestatic __inline fp_prec_t
179175234Sbdefpsetprec(fp_prec_t _m)
180175234Sbde{
181175234Sbde	fp_prec_t _p;
182175234Sbde	unsigned short _cw, _newcw;
183175234Sbde
184175234Sbde	__fnstcw(&_cw);
185175234Sbde	_p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
186175234Sbde	_newcw = _cw & ~FP_PRC_FLD;
187175234Sbde	_newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
188175234Sbde	__fnldcw(_cw, _newcw);
189175234Sbde	return (_p);
190175234Sbde}
191175234Sbde
192109520Smarcel/*
193175234Sbde * Get or set the exception mask.
194175234Sbde * Note that the x87 mask bits are inverted by the API -- a mask bit of 1
195175234Sbde * means disable for x87 and SSE, but for fp*mask() it means enable.
196109520Smarcel */
197109520Smarcel
198175233Sbdestatic __inline fp_except_t
199175234Sbdefpgetmask(void)
200175234Sbde{
201175234Sbde	unsigned short _cw;
202175234Sbde
203175234Sbde	__fnstcw(&_cw);
204175234Sbde	return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF);
205175234Sbde}
206175234Sbde
207175234Sbdestatic __inline fp_except_t
208175234Sbdefpsetmask(fp_except_t _m)
209175234Sbde{
210175234Sbde	fp_except_t _p;
211175234Sbde	unsigned short _cw, _newcw;
212175234Sbde
213175234Sbde	__fnstcw(&_cw);
214175234Sbde	_p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF;
215175234Sbde	_newcw = _cw & ~FP_MSKS_FLD;
216175234Sbde	_newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD;
217175234Sbde	__fnldcw(_cw, _newcw);
218175234Sbde	return (_p);
219175234Sbde}
220175234Sbde
221175234Sbdestatic __inline fp_except_t
222175234Sbdefpgetsticky(void)
223175234Sbde{
224175234Sbde	unsigned _ex;
225175234Sbde	unsigned short _sw;
226175234Sbde
227175234Sbde	__fnstsw(&_sw);
228175234Sbde	_ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF;
229175234Sbde	return ((fp_except_t)_ex);
230175234Sbde}
231175234Sbde
232175234Sbdestatic __inline fp_except_t
233175233Sbdefpresetsticky(fp_except_t _m)
234175233Sbde{
235175233Sbde	struct {
236175233Sbde		unsigned _cw;
237175233Sbde		unsigned _sw;
238175233Sbde		unsigned _other[5];
239175233Sbde	} _env;
240175233Sbde	fp_except_t _p;
241175233Sbde
242175233Sbde	_m &= FP_STKY_FLD >> FP_STKY_OFF;
243175233Sbde	_p = fpgetsticky();
244175233Sbde	if ((_p & ~_m) == _p)
245175233Sbde		return (_p);
246175233Sbde	if ((_p & ~_m) == 0) {
247175233Sbde		__fnclex();
248175233Sbde		return (_p);
249175233Sbde	}
250175233Sbde	__fnstenv(&_env);
251175233Sbde	_env._sw &= ~_m;
252175233Sbde	__fldenv(&_env);
253175233Sbde	return (_p);
254175233Sbde}
255175233Sbde
256175233Sbde#endif /* __GNUCLIKE_ASM */
257175233Sbde
2581862Swollman#endif /* !_MACHINE_IEEEFP_H_ */
259