s_floor.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* @(#)s_floor.c 5.1 93/09/24 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. #if defined(LIBM_SCCS) && !defined(lint)
  13. static char rcsid[] = "$NetBSD: s_floor.c,v 1.8 1995/05/10 20:47:20 jtc Exp $";
  14. #endif
  15. /*
  16. * floor(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 floor(x).
  22. */
  23. #include <features.h>
  24. /* Prevent math.h from defining a colliding inline */
  25. #undef __USE_EXTERN_INLINES
  26. #include "math.h"
  27. #include "math_private.h"
  28. #ifdef __STDC__
  29. static const double huge = 1.0e300;
  30. #else
  31. static double huge = 1.0e300;
  32. #endif
  33. #ifdef __STDC__
  34. double floor(double x)
  35. #else
  36. double floor(x)
  37. double x;
  38. #endif
  39. {
  40. int32_t i0,i1,j0;
  41. u_int32_t i,j;
  42. EXTRACT_WORDS(i0,i1,x);
  43. j0 = ((i0>>20)&0x7ff)-0x3ff;
  44. if(j0<20) {
  45. if(j0<0) { /* raise inexact if x != 0 */
  46. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  47. if(i0>=0) {i0=i1=0;}
  48. else if(((i0&0x7fffffff)|i1)!=0)
  49. { i0=0xbff00000;i1=0;}
  50. }
  51. } else {
  52. i = (0x000fffff)>>j0;
  53. if(((i0&i)|i1)==0) return x; /* x is integral */
  54. if(huge+x>0.0) { /* raise inexact flag */
  55. if(i0<0) i0 += (0x00100000)>>j0;
  56. i0 &= (~i); i1=0;
  57. }
  58. }
  59. } else if (j0>51) {
  60. if(j0==0x400) return x+x; /* inf or NaN */
  61. else return x; /* x is integral */
  62. } else {
  63. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  64. if((i1&i)==0) return x; /* x is integral */
  65. if(huge+x>0.0) { /* raise inexact flag */
  66. if(i0<0) {
  67. if(j0==20) i0+=1;
  68. else {
  69. j = i1+(1<<(52-j0));
  70. if(j<i1) i0 +=1 ; /* got a carry */
  71. i1=j;
  72. }
  73. }
  74. i1 &= (~i);
  75. }
  76. }
  77. INSERT_WORDS(x,i0,i1);
  78. return x;
  79. }
  80. libm_hidden_def(floor)