home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / xdscale.c < prev    next >
C/C++ Source or Header  |  1998-06-16  |  2KB  |  71 lines

  1. /* _Dscale function -- IEEE 754 version */
  2. #include "wctype.h"
  3. #include "xmath.h"
  4. _STD_BEGIN
  5.  
  6. _CRTIMP2 short _Dscale(double *px, long lexp)
  7.     {    /* scale *px by 2^xexp with checking */
  8.     unsigned short *ps = (unsigned short *)px;
  9.     short xchar = (ps[_D0] & _DMASK) >> _DOFF;
  10.  
  11.     if (xchar == _DMAX)
  12.         return ((ps[_D0] & _DFRAC) != 0 || ps[_D1] != 0
  13.             || ps[_D2] != 0 || ps[_D3] != 0 ? NAN : INF);
  14.     else if (xchar == 0 && 0 < (xchar = _Dnorm(ps)))
  15.         return (0);
  16.     lexp += xchar;
  17.     if (_DMAX <= lexp)
  18.         {    /* overflow, return +/-INF */
  19.         *px = ps[_D0] & _DSIGN ? -_Inf._D : _Inf._D;
  20.         return (INF);
  21.         }
  22.     else if (0 < lexp)
  23.         {    /* finite result, repack */
  24.         ps[_D0] = ps[_D0] & ~_DMASK | (short)lexp << _DOFF;
  25.         return (FINITE);
  26.         }
  27.     else
  28.         {    /* denormalized, scale */
  29.         unsigned short sign = ps[_D0] & _DSIGN;
  30.  
  31.         ps[_D0] = 1 << _DOFF | ps[_D0] & _DFRAC;
  32.         if (--lexp < -(48+_DOFF))
  33.             {    /* underflow, return +/-0 */
  34.             ps[_D0] = sign, ps[_D1] = 0;
  35.             ps[_D2] = 0, ps[_D3] = 0;
  36.             return (0);
  37.             }
  38.         else
  39.             {    /* nonzero, align fraction */
  40.             short xexp;
  41.             for (xexp = (short)lexp; xexp <= -16; xexp += 16)
  42.                 {    /* scale by words */
  43.                 ps[_D3] = ps[_D2], ps[_D2] = ps[_D1];
  44.                 ps[_D1] = ps[_D0], ps[_D0] = 0;
  45.                 }
  46.             if ((xexp = -xexp) != 0)
  47.                 {    /* scale by bits */
  48.                 ps[_D3] = ps[_D3] >> xexp
  49.                     | ps[_D2] << (16 - xexp);
  50.                 ps[_D2] = ps[_D2] >> xexp
  51.                     | ps[_D1] << (16 - xexp);
  52.                 ps[_D1] = ps[_D1] >> xexp
  53.                     | ps[_D0] << (16 - xexp);
  54.                 ps[_D0] >>= xexp;
  55.                 }
  56.             ps[_D0] |= sign;
  57.             return (FINITE);
  58.             }
  59.         }
  60.     }
  61. _STD_END
  62.  
  63. /*
  64.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  65.  * Consult your license regarding permissions and restrictions.
  66.  */
  67.  
  68. /*
  69. 941029 pjp: added _STD machinery
  70.  */
  71.