s_nextafter.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /* IEEE functions
  12. * nextafter(x,y)
  13. * return the next machine floating-point number of x in the
  14. * direction toward y.
  15. * Special cases:
  16. */
  17. #include "math.h"
  18. #include "math_private.h"
  19. #include <float.h>
  20. double nextafter(double x, double y)
  21. {
  22. int32_t hx,hy,ix,iy;
  23. u_int32_t lx,ly;
  24. EXTRACT_WORDS(hx,lx,x);
  25. EXTRACT_WORDS(hy,ly,y);
  26. ix = hx&0x7fffffff; /* |x| */
  27. iy = hy&0x7fffffff; /* |y| */
  28. if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
  29. ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
  30. return x+y;
  31. if(x==y) return y; /* x=y, return y */
  32. if((ix|lx)==0) { /* x == 0 */
  33. INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */
  34. y = x*x;
  35. if(y==x) return y; else return x; /* raise underflow flag */
  36. }
  37. if(hx>=0) { /* x > 0 */
  38. if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */
  39. if(lx==0) hx -= 1;
  40. lx -= 1;
  41. } else { /* x < y, x += ulp */
  42. lx += 1;
  43. if(lx==0) hx += 1;
  44. }
  45. } else { /* x < 0 */
  46. if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
  47. if(lx==0) hx -= 1;
  48. lx -= 1;
  49. } else { /* x > y, x += ulp */
  50. lx += 1;
  51. if(lx==0) hx += 1;
  52. }
  53. }
  54. hy = hx&0x7ff00000;
  55. if(hy>=0x7ff00000) return x+x; /* overflow */
  56. if(hy<0x00100000) { /* underflow */
  57. y = x*x;
  58. if(y!=x) { /* raise underflow flag */
  59. INSERT_WORDS(y,hx,lx);
  60. return y;
  61. }
  62. }
  63. INSERT_WORDS(x,hx,lx);
  64. return x;
  65. }
  66. libm_hidden_def(nextafter)
  67. #if LDBL_MANT_DIG == DBL_MANT_DIG
  68. strong_alias_untyped(nextafter, nexttoward)
  69. libm_hidden_def(nexttoward)
  70. #else
  71. double nexttoward(double x, long double y)
  72. {
  73. return nextafter(x, y);
  74. }
  75. libm_hidden_def(nexttoward)
  76. #endif