home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
pd3.lzh
/
SBPROLOG2.2
/
SIM
/
float.c
< prev
next >
Wrap
Text File
|
1991-08-10
|
3KB
|
95 lines
/************************************************************************
* *
* 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.
------------------------------------------------------------------ */
/* 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). */
#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)
extern 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;
diff = op1 - op2; if (diff < 0) diff = -diff;
if (op1 < op2) min = op1; else min = op2;
return ( (diff/min) < EPSILON);
}