nearbyintq.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* s_nearbyintl.c -- long double version of s_nearbyint.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. * nearbyintq(x)
  16. * Return x rounded to integral value according to the prevailing
  17. * rounding mode.
  18. * Method:
  19. * Using floating addition.
  20. * Exception:
  21. * Inexact flag raised if x not equal to rintq(x).
  22. */
  23. #include "quadmath-imp.h"
  24. static const __float128
  25. TWO112[2]={
  26. 5.19229685853482762853049632922009600E+33Q, /* 0x406F000000000000, 0 */
  27. -5.19229685853482762853049632922009600E+33Q /* 0xC06F000000000000, 0 */
  28. };
  29. __float128 nearbyintq(__float128 x)
  30. {
  31. fenv_t env;
  32. int64_t i0,j0,sx;
  33. uint64_t i1 __attribute__ ((unused));
  34. __float128 w,t;
  35. GET_FLT128_WORDS64(i0,i1,x);
  36. sx = (((uint64_t)i0)>>63);
  37. j0 = ((i0>>48)&0x7fff)-0x3fff;
  38. if(j0<112) {
  39. if(j0<0) {
  40. feholdexcept (&env);
  41. w = TWO112[sx] + math_opt_barrier (x);
  42. t = w-TWO112[sx];
  43. math_force_eval (t);
  44. fesetenv (&env);
  45. GET_FLT128_MSW64(i0,t);
  46. SET_FLT128_MSW64(t,(i0&0x7fffffffffffffffLL)|(sx<<63));
  47. return t;
  48. }
  49. } else {
  50. if(j0==0x4000) return x+x; /* inf or NaN */
  51. else return x; /* x is integral */
  52. }
  53. feholdexcept (&env);
  54. w = TWO112[sx] + math_opt_barrier (x);
  55. t = w-TWO112[sx];
  56. math_force_eval (t);
  57. fesetenv (&env);
  58. return t;
  59. }