scalblnq.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* scalblnq.c -- __float128 version of s_scalbn.c.
  2. * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
  3. */
  4. /*
  5. * ====================================================
  6. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7. *
  8. * Developed at SunPro, a Sun Microsystems, Inc. business.
  9. * Permission to use, copy, modify, and distribute this
  10. * software is freely granted, provided that this notice
  11. * is preserved.
  12. * ====================================================
  13. */
  14. /*
  15. * scalblnq (_float128 x, long int n)
  16. * scalblnq(x,n) returns x* 2**n computed by exponent
  17. * manipulation rather than by actually performing an
  18. * exponentiation or a multiplication.
  19. */
  20. #include "quadmath-imp.h"
  21. static const __float128
  22. two114 = 2.0769187434139310514121985316880384E+34Q, /* 0x4071000000000000, 0 */
  23. twom114 = 4.8148248609680896326399448564623183E-35Q, /* 0x3F8D000000000000, 0 */
  24. huge = 1.0E+4900Q,
  25. tiny = 1.0E-4900Q;
  26. __float128
  27. scalblnq (__float128 x, long int n)
  28. {
  29. int64_t k,hx,lx;
  30. GET_FLT128_WORDS64(hx,lx,x);
  31. k = (hx>>48)&0x7fff; /* extract exponent */
  32. if (k==0) { /* 0 or subnormal x */
  33. if ((lx|(hx&0x7fffffffffffffffULL))==0) return x; /* +-0 */
  34. x *= two114;
  35. GET_FLT128_MSW64(hx,x);
  36. k = ((hx>>48)&0x7fff) - 114;
  37. }
  38. if (k==0x7fff) return x+x; /* NaN or Inf */
  39. if (n< -50000) return tiny*copysignq(tiny,x); /*underflow*/
  40. if (n> 50000 || k+n > 0x7ffe)
  41. return huge*copysignq(huge,x); /* overflow */
  42. /* Now k and n are bounded we know that k = k+n does not
  43. overflow. */
  44. k = k+n;
  45. if (k > 0) /* normal result */
  46. {SET_FLT128_MSW64(x,(hx&0x8000ffffffffffffULL)|(k<<48)); return x;}
  47. if (k <= -114)
  48. return tiny*copysignq(tiny,x); /*underflow*/
  49. k += 114; /* subnormal result */
  50. SET_FLT128_MSW64(x,(hx&0x8000ffffffffffffULL)|(k<<48));
  51. return x*twom114;
  52. }