r27195@plastic: rob | 2007-09-07 07:49:52 +1000
[tangerine.git] / compiler / mlib / s_modf.c
blob20aeb3083681ccb223e8f92df1dcf28fc65df915
1 /* @(#)s_modf.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
13 #ifndef lint
14 static char rcsid[] = "$FreeBSD: src/lib/msun/src/s_modf.c,v 1.5 1999/08/28 00:06:53 peter Exp $";
15 #endif
18 * modf(double x, double *iptr)
19 * return fraction part of x, and return x's integral part in *iptr.
20 * Method:
21 * Bit twiddling.
23 * Exception:
24 * No exception.
27 #include "math.h"
28 #include "math_private.h"
30 #ifdef __STDC__
31 static const double one = 1.0;
32 #else
33 static double one = 1.0;
34 #endif
36 #ifdef __STDC__
37 double modf(double x, double *iptr)
38 #else
39 double modf(x, iptr)
40 double x,*iptr;
41 #endif
43 int32_t i0,i1,j0;
44 uint32_t i;
45 EXTRACT_WORDS(i0,i1,x);
46 j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
47 if(j0<20) { /* integer part in high x */
48 if(j0<0) { /* |x|<1 */
49 INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
50 return x;
51 } else {
52 i = (0x000fffff)>>j0;
53 if(((i0&i)|i1)==0) { /* x is integral */
54 uint32_t high;
55 *iptr = x;
56 GET_HIGH_WORD(high,x);
57 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
58 return x;
59 } else {
60 INSERT_WORDS(*iptr,i0&(~i),0);
61 return x - *iptr;
64 } else if (j0>51) { /* no fraction part */
65 uint32_t high;
66 *iptr = x*one;
67 GET_HIGH_WORD(high,x);
68 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
69 return x;
70 } else { /* fraction part in low x */
71 i = ((uint32_t)(0xffffffff))>>(j0-20);
72 if((i1&i)==0) { /* x is integral */
73 uint32_t high;
74 *iptr = x;
75 GET_HIGH_WORD(high,x);
76 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
77 return x;
78 } else {
79 INSERT_WORDS(*iptr,i0,i1&(~i));
80 return x - *iptr;