s_rintl.c revision 175309
1175309Sdas/*- 2175309Sdas * Copyright (c) 2008 David Schultz <das@FreeBSD.ORG> 3175309Sdas * All rights reserved. 4175309Sdas * 5175309Sdas * Redistribution and use in source and binary forms, with or without 6175309Sdas * modification, are permitted provided that the following conditions 7175309Sdas * are met: 8175309Sdas * 1. Redistributions of source code must retain the above copyright 9175309Sdas * notice, this list of conditions and the following disclaimer. 10175309Sdas * 2. Redistributions in binary form must reproduce the above copyright 11175309Sdas * notice, this list of conditions and the following disclaimer in the 12175309Sdas * documentation and/or other materials provided with the distribution. 13175309Sdas * 14175309Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15175309Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16175309Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17175309Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18175309Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19175309Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20175309Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21175309Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22175309Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23175309Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24175309Sdas * SUCH DAMAGE. 25175309Sdas */ 26175309Sdas 27175309Sdas#include <sys/cdefs.h> 28175309Sdas__FBSDID("$FreeBSD: head/lib/msun/src/s_rintl.c 175309 2008-01-14 02:12:07Z das $"); 29175309Sdas 30175309Sdas#include <float.h> 31175309Sdas#include <math.h> 32175309Sdas 33175309Sdas#include "fpmath.h" 34175309Sdas 35175309Sdasstatic const long double 36175309Sdasshift[2]={ 37175309Sdas#if LDBL_MANT_DIG == 64 38175309Sdas 0x1.0p63, -0x1.0p63 39175309Sdas#elif LDBL_MANT_DIG == 113 40175309Sdas 0x1.0p112, -0x1.0p112 41175309Sdas#else 42175309Sdas#error "Unsupported long double format" 43175309Sdas#endif 44175309Sdas}; 45175309Sdas 46175309Sdaslong double 47175309Sdasrintl(long double x) 48175309Sdas{ 49175309Sdas union IEEEl2bits u; 50175309Sdas short sign; 51175309Sdas 52175309Sdas u.e = x; 53175309Sdas 54175309Sdas if (u.bits.exp >= LDBL_MANT_DIG + LDBL_MAX_EXP - 2) { 55175309Sdas /* 56175309Sdas * The biased exponent is greater than the number of digits 57175309Sdas * in the mantissa, so x is inf, NaN, or an integer. 58175309Sdas */ 59175309Sdas if (u.bits.exp == 2 * LDBL_MAX_EXP - 1) 60175309Sdas return (x + x); /* inf or NaN */ 61175309Sdas else 62175309Sdas return (x); 63175309Sdas } 64175309Sdas 65175309Sdas /* 66175309Sdas * The following code assumes that intermediate results are 67175309Sdas * evaluated in long double precision. If they are evaluated in 68175309Sdas * greater precision, double rounding will occur, and if they are 69175309Sdas * evaluated in less precision (as on i386), results will be 70175309Sdas * wildly incorrect. 71175309Sdas */ 72175309Sdas sign = u.bits.sign; 73175309Sdas u.e = shift[sign] + x; 74175309Sdas u.e -= shift[sign]; 75175309Sdas u.bits.sign = sign; 76175309Sdas return (u.e); 77175309Sdas} 78