POK
floorf.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_floorf.c -- float version of s_floor.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 
33 /*
34  * floorf(x)
35  * Return x rounded toward -inf to integral value
36  * Method:
37  * Bit twiddling.
38  * Exception:
39  * Inexact flag raised if x not equal to floorf(x).
40  */
41 
42 #ifdef POK_NEEDS_LIBMATH
43 
44 #include <libm.h>
45 #include "math_private.h"
46 
47 static const float huge = 1.0e30;
48 
49 float
50 floorf(float x)
51 {
52  int32_t i0,jj0;
53  uint32_t i;
54  GET_FLOAT_WORD(i0,x);
55  jj0 = ((i0>>23)&0xff)-0x7f;
56  if(jj0<23) {
57  if(jj0<0) { /* raise inexact if x != 0 */
58  if(huge+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
59  if(i0>=0) {i0=0;}
60  else if((i0&0x7fffffff)!=0)
61  { i0=0xbf800000;}
62  }
63  } else {
64  i = (0x007fffff)>>jj0;
65  if((i0&i)==0) return x; /* x is integral */
66  if(huge+x>(float)0.0) { /* raise inexact flag */
67  if(i0<0) i0 += (0x00800000)>>jj0;
68  i0 &= (~i);
69  }
70  }
71  } else {
72  if(jj0==0x80) return x+x; /* inf or NaN */
73  else return x; /* x is integral */
74  }
75  SET_FLOAT_WORD(x,i0);
76  return x;
77 }
78 
79 #endif