floatingpoint.h revision 97713
1185029Spjd/*- 2185029Spjd * Copyright (c) 1993 Andrew Moore, Talke Studio 3185029Spjd * All rights reserved. 4185029Spjd * 5185029Spjd * Redistribution and use in source and binary forms, with or without 6185029Spjd * modification, are permitted provided that the following conditions 7185029Spjd * are met: 8185029Spjd * 1. Redistributions of source code must retain the above copyright 9185029Spjd * notice, this list of conditions and the following disclaimer. 10185029Spjd * 2. Redistributions in binary form must reproduce the above copyright 11185029Spjd * notice, this list of conditions and the following disclaimer in the 12185029Spjd * documentation and/or other materials provided with the distribution. 13185029Spjd * 3. All advertising materials mentioning features or use of this software 14185029Spjd * must display the following acknowledgement: 15185029Spjd * This product includes software developed by the University of 16185029Spjd * California, Berkeley and its contributors. 17185029Spjd * 4. Neither the name of the University nor the names of its contributors 18185029Spjd * may be used to endorse or promote products derived from this software 19185029Spjd * without specific prior written permission. 20185029Spjd * 21185029Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22185029Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23185029Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24185029Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25185029Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26185029Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27185029Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28185029Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29185029Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30185029Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31185029Spjd * SUCH DAMAGE. 32185029Spjd * 33240162Smm * from: @(#) floatingpoint.h 1.0 (Berkeley) 9/23/93 34240162Smm * $FreeBSD: head/sys/i386/include/floatingpoint.h 97713 2002-06-01 17:39:46Z bde $ 35185029Spjd */ 36185029Spjd 37185029Spjd#ifndef _FLOATINGPOINT_H_ 38185029Spjd#define _FLOATINGPOINT_H_ 39185029Spjd 40185029Spjd/* 41185029Spjd * IEEE floating point structure and function definitions 42240162Smm */ 43185029Spjd 44240162Smm/*- 45240162Smm * XXX the following undocumented pollution is exported: 46240162Smm * fpsetsticky(). 47240162Smm * FP*FLD, FP*OFF and FP*REG from <machine/ieeefp.h> 48230454Spjd */ 49185029Spjd 50185029Spjd#include <sys/cdefs.h> 51185029Spjd#include <machine/ieeefp.h> 52185029Spjd 53185029Spjd#ifdef __GNUC__ 54185029Spjd 55185029Spjd#define __fldenv(addr) __asm __volatile("fldenv %0" : : "m" (*(addr))) 56240162Smm#define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr))) 57185029Spjd#define __fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr))) 58185029Spjd#define __fnstsw(addr) __asm __volatile("fnstsw %0" : "=m" (*(addr))) 59185029Spjd 60219089Spjd/* 61219089Spjd * return the contents of a FP register 62209962Smm */ 63209962Smmstatic __inline__ int 64209962Smm__fpgetreg(int _reg) 65209962Smm{ 66209962Smm unsigned short _mem; 67219089Spjd 68219089Spjd /*- 69219089Spjd * This is more efficient than it looks. The switch gets optimized 70219089Spjd * away if _reg is constant. 71219089Spjd * 72219089Spjd * The default case only supports _reg == 0. We could handle more 73219089Spjd * registers (e.g., tags) using fnstenv, but the interface doesn't 74219089Spjd * support more. 75219089Spjd */ 76219089Spjd switch(_reg) { 77219089Spjd default: 78219089Spjd __fnstcw(&_mem); 79219089Spjd break; 80219089Spjd case FP_STKY_REG: 81219089Spjd __fnstsw(&_mem); 82219089Spjd break; 83219089Spjd } 84185029Spjd return _mem; 85} 86 87/* 88 * set a FP mode; return previous mode 89 */ 90static __inline__ int 91__fpsetreg(int _m, int _reg, int _fld, int _off) 92{ 93 unsigned _env[7]; 94 unsigned _p; 95 96 /* 97 * _reg == 0 could be handled better using fnstcw/fldcw. 98 */ 99 __fnstenv(_env); 100 _p = (_env[_reg] & _fld) >> _off; 101 _env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld); 102 __fldenv(_env); 103 return _p; 104} 105 106#endif /* __GNUC__ */ 107 108/* 109 * SysV/386 FP control interface 110 */ 111#define fpgetround() ((fp_rnd_t) \ 112 ((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF)) 113#define fpsetround(m) ((fp_rnd_t) \ 114 __fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF)) 115#define fpgetprec() ((fp_prec_t) \ 116 ((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF)) 117#define fpsetprec(m) ((fp_prec_t) \ 118 __fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF)) 119#define fpgetmask() ((fp_except_t) \ 120 ((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF)) 121#define fpsetmask(m) ((fp_except_t) \ 122 (~__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)) & \ 123 (FP_MSKS_FLD >> FP_MSKS_OFF)) 124#define fpgetsticky() ((fp_except_t) \ 125 ((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF)) 126#define fpresetsticky(m) ((fp_except_t) \ 127 __fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF)) 128#define fpsetsticky(m) fpresetsticky(m) 129 130#endif /* !_FLOATINGPOINT_H_ */ 131