s_nexttowardf.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include <float.h>
  13. #include <openlibm_math.h>
  14. #include "math_private.h"
  15. float
  16. nexttowardf(float x, long double y)
  17. {
  18. int32_t hx,ix,iy;
  19. u_int32_t hy,ly,esy;
  20. GET_FLOAT_WORD(hx,x);
  21. GET_LDOUBLE_WORDS(esy,hy,ly,y);
  22. ix = hx&0x7fffffff; /* |x| */
  23. iy = esy&0x7fff; /* |y| */
  24. if((ix>0x7f800000) || /* x is nan */
  25. (iy>=0x7fff&&((hy|ly)!=0))) /* y is nan */
  26. return x+y;
  27. if((long double) x==y) return y; /* x=y, return y */
  28. if(ix==0) { /* x == 0 */
  29. volatile float u;
  30. SET_FLOAT_WORD(x,((esy&0x8000)<<16)|1);/* return +-minsub*/
  31. u = x;
  32. u = u * u; /* raise underflow flag */
  33. return x;
  34. }
  35. if(hx>=0) { /* x > 0 */
  36. if(esy>=0x8000||((ix>>23)&0xff)>iy-0x3f80
  37. || (((ix>>23)&0xff)==iy-0x3f80
  38. && ((ix&0x7fffff)<<8)>(hy&0x7fffffff))) {/* x > y, x -= ulp */
  39. hx -= 1;
  40. } else { /* x < y, x += ulp */
  41. hx += 1;
  42. }
  43. } else { /* x < 0 */
  44. if(esy<0x8000||((ix>>23)&0xff)>iy-0x3f80
  45. || (((ix>>23)&0xff)==iy-0x3f80
  46. && ((ix&0x7fffff)<<8)>(hy&0x7fffffff))) {/* x < y, x -= ulp */
  47. hx -= 1;
  48. } else { /* x > y, x += ulp */
  49. hx += 1;
  50. }
  51. }
  52. hy = hx&0x7f800000;
  53. if(hy>=0x7f800000) {
  54. x = x+x; /* overflow */
  55. return x;
  56. }
  57. if(hy<0x00800000) {
  58. volatile float u = x*x; /* underflow */
  59. }
  60. SET_FLOAT_WORD(x,hx);
  61. return x;
  62. }