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