s_floorf.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #if defined(LIBM_SCCS) && !defined(lint)
  15. static char rcsid[] = "$NetBSD: s_floorf.c,v 1.4 1995/05/10 20:47:22 jtc Exp $";
  16. #endif
  17. /*
  18. * floorf(x)
  19. * Return x rounded toward -inf to integral value
  20. * Method:
  21. * Bit twiddling.
  22. * Exception:
  23. * Inexact flag raised if x not equal to floorf(x).
  24. */
  25. #include "math.h"
  26. #include "math_private.h"
  27. #ifdef __STDC__
  28. static const float huge = 1.0e30;
  29. #else
  30. static float huge = 1.0e30;
  31. #endif
  32. #ifdef __STDC__
  33. float __floorf(float x)
  34. #else
  35. float __floorf(x)
  36. float x;
  37. #endif
  38. {
  39. int32_t i0,j0;
  40. u_int32_t i;
  41. GET_FLOAT_WORD(i0,x);
  42. j0 = ((i0>>23)&0xff)-0x7f;
  43. if(j0<23) {
  44. if(j0<0) { /* raise inexact if x != 0 */
  45. if(huge+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  46. if(i0>=0) {i0=0;}
  47. else if((i0&0x7fffffff)!=0)
  48. { i0=0xbf800000;}
  49. }
  50. } else {
  51. i = (0x007fffff)>>j0;
  52. if((i0&i)==0) return x; /* x is integral */
  53. if(huge+x>(float)0.0) { /* raise inexact flag */
  54. if(i0<0) i0 += (0x00800000)>>j0;
  55. i0 &= (~i);
  56. }
  57. }
  58. } else {
  59. if(j0==0x80) return x+x; /* inf or NaN */
  60. else return x; /* x is integral */
  61. }
  62. SET_FLOAT_WORD(x,i0);
  63. return x;
  64. }
  65. weak_alias (__floorf, floorf)