1d3f9671aSDavid Schultz /*- 2d3f9671aSDavid Schultz * Copyright (c) 2008 David Schultz <das@FreeBSD.ORG> 3d3f9671aSDavid Schultz * All rights reserved. 4d3f9671aSDavid Schultz * 5d3f9671aSDavid Schultz * Redistribution and use in source and binary forms, with or without 6d3f9671aSDavid Schultz * modification, are permitted provided that the following conditions 7d3f9671aSDavid Schultz * are met: 8d3f9671aSDavid Schultz * 1. Redistributions of source code must retain the above copyright 9d3f9671aSDavid Schultz * notice, this list of conditions and the following disclaimer. 10d3f9671aSDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright 11d3f9671aSDavid Schultz * notice, this list of conditions and the following disclaimer in the 12d3f9671aSDavid Schultz * documentation and/or other materials provided with the distribution. 13d3f9671aSDavid Schultz * 14d3f9671aSDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15d3f9671aSDavid Schultz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16d3f9671aSDavid Schultz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17d3f9671aSDavid Schultz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18d3f9671aSDavid Schultz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19d3f9671aSDavid Schultz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20d3f9671aSDavid Schultz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21d3f9671aSDavid Schultz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22d3f9671aSDavid Schultz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23d3f9671aSDavid Schultz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24d3f9671aSDavid Schultz * SUCH DAMAGE. 25d3f9671aSDavid Schultz */ 26d3f9671aSDavid Schultz 27d3f9671aSDavid Schultz #include <sys/cdefs.h> 28d3f9671aSDavid Schultz __FBSDID("$FreeBSD$"); 29d3f9671aSDavid Schultz 30d3f9671aSDavid Schultz #include <float.h> 31d3f9671aSDavid Schultz #include <math.h> 32d3f9671aSDavid Schultz 33d3f9671aSDavid Schultz #include "fpmath.h" 34d3f9671aSDavid Schultz 35d3f9671aSDavid Schultz static const long double 36d3f9671aSDavid Schultz shift[2]={ 37d3f9671aSDavid Schultz #if LDBL_MANT_DIG == 64 38d3f9671aSDavid Schultz 0x1.0p63, -0x1.0p63 39d3f9671aSDavid Schultz #elif LDBL_MANT_DIG == 113 40d3f9671aSDavid Schultz 0x1.0p112, -0x1.0p112 41d3f9671aSDavid Schultz #else 42d3f9671aSDavid Schultz #error "Unsupported long double format" 43d3f9671aSDavid Schultz #endif 44d3f9671aSDavid Schultz }; 45d3f9671aSDavid Schultz 46d3f9671aSDavid Schultz long double 47d3f9671aSDavid Schultz rintl(long double x) 48d3f9671aSDavid Schultz { 49d3f9671aSDavid Schultz union IEEEl2bits u; 50d3f9671aSDavid Schultz short sign; 51d3f9671aSDavid Schultz 52d3f9671aSDavid Schultz u.e = x; 53d3f9671aSDavid Schultz 54d3f9671aSDavid Schultz if (u.bits.exp >= LDBL_MANT_DIG + LDBL_MAX_EXP - 2) { 55d3f9671aSDavid Schultz /* 56d3f9671aSDavid Schultz * The biased exponent is greater than the number of digits 57d3f9671aSDavid Schultz * in the mantissa, so x is inf, NaN, or an integer. 58d3f9671aSDavid Schultz */ 59d3f9671aSDavid Schultz if (u.bits.exp == 2 * LDBL_MAX_EXP - 1) 60d3f9671aSDavid Schultz return (x + x); /* inf or NaN */ 61d3f9671aSDavid Schultz else 62d3f9671aSDavid Schultz return (x); 63d3f9671aSDavid Schultz } 64d3f9671aSDavid Schultz 65d3f9671aSDavid Schultz /* 66d3f9671aSDavid Schultz * The following code assumes that intermediate results are 67d3f9671aSDavid Schultz * evaluated in long double precision. If they are evaluated in 68d3f9671aSDavid Schultz * greater precision, double rounding will occur, and if they are 69d3f9671aSDavid Schultz * evaluated in less precision (as on i386), results will be 70d3f9671aSDavid Schultz * wildly incorrect. 71d3f9671aSDavid Schultz */ 72d3f9671aSDavid Schultz sign = u.bits.sign; 73d3f9671aSDavid Schultz u.e = shift[sign] + x; 74d3f9671aSDavid Schultz u.e -= shift[sign]; 75d3f9671aSDavid Schultz u.bits.sign = sign; 76d3f9671aSDavid Schultz return (u.e); 77d3f9671aSDavid Schultz } 78