ceil.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_ceil.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. * ceil(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 ceil(x).
  19. */
  20. #include "libm.h"
  21. static const double huge = 1.0e300;
  22. double ceil(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) {
  31. /* raise inexact if x != 0 */
  32. if (huge+x > 0.0) {
  33. if (i0 < 0) {
  34. i0 = 0x80000000;
  35. i1=0;
  36. } else if ((i0|i1) != 0) {
  37. i0=0x3ff00000;
  38. i1=0;
  39. }
  40. }
  41. } else {
  42. i = 0x000fffff>>j0;
  43. if (((i0&i)|i1) == 0) /* x is integral */
  44. return x;
  45. /* raise inexact flag */
  46. if (huge+x > 0.0) {
  47. if (i0 > 0)
  48. i0 += 0x00100000>>j0;
  49. i0 &= ~i;
  50. i1 = 0;
  51. }
  52. }
  53. } else if (j0 > 51) {
  54. if (j0 == 0x400) /* inf or NaN */
  55. return x+x;
  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 += 1;
  66. else {
  67. j = i1 + (1<<(52-j0));
  68. if (j < i1) /* got a carry */
  69. i0 += 1;
  70. i1 = j;
  71. }
  72. }
  73. i1 &= ~i;
  74. }
  75. }
  76. INSERT_WORDS(x, i0, i1);
  77. return x;
  78. }