home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / Bonus / normal / ldexp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  1.4 KB  |  55 lines

  1. /*
  2.  * ldexp returns the quanity "value" * 2 ^ "exp"
  3.  *
  4.  * For the mc68000 using IEEE format the double precision word format is:
  5.  *
  6.  * WORD N   =>    SEEEEEEEEEEEMMMM
  7.  * WORD N+1 =>    MMMMMMMMMMMMMMMM
  8.  * WORD N+2 =>    MMMMMMMMMMMMMMMM
  9.  * WORD N+3 =>    MMMMMMMMMMMMMMMM
  10.  *
  11.  * Where:          S  =>   Sign bit
  12.  *                 E  =>   Exponent
  13.  *                 X  =>   Ignored (set to 0)
  14.  *                 M  =>   Mantissa bit
  15.  *
  16.  * NOTE:  Beware of 0.0; on some machines which use excess 128 notation for the
  17.  * exponent, if the mantissa is zero the exponent is also.
  18.  *
  19.  */
  20.  
  21.  
  22. #define MANT_MASK 0x800FFFFF    /* Mantissa extraction mask     */
  23. #define ZPOS_MASK 0x3FF00000    /* Positive # mask for exp = 0  */
  24. #define ZNEG_MASK 0x3FF00000    /* Negative # mask for exp = 0  */
  25.  
  26. #define EXP_MASK 0x7FF00000    /* Mask for exponent            */
  27. #define EXP_SHIFTS 20        /* Shifts to get into LSB's     */
  28. #define EXP_BIAS 1023        /* Exponent bias                */
  29.  
  30.  
  31. union dtol
  32. {
  33.   double dval;
  34.   int ival[2];
  35. };
  36.  
  37. double
  38. ldexp (double value, int exp)
  39. {
  40.   union dtol number;
  41.   int *iptr, cexp;
  42.  
  43.   if (value == 0.0)
  44.     return (0.0);
  45.   else
  46.     {
  47.       number.dval = value;
  48.       iptr = &number.ival[0];
  49.       cexp = (((*iptr) & EXP_MASK) >> EXP_SHIFTS) - EXP_BIAS;
  50.       *iptr &= ~EXP_MASK;
  51.       exp += EXP_BIAS;
  52.       *iptr |= ((exp + cexp) << EXP_SHIFTS) & EXP_MASK;
  53.       return (number.dval);
  54.     }
  55. }