s_scalbn.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(scalbln, scalbn) - "error: conflicting types for 'scalbn'"
  60. * because it tries to declare "typeof(scalbln) scalbn;"
  61. * which tries to give "long" parameter to scalbn.
  62. * Doing it by hand:
  63. */
  64. __typeof(scalbn) scalbn __attribute__((alias("scalbln")));
  65. #else
  66. double scalbn(double x, int n)
  67. {
  68. return scalbn(x, n);
  69. }
  70. #endif
  71. libm_hidden_def(scalbn)