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 #if 0 14 #include <sys/cdefs.h> 15 __FBSDID("$FreeBSD: src/lib/msun/src/s_trunc.c,v 1.1 2004/06/20 09:25:43 das Exp $"); 16 #endif 17 #include <sys/cdefs.h> 18 #if defined(LIBM_SCCS) && !defined(lint) 19 __RCSID("$NetBSD: s_trunc.c,v 1.1 2006/03/25 16:41:12 xtraeme Exp $"); 20 #endif 21 22 /* 23 * trunc(x) 24 * Return x rounded toward 0 to integral value 25 * Method: 26 * Bit twiddling. 27 * Exception: 28 * Inexact flag raised if x not equal to trunc(x). 29 */ 30 31 #include "math.h" 32 #include "math_private.h" 33 34 static const double huge = 1.0e300; 35 36 double 37 trunc(double x) 38 { 39 int32_t i0,i1,j0; 40 uint32_t i; 41 EXTRACT_WORDS(i0,i1,x); 42 j0 = ((i0>>20)&0x7ff)-0x3ff; 43 if(j0<20) { 44 if(j0<0) { /* raise inexact if x != 0 */ 45 if(huge+x>0.0) {/* |x|<1, so return 0*sign(x) */ 46 i0 &= 0x80000000U; 47 i1 = 0; 48 } 49 } else { 50 i = (0x000fffff)>>j0; 51 if(((i0&i)|i1)==0) return x; /* x is integral */ 52 if(huge+x>0.0) { /* raise inexact flag */ 53 i0 &= (~i); i1=0; 54 } 55 } 56 } else if (j0>51) { 57 if(j0==0x400) return x+x; /* inf or NaN */ 58 else return x; /* x is integral */ 59 } else { 60 i = ((u_int32_t)(0xffffffff))>>(j0-20); 61 if((i1&i)==0) return x; /* x is integral */ 62 if(huge+x>0.0) /* raise inexact flag */ 63 i1 &= (~i); 64 } 65 INSERT_WORDS(x,i0,i1); 66 return x; 67 } 68