truncf.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_truncf.c */
  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. /*
  13. * truncf(x)
  14. * Return x rounded toward 0 to integral value
  15. * Method:
  16. * Bit twiddling.
  17. * Exception:
  18. * Inexact flag raised if x not equal to truncf(x).
  19. */
  20. #include "libm.h"
  21. static const float huge = 1.0e30f;
  22. float truncf(float x)
  23. {
  24. int32_t i0,j0;
  25. uint32_t i;
  26. GET_FLOAT_WORD(i0, x);
  27. j0 = ((i0>>23)&0xff) - 0x7f;
  28. if (j0 < 23) {
  29. if (j0 < 0) { /* |x|<1, return 0*sign(x) */
  30. /* raise inexact if x != 0 */
  31. if (huge+x > 0.0f)
  32. i0 &= 0x80000000;
  33. } else {
  34. i = 0x007fffff>>j0;
  35. if ((i0&i) == 0)
  36. return x; /* x is integral */
  37. /* raise inexact */
  38. if (huge+x > 0.0f)
  39. i0 &= ~i;
  40. }
  41. } else {
  42. if (j0 == 0x80)
  43. return x + x; /* inf or NaN */
  44. return x; /* x is integral */
  45. }
  46. SET_FLOAT_WORD(x, i0);
  47. return x;
  48. }