2016-09-25 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / sreal.h
blobce9cdbbfded8e938106c72f5d5c548356641cd99
1 /* Definitions for simple data type for real numbers.
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef GCC_SREAL_H
21 #define GCC_SREAL_H
23 /* SREAL_PART_BITS has to be an even number. */
24 #define SREAL_PART_BITS 32
26 #define UINT64_BITS 64
28 #define SREAL_MIN_SIG ((int64_t) 1 << (SREAL_PART_BITS - 2))
29 #define SREAL_MAX_SIG (((int64_t) 1 << (SREAL_PART_BITS - 1)) - 1)
30 #define SREAL_MAX_EXP (INT_MAX / 4)
32 #define SREAL_BITS SREAL_PART_BITS
34 /* Structure for holding a simple real number. */
35 class sreal
37 public:
38 /* Construct an uninitialized sreal. */
39 sreal () : m_sig (-1), m_exp (-1) {}
41 /* Construct a sreal. */
42 sreal (int64_t sig, int exp = 0) : m_sig (sig), m_exp (exp)
44 normalize ();
47 void dump (FILE *) const;
48 int64_t to_int () const;
49 double to_double () const;
50 sreal operator+ (const sreal &other) const;
51 sreal operator- (const sreal &other) const;
52 sreal operator* (const sreal &other) const;
53 sreal operator/ (const sreal &other) const;
55 bool operator< (const sreal &other) const
57 if (m_exp == other.m_exp)
58 return m_sig < other.m_sig;
59 else
61 bool negative = m_sig < 0;
62 bool other_negative = other.m_sig < 0;
64 if (negative != other_negative)
65 return negative > other_negative;
67 bool r = m_exp < other.m_exp;
68 return negative ? !r : r;
72 bool operator== (const sreal &other) const
74 return m_exp == other.m_exp && m_sig == other.m_sig;
77 sreal operator- () const
79 sreal tmp = *this;
80 tmp.m_sig *= -1;
82 return tmp;
85 sreal shift (int s) const
87 /* Zero needs no shifting. */
88 if (!m_sig)
89 return *this;
90 gcc_checking_assert (s <= SREAL_MAX_EXP);
91 gcc_checking_assert (s >= -SREAL_MAX_EXP);
93 /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
94 need to do so. */
95 gcc_checking_assert (m_exp + s <= SREAL_MAX_EXP);
96 gcc_checking_assert (m_exp + s >= -SREAL_MAX_EXP);
98 sreal tmp = *this;
99 tmp.m_exp += s;
101 return tmp;
104 /* Global minimum sreal can hold. */
105 inline static sreal min ()
107 sreal min;
108 /* This never needs normalization. */
109 min.m_sig = -SREAL_MAX_SIG;
110 min.m_exp = SREAL_MAX_EXP;
111 return min;
114 /* Global minimum sreal can hold. */
115 inline static sreal max ()
117 sreal max;
118 /* This never needs normalization. */
119 max.m_sig = SREAL_MAX_SIG;
120 max.m_exp = SREAL_MAX_EXP;
121 return max;
124 private:
125 inline void normalize ();
126 inline void normalize_up ();
127 inline void normalize_down ();
128 void shift_right (int amount);
129 static sreal signedless_plus (const sreal &a, const sreal &b, bool negative);
130 static sreal signedless_minus (const sreal &a, const sreal &b, bool negative);
132 int64_t m_sig; /* Significant. */
133 signed int m_exp; /* Exponent. */
136 extern void debug (const sreal &ref);
137 extern void debug (const sreal *ptr);
139 inline sreal &operator+= (sreal &a, const sreal &b)
141 return a = a + b;
144 inline sreal &operator-= (sreal &a, const sreal &b)
146 return a = a - b;
149 inline sreal &operator/= (sreal &a, const sreal &b)
151 return a = a / b;
154 inline sreal &operator*= (sreal &a, const sreal &b)
156 return a = a * b;
159 inline bool operator!= (const sreal &a, const sreal &b)
161 return !(a == b);
164 inline bool operator> (const sreal &a, const sreal &b)
166 return !(a == b || a < b);
169 inline bool operator<= (const sreal &a, const sreal &b)
171 return a < b || a == b;
174 inline bool operator>= (const sreal &a, const sreal &b)
176 return a == b || a > b;
179 inline sreal operator<< (const sreal &a, int exp)
181 return a.shift (exp);
184 inline sreal operator>> (const sreal &a, int exp)
186 return a.shift (-exp);
189 /* Make significant to be >= SREAL_MIN_SIG.
191 Make this separate method so inliner can handle hot path better. */
193 inline void
194 sreal::normalize_up ()
196 int64_t s = m_sig < 0 ? -1 : 1;
197 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
198 int shift = SREAL_PART_BITS - 2 - floor_log2 (sig);
200 gcc_checking_assert (shift > 0);
201 sig <<= shift;
202 m_exp -= shift;
203 gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);
205 /* Check underflow. */
206 if (m_exp < -SREAL_MAX_EXP)
208 m_exp = -SREAL_MAX_EXP;
209 sig = 0;
211 if (s == -1)
212 m_sig = -sig;
213 else
214 m_sig = sig;
217 /* Make significant to be <= SREAL_MAX_SIG.
219 Make this separate method so inliner can handle hot path better. */
221 inline void
222 sreal::normalize_down ()
224 int64_t s = m_sig < 0 ? -1 : 1;
225 int last_bit;
226 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
227 int shift = floor_log2 (sig) - SREAL_PART_BITS + 2;
229 gcc_checking_assert (shift > 0);
230 last_bit = (sig >> (shift-1)) & 1;
231 sig >>= shift;
232 m_exp += shift;
233 gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);
235 /* Round the number. */
236 sig += last_bit;
237 if (sig > SREAL_MAX_SIG)
239 sig >>= 1;
240 m_exp++;
243 /* Check overflow. */
244 if (m_exp > SREAL_MAX_EXP)
246 m_exp = SREAL_MAX_EXP;
247 sig = SREAL_MAX_SIG;
249 if (s == -1)
250 m_sig = -sig;
251 else
252 m_sig = sig;
255 /* Normalize *this; the hot path. */
257 inline void
258 sreal::normalize ()
260 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
262 if (sig == 0)
263 m_exp = -SREAL_MAX_EXP;
264 else if (sig > SREAL_MAX_SIG)
265 normalize_down ();
266 else if (sig < SREAL_MIN_SIG)
267 normalize_up ();
270 #endif