1
0

floor.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_floor.c */
  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. /*
  13. * floor(x)
  14. * Return x rounded toward -inf to integral value
  15. * Method:
  16. * Bit twiddling.
  17. * Exception:
  18. * Inexact flag raised if x not equal to floor(x).
  19. */
  20. #include "libm.h"
  21. static const double huge = 1.0e300;
  22. double floor(double x)
  23. {
  24. int32_t i0,i1,j0;
  25. uint32_t i,j;
  26. EXTRACT_WORDS(i0, i1, x);
  27. // FIXME: signed shift
  28. j0 = ((i0>>20)&0x7ff) - 0x3ff;
  29. if (j0 < 20) {
  30. if (j0 < 0) { /* |x| < 1 */
  31. /* raise inexact if x != 0 */
  32. if (huge+x > 0.0) {
  33. if (i0 >= 0) { /* x >= 0 */
  34. i0 = i1 = 0;
  35. } else if (((i0&0x7fffffff)|i1) != 0) {
  36. i0 = 0xbff00000;
  37. i1 = 0;
  38. }
  39. }
  40. } else {
  41. i = 0x000fffff>>j0;
  42. if (((i0&i)|i1) == 0)
  43. return x; /* x is integral */
  44. /* raise inexact flag */
  45. if (huge+x > 0.0) {
  46. if (i0 < 0)
  47. i0 += 0x00100000>>j0;
  48. i0 &= ~i;
  49. i1 = 0;
  50. }
  51. }
  52. } else if (j0 > 51) {
  53. if (j0 == 0x400)
  54. return x+x; /* inf or NaN */
  55. else
  56. return x; /* x is integral */
  57. } else {
  58. i = (uint32_t)0xffffffff>>(j0-20);
  59. if ((i1&i) == 0)
  60. return x; /* x is integral */
  61. /* raise inexact flag */
  62. if (huge+x > 0.0) {
  63. if (i0 < 0) {
  64. if (j0 == 20)
  65. i0++;
  66. else {
  67. j = i1+(1<<(52-j0));
  68. if (j < i1)
  69. i0++; /* got a carry */
  70. i1 = j;
  71. }
  72. }
  73. i1 &= ~i;
  74. }
  75. }
  76. INSERT_WORDS(x, i0, i1);
  77. return x;
  78. }