s_floor.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/s_floor.c,v 1.11 2008/02/15 07:01:40 bde Exp $");
  14. /*
  15. * floor(x)
  16. * Return x rounded toward -inf to integral value
  17. * Method:
  18. * Bit twiddling.
  19. * Exception:
  20. * Inexact flag raised if x not equal to floor(x).
  21. */
  22. #include <float.h>
  23. #include "openlibm.h"
  24. #include "math_private.h"
  25. static const double huge = 1.0e300;
  26. double
  27. floor(double x)
  28. {
  29. int32_t i0,i1,j0;
  30. u_int32_t i,j;
  31. EXTRACT_WORDS(i0,i1,x);
  32. j0 = ((i0>>20)&0x7ff)-0x3ff;
  33. if(j0<20) {
  34. if(j0<0) { /* raise inexact if x != 0 */
  35. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  36. if(i0>=0) {i0=i1=0;}
  37. else if(((i0&0x7fffffff)|i1)!=0)
  38. { i0=0xbff00000;i1=0;}
  39. }
  40. } else {
  41. i = (0x000fffff)>>j0;
  42. if(((i0&i)|i1)==0) return x; /* x is integral */
  43. if(huge+x>0.0) { /* raise inexact flag */
  44. if(i0<0) i0 += (0x00100000)>>j0;
  45. i0 &= (~i); i1=0;
  46. }
  47. }
  48. } else if (j0>51) {
  49. if(j0==0x400) return x+x; /* inf or NaN */
  50. else return x; /* x is integral */
  51. } else {
  52. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  53. if((i1&i)==0) return x; /* x is integral */
  54. if(huge+x>0.0) { /* raise inexact flag */
  55. if(i0<0) {
  56. if(j0==20) i0+=1;
  57. else {
  58. j = i1+(1<<(52-j0));
  59. if(j<i1) i0 +=1 ; /* got a carry */
  60. i1=j;
  61. }
  62. }
  63. i1 &= (~i);
  64. }
  65. }
  66. INSERT_WORDS(x,i0,i1);
  67. return x;
  68. }
  69. #if LDBL_MANT_DIG == 53
  70. __weak_reference(floor, floorl);
  71. #endif