sreal.h revision 1.6
1/* Definitions for simple data type for real numbers. 2 Copyright (C) 2002-2016 Free Software Foundation, Inc. 3 4This file is part of GCC. 5 6GCC is free software; you can redistribute it and/or modify it under 7the terms of the GNU General Public License as published by the Free 8Software Foundation; either version 3, or (at your option) any later 9version. 10 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12WARRANTY; without even the implied warranty of MERCHANTABILITY or 13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14for more details. 15 16You should have received a copy of the GNU General Public License 17along with GCC; see the file COPYING3. If not see 18<http://www.gnu.org/licenses/>. */ 19 20#ifndef GCC_SREAL_H 21#define GCC_SREAL_H 22 23/* SREAL_PART_BITS has to be an even number. */ 24#define SREAL_PART_BITS 32 25 26#define UINT64_BITS 64 27 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) 31 32#define SREAL_BITS SREAL_PART_BITS 33 34/* Structure for holding a simple real number. */ 35class sreal 36{ 37public: 38 /* Construct an uninitialized sreal. */ 39 sreal () : m_sig (-1), m_exp (-1) {} 40 41 /* Construct a sreal. */ 42 sreal (int64_t sig, int exp = 0) : m_sig (sig), m_exp (exp) 43 { 44 normalize (); 45 } 46 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; 54 55 bool operator< (const sreal &other) const 56 { 57 if (m_exp == other.m_exp) 58 return m_sig < other.m_sig; 59 else 60 { 61 bool negative = m_sig < 0; 62 bool other_negative = other.m_sig < 0; 63 64 if (negative != other_negative) 65 return negative > other_negative; 66 67 bool r = m_exp < other.m_exp; 68 return negative ? !r : r; 69 } 70 } 71 72 bool operator== (const sreal &other) const 73 { 74 return m_exp == other.m_exp && m_sig == other.m_sig; 75 } 76 77 sreal operator- () const 78 { 79 sreal tmp = *this; 80 tmp.m_sig *= -1; 81 82 return tmp; 83 } 84 85 sreal shift (int s) const 86 { 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); 92 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); 97 98 sreal tmp = *this; 99 tmp.m_exp += s; 100 101 return tmp; 102 } 103 104 /* Global minimum sreal can hold. */ 105 inline static sreal min () 106 { 107 static sreal min = sreal (-SREAL_MAX_SIG, SREAL_MAX_EXP); 108 return min; 109 } 110 111 /* Global minimum sreal can hold. */ 112 inline static sreal max () 113 { 114 static sreal max = sreal (SREAL_MAX_SIG, SREAL_MAX_EXP); 115 return max; 116 } 117 118private: 119 inline void normalize (); 120 inline void normalize_up (); 121 inline void normalize_down (); 122 void shift_right (int amount); 123 static sreal signedless_plus (const sreal &a, const sreal &b, bool negative); 124 static sreal signedless_minus (const sreal &a, const sreal &b, bool negative); 125 126 int64_t m_sig; /* Significant. */ 127 signed int m_exp; /* Exponent. */ 128}; 129 130extern void debug (const sreal &ref); 131extern void debug (const sreal *ptr); 132 133inline sreal &operator+= (sreal &a, const sreal &b) 134{ 135 return a = a + b; 136} 137 138inline sreal &operator-= (sreal &a, const sreal &b) 139{ 140 return a = a - b; 141} 142 143inline sreal &operator/= (sreal &a, const sreal &b) 144{ 145 return a = a / b; 146} 147 148inline sreal &operator*= (sreal &a, const sreal &b) 149{ 150 return a = a * b; 151} 152 153inline bool operator!= (const sreal &a, const sreal &b) 154{ 155 return !(a == b); 156} 157 158inline bool operator> (const sreal &a, const sreal &b) 159{ 160 return !(a == b || a < b); 161} 162 163inline bool operator<= (const sreal &a, const sreal &b) 164{ 165 return a < b || a == b; 166} 167 168inline bool operator>= (const sreal &a, const sreal &b) 169{ 170 return a == b || a > b; 171} 172 173inline sreal operator<< (const sreal &a, int exp) 174{ 175 return a.shift (exp); 176} 177 178inline sreal operator>> (const sreal &a, int exp) 179{ 180 return a.shift (-exp); 181} 182 183/* Make significant to be >= SREAL_MIN_SIG. 184 185 Make this separate method so inliner can handle hot path better. */ 186 187inline void 188sreal::normalize_up () 189{ 190 int64_t s = m_sig < 0 ? -1 : 1; 191 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig); 192 int shift = SREAL_PART_BITS - 2 - floor_log2 (sig); 193 194 gcc_checking_assert (shift > 0); 195 sig <<= shift; 196 m_exp -= shift; 197 gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG); 198 199 /* Check underflow. */ 200 if (m_exp < -SREAL_MAX_EXP) 201 { 202 m_exp = -SREAL_MAX_EXP; 203 sig = 0; 204 } 205 if (s == -1) 206 m_sig = -sig; 207 else 208 m_sig = sig; 209} 210 211/* Make significant to be <= SREAL_MAX_SIG. 212 213 Make this separate method so inliner can handle hot path better. */ 214 215inline void 216sreal::normalize_down () 217{ 218 int64_t s = m_sig < 0 ? -1 : 1; 219 int last_bit; 220 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig); 221 int shift = floor_log2 (sig) - SREAL_PART_BITS + 2; 222 223 gcc_checking_assert (shift > 0); 224 last_bit = (sig >> (shift-1)) & 1; 225 sig >>= shift; 226 m_exp += shift; 227 gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG); 228 229 /* Round the number. */ 230 sig += last_bit; 231 if (sig > SREAL_MAX_SIG) 232 { 233 sig >>= 1; 234 m_exp++; 235 } 236 237 /* Check overflow. */ 238 if (m_exp > SREAL_MAX_EXP) 239 { 240 m_exp = SREAL_MAX_EXP; 241 sig = SREAL_MAX_SIG; 242 } 243 if (s == -1) 244 m_sig = -sig; 245 else 246 m_sig = sig; 247} 248 249/* Normalize *this; the hot path. */ 250 251inline void 252sreal::normalize () 253{ 254 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig); 255 256 if (sig == 0) 257 m_exp = -SREAL_MAX_EXP; 258 else if (sig > SREAL_MAX_SIG) 259 normalize_down (); 260 else if (sig < SREAL_MIN_SIG) 261 normalize_up (); 262} 263 264#endif 265