1/* Round to nearest integer value, rounding halfway cases to even.
2 ldbl-96 version.
3 Copyright (C) 2016-2017 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <math.h>
21#include <math_private.h>
22#include <stdint.h>
23
24#define BIAS 0x3fff
25#define MANT_DIG 64
26#define MAX_EXP (2 * BIAS + 1)
27
28long double
29roundevenl (long double x)
30{
31 uint16_t se;
32 uint32_t hx, lx;
33 GET_LDOUBLE_WORDS (se, hx, lx, x);
34 int exponent = se & 0x7fff;
35 if (exponent >= BIAS + MANT_DIG - 1)
36 {
37 /* Integer, infinity or NaN. */
38 if (exponent == MAX_EXP)
39 /* Infinity or NaN; quiet signaling NaNs. */
40 return x + x;
41 else
42 return x;
43 }
44 else if (exponent >= BIAS + MANT_DIG - 32)
45 {
46 /* Not necessarily an integer; integer bit is in low word.
47 Locate the bits with exponents 0 and -1. */
48 int int_pos = (BIAS + MANT_DIG - 1) - exponent;
49 int half_pos = int_pos - 1;
50 uint32_t half_bit = 1U << half_pos;
51 uint32_t int_bit = 1U << int_pos;
52 if ((lx & (int_bit | (half_bit - 1))) != 0)
53 {
54 /* No need to test whether HALF_BIT is set. */
55 lx += half_bit;
56 if (lx < half_bit)
57 {
58 hx++;
59 if (hx == 0)
60 {
61 hx = 0x80000000;
62 se++;
63 }
64 }
65 }
66 lx &= ~(int_bit - 1);
67 }
68 else if (exponent == BIAS + MANT_DIG - 33)
69 {
70 /* Not necessarily an integer; integer bit is bottom of high
71 word, half bit is top of low word. */
72 if (((hx & 1) | (lx & 0x7fffffff)) != 0)
73 {
74 lx += 0x80000000;
75 if (lx < 0x80000000)
76 {
77 hx++;
78 if (hx == 0)
79 {
80 hx = 0x80000000;
81 se++;
82 }
83 }
84 }
85 lx = 0;
86 }
87 else if (exponent >= BIAS)
88 {
89 /* At least 1; not necessarily an integer, integer bit and half
90 bit are in the high word. Locate the bits with exponents 0
91 and -1. */
92 int int_pos = (BIAS + MANT_DIG - 33) - exponent;
93 int half_pos = int_pos - 1;
94 uint32_t half_bit = 1U << half_pos;
95 uint32_t int_bit = 1U << int_pos;
96 if (((hx & (int_bit | (half_bit - 1))) | lx) != 0)
97 {
98 hx += half_bit;
99 if (hx < half_bit)
100 {
101 hx = 0x80000000;
102 se++;
103 }
104 }
105 hx &= ~(int_bit - 1);
106 lx = 0;
107 }
108 else if (exponent == BIAS - 1 && (hx > 0x80000000 || lx != 0))
109 {
110 /* Interval (0.5, 1). */
111 se = (se & 0x8000) | 0x3fff;
112 hx = 0x80000000;
113 lx = 0;
114 }
115 else
116 {
117 /* Rounds to 0. */
118 se &= 0x8000;
119 hx = 0;
120 lx = 0;
121 }
122 SET_LDOUBLE_WORDS (x, se, hx, lx);
123 return x;
124}
125