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

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