1*2f2c0062Sguenther /* $OpenBSD: s_rintl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $ */
2390c8400Smartynas /*-
3390c8400Smartynas * Copyright (c) 2008 David Schultz <das@FreeBSD.ORG>
4390c8400Smartynas * All rights reserved.
5390c8400Smartynas *
6390c8400Smartynas * Redistribution and use in source and binary forms, with or without
7390c8400Smartynas * modification, are permitted provided that the following conditions
8390c8400Smartynas * are met:
9390c8400Smartynas * 1. Redistributions of source code must retain the above copyright
10390c8400Smartynas * notice, this list of conditions and the following disclaimer.
11390c8400Smartynas * 2. Redistributions in binary form must reproduce the above copyright
12390c8400Smartynas * notice, this list of conditions and the following disclaimer in the
13390c8400Smartynas * documentation and/or other materials provided with the distribution.
14390c8400Smartynas *
15390c8400Smartynas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16390c8400Smartynas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17390c8400Smartynas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18390c8400Smartynas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19390c8400Smartynas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20390c8400Smartynas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21390c8400Smartynas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22390c8400Smartynas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23390c8400Smartynas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24390c8400Smartynas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25390c8400Smartynas * SUCH DAMAGE.
26390c8400Smartynas */
27390c8400Smartynas
28390c8400Smartynas #include <sys/types.h>
29390c8400Smartynas #include <machine/ieee.h>
30390c8400Smartynas #include <float.h>
31390c8400Smartynas #include <math.h>
32390c8400Smartynas
33390c8400Smartynas #if LDBL_MAX_EXP != 0x4000
34390c8400Smartynas /* We also require the usual bias, min exp and expsign packing. */
35390c8400Smartynas #error "Unsupported long double format"
36390c8400Smartynas #endif
37390c8400Smartynas
38390c8400Smartynas #define BIAS (LDBL_MAX_EXP - 1)
39390c8400Smartynas
40390c8400Smartynas static const float
41390c8400Smartynas shift[2] = {
42390c8400Smartynas #if LDBL_MANT_DIG == 64
43390c8400Smartynas 0x1.0p63, -0x1.0p63
44390c8400Smartynas #elif LDBL_MANT_DIG == 113
45390c8400Smartynas 0x1.0p112, -0x1.0p112
46390c8400Smartynas #else
47390c8400Smartynas #error "Unsupported long double format"
48390c8400Smartynas #endif
49390c8400Smartynas };
50390c8400Smartynas static const float zero[2] = { 0.0, -0.0 };
51390c8400Smartynas
52390c8400Smartynas long double
rintl(long double x)53390c8400Smartynas rintl(long double x)
54390c8400Smartynas {
55390c8400Smartynas union {
56390c8400Smartynas long double e;
57390c8400Smartynas struct ieee_ext bits;
58390c8400Smartynas } u;
59390c8400Smartynas uint32_t expsign;
60390c8400Smartynas int ex, sign;
61390c8400Smartynas
62390c8400Smartynas u.e = x;
63390c8400Smartynas expsign = (u.bits.ext_sign << 15) | u.bits.ext_exp;
64390c8400Smartynas ex = expsign & 0x7fff;
65390c8400Smartynas
66390c8400Smartynas if (ex >= BIAS + LDBL_MANT_DIG - 1) {
67390c8400Smartynas if (ex == BIAS + LDBL_MAX_EXP)
68390c8400Smartynas return (x + x); /* Inf, NaN, or unsupported format */
69390c8400Smartynas return (x); /* finite and already an integer */
70390c8400Smartynas }
71390c8400Smartynas sign = expsign >> 15;
72390c8400Smartynas
73390c8400Smartynas /*
74390c8400Smartynas * The following code assumes that intermediate results are
75390c8400Smartynas * evaluated in long double precision. If they are evaluated in
76390c8400Smartynas * greater precision, double rounding may occur, and if they are
77390c8400Smartynas * evaluated in less precision (as on i386), results will be
78390c8400Smartynas * wildly incorrect.
79390c8400Smartynas */
80390c8400Smartynas x += shift[sign];
81390c8400Smartynas x -= shift[sign];
82390c8400Smartynas
83390c8400Smartynas /*
84390c8400Smartynas * If the result is +-0, then it must have the same sign as x, but
85390c8400Smartynas * the above calculation doesn't always give this. Fix up the sign.
86390c8400Smartynas */
87390c8400Smartynas if (ex < BIAS && x == 0.0L)
88390c8400Smartynas return (zero[sign]);
89390c8400Smartynas
90390c8400Smartynas return (x);
91390c8400Smartynas }
92*2f2c0062Sguenther DEF_NONSTD(rintl);
93