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 >
Text File  |  1991-08-10  |  3KB  |  95 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /*  WAM representation of floats: bits 0-2: tag (010); bits 3-7: absolute
  26.     value of exponent; bits 8-29: absolute value of mantissa; bit 30:
  27.     sign of exponent (1: negative); bit 31: sign of mantissa (1: negative).  */
  28.  
  29. #include "sim.h"
  30. #include "aux.h"
  31.  
  32. #define Bit21        0x200000
  33. #define EXP_SIGN    0x40000000
  34. #define MANT_SIGN   0x80000000
  35. #define exp_magn(op)    (((unsigned)(op & 0xff)) >> 3)
  36. #define mant_magn(op)    (((unsigned)(op & 0x3fffff00)) >> 8)
  37.  
  38. extern double frexp(), ldexp();    /* C library routines */
  39.  
  40. /* "floatval" converts floats from the WAM representation to the machine
  41.    representation.                            */
  42.  
  43. double floatval(op)
  44. word op;
  45. {
  46.     register long exponent, exp_sign;
  47.     double fval; int exp;
  48.  
  49.     exp_sign = op & EXP_SIGN;
  50.     fval = (double)mant_magn(op); exponent = exp_magn(op);
  51.     if (exp_sign) exp = -exponent; else exp = exponent;
  52.     if (op & MANT_SIGN) fval = -fval;
  53.     return ldexp(fval, exp);
  54. }
  55.  
  56. /* "makefloat" converts floats from the machine representation to the WAM
  57.    representation.                              */
  58.  
  59. word makefloat(op)
  60. double op;
  61. {
  62.     long exp_sign, mant_sign;
  63.     int exponent;
  64.  
  65.     if (op < 0) {
  66.     mant_sign = MANT_SIGN;
  67.     op = -op;}
  68.     else mant_sign = 0;
  69.     op = frexp(op, &exponent);
  70.     if ((op == 0.0) || (exponent <= -32)) {op = 0.0; exponent = 0;}
  71.     else
  72.     {    while ( !(((int)op)&Bit21) && (exponent > -31))  /* keep top 10 bits */
  73.     {   op *= 2;                         /* clear for shifting */
  74.         exponent -= 1;
  75.     };
  76.     };
  77.     if (exponent < 0) {
  78.     exponent = -exponent;
  79.     exp_sign = EXP_SIGN;
  80.     } else exp_sign = 0;
  81.     return ( ((word)op<<8) | (exponent<<3) | exp_sign | mant_sign | FLOAT_TAG);
  82. }
  83.  
  84. prettymuch_equal(op1, op2)
  85. double op1, op2;
  86. {
  87.     double min, diff;
  88.  
  89.     if (op1<0) op1 = -op1;
  90.     if (op2<0) op2 = -op2;
  91.     diff = op1 - op2; if (diff < 0) diff = -diff;
  92.     if (op1 < op2) min = op1; else min = op2;
  93.     return ( (diff/min) < EPSILON);
  94. }
  95.