Return to BSD News archive
Newsgroups: comp.os.386bsd.questions Path: sserve!newshost.anu.edu.au!munnari.oz.au!news.Hawaii.Edu!ames!agate!spool.mu.edu!umn.edu!csus.edu!netcom.com!alm From: alm@netcom.com (Andrew Moore) Subject: Re: floating exception on netbsd 0.9 using cpicker Message-ID: <almCDLCyv.BML@netcom.com> Organization: Netcom Online Communications Services (408-241-9760 login: guest) References: <RAM.93Sep18194359@xor.epi.wisc.edu> <almCDKyJu.8wx@netcom.com> Date: Sun, 19 Sep 1993 08:01:43 GMT Lines: 559 In article <almCDKyJu.8wx@netcom.com> alm@netcom.com (Andrew Moore) writes: >In article <RAM.93Sep18194359@xor.epi.wisc.edu> ram@xor.epi.wisc.edu (Ram Bhamidipaty) writes: >> >>I grabbed cpicker from the net and compiled it cleanly, but when I run >>it I get the following error (it happens after clicking on a window >>and selecting a color). Has anyone else had this problem or managed >>to get the program to work? > >You might try disabling the floating point traps. See fpgetround(3) in >the FreeBSD code. These floating point rountines are contained in two >headers, and so should be usable under NetBSD. ---------------------------------------------------------- This message was not deliverable To: ram@epi.wisc.edu Reason: epi.wisc.edu is an unknown host or domain. ---------------------------------------------------------- Received: from netcom4.netcom.com by wigate.nic.wisc.edu; Sun, 19 Sep 93 02:39 CDT Received: by netcom4.netcom.com (5.65/SMI-4.1/Netcom) id AA11851; Sun, 19 Sep 93 00:39:34 -0700 Message-Id: <9309190739.AA11851@netcom4.netcom.com> Date: Sun, 19 Sep 93 00:39:33 -0700 From: Andrew Moore <alm@netcom.com> Subject: Re: floating exception on netbsd 0.9 using cpicker To: Ram Bhamidipaty <ram%epi.wisc.edu@macc.wisc.edu> CC: (Andrew Moore) <alm%netcom.com@macc.wisc.edu>, alm@netcom.com In-Reply-To: Your message of "Sun, 19 Sep 93 01:01:39 CDT." <9309190601.AA01719@xor.epi.wisc.edu> >Umm, since I'm running NetBSD it would be a bit hard to get a hold of them. >Any chance they could be sent to me? Keep in mind that your program might have a bug and that using fpgetround is the wrong approach. But some programs (usually math programs) use IEEE arithmetic which permits overflow and so on without causing the program to core. For these, it is necessary to disable the default floating-point traps using fpsetmask(3), or some other means. The test program, fptest2.c, should illustrate how this is used. floatingpoint.h goes in /usr/include ieeefp.h goes in /usr/include/sys fpgetround.3 goes in /usr/src/share/man/man3 (and is installed with links to fpsetround.3, etc...) -AM # This is a shell archive. Save it in a file, remove anything before # this line, and then unpack it by entering "sh file". Note, it may # create directories; files and directories will be owned by you and # have default permissions. # # This archive contains: # # Makefile # floatingpoint.h # fpgetround.3 # fptest2.c # ieeefp.h # echo x - Makefile sed 's/^X//' >Makefile << 'END-of-Makefile' XPROG= fptest2 XCC=g++ XSRCS= ${PROG}.c XCFLAGS=-g XLDADD= -lm X Xfptest2.o: fptest2.c floatingpoint.h ieeefp.h X X.include <bsd.prog.mk> X END-of-Makefile echo x - floatingpoint.h sed 's/^X//' >floatingpoint.h << 'END-of-floatingpoint.h' X/*- X * Copyright (c) 1993 Andrew Moore, Talke Studio X * All rights reserved. X * X * Redistribution and use in source and binary forms, with or without X * modification, are permitted provided that the following conditions X * are met: X * 1. Redistributions of source code must retain the above copyright X * notice, this list of conditions and the following disclaimer. X * 2. Redistributions in binary form must reproduce the above copyright X * notice, this list of conditions and the following disclaimer in the X * documentation and/or other materials provided with the distribution. X * 3. All advertising materials mentioning features or use of this software X * must display the following acknowledgement: X * This product includes software developed by the University of X * California, Berkeley and its contributors. X * 4. Neither the name of the University nor the names of its contributors X * may be used to endorse or promote products derived from this software X * without specific prior written permission. X * X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X * X * @(#) floatingpoint.h 1.0 (Berkeley) 9/23/93 X */ X X/* X * IEEE floating point structure and function definitions X */ X X#ifndef _FLOATINGPOINT_H_ X#define _FLOATINGPOINT_H_ X X#include <sys/cdefs.h> X#include <sys/ieeefp.h> X X#ifdef __GNUC__ X X#define fnstcw(addr) __asm("fnstcw %0" : "=m" (*addr) : "0" (*addr)) X#define fnstsw(addr) __asm("fnstsw %0" : "=m" (*addr) : "0" (*addr)) X#define fnstenv(addr) __asm("fnstenv %0" : "=m" (*addr) : "0" (*addr)) X#define fldenv(addr) __asm("fldenv %0" : : "m" (*addr)) X X#ifdef __i386__ X X/* X * return the contents of a FP register X */ Xstatic __inline__ X__fpgetreg(reg) X{ X unsigned short mem; X X switch(reg) { X default: X fnstcw(&mem); X break; X case FP_STKY_REG: X fnstsw(&mem); X break; X } X return mem; X} X X/* X * set a FP mode; return previous mode X */ Xstatic __inline__ X__fpsetreg(m, reg, fld, off) X{ X unsigned env[7]; X unsigned p; X X fnstenv(env); X p = (env[reg] & fld) >> off; X env[reg] = (env[reg] & ~fld) | (m << off & fld); X fldenv(env); X return p; X} X X#endif /* __i386__ */ X X#endif /* __GNUC__ */ X X/* X * SysV/386 FP control interface X */ X#define fpgetround() ((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF) X#define fpsetround(m) __fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF) X#define fpgetprec() ((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF) X#define fpsetprec(m) __fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF) X#define fpgetmask() ((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF) X#define fpsetmask(m) __fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF) X#define fpgetsticky() ((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF) X#define fpresetsticky(m) __fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF) X#define fpsetsticky(m) fpresetsticky(m) X X#endif /* !_FLOATINGPOINT_H_ */ END-of-floatingpoint.h echo x - fpgetround.3 sed 's/^X//' >fpgetround.3 << 'END-of-fpgetround.3' X.\" Copyright (c) 1993 Andrew Moore, Talke Studio X.\" All rights reserved. X.\" X.\" Redistribution and use in source and binary forms, with or without X.\" modification, are permitted provided that the following conditions X.\" are met: X.\" 1. Redistributions of source code must retain the above copyright X.\" notice, this list of conditions and the following disclaimer. X.\" 2. Redistributions in binary form must reproduce the above copyright X.\" notice, this list of conditions and the following disclaimer in the X.\" documentation and/or other materials provided with the distribution. X.\" 3. All advertising materials mentioning features or use of this software X.\" must display the following acknowledgement: X.\" This product includes software developed by the University of X.\" California, Berkeley and its contributors. X.\" 4. Neither the name of the University nor the names of its contributors X.\" may be used to endorse or promote products derived from this software X.\" without specific prior written permission. X.\" X.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X.\" SUCH DAMAGE. X.\" X.\" @(#)fpgetround.3 1.0 (Berkeley) 9/23/93 X.\" X.Dd August 23, 1993 X.Dt FPGETROUND 3 X.Os BSD 4.3 X.Sh NAME X.Nm fpgetround , X.Nm fpsetround , X.Nm fpgetmask , X.Nm fpsetmask , X.Nm fpgetsticky , X.Nm fpresetsticky X.Nd IEEE floating point interface X.Sh SYNOPSIS X.Fd #include <floatingpoint.h> X.Ft typedef enum { X.br X.Ft FP_RN, X.Li /* round to nearest */ X.br X.Ft FP_RM, X.Li /* round to minus infinity */ X.br X.Ft FP_RP, X.Li /* round to plus inifinity */ X.br X.Ft FP_RZ, X.Li /* truncate */ X.br X.Ft } fp_rnd; X.Pp X.Ft fp_rnd X.Fn fpgetround "" X.Ft fp_rnd X.Fn fpsetround "fp_rnd direction" X.Fd #define fp_except int X.Fd #define FP_X_INV 0x01 /* invalid */ X.Fd #define FP_X_OFL 0x08 /* overflow */ X.Fd #define FP_X_UFL 0x10 /* underflow */ X.Fd #define FP_X_DZ 0x04 /* divide-by-zero */ X.Fd #define FP_X_IMP 0x20 /* loss of precision */ X.Fd #define FP_X_DNML 0x02 /* denormal */ X.Ft fp_except X.Fn fpgetmask "" X.Ft fp_except X.Fn fpsetmask "fp_except mask" X.Ft fp_except X.Fn fpgetsticky "" X.Ft fp_except X.Fn fpresetsticky "fp_except sticky" X.Sh DESCRIPTION XWhen a floating point exception is detected, the exception sticky flag is Xset and the exception mask is tested. If the mask is set, then a trap Xoccurs. These routines allow both setting the floating point exception Xmasks, and resetting the exception sticky flags after an exception is Xdetected. In addition, they allow setting the floating point rounding mode. X.Pp XThe X.Fn fpgetround Xfunction Xreturns the current floating point rounding mode. X.Pp XThe X.Fn fpsetround Xfunction Xsets the floating point rounding mode and returns Xthe previous mode. X.Pp XThe X.Fn fpgetmask Xfunction Xreturns the current floating point exception masks. X.Pp XThe X.Fn fpsetmask Xfunction Xsets the floating point exception masks and returns the Xprevious masks. X.Pp XThe X.Fn fpgetsticky Xfunction Xreturns the current floating point sticky flags. X.Pp XThe X.Fn fpresetsticky Xfunction Xclears the floating point sticky flags and returns Xthe previous flags. X.Pp XSample code which prevents a trap on divide-by-zero: X.Bd -literal -offset indent Xfpsetmask(~FP_X_DZ); Xa = 1.0; Xb = 0; Xc = a / b; Xfpresetsticky(FP_X_DZ); Xfpsetmask(FP_X_DZ); X.Ed X.Sh SEE ALSO X.Xr isnan 3 X.Sh CAVEAT XAfter a floating point exception and before a mask is set, the sticky Xflags must be reset. If another exception occurs before the sticky Xflags are reset, then a wrong exception type may be signaled. X.Sh HISTORY XThese routines are based on SysV/386 routines of the same name. END-of-fpgetround.3 echo x - fptest2.c sed 's/^X//' >fptest2.c << 'END-of-fptest2.c' X#include <stdio.h> X#include <math.h> X#include <floatingpoint.h> X X#define FP_ALL (FP_X_INV | FP_X_OFL | FP_X_UFL| FP_X_DZ | FP_X_IMP | FP_X_DNML) X Xstruct { X char *name; X int flag; X} exception[] = { X "invalid", FP_X_INV, X "overflow", FP_X_OFL, X "underflow", FP_X_UFL, X "divide-by-zero", FP_X_DZ, X "imprecsion", FP_X_IMP, X "denormal", FP_X_DNML, X 0, 0 X}, direction[] = { X "nearest", FP_RN, X "tozero", FP_RZ, X "minus", FP_RM, X "plus", FP_RP, X 0, 0 X}; X Xmain() X{ X int i, j, k, w, x, y, z; X double a, b, c; X X fpsetsticky(FP_ALL); X fpsetmask(FP_ALL); X x = fpgetmask(); X printf("set mask all: %x\n", x); X fpsetmask(0); X x = fpgetmask(); X printf("cleared all: %x\n", x); X X fpsetmask(0); X x = fpgetmask(); X printf("cleared mask all: %x\n", x); X fpsetmask(FP_ALL); X x = fpgetmask(); X printf("set all: %x\n", x); X X printf( "First, clear all FP masks, sticky bits and round\n"); X fpsetmask(0); X fpsetsticky(FP_ALL); X fpsetround(FP_RN); X x = fpgetmask(); X y = fpgetsticky(); X z = fpgetround(); X printf("mask: %x sticky: %x round: %x\n", x, y, z); X X printf("Now, generate some exceptions...\n"); X X /* divide-by-zero */ X x = fpsetmask(~FP_X_DZ); X x = fpgetmask(); X printf("mask: %x\n", x); X a = 1.0; X b = 0; X c = a / b; X printf("1.0/0 == %g\n", c); X x = fpgetmask(); X y = fpgetsticky(); X z = fpgetround(); X printf("mask: %x sticky: %x round: %x\n", x, y, z); X fpsetsticky(FP_X_DZ); X y = fpgetsticky(); X printf("cleared sticky: %x\n", y); X X /* invalid */ X x = fpsetmask(~FP_X_INV); X x = fpgetmask(); X printf("mask: %x\n", x); X a = 0; X b = 0; X c = a / b; X printf("0/0 == %g\n", c); X x = fpgetmask(); X y = fpgetsticky(); X z = fpgetround(); X printf("mask: %x sticky: %x round: %x\n", x, y, z); X fpsetsticky(FP_X_INV); X y = fpgetsticky(); X printf("cleared sticky: %x\n", y); X X /* overflow */ X x = fpsetmask(~(FP_X_OFL|FP_X_IMP)); X x = fpgetmask(); X printf("mask: %x\n", x); X a = 1.0e100; X b = 1.0e100; X c = pow(a, b); X printf("pow(1.0e100,1.0e100) == %g\n", c); X x = fpgetmask(); X y = fpgetsticky(); X z = fpgetround(); X printf("mask: %x sticky: %x round: %x\n", x, y, z); X fpsetsticky(FP_X_OFL|FP_X_IMP); X y = fpgetsticky(); X printf("cleared sticky: %x\n", y); X X x = fpgetmask(); X y = fpgetsticky(); X z = fpgetround(); X printf("mask: %x sticky: %x round: %x\n", x, y, z); X X for (i = 0; exception[i].name; i++) X { X fpsetmask(exception[i].flag); X x = fpgetmask(); X printf("set mask %s: %x\n", exception[i].name, x); X } X for (i = 0; direction[i].name; i++) X { X fpsetround(direction[i].flag); X x = fpgetround(); X printf("set round %s: %x\n", direction[i].name, x); X } X} END-of-fptest2.c echo x - ieeefp.h sed 's/^X//' >ieeefp.h << 'END-of-ieeefp.h' X/*- X * Copyright (c) 1990 Andrew Moore, Talke Studio X * All rights reserved. X * X * Redistribution and use in source and binary forms, with or without X * modification, are permitted provided that the following conditions X * are met: X * 1. Redistributions of source code must retain the above copyright X * notice, this list of conditions and the following disclaimer. X * 2. Redistributions in binary form must reproduce the above copyright X * notice, this list of conditions and the following disclaimer in the X * documentation and/or other materials provided with the distribution. X * 3. All advertising materials mentioning features or use of this software X * must display the following acknowledgement: X * This product includes software developed by the University of X * California, Berkeley and its contributors. X * 4. Neither the name of the University nor the names of its contributors X * may be used to endorse or promote products derived from this software X * without specific prior written permission. X * X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X * X * @(#) ieeefp.h 1.0 (Berkeley) 9/23/93 X */ X X/* X * IEEE floating point type and constant definitions. X */ X X#ifndef _IEEEFP_H_ X#define _IEEEFP_H_ X X#ifdef __i386__ X X/* X * FP rounding modes X */ Xtypedef enum { X FP_RN=0, /* round to nearest */ X FP_RM, /* round down to minus infinity */ X FP_RP, /* round up to plus infinity */ X FP_RZ /* truncate */ X} fp_rnd_t; X X/* X * FP precison modes X */ Xtypedef enum { X FP_PS=0, /* 24 bit (single-precsion) */ X FP_PRS, /* reserved */ X FP_PD, /* 53 bit (double-precision) */ X FP_PE /* 64 bit (extended-precsion) */ X} fp_prec_t; X X#define fp_except_t int X X/* X * FP exception masks X */ X#define FP_X_INV 0x01 /* invalid operation */ X#define FP_X_DNML 0x02 /* denormal */ X#define FP_X_DZ 0x04 /* zero divide */ X#define FP_X_OFL 0x08 /* overflow */ X#define FP_X_UFL 0x10 /* underflow */ X#define FP_X_IMP 0x20 /* (im)precision */ X X/* X * FP registers X */ X#define FP_MSKS_REG 0 /* exception masks */ X#define FP_PRC_REG 0 /* precision */ X#define FP_RND_REG 0 /* direction */ X#define FP_STKY_REG 1 /* sticky flags */ X X/* X * FP register bit field masks X */ X#define FP_MSKS_FLD 0x3f /* exception masks field */ X#define FP_PRC_FLD 0x300 /* precision control field */ X#define FP_RND_FLD 0xc00 /* round control field */ X#define FP_STKY_FLD 0x3f /* sticky flags field */ X X/* X * FP register bit field offsets X */ X#define FP_MSKS_OFF 0 /* exception masks offset */ X#define FP_PRC_OFF 8 /* precision control offset */ X#define FP_RND_OFF 10 /* round control offset */ X#define FP_STKY_OFF 0 /* sticky flags offset */ X X#endif /* __i386__ */ X X#endif /* !_IEEEFP_H_ */ END-of-ieeefp.h exit