s_ceil.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. double ceil ( double x )
  46. {
  47. DblInHex xInHex,OldEnvironment;
  48. register double y;
  49. register unsigned long int xhi;
  50. register int target;
  51. xInHex.dbl = x;
  52. xhi = xInHex.words.hi & 0x7fffffffUL; // xhi is the high half of |x|
  53. target = ( xInHex.words.hi < signMask );
  54. if ( xhi < 0x43300000ul )
  55. /*******************************************************************************
  56. * Is |x| < 2.0^52? *
  57. *******************************************************************************/
  58. {
  59. if ( xhi < 0x3ff00000ul )
  60. /*******************************************************************************
  61. * Is |x| < 1.0? *
  62. *******************************************************************************/
  63. {
  64. if ( ( xhi | xInHex.words.lo ) == 0ul ) // zero x is exact case
  65. return ( x );
  66. else
  67. { // inexact case
  68. asm ("mffs %0" : "=f" (OldEnvironment.dbl));
  69. OldEnvironment.words.lo |= 0x02000000ul;
  70. asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
  71. if ( target )
  72. return ( 1.0 );
  73. else
  74. return ( -0.0 );
  75. }
  76. }
  77. /*******************************************************************************
  78. * Is 1.0 < |x| < 2.0^52? *
  79. *******************************************************************************/
  80. if ( target )
  81. {
  82. y = ( x + twoTo52 ) - twoTo52; // round at binary pt.
  83. if ( y < x )
  84. return ( y + 1.0 );
  85. else
  86. return ( y );
  87. }
  88. else
  89. {
  90. y = ( x - twoTo52 ) + twoTo52; // round at binary pt.
  91. if ( y < x )
  92. return ( y + 1.0 );
  93. else
  94. return ( y );
  95. }
  96. }
  97. /*******************************************************************************
  98. * |x| >= 2.0^52 or x is a NaN. *
  99. *******************************************************************************/
  100. return ( x );
  101. }