1130767Sdas/* @(#)s_floor.c 5.1 93/09/24 */
2130767Sdas/*
3130767Sdas * ====================================================
4130767Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5130767Sdas *
6130767Sdas * Developed at SunPro, a Sun Microsystems, Inc. business.
7130767Sdas * Permission to use, copy, modify, and distribute this
8130767Sdas * software is freely granted, provided that this notice
9130767Sdas * is preserved.
10130767Sdas * ====================================================
11130767Sdas */
12130767Sdas
13130767Sdas#include <sys/cdefs.h>
14130767Sdas__FBSDID("$FreeBSD$");
15130767Sdas
16130767Sdas/*
17130767Sdas * truncf(x)
18130767Sdas * Return x rounded toward 0 to integral value
19130767Sdas * Method:
20130767Sdas *	Bit twiddling.
21130767Sdas * Exception:
22130767Sdas *	Inexact flag raised if x not equal to truncf(x).
23130767Sdas */
24130767Sdas
25130767Sdas#include "math.h"
26130767Sdas#include "math_private.h"
27130767Sdas
28130767Sdasstatic const float huge = 1.0e30F;
29130767Sdas
30130767Sdasfloat
31130767Sdastruncf(float x)
32130767Sdas{
33130767Sdas	int32_t i0,j0;
34130767Sdas	u_int32_t i;
35130767Sdas	GET_FLOAT_WORD(i0,x);
36130767Sdas	j0 = ((i0>>23)&0xff)-0x7f;
37130767Sdas	if(j0<23) {
38130767Sdas	    if(j0<0) { 	/* raise inexact if x != 0 */
39130767Sdas		if(huge+x>0.0F)		/* |x|<1, so return 0*sign(x) */
40130767Sdas		    i0 &= 0x80000000;
41130767Sdas	    } else {
42130767Sdas		i = (0x007fffff)>>j0;
43130767Sdas		if((i0&i)==0) return x; /* x is integral */
44130767Sdas		if(huge+x>0.0F)		/* raise inexact flag */
45130767Sdas		    i0 &= (~i);
46130767Sdas	    }
47130767Sdas	} else {
48130767Sdas	    if(j0==0x80) return x+x;	/* inf or NaN */
49130767Sdas	    else return x;		/* x is integral */
50130767Sdas	}
51130767Sdas	SET_FLOAT_WORD(x,i0);
52130767Sdas	return x;
53130767Sdas}
54