POK
floor.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 /*
30  * floor(x)
31  * Return x rounded toward -inf to integral value
32  * Method:
33  * Bit twiddling.
34  * Exception:
35  * Inexact flag raised if x not equal to floor(x).
36  */
37 
38 #ifdef POK_NEEDS_LIBMATH
39 
40 #include "math_private.h"
41 
42 static const double huge = 1.0e300;
43 
44 double
45 floor(double x)
46 {
47  int32_t i0,i1,jj0;
48  uint32_t i,j;
49  EXTRACT_WORDS(i0,i1,x);
50  jj0 = ((i0>>20)&0x7ff)-0x3ff;
51  if(jj0<20) {
52  if(jj0<0) { /* raise inexact if x != 0 */
53  if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
54  if(i0>=0) {i0=i1=0;}
55  else if(((i0&0x7fffffff)|i1)!=0)
56  { i0=0xbff00000;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  if(i0<0) i0 += (0x00100000)>>jj0;
63  i0 &= (~i); i1=0;
64  }
65  }
66  } else if (jj0>51) {
67  if(jj0==0x400) return x+x; /* inf or NaN */
68  else return x; /* x is integral */
69  } else {
70  i = ((uint32_t)(0xffffffff))>>(jj0-20);
71  if((i1&i)==0) return x; /* x is integral */
72  if(huge+x>0.0) { /* raise inexact flag */
73  if(i0<0) {
74  if(jj0==20) i0+=1;
75  else {
76  j = i1+(1<<(52-jj0));
77  if(j<(uint32_t)i1) i0 +=1 ; /* got a carry */
78  i1=j;
79  }
80  }
81  i1 &= (~i);
82  }
83  }
84  INSERT_WORDS(x,i0,i1);
85  return x;
86 }
87 
88 #endif