re PR inline-asm/61692 (ICE in extract_insn in recog.c for asm with many parameters)
[official-gcc.git] / gcc / sreal.h
blob3938c6ef12a98ce9ad4e7bf8d54c666ccb4b4d14
1 /* Definitions for simple data type for real numbers.
2 Copyright (C) 2002-2014 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 ((uint64_t) 1 << (SREAL_PART_BITS - 1))
29 #define SREAL_MAX_SIG (((uint64_t) 1 << SREAL_PART_BITS) - 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), m_negative (0) {}
41 /* Construct a sreal. */
42 sreal (int64_t sig, int exp = 0) : m_exp (exp)
44 m_negative = sig < 0;
46 if (sig < 0)
47 sig = -sig;
49 m_sig = (uint64_t) sig;
51 normalize ();
54 void dump (FILE *) const;
55 int64_t to_int () const;
56 sreal operator+ (const sreal &other) const;
57 sreal operator- (const sreal &other) const;
58 sreal operator* (const sreal &other) const;
59 sreal operator/ (const sreal &other) const;
61 bool operator< (const sreal &other) const
63 /* We negate result in case of negative numbers and
64 it would return true for equal negative numbers. */
65 if (*this == other)
66 return false;
68 if (m_negative != other.m_negative)
69 return m_negative > other.m_negative;
71 bool r = m_exp < other.m_exp
72 || (m_exp == other.m_exp && m_sig < other.m_sig);
74 return m_negative ? !r : r;
77 bool operator== (const sreal &other) const
79 return m_exp == other.m_exp && m_sig == other.m_sig
80 && m_negative == other.m_negative;
83 sreal operator- () const
85 if (m_sig == 0)
86 return *this;
88 sreal tmp = *this;
89 tmp.m_negative = !tmp.m_negative;
91 return tmp;
94 sreal shift (int s) const
96 gcc_checking_assert (s <= SREAL_BITS);
97 gcc_checking_assert (s >= -SREAL_BITS);
99 /* Exponent should never be so large because shift_right is used only by
100 sreal_add and sreal_sub ant thus the number cannot be shifted out from
101 exponent range. */
102 gcc_checking_assert (m_exp + s <= SREAL_MAX_EXP);
103 gcc_checking_assert (m_exp + s >= -SREAL_MAX_EXP);
105 sreal tmp = *this;
106 tmp.m_exp += s;
108 return tmp;
111 /* Global minimum sreal can hold. */
112 inline static sreal min ()
114 static sreal min = sreal (-SREAL_MAX_SIG, SREAL_MAX_EXP);
115 return min;
118 /* Global minimum sreal can hold. */
119 inline static sreal max ()
121 static sreal max = sreal (SREAL_MAX_SIG, SREAL_MAX_EXP);
122 return max;
125 private:
126 void normalize ();
127 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 uint64_t m_sig; /* Significant. */
133 signed int m_exp; /* Exponent. */
134 bool m_negative; /* Negative sign. */
137 extern void debug (sreal &ref);
138 extern void debug (sreal *ptr);
140 inline sreal &operator+= (sreal &a, const sreal &b)
142 return a = a + b;
145 inline sreal &operator-= (sreal &a, const sreal &b)
147 return a = a - b;
150 inline sreal &operator/= (sreal &a, const sreal &b)
152 return a = a / b;
155 inline sreal &operator*= (sreal &a, const sreal &b)
157 return a = a * b;
160 inline bool operator!= (const sreal &a, const sreal &b)
162 return !(a == b);
165 inline bool operator> (const sreal &a, const sreal &b)
167 return !(a == b || a < b);
170 inline bool operator<= (const sreal &a, const sreal &b)
172 return a < b || a == b;
175 inline bool operator>= (const sreal &a, const sreal &b)
177 return a == b || a > b;
180 inline sreal operator<< (const sreal &a, int exp)
182 return a.shift (exp);
185 inline sreal operator>> (const sreal &a, int exp)
187 return a.shift (-exp);
190 #endif