1 /* origin: FreeBSD /usr/src/lib/msun/src/s_modfl.c */
3 * Copyright (c) 2007 David Schultz <das@FreeBSD.ORG>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * Derived from s_modf.c, which has the following Copyright:
28 * ====================================================
29 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
35 * ====================================================
40 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
41 long double modfl(long double x, long double *iptr)
43 return modf(x, (double *)iptr);
45 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
47 #if LDBL_MANL_SIZE > 32
48 #define MASK ((uint64_t)-1)
50 #define MASK ((uint32_t)-1)
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)
57 static const long double zero[] = { 0.0, -0.0 };
59 long double modfl(long double x, long double *iptr)
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];
71 if ((GETFRAC(ux.bits.manh, HIBITS - 1 - e)|ux.bits.manl) == 0) {
72 /* x is an integer. */
74 return zero[ux.bits.sign];
76 /* Clear all but the top e+1 bits. */
77 ux.bits.manh >>= HIBITS - 1 - e;
78 ux.bits.manh <<= HIBITS - 1 - e;
82 } else if (e >= LDBL_MANT_DIG - 1) { /* x has no fraction part. */
84 if (e == LDBL_MAX_EXP && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)) /* nan */
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) {
91 return zero[ux.bits.sign];
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;