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