floorf.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_floorf.c */
  2. /*
  3. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  4. */
  5. /*
  6. * ====================================================
  7. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  8. *
  9. * Developed at SunPro, a Sun Microsystems, Inc. business.
  10. * Permission to use, copy, modify, and distribute this
  11. * software is freely granted, provided that this notice
  12. * is preserved.
  13. * ====================================================
  14. */
  15. /*
  16. * floorf(x)
  17. * Return x rounded toward -inf to integral value
  18. * Method:
  19. * Bit twiddling.
  20. * Exception:
  21. * Inexact flag raised if x not equal to floorf(x).
  22. */
  23. #include "libm.h"
  24. static const float huge = 1.0e30;
  25. float floorf(float x)
  26. {
  27. int32_t i0,j0;
  28. uint32_t i;
  29. GET_FLOAT_WORD(i0, x);
  30. // FIXME: signed shift
  31. j0 = ((i0>>23)&0xff) - 0x7f;
  32. if (j0 < 23) {
  33. if (j0 < 0) { /* |x| < 1 */
  34. /* raise inexact if x != 0 */
  35. if (huge+x > 0.0f) {
  36. if (i0 >= 0) /* x >= 0 */
  37. i0 = 0;
  38. else if ((i0&0x7fffffff) != 0)
  39. i0 = 0xbf800000;
  40. }
  41. } else {
  42. i = 0x007fffff>>j0;
  43. if ((i0&i) == 0)
  44. return x; /* x is integral */
  45. /* raise inexact flag */
  46. if (huge+x > 0.0f) {
  47. if (i0 < 0)
  48. i0 += 0x00800000>>j0;
  49. i0 &= ~i;
  50. }
  51. }
  52. } else {
  53. if (j0 == 0x80) /* inf or NaN */
  54. return x+x;
  55. else
  56. return x; /* x is integral */
  57. }
  58. SET_FLOAT_WORD(x, i0);
  59. return x;
  60. }