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 ((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. */
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
)
49 m_sig
= (uint64_t) sig
;
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. */
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
89 tmp
.m_negative
= !tmp
.m_negative
;
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
102 gcc_checking_assert (m_exp
+ s
<= SREAL_MAX_EXP
);
103 gcc_checking_assert (m_exp
+ s
>= -SREAL_MAX_EXP
);
111 /* Global minimum sreal can hold. */
112 inline static sreal
min ()
114 static sreal min
= sreal (-SREAL_MAX_SIG
, SREAL_MAX_EXP
);
118 /* Global minimum sreal can hold. */
119 inline static sreal
max ()
121 static sreal max
= sreal (SREAL_MAX_SIG
, SREAL_MAX_EXP
);
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
)
145 inline sreal
&operator-= (sreal
&a
, const sreal
&b
)
150 inline sreal
&operator/= (sreal
&a
, const sreal
&b
)
155 inline sreal
&operator*= (sreal
&a
, const sreal
&b
)
160 inline bool operator!= (const sreal
&a
, const sreal
&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
);