new modff.c code, fix nan handling in modfl
[musl] / src / math / modfl.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_modfl.c */
2 /*-
3  * Copyright (c) 2007 David Schultz <das@FreeBSD.ORG>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Derived from s_modf.c, which has the following Copyright:
28  * ====================================================
29  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
30  *
31  * Developed at SunPro, a Sun Microsystems, Inc. business.
32  * Permission to use, copy, modify, and distribute this
33  * software is freely granted, provided that this notice
34  * is preserved.
35  * ====================================================
36  */
37
38 #include "libm.h"
39
40 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
41 long double modfl(long double x, long double *iptr)
42 {
43         return modf(x, iptr);
44 }
45 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
46
47 #if LDBL_MANL_SIZE > 32
48 #define MASK    ((uint64_t)-1)
49 #else
50 #define MASK    ((uint32_t)-1)
51 #endif
52 /* Return the last n bits of a word, representing the fractional part. */
53 #define GETFRAC(bits, n)        ((bits) & ~(MASK << (n)))
54 /* The number of fraction bits in manh, not counting the integer bit */
55 #define HIBITS  (LDBL_MANT_DIG - LDBL_MANL_SIZE)
56
57 static const long double zero[] = { 0.0, -0.0 };
58
59 long double modfl(long double x, long double *iptr)
60 {
61         union IEEEl2bits ux;
62         int e;
63
64         ux.e = x;
65         e = ux.bits.exp - LDBL_MAX_EXP + 1;
66         if (e < HIBITS) {                       /* Integer part is in manh. */
67                 if (e < 0) {                    /* |x|<1 */
68                         *iptr = zero[ux.bits.sign];
69                         return x;
70                 }
71                 if ((GETFRAC(ux.bits.manh, HIBITS - 1 - e)|ux.bits.manl) == 0) {
72                         /* x is an integer. */
73                         *iptr = x;
74                         return zero[ux.bits.sign];
75                 }
76                 /* Clear all but the top e+1 bits. */
77                 ux.bits.manh >>= HIBITS - 1 - e;
78                 ux.bits.manh <<= HIBITS - 1 - e;
79                 ux.bits.manl = 0;
80                 *iptr = ux.e;
81                 return x - ux.e;
82         } else if (e >= LDBL_MANT_DIG - 1) {    /* x has no fraction part. */
83                 *iptr = x;
84                 if (e == LDBL_MAX_EXP && (ux.bits.manh|ux.bits.manl)) /* nan */
85                         return x;
86                 return zero[ux.bits.sign];
87         } else {                                /* Fraction part is in manl. */
88                 if (GETFRAC(ux.bits.manl, LDBL_MANT_DIG - 1 - e) == 0) {
89                         /* x is integral. */
90                         *iptr = x;
91                         return zero[ux.bits.sign];
92                 }
93                 /* Clear all but the top e+1 bits. */
94                 ux.bits.manl >>= LDBL_MANT_DIG - 1 - e;
95                 ux.bits.manl <<= LDBL_MANT_DIG - 1 - e;
96                 *iptr = ux.e;
97                 return x - ux.e;
98         }
99 }
100 #endif