s_nexttowardf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include "cdefs-compat.h"
  12. //__FBSDID("$FreeBSD: src/lib/msun/src/s_nexttowardf.c,v 1.3 2011/02/10 07:38:38 das Exp $");
  13. #include <float.h>
  14. #include "fpmath.h"
  15. #include "openlibm.h"
  16. #include "math_private.h"
  17. #define LDBL_INFNAN_EXP (LDBL_MAX_EXP * 2 - 1)
  18. DLLEXPORT float
  19. nexttowardf(float x, long double y)
  20. {
  21. union IEEEl2bits uy;
  22. volatile float t;
  23. int32_t hx,ix;
  24. GET_FLOAT_WORD(hx,x);
  25. ix = hx&0x7fffffff; /* |x| */
  26. uy.e = y;
  27. if((ix>0x7f800000) ||
  28. (uy.bits.exp == LDBL_INFNAN_EXP &&
  29. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
  30. return x+y; /* x or y is nan */
  31. if(x==y) return (float)y; /* x=y, return y */
  32. if(ix==0) { /* x == 0 */
  33. SET_FLOAT_WORD(x,(uy.bits.sign<<31)|1);/* return +-minsubnormal */
  34. t = x*x;
  35. if(t==x) return t; else return x; /* raise underflow flag */
  36. }
  37. if((hx>=0) ^ (x < y)) /* x -= ulp */
  38. hx -= 1;
  39. else /* x += ulp */
  40. hx += 1;
  41. ix = hx&0x7f800000;
  42. if(ix>=0x7f800000) return x+x; /* overflow */
  43. if(ix<0x00800000) { /* underflow */
  44. t = x*x;
  45. if(t!=x) { /* raise underflow flag */
  46. SET_FLOAT_WORD(x,hx);
  47. return x;
  48. }
  49. }
  50. SET_FLOAT_WORD(x,hx);
  51. return x;
  52. }