s_floorf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* s_floorf.c -- float version of s_floor.c.
  2. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  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 "cdefs-compat.h"
  15. //__FBSDID("$FreeBSD: src/lib/msun/src/s_floorf.c,v 1.8 2008/02/22 02:30:35 das Exp $");
  16. /*
  17. * floorf(x)
  18. * Return x rounded toward -inf to integral value
  19. * Method:
  20. * Bit twiddling.
  21. * Exception:
  22. * Inexact flag raised if x not equal to floorf(x).
  23. */
  24. #include <openlibm_math.h>
  25. #include "math_private.h"
  26. static const float huge = 1.0e30;
  27. DLLEXPORT float
  28. floorf(float x)
  29. {
  30. int32_t i0,j0;
  31. u_int32_t i;
  32. GET_FLOAT_WORD(i0,x);
  33. j0 = ((i0>>23)&0xff)-0x7f;
  34. if(j0<23) {
  35. if(j0<0) { /* raise inexact if x != 0 */
  36. if(huge+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  37. if(i0>=0) {i0=0;}
  38. else if((i0&0x7fffffff)!=0)
  39. { i0=0xbf800000;}
  40. }
  41. } else {
  42. i = (0x007fffff)>>j0;
  43. if((i0&i)==0) return x; /* x is integral */
  44. if(huge+x>(float)0.0) { /* raise inexact flag */
  45. if(i0<0) i0 += (0x00800000)>>j0;
  46. i0 &= (~i);
  47. }
  48. }
  49. } else {
  50. if(j0==0x80) return x+x; /* inf or NaN */
  51. else return x; /* x is integral */
  52. }
  53. SET_FLOAT_WORD(x,i0);
  54. return x;
  55. }