source: trunk/libs/newlib/src/newlib/libm/common/s_modf.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.9 KB
Line 
1
2/* @(#)s_modf.c 5.1 93/09/24 */
3/*
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 *
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 */
13
14/*
15FUNCTION
16       <<modf>>, <<modff>>---split fractional and integer parts
17
18INDEX
19        modf
20INDEX
21        modff
22
23SYNOPSIS
24        #include <math.h>
25        double modf(double <[val]>, double *<[ipart]>);
26        float modff(float <[val]>, float *<[ipart]>);
27
28DESCRIPTION
29        <<modf>> splits the double <[val]> apart into an integer part
30        and a fractional part, returning the fractional part and
31        storing the integer part in <<*<[ipart]>>>.  No rounding
32        whatsoever is done; the sum of the integer and fractional
33        parts is guaranteed to be exactly  equal to <[val]>.   That
34        is, if <[realpart]> = modf(<[val]>, &<[intpart]>); then
35        `<<<[realpart]>+<[intpart]>>>' is the same as <[val]>.
36        <<modff>> is identical, save that it takes and returns
37        <<float>> rather than <<double>> values.
38
39RETURNS
40        The fractional part is returned.  Each result has the same
41        sign as the supplied argument <[val]>.
42
43PORTABILITY
44        <<modf>> is ANSI C. <<modff>> is an extension.
45
46QUICKREF
47        modf  ansi pure
48        modff - pure
49
50*/
51
52/*
53 * modf(double x, double *iptr)
54 * return fraction part of x, and return x's integral part in *iptr.
55 * Method:
56 *      Bit twiddling.
57 *
58 * Exception:
59 *      No exception.
60 */
61
62#include "fdlibm.h"
63
64#ifndef _DOUBLE_IS_32BITS
65
66#ifdef __STDC__
67static const double one = 1.0;
68#else
69static double one = 1.0;
70#endif
71
72#ifdef __STDC__
73        double modf(double x, double *iptr)
74#else
75        double modf(x, iptr)
76        double x,*iptr;
77#endif
78{
79        __int32_t i0,i1,j0;
80        __uint32_t i;
81        EXTRACT_WORDS(i0,i1,x);
82        j0 = ((i0>>20)&0x7ff)-0x3ff;    /* exponent of x */
83        if(j0<20) {                     /* integer part in high x */
84            if(j0<0) {                  /* |x|<1 */
85                INSERT_WORDS(*iptr,i0&0x80000000,0);    /* *iptr = +-0 */
86                return x;
87            } else {
88                i = (0x000fffff)>>j0;
89                if(((i0&i)|i1)==0) {            /* x is integral */
90                    __uint32_t high;
91                    *iptr = x;
92                    GET_HIGH_WORD(high,x);
93                    INSERT_WORDS(x,high&0x80000000,0);  /* return +-0 */
94                    return x;
95                } else {
96                    INSERT_WORDS(*iptr,i0&(~i),0);
97                    return x - *iptr;
98                }
99            }
100        } else if (j0>51) {             /* no fraction part */
101            __uint32_t high;
102            *iptr = x*one;
103            GET_HIGH_WORD(high,x);
104            INSERT_WORDS(x,high&0x80000000,0);  /* return +-0 */
105            return x;
106        } else {                        /* fraction part in low x */
107            i = ((__uint32_t)(0xffffffff))>>(j0-20);
108            if((i1&i)==0) {             /* x is integral */
109                __uint32_t high;
110                *iptr = x;
111                GET_HIGH_WORD(high,x);
112                INSERT_WORDS(x,high&0x80000000,0);      /* return +-0 */
113                return x;
114            } else {
115                INSERT_WORDS(*iptr,i0,i1&(~i));
116                return x - *iptr;
117            }
118        }
119}
120
121#endif /* _DOUBLE_IS_32BITS */
Note: See TracBrowser for help on using the repository browser.