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

  1. /* _Dnorm function -- IEEE 754 version */
  2. #include "wctype.h"
  3. #include "xmath.h"
  4. _STD_BEGIN
  5.  
  6. _CRTIMP2 short _Dnorm(unsigned short *ps)
  7.     {    /* normalize double fraction */
  8.     short xchar;
  9.     unsigned short sign = ps[_D0] & _DSIGN;
  10.  
  11.     xchar = 1;
  12.     if ((ps[_D0] &= _DFRAC) != 0 || ps[_D1]
  13.         || ps[_D2] || ps[_D3])
  14.         {    /* nonzero, scale */
  15.         for (; ps[_D0] == 0; xchar -= 16)
  16.             {    /* shift left by 16 */
  17.             ps[_D0] = ps[_D1], ps[_D1] = ps[_D2];
  18.             ps[_D2] = ps[_D3], ps[_D3] = 0;
  19.             }
  20.         for (; ps[_D0] < 1<<_DOFF; --xchar)
  21.             {    /* shift left by 1 */
  22.             ps[_D0] = ps[_D0] << 1 | ps[_D1] >> 15;
  23.             ps[_D1] = ps[_D1] << 1 | ps[_D2] >> 15;
  24.             ps[_D2] = ps[_D2] << 1 | ps[_D3] >> 15;
  25.             ps[_D3] <<= 1;
  26.             }
  27.         for (; 1<<_DOFF+1 <= ps[_D0]; ++xchar)
  28.             {    /* shift right by 1 */
  29.             ps[_D3] = ps[_D3] >> 1 | ps[_D2] << 15;
  30.             ps[_D2] = ps[_D2] >> 1 | ps[_D1] << 15;
  31.             ps[_D1] = ps[_D1] >> 1 | ps[_D0] << 15;
  32.             ps[_D0] >>= 1;
  33.             }
  34.         ps[_D0] &= _DFRAC;
  35.         }
  36.     ps[_D0] |= sign;
  37.     return (xchar);
  38.     }
  39. _STD_END
  40.  
  41. /*
  42.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  43.  * Consult your license regarding permissions and restrictions.
  44.  */
  45.  
  46. /*
  47. 941029 pjp: added _STD machinery
  48.  */
  49.