s_ceil.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* @(#)s_ceil.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_ceil.c,v 1.11 2008/02/15 07:01:40 bde Exp $");
  14. /*
  15. * ceil(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 ceil(x).
  21. */
  22. #include <float.h>
  23. #include <openlibm_math.h>
  24. #include "math_private.h"
  25. static const double huge = 1.0e300;
  26. DLLEXPORT double
  27. ceil(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=0x80000000;i1=0;}
  37. else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
  38. }
  39. } else {
  40. i = (0x000fffff)>>j0;
  41. if(((i0&i)|i1)==0) return x; /* x is integral */
  42. if(huge+x>0.0) { /* raise inexact flag */
  43. if(i0>0) i0 += (0x00100000)>>j0;
  44. i0 &= (~i); i1=0;
  45. }
  46. }
  47. } else if (j0>51) {
  48. if(j0==0x400) return x+x; /* inf or NaN */
  49. else return x; /* x is integral */
  50. } else {
  51. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  52. if((i1&i)==0) return x; /* x is integral */
  53. if(huge+x>0.0) { /* raise inexact flag */
  54. if(i0>0) {
  55. if(j0==20) i0+=1;
  56. else {
  57. j = i1 + (1<<(52-j0));
  58. if(j<i1) i0+=1; /* got a carry */
  59. i1 = j;
  60. }
  61. }
  62. i1 &= (~i);
  63. }
  64. }
  65. INSERT_WORDS(x,i0,i1);
  66. return x;
  67. }
  68. #if LDBL_MANT_DIG == 53
  69. __weak_reference(ceil, ceill);
  70. #endif