s_nextafter.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* @(#)s_nextafter.c 5.1 93/09/24 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. #if defined(LIBM_SCCS) && !defined(lint)
  13. static char rcsid[] = "$NetBSD: s_nextafter.c,v 1.8 1995/05/10 20:47:58 jtc Exp $";
  14. #endif
  15. /* IEEE functions
  16. * nextafter(x,y)
  17. * return the next machine floating-point number of x in the
  18. * direction toward y.
  19. * Special cases:
  20. */
  21. #include "math.h"
  22. #include "math_private.h"
  23. libm_hidden_proto(nextafter)
  24. #ifdef __STDC__
  25. double nextafter(double x, double y)
  26. #else
  27. double nextafter(x,y)
  28. double x,y;
  29. #endif
  30. {
  31. int32_t hx,hy,ix,iy;
  32. u_int32_t lx,ly;
  33. EXTRACT_WORDS(hx,lx,x);
  34. EXTRACT_WORDS(hy,ly,y);
  35. ix = hx&0x7fffffff; /* |x| */
  36. iy = hy&0x7fffffff; /* |y| */
  37. if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
  38. ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
  39. return x+y;
  40. if(x==y) return x; /* x=y, return x */
  41. if((ix|lx)==0) { /* x == 0 */
  42. INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */
  43. y = x*x;
  44. if(y==x) return y; else return x; /* raise underflow flag */
  45. }
  46. if(hx>=0) { /* x > 0 */
  47. if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */
  48. if(lx==0) hx -= 1;
  49. lx -= 1;
  50. } else { /* x < y, x += ulp */
  51. lx += 1;
  52. if(lx==0) hx += 1;
  53. }
  54. } else { /* x < 0 */
  55. if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
  56. if(lx==0) hx -= 1;
  57. lx -= 1;
  58. } else { /* x > y, x += ulp */
  59. lx += 1;
  60. if(lx==0) hx += 1;
  61. }
  62. }
  63. hy = hx&0x7ff00000;
  64. if(hy>=0x7ff00000) return x+x; /* overflow */
  65. if(hy<0x00100000) { /* underflow */
  66. y = x*x;
  67. if(y!=x) { /* raise underflow flag */
  68. INSERT_WORDS(y,hx,lx);
  69. return y;
  70. }
  71. }
  72. INSERT_WORDS(x,hx,lx);
  73. return x;
  74. }
  75. libm_hidden_def(nextafter)