s_ceil.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************
  2. * *
  3. * File ceilfloor.c, *
  4. * Function ceil(x) and floor(x), *
  5. * Implementation of ceil and floor for the PowerPC. *
  6. * *
  7. * Copyright © 1991 Apple Computer, Inc. All rights reserved. *
  8. * *
  9. * Written by Ali Sazegari, started on November 1991, *
  10. * *
  11. * based on math.h, library code for Macintoshes with a 68881/68882 *
  12. * by Jim Thomas. *
  13. * *
  14. * W A R N I N G: This routine expects a 64 bit double model. *
  15. * *
  16. * December 03 1992: first rs6000 port. *
  17. * July 14 1993: comment changes and addition of #pragma fenv_access. *
  18. * May 06 1997: port of the ibm/taligent ceil and floor routines. *
  19. * April 11 2001: first port to os x using gcc. *
  20. * June 13 2001: replaced __setflm with in-line assembly *
  21. * *
  22. *******************************************************************************/
  23. #include <endian.h>
  24. static const double twoTo52 = 4503599627370496.0;
  25. static const unsigned long signMask = 0x80000000ul;
  26. typedef union
  27. {
  28. struct {
  29. #if (__BYTE_ORDER == __BIG_ENDIAN)
  30. unsigned long int hi;
  31. unsigned long int lo;
  32. #else
  33. unsigned long int lo;
  34. unsigned long int hi;
  35. #endif
  36. } words;
  37. double dbl;
  38. } DblInHex;
  39. /*******************************************************************************
  40. * Functions needed for the computation. *
  41. *******************************************************************************/
  42. /*******************************************************************************
  43. * Ceil(x) returns the smallest integer not less than x. *
  44. *******************************************************************************/
  45. libm_hidden_proto(ceil)
  46. double ceil ( double x )
  47. {
  48. DblInHex xInHex,OldEnvironment;
  49. register double y;
  50. register unsigned long int xhi;
  51. register int target;
  52. xInHex.dbl = x;
  53. xhi = xInHex.words.hi & 0x7fffffffUL; // xhi is the high half of |x|
  54. target = ( xInHex.words.hi < signMask );
  55. if ( xhi < 0x43300000ul )
  56. /*******************************************************************************
  57. * Is |x| < 2.0^52? *
  58. *******************************************************************************/
  59. {
  60. if ( xhi < 0x3ff00000ul )
  61. /*******************************************************************************
  62. * Is |x| < 1.0? *
  63. *******************************************************************************/
  64. {
  65. if ( ( xhi | xInHex.words.lo ) == 0ul ) // zero x is exact case
  66. return ( x );
  67. else
  68. { // inexact case
  69. asm ("mffs %0" : "=f" (OldEnvironment.dbl));
  70. OldEnvironment.words.lo |= 0x02000000ul;
  71. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  72. if ( target )
  73. return ( 1.0 );
  74. else
  75. return ( -0.0 );
  76. }
  77. }
  78. /*******************************************************************************
  79. * Is 1.0 < |x| < 2.0^52? *
  80. *******************************************************************************/
  81. if ( target )
  82. {
  83. y = ( x + twoTo52 ) - twoTo52; // round at binary pt.
  84. if ( y < x )
  85. return ( y + 1.0 );
  86. else
  87. return ( y );
  88. }
  89. else
  90. {
  91. y = ( x - twoTo52 ) + twoTo52; // round at binary pt.
  92. if ( y < x )
  93. return ( y + 1.0 );
  94. else
  95. return ( y );
  96. }
  97. }
  98. /*******************************************************************************
  99. * |x| >= 2.0^52 or x is a NaN. *
  100. *******************************************************************************/
  101. return ( x );
  102. }
  103. libm_hidden_def(ceil)