home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / vbcc / machines / amigappc / libsrc / math / ceil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-24  |  1.4 KB  |  60 lines

  1. /* Modified version of s_ceil.c for vbcc-PowerPC */
  2.  
  3. /*
  4.  * ====================================================
  5.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  6.  *
  7.  * Developed at SunSoft, a Sun Microsystems, Inc. business.
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software is freely granted, provided that this notice 
  10.  * is preserved.
  11.  * ====================================================
  12.  */
  13.  
  14. static const double huge = 1.0e300;
  15.  
  16.  
  17. double ceil(double x)
  18. {
  19.     int i0,i1,j0;
  20.     unsigned i,j;
  21.     i0 = *(int*)&x;
  22.     i1 = *(1+(int*)&x);
  23.     j0 = ((i0>>20)&0x7ff)-0x3ff;
  24.     if(j0<20) {
  25.         if(j0<0) {     /* raise inexact if x != 0 */
  26.         if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  27.             if(i0<0) {i0=0x80000000;i1=0;} 
  28.             else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
  29.         }
  30.         } else {
  31.         i = (0x000fffff)>>j0;
  32.         if(((i0&i)|i1)==0) return x; /* x is integral */
  33.         if(huge+x>0.0) {    /* raise inexact flag */
  34.             if(i0>0) i0 += (0x00100000)>>j0;
  35.             i0 &= (~i); i1=0;
  36.         }
  37.         }
  38.     } else if (j0>51) {
  39.         if(j0==0x400) return x+x;    /* inf or NaN */
  40.         else return x;        /* x is integral */
  41.     } else {
  42.         i = ((unsigned)(0xffffffff))>>(j0-20);
  43.         if((i1&i)==0) return x;    /* x is integral */
  44.         if(huge+x>0.0) {         /* raise inexact flag */
  45.         if(i0>0) {
  46.             if(j0==20) i0+=1; 
  47.             else {
  48.             j = i1 + (1<<(52-j0));
  49.             if(j<i1) i0+=1;    /* got a carry */
  50.             i1 = j;
  51.             }
  52.         }
  53.         i1 &= (~i);
  54.         }
  55.     }
  56.     *(int*)&x = i0;
  57.     *(1+(int*)&x) = i1;
  58.     return x;
  59. }
  60.