POK
modf.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_modf.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  * modf(double x, double *iptr)
31  * return fraction part of x, and return x's integral part in *iptr.
32  * Method:
33  * Bit twiddling.
34  *
35  * Exception:
36  * No exception.
37  */
38 
39 #ifdef POK_NEEDS_LIBMATH
40 
41 #include <libm.h>
42 #include "math_private.h"
43 
44 static const double one = 1.0;
45 
46 double
47 modf(double x, double *iptr)
48 {
49  int32_t i0,i1,jj0;
50  uint32_t i;
51  EXTRACT_WORDS(i0,i1,x);
52  jj0 = (((uint32_t)i0>>20)&0x7ff)-0x3ff; /* exponent of x */
53  if(jj0<20) { /* integer part in high x */
54  if(jj0<0) { /* |x|<1 */
55  INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
56  return x;
57  } else {
58  i = (0x000fffff)>>jj0;
59  if(((i0&i)|i1)==0) { /* x is integral */
60  uint32_t high;
61  *iptr = x;
62  GET_HIGH_WORD(high,x);
63  INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
64  return x;
65  } else {
66  INSERT_WORDS(*iptr,i0&(~i),0);
67  return x - *iptr;
68  }
69  }
70  } else if (jj0>51) { /* no fraction part */
71  uint32_t high;
72  *iptr = x*one;
73  GET_HIGH_WORD(high,x);
74  INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
75  return x;
76  } else { /* fraction part in low x */
77  i = ((uint32_t)(0xffffffff))>>(jj0-20);
78  if((i1&i)==0) { /* x is integral */
79  uint32_t high;
80  *iptr = x;
81  GET_HIGH_WORD(high,x);
82  INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
83  return x;
84  } else {
85  INSERT_WORDS(*iptr,i0,i1&(~i));
86  return x - *iptr;
87  }
88  }
89 }
90 
91 #endif