s_ceil.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. * ceil(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 ceil(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 ceil(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=0x80000000;i1=0;}
  35. else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
  36. }
  37. } else {
  38. i = (0x000fffff)>>_j0;
  39. if(((i0&i)|i1)==0) return x; /* x is integral */
  40. if(huge+x>0.0) { /* raise inexact flag */
  41. if(i0>0) i0 += (0x00100000)>>_j0;
  42. i0 &= (~i); i1=0;
  43. }
  44. }
  45. } else if (_j0>51) {
  46. if(_j0==0x400) return x+x; /* inf or NaN */
  47. else return x; /* x is integral */
  48. } else {
  49. i = ((u_int32_t)(0xffffffff))>>(_j0-20);
  50. if((i1&i)==0) return x; /* x is integral */
  51. if(huge+x>0.0) { /* raise inexact flag */
  52. if(i0>0) {
  53. if(_j0==20) i0+=1;
  54. else {
  55. j = i1 + (1<<(52-_j0));
  56. if(j<i1) i0+=1; /* got a carry */
  57. i1 = j;
  58. }
  59. }
  60. i1 &= (~i);
  61. }
  62. }
  63. INSERT_WORDS(x,i0,i1);
  64. return x;
  65. }
  66. libm_hidden_def(ceil)