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 / mathutil.c < prev    next >
Text File  |  1991-08-10  |  2KB  |  84 lines

  1.  
  2. #ifdef OS9
  3.  
  4. /*    This file implements ldexp() and frexp() functions.
  5.     It is totally OS9 Microware C dependent and relies on
  6.     doubles being IEEE 64-bit and long ints being 32-bit. */
  7.  
  8. #define    EXP_MASK        0x7ff00000
  9. #define    EXP_LOC            20
  10. #define    BIAS            1023
  11. #define SIZE            64
  12. #define    MANTISSA_BITS    52
  13.  
  14. struct double_word { unsigned long lower, upper; };
  15.  
  16. union hack {
  17.     double float_num;
  18.     struct double_word int_num;
  19.     };
  20.  
  21. /*    double ldexp(mantissa,exponent)
  22.     double mantissa;
  23.     int exponent;
  24.  
  25.     Returns the value of mantissa * 2 ^ exponent */
  26.  
  27. double ldexp(mantissa,exponent)
  28. double mantissa;
  29. int exponent;
  30. {
  31.     union hack magic;
  32.     register int exp;
  33.  
  34.     magic.float_num = mantissa;
  35.     if (mantissa != 0.0)
  36.     {
  37.         /* retrieve exponent */
  38.         exp = (magic.int_num.lower & EXP_MASK) >> EXP_LOC;
  39.         magic.int_num.lower &= ~EXP_MASK;
  40.         /* add new exponent */
  41.         exp += exponent;
  42.         magic.int_num.lower |= (exp << EXP_LOC) & EXP_MASK;
  43.     }
  44.     return (magic.float_num);
  45. }
  46.  
  47. /*    double frexp(num,exponent)
  48.     double num;
  49.     int *exponent;
  50.  
  51.     frexp() returns the mantissa of num (of magnitude < 1), which
  52.     gives num when multiplied with 2 ^ exponent. */
  53.  
  54. double frexp(num,exponent)
  55. double num;
  56. int *exponent;
  57. {
  58.     union hack magic;
  59.     register unsigned long mant;
  60.     register int i, exp;
  61.  
  62.     /*    IEEE 64-bit double precision format :
  63.         sign        : 1 bit
  64.         exponent    : 11 bit
  65.         mantissa    : 52 bit
  66.         => num = 2 ^ (exponent-bias) * 1.mantissa (1 hidden bit),
  67.         where bias = 1024.
  68.         0.0 is represented by exponent = mantissa = 0 */
  69.  
  70.     magic.float_num = num;
  71.     if (num != 0.0)
  72.     {
  73.         /* retrieve exponent */
  74.         exp = (magic.int_num.lower & EXP_MASK) >> EXP_LOC;
  75.         *exponent = exp-BIAS+1;
  76.         exp = BIAS-1;
  77.         magic.int_num.lower &= ~EXP_MASK;
  78.         magic.int_num.lower |= (exp << EXP_LOC) & EXP_MASK;
  79.     } else *exponent = 0;
  80.     return (magic.float_num);
  81. }
  82.  
  83. #endif
  84.