floatingpoint.h revision 1834
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
341834Swollman *	$Id: floatingpoint.h,v 1.4 1993/11/07 17:42:55 wollman Exp $
35326Salm */
36326Salm
37326Salm/*
38326Salm *	IEEE floating point structure and function definitions
39326Salm */
40326Salm
41326Salm#ifndef _FLOATINGPOINT_H_
42326Salm#define _FLOATINGPOINT_H_
43326Salm
44326Salm#include <sys/cdefs.h>
451834Swollman#include <machine/ieeefp.h>
46326Salm
47326Salm#ifdef __GNUC__
48326Salm
49719Swollman#ifdef __i386__
50719Swollman
51326Salm#define fnstcw(addr)	__asm("fnstcw %0" : "=m" (*addr) : "0" (*addr))
52326Salm#define fnstsw(addr)	__asm("fnstsw %0" : "=m" (*addr) : "0" (*addr))
53326Salm#define fnstenv(addr)	__asm("fnstenv %0" : "=m" (*addr) : "0" (*addr))
54326Salm#define fldenv(addr)	__asm("fldenv %0" : : "m" (*addr))
55326Salm
56326Salm
57326Salm/*
58326Salm * return the contents of a FP register
59326Salm */
60338Salmstatic __inline__ int
61338Salm__fpgetreg(int _reg)
62326Salm{
63338Salm	unsigned short _mem;
64326Salm
65338Salm	switch(_reg) {
66326Salm	default:
67338Salm		fnstcw(&_mem);
68326Salm		break;
69326Salm	case FP_STKY_REG:
70338Salm		fnstsw(&_mem);
71326Salm		break;
72326Salm	}
73338Salm	return _mem;
74326Salm}
75326Salm
76326Salm/*
77326Salm * set a FP mode; return previous mode
78326Salm */
79338Salmstatic __inline__ int
80338Salm__fpsetreg(int _m, int _reg, int _fld, int _off)
81326Salm{
82338Salm	unsigned _env[7];
83338Salm	unsigned _p;
84326Salm
85338Salm	fnstenv(_env);
86338Salm	_p =  (_env[_reg] & _fld) >> _off;
87338Salm	_env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld);
88338Salm	fldenv(_env);
89338Salm	return _p;
90326Salm}
91326Salm
92326Salm#endif /* __i386__ */
93326Salm
94326Salm#endif /* __GNUC__ */
95326Salm
96326Salm/*
97326Salm * SysV/386 FP control interface
98326Salm */
99326Salm#define fpgetround()	((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF)
100326Salm#define fpsetround(m)	__fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF)
101326Salm#define fpgetprec()	((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF)
102326Salm#define fpsetprec(m)	__fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF)
103326Salm#define fpgetmask()	((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF)
104326Salm#define fpsetmask(m)	__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)
105326Salm#define fpgetsticky()	((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF)
106326Salm#define fpresetsticky(m)	__fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF)
107326Salm#define fpsetsticky(m)	fpresetsticky(m)
108326Salm
109326Salm#endif /* !_FLOATINGPOINT_H_ */
110