floatingpoint.h revision 621
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
34621Srgrimes *	$Id$
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>
45326Salm#include <sys/ieeefp.h>
46326Salm
47326Salm#ifdef __GNUC__
48326Salm
49326Salm#define fnstcw(addr)	__asm("fnstcw %0" : "=m" (*addr) : "0" (*addr))
50326Salm#define fnstsw(addr)	__asm("fnstsw %0" : "=m" (*addr) : "0" (*addr))
51326Salm#define fnstenv(addr)	__asm("fnstenv %0" : "=m" (*addr) : "0" (*addr))
52326Salm#define fldenv(addr)	__asm("fldenv %0" : : "m" (*addr))
53326Salm
54326Salm#ifdef __i386__
55326Salm
56326Salm/*
57326Salm * return the contents of a FP register
58326Salm */
59338Salmstatic __inline__ int
60338Salm__fpgetreg(int _reg)
61326Salm{
62338Salm	unsigned short _mem;
63326Salm
64338Salm	switch(_reg) {
65326Salm	default:
66338Salm		fnstcw(&_mem);
67326Salm		break;
68326Salm	case FP_STKY_REG:
69338Salm		fnstsw(&_mem);
70326Salm		break;
71326Salm	}
72338Salm	return _mem;
73326Salm}
74326Salm
75326Salm/*
76326Salm * set a FP mode; return previous mode
77326Salm */
78338Salmstatic __inline__ int
79338Salm__fpsetreg(int _m, int _reg, int _fld, int _off)
80326Salm{
81338Salm	unsigned _env[7];
82338Salm	unsigned _p;
83326Salm
84338Salm	fnstenv(_env);
85338Salm	_p =  (_env[_reg] & _fld) >> _off;
86338Salm	_env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld);
87338Salm	fldenv(_env);
88338Salm	return _p;
89326Salm}
90326Salm
91326Salm#endif /* __i386__ */
92326Salm
93326Salm#endif /* __GNUC__ */
94326Salm
95326Salm/*
96326Salm * SysV/386 FP control interface
97326Salm */
98326Salm#define fpgetround()	((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF)
99326Salm#define fpsetround(m)	__fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF)
100326Salm#define fpgetprec()	((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF)
101326Salm#define fpsetprec(m)	__fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF)
102326Salm#define fpgetmask()	((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF)
103326Salm#define fpsetmask(m)	__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)
104326Salm#define fpgetsticky()	((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF)
105326Salm#define fpresetsticky(m)	__fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF)
106326Salm#define fpsetsticky(m)	fpresetsticky(m)
107326Salm
108326Salm#endif /* !_FLOATINGPOINT_H_ */
109