/************************************************************************ * * * The SB-Prolog System * * Copyright SUNY at Stony Brook, 1986 * * * ************************************************************************/ /*----------------------------------------------------------------- SB-Prolog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the SB-Prolog General Public License for full details. Everyone is granted permission to copy, modify and redistribute SB-Prolog, but only under the conditions described in the SB-Prolog General Public License. A copy of this license is supposed to have been given to you along with SB-Prolog so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. ------------------------------------------------------------------ */ #ifndef lint static char rcsid[] = "$Header: float.c 1.2 87/10/24 $"; #endif /* * float.c * * WAM representation of floats: bits 0-2: tag (010); bits 3-7: absolute * value of exponent; bits 8-29: absolute value of mantissa; bit 30: * sign of exponent (1: negative); bit 31: sign of mantissa (1: negative). */ /* $Log: float.c,v $ * Revision 1.2 87/10/24 14:55:03 rbk * Fix prettymuch_equal() to unify floating zero with only floating zero; * previous version would dump core with floating-point zero-divide. * This is a minor (but reasonable) semantic change. * */ #include "sim.h" #include "aux.h" #define Bit21 0x200000 #define EXP_SIGN 0x40000000 #define MANT_SIGN 0x80000000 #define exp_magn(op) (((unsigned)(op & 0xff)) >> 3) #define mant_magn(op) (((unsigned)(op & 0x3fffff00)) >> 8) double frexp(), ldexp(); /* C library routines */ /* "floatval" converts floats from the WAM representation to the machine representation. */ double floatval(op) word op; { register long exponent, exp_sign; double fval; int exp; exp_sign = op & EXP_SIGN; fval = (double)mant_magn(op); exponent = exp_magn(op); if (exp_sign) exp = -exponent; else exp = exponent; if (op & MANT_SIGN) fval = -fval; return ldexp(fval, exp); } /* "makefloat" converts floats from the machine representation to the WAM representation. */ word makefloat(op) double op; { long exp_sign, mant_sign; int exponent; if (op < 0) { mant_sign = MANT_SIGN; op = -op;} else mant_sign = 0; op = frexp(op, &exponent); if ((op == 0.0) || (exponent <= -32)) {op = 0.0; exponent = 0;} else { while ( !(((int)op)&Bit21) && (exponent > -31)) /* keep top 10 bits */ { op *= 2; /* clear for shifting */ exponent -= 1; }; }; if (exponent < 0) { exponent = -exponent; exp_sign = EXP_SIGN; } else exp_sign = 0; return ( ((word)op<<8) | (exponent<<3) | exp_sign | mant_sign | FLOAT_TAG); } prettymuch_equal(op1, op2) double op1, op2; { double min, diff; if (op1<0) op1 = -op1; if (op2<0) op2 = -op2; if (op1 < op2) min = op1; else min = op2; if (min == 0.0) return(op1 == op2); /* if one is zero, insist both be */ diff = op1 - op2; if (diff < 0) diff = -diff; return ( (diff/min) < EPSILON); }