s_scalbn.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * scalbln(double x, long n)
  13. * scalbln(x,n) returns x * 2**n computed by exponent
  14. * manipulation rather than by actually performing an
  15. * exponentiation or a multiplication.
  16. */
  17. #include "math.h"
  18. #include "math_private.h"
  19. #include <limits.h>
  20. static const double
  21. two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
  22. twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
  23. huge = 1.0e+300,
  24. tiny = 1.0e-300;
  25. double scalbln(double x, long n)
  26. {
  27. int32_t k, hx, lx;
  28. EXTRACT_WORDS(hx, lx, x);
  29. k = (hx & 0x7ff00000) >> 20; /* extract exponent */
  30. if (k == 0) { /* 0 or subnormal x */
  31. if ((lx | (hx & 0x7fffffff)) == 0)
  32. return x; /* +-0 */
  33. x *= two54;
  34. GET_HIGH_WORD(hx, x);
  35. k = ((hx & 0x7ff00000) >> 20) - 54;
  36. }
  37. if (k == 0x7ff)
  38. return x + x; /* NaN or Inf */
  39. k = k + n;
  40. if (k > 0x7fe)
  41. return huge * copysign(huge, x); /* overflow */
  42. if (n < -50000)
  43. return tiny * copysign(tiny, x); /* underflow */
  44. if (k > 0) { /* normal result */
  45. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  46. return x;
  47. }
  48. if (k <= -54) {
  49. if (n > 50000) /* in case integer overflow in n+k */
  50. return huge * copysign(huge, x); /* overflow */
  51. return tiny * copysign(tiny, x); /* underflow */
  52. }
  53. k += 54; /* subnormal result */
  54. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  55. return x * twom54;
  56. }
  57. libm_hidden_def(scalbln)
  58. #if LONG_MAX == INT_MAX
  59. strong_alias_untyped(scalbln,scalbn)
  60. #else
  61. double scalbn(double x, int n)
  62. {
  63. return scalbln(x, n);
  64. }
  65. #endif
  66. libm_hidden_def(scalbn)