floatingpoint.h revision 338
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 * 33326Salm * @(#) floatingpoint.h 1.0 (Berkeley) 9/23/93 34326Salm */ 35326Salm 36326Salm/* 37326Salm * IEEE floating point structure and function definitions 38326Salm */ 39326Salm 40326Salm#ifndef _FLOATINGPOINT_H_ 41326Salm#define _FLOATINGPOINT_H_ 42326Salm 43326Salm#include <sys/cdefs.h> 44326Salm#include <sys/ieeefp.h> 45326Salm 46326Salm#ifdef __GNUC__ 47326Salm 48326Salm#define fnstcw(addr) __asm("fnstcw %0" : "=m" (*addr) : "0" (*addr)) 49326Salm#define fnstsw(addr) __asm("fnstsw %0" : "=m" (*addr) : "0" (*addr)) 50326Salm#define fnstenv(addr) __asm("fnstenv %0" : "=m" (*addr) : "0" (*addr)) 51326Salm#define fldenv(addr) __asm("fldenv %0" : : "m" (*addr)) 52326Salm 53326Salm#ifdef __i386__ 54326Salm 55326Salm/* 56326Salm * return the contents of a FP register 57326Salm */ 58338Salmstatic __inline__ int 59338Salm__fpgetreg(int _reg) 60326Salm{ 61338Salm unsigned short _mem; 62326Salm 63338Salm switch(_reg) { 64326Salm default: 65338Salm fnstcw(&_mem); 66326Salm break; 67326Salm case FP_STKY_REG: 68338Salm fnstsw(&_mem); 69326Salm break; 70326Salm } 71338Salm return _mem; 72326Salm} 73326Salm 74326Salm/* 75326Salm * set a FP mode; return previous mode 76326Salm */ 77338Salmstatic __inline__ int 78338Salm__fpsetreg(int _m, int _reg, int _fld, int _off) 79326Salm{ 80338Salm unsigned _env[7]; 81338Salm unsigned _p; 82326Salm 83338Salm fnstenv(_env); 84338Salm _p = (_env[_reg] & _fld) >> _off; 85338Salm _env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld); 86338Salm fldenv(_env); 87338Salm return _p; 88326Salm} 89326Salm 90326Salm#endif /* __i386__ */ 91326Salm 92326Salm#endif /* __GNUC__ */ 93326Salm 94326Salm/* 95326Salm * SysV/386 FP control interface 96326Salm */ 97326Salm#define fpgetround() ((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF) 98326Salm#define fpsetround(m) __fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF) 99326Salm#define fpgetprec() ((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF) 100326Salm#define fpsetprec(m) __fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF) 101326Salm#define fpgetmask() ((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF) 102326Salm#define fpsetmask(m) __fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF) 103326Salm#define fpgetsticky() ((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF) 104326Salm#define fpresetsticky(m) __fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF) 105326Salm#define fpsetsticky(m) fpresetsticky(m) 106326Salm 107326Salm#endif /* !_FLOATINGPOINT_H_ */ 108