1 /* Definitions for simple data type for real numbers.
2 Copyright (C) 2002-2017 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 #define SREAL_SIGN(v) (v < 0 ? -1: 1)
35 #define SREAL_ABS(v) (v < 0 ? -v: v)
38 struct lto_input_block
;
40 /* Structure for holding a simple real number. */
44 /* Construct an uninitialized sreal. */
45 sreal () : m_sig (-1), m_exp (-1) {}
47 /* Construct a sreal. */
48 sreal (int64_t sig
, int exp
= 0) : m_sig (sig
), m_exp (exp
)
53 void dump (FILE *) const;
54 int64_t to_int () const;
55 double to_double () const;
56 void stream_out (struct output_block
*);
57 static sreal
stream_in (struct lto_input_block
*);
58 sreal
operator+ (const sreal
&other
) const;
59 sreal
operator- (const sreal
&other
) const;
60 sreal
operator* (const sreal
&other
) const;
61 sreal
operator/ (const sreal
&other
) const;
63 bool operator< (const sreal
&other
) const
65 if (m_exp
== other
.m_exp
)
66 return m_sig
< other
.m_sig
;
69 bool negative
= m_sig
< 0;
70 bool other_negative
= other
.m_sig
< 0;
72 if (negative
!= other_negative
)
73 return negative
> other_negative
;
75 bool r
= m_exp
< other
.m_exp
;
76 return negative
? !r
: r
;
80 bool operator== (const sreal
&other
) const
82 return m_exp
== other
.m_exp
&& m_sig
== other
.m_sig
;
85 sreal
operator- () const
93 sreal
shift (int s
) const
95 /* Zero needs no shifting. */
98 gcc_checking_assert (s
<= SREAL_MAX_EXP
);
99 gcc_checking_assert (s
>= -SREAL_MAX_EXP
);
101 /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
103 gcc_checking_assert (m_exp
+ s
<= SREAL_MAX_EXP
);
104 gcc_checking_assert (m_exp
+ s
>= -SREAL_MAX_EXP
);
112 /* Global minimum sreal can hold. */
113 inline static sreal
min ()
116 /* This never needs normalization. */
117 min
.m_sig
= -SREAL_MAX_SIG
;
118 min
.m_exp
= SREAL_MAX_EXP
;
122 /* Global minimum sreal can hold. */
123 inline static sreal
max ()
126 /* This never needs normalization. */
127 max
.m_sig
= SREAL_MAX_SIG
;
128 max
.m_exp
= SREAL_MAX_EXP
;
133 inline void normalize ();
134 inline void normalize_up ();
135 inline void normalize_down ();
136 void shift_right (int amount
);
137 static sreal
signedless_plus (const sreal
&a
, const sreal
&b
, bool negative
);
138 static sreal
signedless_minus (const sreal
&a
, const sreal
&b
, bool negative
);
140 int64_t m_sig
; /* Significant. */
141 signed int m_exp
; /* Exponent. */
144 extern void debug (const sreal
&ref
);
145 extern void debug (const sreal
*ptr
);
147 inline sreal
&operator+= (sreal
&a
, const sreal
&b
)
152 inline sreal
&operator-= (sreal
&a
, const sreal
&b
)
157 inline sreal
&operator/= (sreal
&a
, const sreal
&b
)
162 inline sreal
&operator*= (sreal
&a
, const sreal
&b
)
167 inline bool operator!= (const sreal
&a
, const sreal
&b
)
172 inline bool operator> (const sreal
&a
, const sreal
&b
)
174 return !(a
== b
|| a
< b
);
177 inline bool operator<= (const sreal
&a
, const sreal
&b
)
179 return a
< b
|| a
== b
;
182 inline bool operator>= (const sreal
&a
, const sreal
&b
)
184 return a
== b
|| a
> b
;
187 inline sreal
operator<< (const sreal
&a
, int exp
)
189 return a
.shift (exp
);
192 inline sreal
operator>> (const sreal
&a
, int exp
)
194 return a
.shift (-exp
);
197 /* Make significant to be >= SREAL_MIN_SIG.
199 Make this separate method so inliner can handle hot path better. */
202 sreal::normalize_up ()
204 unsigned HOST_WIDE_INT sig
= absu_hwi (m_sig
);
205 int shift
= SREAL_PART_BITS
- 2 - floor_log2 (sig
);
207 gcc_checking_assert (shift
> 0);
210 gcc_checking_assert (sig
<= SREAL_MAX_SIG
&& sig
>= SREAL_MIN_SIG
);
212 /* Check underflow. */
213 if (m_exp
< -SREAL_MAX_EXP
)
215 m_exp
= -SREAL_MAX_EXP
;
218 if (SREAL_SIGN (m_sig
) == -1)
224 /* Make significant to be <= SREAL_MAX_SIG.
226 Make this separate method so inliner can handle hot path better. */
229 sreal::normalize_down ()
232 unsigned HOST_WIDE_INT sig
= absu_hwi (m_sig
);
233 int shift
= floor_log2 (sig
) - SREAL_PART_BITS
+ 2;
235 gcc_checking_assert (shift
> 0);
236 last_bit
= (sig
>> (shift
-1)) & 1;
239 gcc_checking_assert (sig
<= SREAL_MAX_SIG
&& sig
>= SREAL_MIN_SIG
);
241 /* Round the number. */
243 if (sig
> SREAL_MAX_SIG
)
249 /* Check overflow. */
250 if (m_exp
> SREAL_MAX_EXP
)
252 m_exp
= SREAL_MAX_EXP
;
255 if (SREAL_SIGN (m_sig
) == -1)
261 /* Normalize *this; the hot path. */
266 unsigned HOST_WIDE_INT sig
= absu_hwi (m_sig
);
269 m_exp
= -SREAL_MAX_EXP
;
270 else if (sig
> SREAL_MAX_SIG
)
272 else if (sig
< SREAL_MIN_SIG
)