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