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
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
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/>. */
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. */
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
)
47 void dump (FILE *) const;
48 int64_t to_int () const;
49 sreal
operator+ (const sreal
&other
) const;
50 sreal
operator- (const sreal
&other
) const;
51 sreal
operator* (const sreal
&other
) const;
52 sreal
operator/ (const sreal
&other
) const;
54 bool operator< (const sreal
&other
) const
56 if (m_exp
== other
.m_exp
)
57 return m_sig
< other
.m_sig
;
60 bool negative
= m_sig
< 0;
61 bool other_negative
= other
.m_sig
< 0;
63 if (negative
!= other_negative
)
64 return negative
> other_negative
;
66 bool r
= m_exp
< other
.m_exp
;
67 return negative
? !r
: r
;
71 bool operator== (const sreal
&other
) const
73 return m_exp
== other
.m_exp
&& m_sig
== other
.m_sig
;
76 sreal
operator- () const
84 sreal
shift (int s
) const
86 gcc_checking_assert (s
<= SREAL_BITS
);
87 gcc_checking_assert (s
>= -SREAL_BITS
);
89 /* Exponent should never be so large because shift_right is used only by
90 sreal_add and sreal_sub ant thus the number cannot be shifted out from
92 gcc_checking_assert (m_exp
+ s
<= SREAL_MAX_EXP
);
93 gcc_checking_assert (m_exp
+ s
>= -SREAL_MAX_EXP
);
101 /* Global minimum sreal can hold. */
102 inline static sreal
min ()
104 static sreal min
= sreal (-SREAL_MAX_SIG
, SREAL_MAX_EXP
);
108 /* Global minimum sreal can hold. */
109 inline static sreal
max ()
111 static sreal max
= sreal (SREAL_MAX_SIG
, SREAL_MAX_EXP
);
117 void shift_right (int amount
);
118 static sreal
signedless_plus (const sreal
&a
, const sreal
&b
, bool negative
);
119 static sreal
signedless_minus (const sreal
&a
, const sreal
&b
, bool negative
);
121 int64_t m_sig
; /* Significant. */
122 signed int m_exp
; /* Exponent. */
125 extern void debug (const sreal
&ref
);
126 extern void debug (const sreal
*ptr
);
128 inline sreal
&operator+= (sreal
&a
, const sreal
&b
)
133 inline sreal
&operator-= (sreal
&a
, const sreal
&b
)
138 inline sreal
&operator/= (sreal
&a
, const sreal
&b
)
143 inline sreal
&operator*= (sreal
&a
, const sreal
&b
)
148 inline bool operator!= (const sreal
&a
, const sreal
&b
)
153 inline bool operator> (const sreal
&a
, const sreal
&b
)
155 return !(a
== b
|| a
< b
);
158 inline bool operator<= (const sreal
&a
, const sreal
&b
)
160 return a
< b
|| a
== b
;
163 inline bool operator>= (const sreal
&a
, const sreal
&b
)
165 return a
== b
|| a
> b
;
168 inline sreal
operator<< (const sreal
&a
, int exp
)
170 return a
.shift (exp
);
173 inline sreal
operator>> (const sreal
&a
, int exp
)
175 return a
.shift (-exp
);