s_rintl.c revision 176458
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 176458 2008-02-22 10:04:53Z bde $"); 29175309Sdas 30175309Sdas#include <float.h> 31175309Sdas#include <math.h> 32175309Sdas 33175309Sdas#include "fpmath.h" 34175309Sdas 35175371Sdas#define BIAS (LDBL_MAX_EXP - 1) 36175371Sdas 37175371Sdasstatic const float 38175371Sdasshift[2] = { 39175309Sdas#if LDBL_MANT_DIG == 64 40175309Sdas 0x1.0p63, -0x1.0p63 41175309Sdas#elif LDBL_MANT_DIG == 113 42175309Sdas 0x1.0p112, -0x1.0p112 43175309Sdas#else 44175309Sdas#error "Unsupported long double format" 45175309Sdas#endif 46175309Sdas}; 47176458Sbdestatic const float zero[2] = { 0.0, -0.0 }; 48175309Sdas 49175309Sdaslong double 50175309Sdasrintl(long double x) 51175309Sdas{ 52175309Sdas union IEEEl2bits u; 53175309Sdas short sign; 54175309Sdas 55175309Sdas u.e = x; 56175309Sdas 57175371Sdas if (u.bits.exp >= BIAS + LDBL_MANT_DIG - 1) { 58176456Sbde if (u.bits.exp == BIAS + LDBL_MAX_EXP) 59176456Sbde return (x + x); /* Inf, NaN, or unsupported format */ 60176456Sbde return (x); /* finite and already an integer */ 61175309Sdas } 62175371Sdas sign = u.bits.sign; 63175309Sdas 64175309Sdas /* 65175309Sdas * The following code assumes that intermediate results are 66175309Sdas * evaluated in long double precision. If they are evaluated in 67175371Sdas * greater precision, double rounding may occur, and if they are 68175309Sdas * evaluated in less precision (as on i386), results will be 69175309Sdas * wildly incorrect. 70175309Sdas */ 71175371Sdas x += shift[sign]; 72175371Sdas x -= shift[sign]; 73175371Sdas 74175371Sdas /* 75175371Sdas * If the result is +-0, then it must have the same sign as x, but 76175371Sdas * the above calculation doesn't always give this. Fix up the sign. 77175371Sdas */ 78176458Sbde if (u.bits.exp < BIAS && x == 0.0L) 79176458Sbde return (zero[sign]); 80176458Sbde 81175371Sdas return (x); 82175309Sdas} 83