s_nexttoward.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/s_nexttoward.c,v 1.3 2011/02/10 07:38:13 das Exp $");
  14. /*
  15. * We assume that a long double has a 15-bit exponent. On systems
  16. * where long double is the same as double, nexttoward() is an alias
  17. * for nextafter(), so we don't use this routine.
  18. */
  19. #include <float.h>
  20. #include "fpmath.h"
  21. #include "openlibm.h"
  22. #include "math_private.h"
  23. #if LDBL_MAX_EXP != 0x4000
  24. #error "Unsupported long double format"
  25. #endif
  26. DLLEXPORT double
  27. nexttoward(double x, long double y)
  28. {
  29. union IEEEl2bits uy;
  30. volatile double t;
  31. int32_t hx,ix;
  32. u_int32_t lx;
  33. EXTRACT_WORDS(hx,lx,x);
  34. ix = hx&0x7fffffff; /* |x| */
  35. uy.e = y;
  36. if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||
  37. (uy.bits.exp == 0x7fff &&
  38. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
  39. return x+y; /* x or y is nan */
  40. if(x==y) return (double)y; /* x=y, return y */
  41. if(x==0.0) {
  42. INSERT_WORDS(x,uy.bits.sign<<31,1); /* return +-minsubnormal */
  43. t = x*x;
  44. if(t==x) return t; else return x; /* raise underflow flag */
  45. }
  46. if((hx>0.0) ^ (x < y)) { /* x -= ulp */
  47. if(lx==0) hx -= 1;
  48. lx -= 1;
  49. } else { /* x += ulp */
  50. lx += 1;
  51. if(lx==0) hx += 1;
  52. }
  53. ix = hx&0x7ff00000;
  54. if(ix>=0x7ff00000) return x+x; /* overflow */
  55. if(ix<0x00100000) { /* underflow */
  56. t = x*x;
  57. if(t!=x) { /* raise underflow flag */
  58. INSERT_WORDS(x,hx,lx);
  59. return x;
  60. }
  61. }
  62. INSERT_WORDS(x,hx,lx);
  63. return x;
  64. }