s_floor.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * floor(x)
  13. * Return x rounded toward -inf to integral value
  14. * Method:
  15. * Bit twiddling.
  16. * Exception:
  17. * Inexact flag raised if x not equal to floor(x).
  18. */
  19. #include <features.h>
  20. /* Prevent math.h from defining a colliding inline */
  21. #undef __USE_EXTERN_INLINES
  22. #include "math.h"
  23. #include "math_private.h"
  24. static const double huge = 1.0e300;
  25. double floor(double x)
  26. {
  27. int32_t i0,i1,_j0;
  28. u_int32_t i,j;
  29. EXTRACT_WORDS(i0,i1,x);
  30. _j0 = ((i0>>20)&0x7ff)-0x3ff;
  31. if(_j0<20) {
  32. if(_j0<0) { /* raise inexact if x != 0 */
  33. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  34. if(i0>=0) {i0=i1=0;}
  35. else if(((i0&0x7fffffff)|i1)!=0)
  36. { i0=0xbff00000;i1=0;}
  37. }
  38. } else {
  39. i = (0x000fffff)>>_j0;
  40. if(((i0&i)|i1)==0) return x; /* x is integral */
  41. if(huge+x>0.0) { /* raise inexact flag */
  42. if(i0<0) i0 += (0x00100000)>>_j0;
  43. i0 &= (~i); i1=0;
  44. }
  45. }
  46. } else if (_j0>51) {
  47. if(_j0==0x400) return x+x; /* inf or NaN */
  48. else return x; /* x is integral */
  49. } else {
  50. i = ((u_int32_t)(0xffffffff))>>(_j0-20);
  51. if((i1&i)==0) return x; /* x is integral */
  52. if(huge+x>0.0) { /* raise inexact flag */
  53. if(i0<0) {
  54. if(_j0==20) i0+=1;
  55. else {
  56. j = i1+(1<<(52-_j0));
  57. if(j<i1) i0 +=1 ; /* got a carry */
  58. i1=j;
  59. }
  60. }
  61. i1 &= (~i);
  62. }
  63. }
  64. INSERT_WORDS(x,i0,i1);
  65. return x;
  66. }
  67. libm_hidden_def(floor)