1*6ff43c94SPeter Avalos /* @(#)s_modf.c 5.1 93/09/24 */
2*6ff43c94SPeter Avalos /* $FreeBSD: head/lib/libc/gen/modf.c 226606 2011-10-21 06:40:36Z das $ */
3*6ff43c94SPeter Avalos /*
4*6ff43c94SPeter Avalos * ====================================================
5*6ff43c94SPeter Avalos * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6*6ff43c94SPeter Avalos *
7*6ff43c94SPeter Avalos * Developed at SunPro, a Sun Microsystems, Inc. business.
8*6ff43c94SPeter Avalos * Permission to use, copy, modify, and distribute this
9*6ff43c94SPeter Avalos * software is freely granted, provided that this notice
10*6ff43c94SPeter Avalos * is preserved.
11*6ff43c94SPeter Avalos * ====================================================
12*6ff43c94SPeter Avalos */
13*6ff43c94SPeter Avalos
14*6ff43c94SPeter Avalos /*
15*6ff43c94SPeter Avalos * modf(double x, double *iptr)
16*6ff43c94SPeter Avalos * return fraction part of x, and return x's integral part in *iptr.
17*6ff43c94SPeter Avalos * Method:
18*6ff43c94SPeter Avalos * Bit twiddling.
19*6ff43c94SPeter Avalos *
20*6ff43c94SPeter Avalos * Exception:
21*6ff43c94SPeter Avalos * No exception.
22*6ff43c94SPeter Avalos */
23*6ff43c94SPeter Avalos
24*6ff43c94SPeter Avalos #include <sys/types.h>
25*6ff43c94SPeter Avalos #include <machine/endian.h>
26*6ff43c94SPeter Avalos #include <math.h>
27*6ff43c94SPeter Avalos
28*6ff43c94SPeter Avalos /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
29*6ff43c94SPeter Avalos
30*6ff43c94SPeter Avalos #if BYTE_ORDER == BIG_ENDIAN
31*6ff43c94SPeter Avalos
32*6ff43c94SPeter Avalos typedef union
33*6ff43c94SPeter Avalos {
34*6ff43c94SPeter Avalos double value;
35*6ff43c94SPeter Avalos struct
36*6ff43c94SPeter Avalos {
37*6ff43c94SPeter Avalos u_int32_t msw;
38*6ff43c94SPeter Avalos u_int32_t lsw;
39*6ff43c94SPeter Avalos } parts;
40*6ff43c94SPeter Avalos } ieee_double_shape_type;
41*6ff43c94SPeter Avalos
42*6ff43c94SPeter Avalos #endif
43*6ff43c94SPeter Avalos
44*6ff43c94SPeter Avalos #if BYTE_ORDER == LITTLE_ENDIAN
45*6ff43c94SPeter Avalos
46*6ff43c94SPeter Avalos typedef union
47*6ff43c94SPeter Avalos {
48*6ff43c94SPeter Avalos double value;
49*6ff43c94SPeter Avalos struct
50*6ff43c94SPeter Avalos {
51*6ff43c94SPeter Avalos u_int32_t lsw;
52*6ff43c94SPeter Avalos u_int32_t msw;
53*6ff43c94SPeter Avalos } parts;
54*6ff43c94SPeter Avalos } ieee_double_shape_type;
55*6ff43c94SPeter Avalos
56*6ff43c94SPeter Avalos #endif
57*6ff43c94SPeter Avalos
58*6ff43c94SPeter Avalos /* Get two 32 bit ints from a double. */
59*6ff43c94SPeter Avalos
60*6ff43c94SPeter Avalos #define EXTRACT_WORDS(ix0,ix1,d) \
61*6ff43c94SPeter Avalos do { \
62*6ff43c94SPeter Avalos ieee_double_shape_type ew_u; \
63*6ff43c94SPeter Avalos ew_u.value = (d); \
64*6ff43c94SPeter Avalos (ix0) = ew_u.parts.msw; \
65*6ff43c94SPeter Avalos (ix1) = ew_u.parts.lsw; \
66*6ff43c94SPeter Avalos } while (0)
67*6ff43c94SPeter Avalos
68*6ff43c94SPeter Avalos /* Get the more significant 32 bit int from a double. */
69*6ff43c94SPeter Avalos
70*6ff43c94SPeter Avalos #define GET_HIGH_WORD(i,d) \
71*6ff43c94SPeter Avalos do { \
72*6ff43c94SPeter Avalos ieee_double_shape_type gh_u; \
73*6ff43c94SPeter Avalos gh_u.value = (d); \
74*6ff43c94SPeter Avalos (i) = gh_u.parts.msw; \
75*6ff43c94SPeter Avalos } while (0)
76*6ff43c94SPeter Avalos
77*6ff43c94SPeter Avalos /* Set a double from two 32 bit ints. */
78*6ff43c94SPeter Avalos
79*6ff43c94SPeter Avalos #define INSERT_WORDS(d,ix0,ix1) \
80*6ff43c94SPeter Avalos do { \
81*6ff43c94SPeter Avalos ieee_double_shape_type iw_u; \
82*6ff43c94SPeter Avalos iw_u.parts.msw = (ix0); \
83*6ff43c94SPeter Avalos iw_u.parts.lsw = (ix1); \
84*6ff43c94SPeter Avalos (d) = iw_u.value; \
85*6ff43c94SPeter Avalos } while (0)
86*6ff43c94SPeter Avalos
87*6ff43c94SPeter Avalos static const double one = 1.0;
88*6ff43c94SPeter Avalos
89*6ff43c94SPeter Avalos double
modf(double x,double * iptr)90*6ff43c94SPeter Avalos modf(double x, double *iptr)
91*6ff43c94SPeter Avalos {
92*6ff43c94SPeter Avalos int32_t i0,i1,j0;
93*6ff43c94SPeter Avalos u_int32_t i;
94*6ff43c94SPeter Avalos EXTRACT_WORDS(i0,i1,x);
95*6ff43c94SPeter Avalos j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
96*6ff43c94SPeter Avalos if(j0<20) { /* integer part in high x */
97*6ff43c94SPeter Avalos if(j0<0) { /* |x|<1 */
98*6ff43c94SPeter Avalos INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
99*6ff43c94SPeter Avalos return x;
100*6ff43c94SPeter Avalos } else {
101*6ff43c94SPeter Avalos i = (0x000fffff)>>j0;
102*6ff43c94SPeter Avalos if(((i0&i)|i1)==0) { /* x is integral */
103*6ff43c94SPeter Avalos u_int32_t high;
104*6ff43c94SPeter Avalos *iptr = x;
105*6ff43c94SPeter Avalos GET_HIGH_WORD(high,x);
106*6ff43c94SPeter Avalos INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
107*6ff43c94SPeter Avalos return x;
108*6ff43c94SPeter Avalos } else {
109*6ff43c94SPeter Avalos INSERT_WORDS(*iptr,i0&(~i),0);
110*6ff43c94SPeter Avalos return x - *iptr;
111*6ff43c94SPeter Avalos }
112*6ff43c94SPeter Avalos }
113*6ff43c94SPeter Avalos } else if (j0>51) { /* no fraction part */
114*6ff43c94SPeter Avalos u_int32_t high;
115*6ff43c94SPeter Avalos if (j0 == 0x400) { /* inf/NaN */
116*6ff43c94SPeter Avalos *iptr = x;
117*6ff43c94SPeter Avalos return 0.0 / x;
118*6ff43c94SPeter Avalos }
119*6ff43c94SPeter Avalos *iptr = x*one;
120*6ff43c94SPeter Avalos GET_HIGH_WORD(high,x);
121*6ff43c94SPeter Avalos INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
122*6ff43c94SPeter Avalos return x;
123*6ff43c94SPeter Avalos } else { /* fraction part in low x */
124*6ff43c94SPeter Avalos i = ((u_int32_t)(0xffffffff))>>(j0-20);
125*6ff43c94SPeter Avalos if((i1&i)==0) { /* x is integral */
126*6ff43c94SPeter Avalos u_int32_t high;
127*6ff43c94SPeter Avalos *iptr = x;
128*6ff43c94SPeter Avalos GET_HIGH_WORD(high,x);
129*6ff43c94SPeter Avalos INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
130*6ff43c94SPeter Avalos return x;
131*6ff43c94SPeter Avalos } else {
132*6ff43c94SPeter Avalos INSERT_WORDS(*iptr,i0,i1&(~i));
133*6ff43c94SPeter Avalos return x - *iptr;
134*6ff43c94SPeter Avalos }
135*6ff43c94SPeter Avalos }
136*6ff43c94SPeter Avalos }
137