POK
truncf.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* @(#)s_floor.c 5.1 93/09/24 */
18 /*
19  * ====================================================
20  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
21  *
22  * Developed at SunPro, a Sun Microsystems, Inc. business.
23  * Permission to use, copy, modify, and distribute this
24  * software is freely granted, provided that this notice
25  * is preserved.
26  * ====================================================
27  */
28 
29 #ifdef POK_NEEDS_LIBMATH
30 
31 /*
32  * truncf(x)
33  * Return x rounded toward 0 to integral value
34  * Method:
35  * Bit twiddling.
36  * Exception:
37  * Inexact flag raised if x not equal to truncf(x).
38  */
39 
40 #include <libm.h>
41 #include "math_private.h"
42 
43 static const float huge = 1.0e30F;
44 
45 float
46 truncf(float x)
47 {
48  int32_t i0,jj0;
49  uint32_t i;
50  GET_FLOAT_WORD(i0,x);
51  jj0 = ((i0>>23)&0xff)-0x7f;
52  if(jj0<23) {
53  if(jj0<0) { /* raise inexact if x != 0 */
54  if(huge+x>0.0F) /* |x|<1, so return 0*sign(x) */
55  i0 &= 0x80000000;
56  } else {
57  i = (0x007fffff)>>jj0;
58  if((i0&i)==0) return x; /* x is integral */
59  if(huge+x>0.0F) /* raise inexact flag */
60  i0 &= (~i);
61  }
62  } else {
63  if(jj0==0x80) return x+x; /* inf or NaN */
64  else return x; /* x is integral */
65  }
66  SET_FLOAT_WORD(x,i0);
67  return x;
68 }
69 
70 #endif