s_nextafterl.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /* IEEE functions
  13. * nextafterl(x,y)
  14. * return the next machine floating-point number of x in the
  15. * direction toward y.
  16. * Special cases:
  17. */
  18. #include <openlibm_math.h>
  19. #include "math_private.h"
  20. long double
  21. nextafterl(long double x, long double y)
  22. {
  23. int32_t hx,hy,ix,iy;
  24. u_int32_t lx,ly,esx,esy;
  25. GET_LDOUBLE_WORDS(esx,hx,lx,x);
  26. GET_LDOUBLE_WORDS(esy,hy,ly,y);
  27. ix = esx&0x7fff; /* |x| */
  28. iy = esy&0x7fff; /* |y| */
  29. if (((ix==0x7fff)&&((hx&0x7fffffff|lx)!=0)) || /* x is nan */
  30. ((iy==0x7fff)&&((hy&0x7fffffff|ly)!=0))) /* y is nan */
  31. return x+y;
  32. if(x==y) return y; /* x=y, return y */
  33. if((ix|hx|lx)==0) { /* x == 0 */
  34. volatile long double u;
  35. SET_LDOUBLE_WORDS(x,esy&0x8000,0,1);/* return +-minsubnormal */
  36. u = x;
  37. u = u * u; /* raise underflow flag */
  38. return x;
  39. }
  40. if(esx<0x8000) { /* x > 0 */
  41. if(ix>iy||((ix==iy) && (hx>hy||((hx==hy)&&(lx>ly))))) {
  42. /* x > y, x -= ulp */
  43. if(lx==0) {
  44. if ((hx&0x7fffffff)==0) esx -= 1;
  45. hx = (hx - 1) | (hx & 0x80000000);
  46. }
  47. lx -= 1;
  48. } else { /* x < y, x += ulp */
  49. lx += 1;
  50. if(lx==0) {
  51. hx = (hx + 1) | (hx & 0x80000000);
  52. if ((hx&0x7fffffff)==0) esx += 1;
  53. }
  54. }
  55. } else { /* x < 0 */
  56. if(esy>=0||(ix>iy||((ix==iy)&&(hx>hy||((hx==hy)&&(lx>ly)))))){
  57. /* x < y, x -= ulp */
  58. if(lx==0) {
  59. if ((hx&0x7fffffff)==0) esx -= 1;
  60. hx = (hx - 1) | (hx & 0x80000000);
  61. }
  62. lx -= 1;
  63. } else { /* x > y, x += ulp */
  64. lx += 1;
  65. if(lx==0) {
  66. hx = (hx + 1) | (hx & 0x80000000);
  67. if ((hx&0x7fffffff)==0) esx += 1;
  68. }
  69. }
  70. }
  71. esy = esx&0x7fff;
  72. if(esy==0x7fff) return x+x; /* overflow */
  73. if(esy==0) {
  74. volatile long double u = x*x; /* underflow */
  75. if(u==x) {
  76. SET_LDOUBLE_WORDS(x,esx,hx,lx);
  77. return x;
  78. }
  79. }
  80. SET_LDOUBLE_WORDS(x,esx,hx,lx);
  81. return x;
  82. }
  83. //__strong_alias(nexttowardl, nextafterl);