s_floorf.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* s_floorf.c -- float version of s_floor.c.
  2. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  3. */
  4. /*
  5. * ====================================================
  6. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7. *
  8. * Developed at SunPro, a Sun Microsystems, Inc. business.
  9. * Permission to use, copy, modify, and distribute this
  10. * software is freely granted, provided that this notice
  11. * is preserved.
  12. * ====================================================
  13. */
  14. #include <sys/cdefs.h>
  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 "openlibm.h"
  24. #include "math_private.h"
  25. static const float huge = 1.0e30;
  26. float
  27. floorf(float x)
  28. {
  29. int32_t i0,j0;
  30. u_int32_t i;
  31. GET_FLOAT_WORD(i0,x);
  32. j0 = ((i0>>23)&0xff)-0x7f;
  33. if(j0<23) {
  34. if(j0<0) { /* raise inexact if x != 0 */
  35. if(huge+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  36. if(i0>=0) {i0=0;}
  37. else if((i0&0x7fffffff)!=0)
  38. { i0=0xbf800000;}
  39. }
  40. } else {
  41. i = (0x007fffff)>>j0;
  42. if((i0&i)==0) return x; /* x is integral */
  43. if(huge+x>(float)0.0) { /* raise inexact flag */
  44. if(i0<0) i0 += (0x00800000)>>j0;
  45. i0 &= (~i);
  46. }
  47. }
  48. } else {
  49. if(j0==0x80) return x+x; /* inf or NaN */
  50. else return x; /* x is integral */
  51. }
  52. SET_FLOAT_WORD(x,i0);
  53. return x;
  54. }