Source code of Windows XP (NT5)
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.

66 lines
1.7 KiB

  1. /* _Dscale function -- IEEE 754 version */
  2. #include "xmath.h"
  3. _STD_BEGIN
  4. _CRTIMP2 short __cdecl _Dscale(double *px, long lexp)
  5. { /* scale *px by 2^xexp with checking */
  6. unsigned short *ps = (unsigned short *)px;
  7. short xchar = (short)((ps[_D0] & _DMASK) >> _DOFF);
  8. if (xchar == _DMAX)
  9. return ((ps[_D0] & _DFRAC) != 0 || ps[_D1] != 0
  10. || ps[_D2] != 0 || ps[_D3] != 0 ? _NANCODE : _INFCODE);
  11. else if (xchar == 0 && 0 < (xchar = _Dnorm(ps)))
  12. return (0);
  13. lexp += xchar;
  14. if (_DMAX <= lexp)
  15. { /* overflow, return +/-INF */
  16. *px = ps[_D0] & _DSIGN ? -_Inf._Double : _Inf._Double;
  17. return (_INFCODE);
  18. }
  19. else if (0 < lexp)
  20. { /* finite result, repack */
  21. ps[_D0] = ps[_D0] & ~_DMASK | (short)lexp << _DOFF;
  22. return (_FINITE);
  23. }
  24. else
  25. { /* denormalized, scale */
  26. unsigned short sign = ps[_D0] & _DSIGN;
  27. ps[_D0] = (unsigned short)(1 << _DOFF
  28. | 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) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  61. * Consult your license regarding permissions and restrictions.
  62. V3.10:0009 */