POK
trunc.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  * trunc(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 trunc(x).
38  */
39 
40 #include <libm.h>
41 #include "math_private.h"
42 
43 static const double huge = 1.0e300;
44 
45 double
46 trunc(double x)
47 {
48  int32_t i0,i1,jj0;
49  uint32_t i;
50  EXTRACT_WORDS(i0,i1,x);
51  jj0 = ((i0>>20)&0x7ff)-0x3ff;
52  if(jj0<20) {
53  if(jj0<0) { /* raise inexact if x != 0 */
54  if(huge+x>0.0) {/* |x|<1, so return 0*sign(x) */
55  i0 &= 0x80000000U;
56  i1 = 0;
57  }
58  } else {
59  i = (0x000fffff)>>jj0;
60  if(((i0&i)|i1)==0) return x; /* x is integral */
61  if(huge+x>0.0) { /* raise inexact flag */
62  i0 &= (~i); i1=0;
63  }
64  }
65  } else if (jj0>51) {
66  if(jj0==0x400) return x+x; /* inf or NaN */
67  else return x; /* x is integral */
68  } else {
69  i = ((uint32_t)(0xffffffff))>>(jj0-20);
70  if((i1&i)==0) return x; /* x is integral */
71  if(huge+x>0.0) /* raise inexact flag */
72  i1 &= (~i);
73  }
74  INSERT_WORDS(x,i0,i1);
75  return x;
76 }
77 
78 #endif