1 /*******************************************************************************
4 * Function ceil(x) and floor(x), *
5 * Implementation of ceil and floor for the PowerPC. *
7 * Copyright © 1991 Apple Computer, Inc. All rights reserved. *
9 * Written by Ali Sazegari, started on November 1991, *
11 * based on math.h, library code for Macintoshes with a 68881/68882 *
14 * W A R N I N G: This routine expects a 64 bit double model. *
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 *
22 *******************************************************************************/
27 static const double twoTo52 = 4503599627370496.0;
28 static const unsigned long signMask = 0x80000000ul;
33 #if (__BYTE_ORDER == __BIG_ENDIAN)
44 /*******************************************************************************
45 * Functions needed for the computation. *
46 *******************************************************************************/
48 /*******************************************************************************
49 * Ceil(x) returns the smallest integer not less than x. *
50 *******************************************************************************/
52 libm_hidden_proto(ceil)
53 double ceil ( double x )
55 DblInHex xInHex,OldEnvironment;
57 register unsigned long int xhi;
61 xhi = xInHex.words.hi & 0x7fffffffUL; // xhi is the high half of |x|
62 target = ( xInHex.words.hi < signMask );
64 if ( xhi < 0x43300000ul )
65 /*******************************************************************************
67 *******************************************************************************/
69 if ( xhi < 0x3ff00000ul )
70 /*******************************************************************************
72 *******************************************************************************/
74 if ( ( xhi | xInHex.words.lo ) == 0ul ) // zero x is exact case
78 asm ("mffs %0" : "=f" (OldEnvironment.dbl));
79 OldEnvironment.words.lo |= 0x02000000ul;
80 asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
87 /*******************************************************************************
88 * Is 1.0 < |x| < 2.0^52? *
89 *******************************************************************************/
92 y = ( x + twoTo52 ) - twoTo52; // round at binary pt.
101 y = ( x - twoTo52 ) + twoTo52; // round at binary pt.
108 /*******************************************************************************
109 * |x| >= 2.0^52 or x is a NaN. *
110 *******************************************************************************/
113 libm_hidden_def(ceil)