Subversion Repositories Kolibri OS

Rev

Rev 1906 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3362 Serge 1
 
2
/*
3
 * ====================================================
4
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5
 *
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
 * ====================================================
11
 */
12
13
 
14
FUNCTION
15
       <>, <>---split fractional and integer parts
16
17
 
18
	modf
19
INDEX
20
	modff
21
22
 
23
	#include 
24
	double modf(double <[val]>, double *<[ipart]>);
25
        float modff(float <[val]>, float *<[ipart]>);
26
27
 
28
	#include 
29
	double modf(<[val]>, <[ipart]>)
30
        double <[val]>;
31
        double *<[ipart]>;
32
33
 
34
	float <[val]>;
35
        float *<[ipart]>;
36
37
 
38
	<> splits the double <[val]> apart into an integer part
39
	and a fractional part, returning the fractional part and
40
	storing the integer part in <<*<[ipart]>>>.  No rounding
41
	whatsoever is done; the sum of the integer and fractional
42
	parts is guaranteed to be exactly  equal to <[val]>.   That
43
	is, if <[realpart]> = modf(<[val]>, &<[intpart]>); then
44
	`<<<[realpart]>+<[intpart]>>>' is the same as <[val]>.
45
	<> is identical, save that it takes and returns
46
	<> rather than <> values.
47
48
 
49
	The fractional part is returned.  Each result has the same
50
	sign as the supplied argument <[val]>.
51
52
 
53
	<> is ANSI C. <> is an extension.
54
55
 
56
	modf  ansi pure
57
	modff - pure
58
59
 
60
61
 
62
 * modf(double x, double *iptr)
63
 * return fraction part of x, and return x's integral part in *iptr.
64
 * Method:
65
 *	Bit twiddling.
66
 *
67
 * Exception:
68
 *	No exception.
69
 */
70
71
 
72
73
 
74
75
 
76
static const double one = 1.0;
77
#else
78
static double one = 1.0;
79
#endif
80
81
 
82
	double modf(double x, double *iptr)
83
#else
84
	double modf(x, iptr)
85
	double x,*iptr;
86
#endif
87
{
88
	__int32_t i0,i1,j0;
89
	__uint32_t i;
90
	EXTRACT_WORDS(i0,i1,x);
91
	j0 = ((i0>>20)&0x7ff)-0x3ff;	/* exponent of x */
92
	if(j0<20) {			/* integer part in high x */
93
	    if(j0<0) {			/* |x|<1 */
94
	        INSERT_WORDS(*iptr,i0&0x80000000,0);	/* *iptr = +-0 */
95
		return x;
96
	    } else {
97
		i = (0x000fffff)>>j0;
98
		if(((i0&i)|i1)==0) {		/* x is integral */
99
		    __uint32_t high;
100
		    *iptr = x;
101
		    GET_HIGH_WORD(high,x);
102
		    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
103
		    return x;
104
		} else {
105
		    INSERT_WORDS(*iptr,i0&(~i),0);
106
		    return x - *iptr;
107
		}
108
	    }
109
	} else if (j0>51) {		/* no fraction part */
110
	    __uint32_t high;
111
	    *iptr = x*one;
112
	    GET_HIGH_WORD(high,x);
113
	    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
114
	    return x;
115
	} else {			/* fraction part in low x */
116
	    i = ((__uint32_t)(0xffffffff))>>(j0-20);
117
	    if((i1&i)==0) { 		/* x is integral */
118
	        __uint32_t high;
119
		*iptr = x;
120
		GET_HIGH_WORD(high,x);
121
		INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
122
		return x;
123
	    } else {
124
	        INSERT_WORDS(*iptr,i0,i1&(~i));
125
		return x - *iptr;
126
	    }
127
	}
128
}
129
130
 
131