Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

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