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$");
29175309Sdas
30175309Sdas#include <float.h>
31175309Sdas#include <math.h>
32175309Sdas
33175309Sdas#include "fpmath.h"
34175309Sdas
35176461Sbde#if LDBL_MAX_EXP != 0x4000
36176461Sbde/* We also require the usual bias, min exp and expsign packing. */
37176461Sbde#error "Unsupported long double format"
38176461Sbde#endif
39176461Sbde
40175371Sdas#define	BIAS	(LDBL_MAX_EXP - 1)
41175371Sdas
42175371Sdasstatic const float
43175371Sdasshift[2] = {
44175309Sdas#if LDBL_MANT_DIG == 64
45175309Sdas	0x1.0p63, -0x1.0p63
46175309Sdas#elif LDBL_MANT_DIG == 113
47175309Sdas	0x1.0p112, -0x1.0p112
48175309Sdas#else
49175309Sdas#error "Unsupported long double format"
50175309Sdas#endif
51175309Sdas};
52176458Sbdestatic const float zero[2] = { 0.0, -0.0 };
53175309Sdas
54175309Sdaslong double
55175309Sdasrintl(long double x)
56175309Sdas{
57175309Sdas	union IEEEl2bits u;
58176461Sbde	uint32_t expsign;
59176461Sbde	int ex, sign;
60175309Sdas
61175309Sdas	u.e = x;
62176461Sbde	expsign = u.xbits.expsign;
63176461Sbde	ex = expsign & 0x7fff;
64175309Sdas
65176461Sbde	if (ex >= BIAS + LDBL_MANT_DIG - 1) {
66176461Sbde		if (ex == BIAS + LDBL_MAX_EXP)
67176456Sbde			return (x + x);	/* Inf, NaN, or unsupported format */
68176456Sbde		return (x);		/* finite and already an integer */
69175309Sdas	}
70176461Sbde	sign = expsign >> 15;
71175309Sdas
72175309Sdas	/*
73175309Sdas	 * The following code assumes that intermediate results are
74175309Sdas	 * evaluated in long double precision. If they are evaluated in
75175371Sdas	 * greater precision, double rounding may occur, and if they are
76175309Sdas	 * evaluated in less precision (as on i386), results will be
77175309Sdas	 * wildly incorrect.
78175309Sdas	 */
79175371Sdas	x += shift[sign];
80175371Sdas	x -= shift[sign];
81175371Sdas
82175371Sdas	/*
83175371Sdas	 * If the result is +-0, then it must have the same sign as x, but
84175371Sdas	 * the above calculation doesn't always give this.  Fix up the sign.
85175371Sdas	 */
86176461Sbde	if (ex < BIAS && x == 0.0L)
87176458Sbde		return (zero[sign]);
88176458Sbde
89175371Sdas	return (x);
90175309Sdas}
91