]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/sreal.h
1 /* Definitions for simple data type for real numbers.
2 Copyright (C) 2002-2018 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 #define SREAL_PART_BITS 31
25 #define UINT64_BITS 64
27 #define SREAL_MIN_SIG ((int64_t) 1 << (SREAL_PART_BITS - 2))
28 #define SREAL_MAX_SIG (((int64_t) 1 << (SREAL_PART_BITS - 1)) - 1)
29 #define SREAL_MAX_EXP (INT_MAX / 4)
31 #define SREAL_BITS SREAL_PART_BITS
33 #define SREAL_SIGN(v) (v < 0 ? -1: 1)
34 #define SREAL_ABS(v) (v < 0 ? -v: v)
37 struct lto_input_block
;
39 /* Structure for holding a simple real number. */
43 /* Construct an uninitialized sreal. */
44 sreal () : m_sig (-1), m_exp (-1) {}
46 /* Construct a sreal. */
47 sreal (int64_t sig
, int exp
= 0)
52 void dump (FILE *) const;
53 int64_t to_int () const;
54 double to_double () const;
55 void stream_out (struct output_block
*);
56 static sreal
stream_in (struct lto_input_block
*);
57 sreal
operator+ (const sreal
&other
) const;
58 sreal
operator- (const sreal
&other
) const;
59 sreal
operator* (const sreal
&other
) const;
60 sreal
operator/ (const sreal
&other
) const;
62 bool operator< (const sreal
&other
) const
64 if (m_exp
== other
.m_exp
)
65 return m_sig
< other
.m_sig
;
68 bool negative
= m_sig
< 0;
69 bool other_negative
= other
.m_sig
< 0;
71 if (negative
!= other_negative
)
72 return negative
> other_negative
;
74 bool r
= m_exp
< other
.m_exp
;
75 return negative
? !r
: r
;
79 bool operator== (const sreal
&other
) const
81 return m_exp
== other
.m_exp
&& m_sig
== other
.m_sig
;
84 sreal
operator- () const
92 sreal
shift (int s
) const
94 /* Zero needs no shifting. */
97 gcc_checking_assert (s
<= SREAL_MAX_EXP
);
98 gcc_checking_assert (s
>= -SREAL_MAX_EXP
);
100 /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
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 ()
115 /* This never needs normalization. */
116 min
.m_sig
= -SREAL_MAX_SIG
;
117 min
.m_exp
= SREAL_MAX_EXP
;
121 /* Global minimum sreal can hold. */
122 inline static sreal
max ()
125 /* This never needs normalization. */
126 max
.m_sig
= SREAL_MAX_SIG
;
127 max
.m_exp
= SREAL_MAX_EXP
;
132 inline void normalize (int64_t new_sig
, signed int new_exp
);
133 inline void normalize_up (int64_t new_sig
, signed int new_exp
);
134 inline void normalize_down (int64_t new_sig
, signed int new_exp
);
135 void shift_right (int amount
);
136 static sreal
signedless_plus (const sreal
&a
, const sreal
&b
, bool negative
);
137 static sreal
signedless_minus (const sreal
&a
, const sreal
&b
, bool negative
);
139 int32_t m_sig
; /* Significant. */
140 signed int m_exp
; /* Exponent. */
143 extern void debug (const sreal
&ref
);
144 extern void debug (const sreal
*ptr
);
146 inline sreal
&operator+= (sreal
&a
, const sreal
&b
)
151 inline sreal
&operator-= (sreal
&a
, const sreal
&b
)
156 inline sreal
&operator/= (sreal
&a
, const sreal
&b
)
161 inline sreal
&operator*= (sreal
&a
, const sreal
&b
)
166 inline bool operator!= (const sreal
&a
, const sreal
&b
)
171 inline bool operator> (const sreal
&a
, const sreal
&b
)
173 return !(a
== b
|| a
< b
);
176 inline bool operator<= (const sreal
&a
, const sreal
&b
)
178 return a
< b
|| a
== b
;
181 inline bool operator>= (const sreal
&a
, const sreal
&b
)
183 return a
== b
|| a
> b
;
186 inline sreal
operator<< (const sreal
&a
, int exp
)
188 return a
.shift (exp
);
191 inline sreal
operator>> (const sreal
&a
, int exp
)
193 return a
.shift (-exp
);
196 /* Make significant to be >= SREAL_MIN_SIG.
198 Make this separate method so inliner can handle hot path better. */
201 sreal::normalize_up (int64_t new_sig
, signed int new_exp
)
203 unsigned HOST_WIDE_INT sig
= absu_hwi (new_sig
);
204 int shift
= SREAL_PART_BITS
- 2 - floor_log2 (sig
);
206 gcc_checking_assert (shift
> 0);
209 gcc_checking_assert (sig
<= SREAL_MAX_SIG
&& sig
>= SREAL_MIN_SIG
);
211 /* Check underflow. */
212 if (new_exp
< -SREAL_MAX_EXP
)
214 new_exp
= -SREAL_MAX_EXP
;
218 if (SREAL_SIGN (new_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 (int64_t new_sig
, signed int new_exp
)
232 unsigned HOST_WIDE_INT sig
= absu_hwi (new_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 (new_exp
> SREAL_MAX_EXP
)
252 new_exp
= SREAL_MAX_EXP
;
256 if (SREAL_SIGN (new_sig
) == -1)
262 /* Normalize *this; the hot path. */
265 sreal::normalize (int64_t new_sig
, signed int new_exp
)
267 unsigned HOST_WIDE_INT sig
= absu_hwi (new_sig
);
272 m_exp
= -SREAL_MAX_EXP
;
274 else if (sig
> SREAL_MAX_SIG
)
275 normalize_down (new_sig
, new_exp
);
276 else if (sig
< SREAL_MIN_SIG
)
277 normalize_up (new_sig
, new_exp
);