s_nextafter.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* Ugly hack so that the aliasing works. */
  22. #define __nexttoward __internal___nexttoward
  23. #define nexttoward __internal_nexttoward
  24. #include <errno.h>
  25. #include <math.h>
  26. #include <math-barriers.h>
  27. #include <math_private.h>
  28. #include <float.h>
  29. #include <libm-alias-double.h>
  30. double __nextafter(double x, double y)
  31. {
  32. int32_t hx,hy,ix,iy;
  33. uint32_t lx,ly;
  34. EXTRACT_WORDS(hx,lx,x);
  35. EXTRACT_WORDS(hy,ly,y);
  36. ix = hx&0x7fffffff; /* |x| */
  37. iy = hy&0x7fffffff; /* |y| */
  38. if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
  39. ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
  40. return x+y;
  41. if(x==y) return y; /* x=y, return y */
  42. if((ix|lx)==0) { /* x == 0 */
  43. double u;
  44. INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */
  45. u = math_opt_barrier (x);
  46. u = u*u;
  47. math_force_eval (u); /* raise underflow flag */
  48. return x;
  49. }
  50. if(hx>=0) { /* x > 0 */
  51. if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */
  52. if(lx==0) hx -= 1;
  53. lx -= 1;
  54. } else { /* x < y, x += ulp */
  55. lx += 1;
  56. if(lx==0) hx += 1;
  57. }
  58. } else { /* x < 0 */
  59. if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
  60. if(lx==0) hx -= 1;
  61. lx -= 1;
  62. } else { /* x > y, x += ulp */
  63. lx += 1;
  64. if(lx==0) hx += 1;
  65. }
  66. }
  67. hy = hx&0x7ff00000;
  68. if(hy>=0x7ff00000) {
  69. double u = x+x; /* overflow */
  70. math_force_eval (u);
  71. __set_errno (ERANGE);
  72. }
  73. if(hy<0x00100000) {
  74. double u = x*x; /* underflow */
  75. math_force_eval (u); /* raise underflow flag */
  76. __set_errno (ERANGE);
  77. }
  78. INSERT_WORDS(x,hx,lx);
  79. return x;
  80. }
  81. libm_alias_double (__nextafter, nextafter)
  82. #ifdef NO_LONG_DOUBLE
  83. strong_alias (__nextafter, __nexttowardl)
  84. weak_alias (__nexttowardl, nexttowardl)
  85. #undef __nexttoward
  86. strong_alias (__nextafter, __nexttoward)
  87. #undef nexttoward
  88. weak_alias (__nextafter, nexttoward)
  89. #endif