vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

s_trunc.c (1408B)


      1 /* @(#)s_floor.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 
     13 /*
     14  * trunc(x)
     15  * Return x rounded toward 0 to integral value
     16  * Method:
     17  *	Bit twiddling.
     18  * Exception:
     19  *	Inexact flag raised if x not equal to trunc(x).
     20  */
     21 
     22 #include "math.h"
     23 #include "math_private.h"
     24 
     25 static const double huge = 1.0e300;
     26 
     27 double
     28 trunc(double x)
     29 {
     30 	int32_t i0,i1,j0;
     31 	u_int32_t i,j;
     32 	EXTRACT_WORDS(i0,i1,x);
     33 	j0 = ((i0>>20)&0x7ff)-0x3ff;
     34 	if(j0<20) {
     35 	    if(j0<0) { 	/* raise inexact if x != 0 */
     36 		if(huge+x>0.0) {/* |x|<1, so return 0*sign(x) */
     37 		    i0 &= 0x80000000U;
     38 		    i1 = 0;
     39 		}
     40 	    } else {
     41 		i = (0x000fffff)>>j0;
     42 		if(((i0&i)|i1)==0) return x; /* x is integral */
     43 		if(huge+x>0.0) {	/* raise inexact flag */
     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 		i1 &= (~i);
     55 	}
     56 	INSERT_WORDS(x,i0,i1);
     57 	return x;
     58 }