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