xref: /netbsd-src/lib/libm/src/s_truncf.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
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_truncf.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_truncf.c,v 1.2 2006/03/25 16:55:53 xtraeme Exp $");
20 #endif
21 
22 /*
23  * truncf(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 truncf(x).
29  */
30 
31 #include "math.h"
32 #include "math_private.h"
33 
34 static const float huge = 1.0e30F;
35 
36 float
37 truncf(float x)
38 {
39 	int32_t i0,j0;
40 	uint32_t i;
41 	GET_FLOAT_WORD(i0,x);
42 	j0 = ((i0>>23)&0xff)-0x7f;
43 	if(j0<23) {
44 	    if(j0<0) { 	/* raise inexact if x != 0 */
45 		if(huge+x>0.0F)		/* |x|<1, so return 0*sign(x) */
46 		    i0 &= 0x80000000;
47 	    } else {
48 		i = (0x007fffff)>>j0;
49 		if((i0&i)==0) return x; /* x is integral */
50 		if(huge+x>0.0F)		/* raise inexact flag */
51 		    i0 &= (~i);
52 	    }
53 	} else {
54 	    if(j0==0x80) return x+x;	/* inf or NaN */
55 	    else return x;		/* x is integral */
56 	}
57 	SET_FLOAT_WORD(x,i0);
58 	return x;
59 }
60