annotate gcc/sreal.c @ 127:4c56639505ff

fix function.c and add CbC-example Makefile
author mir3636
date Wed, 11 Apr 2018 18:46:58 +0900
parents 04ced10e8804
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents: 67
diff changeset
1 /* Simple data type for real numbers for the GNU compiler.
kono
parents: 67
diff changeset
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
111
kono
parents: 67
diff changeset
20 /* This library supports real numbers;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 inf and nan are NOT supported.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 It is written to be simple and fast.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 Value of sreal is
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 x = sig * 2 ^ exp
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 where
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 sig = significant
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 (for < 64-bit machines sig = sig_lo + sig_hi * 2 ^ SREAL_PART_BITS)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 exp = exponent
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
111
kono
parents: 67
diff changeset
31 One uint64_t is used for the significant.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 Only a half of significant bits is used (in normalized sreals) so that we do
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 not have problems with overflow, for example when c->sig = a->sig * b->sig.
111
kono
parents: 67
diff changeset
34 So the precision is 32-bit.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 Invariant: The numbers are normalized before and after each call of sreal_*.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 Normalized sreals:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 All numbers (except zero) meet following conditions:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 SREAL_MIN_SIG <= sig && sig <= SREAL_MAX_SIG
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 -SREAL_MAX_EXP <= exp && exp <= SREAL_MAX_EXP
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 If the number would be too large, it is set to upper bounds of these
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 conditions.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 If the number is zero or would be too small it meets following conditions:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 sig == 0 && exp == -SREAL_MAX_EXP
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 #include "config.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 #include "system.h"
111
kono
parents: 67
diff changeset
52 #include <math.h>
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 #include "coretypes.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 #include "sreal.h"
111
kono
parents: 67
diff changeset
55 #include "selftest.h"
kono
parents: 67
diff changeset
56 #include "backend.h"
kono
parents: 67
diff changeset
57 #include "tree.h"
kono
parents: 67
diff changeset
58 #include "gimple.h"
kono
parents: 67
diff changeset
59 #include "cgraph.h"
kono
parents: 67
diff changeset
60 #include "data-streamer.h"
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 /* Print the content of struct sreal. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 void
111
kono
parents: 67
diff changeset
65 sreal::dump (FILE *file) const
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 {
111
kono
parents: 67
diff changeset
67 fprintf (file, "(%" PRIi64 " * 2^%d)", m_sig, m_exp);
kono
parents: 67
diff changeset
68 }
kono
parents: 67
diff changeset
69
kono
parents: 67
diff changeset
70 DEBUG_FUNCTION void
kono
parents: 67
diff changeset
71 debug (const sreal &ref)
kono
parents: 67
diff changeset
72 {
kono
parents: 67
diff changeset
73 ref.dump (stderr);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75
111
kono
parents: 67
diff changeset
76 DEBUG_FUNCTION void
kono
parents: 67
diff changeset
77 debug (const sreal *ptr)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 {
111
kono
parents: 67
diff changeset
79 if (ptr)
kono
parents: 67
diff changeset
80 debug (*ptr);
kono
parents: 67
diff changeset
81 else
kono
parents: 67
diff changeset
82 fprintf (stderr, "<nil>\n");
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
111
kono
parents: 67
diff changeset
85 /* Shift this right by S bits. Needed: 0 < S <= SREAL_BITS.
kono
parents: 67
diff changeset
86 When the most significant bit shifted out is 1, add 1 to this (rounding).
kono
parents: 67
diff changeset
87 */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
111
kono
parents: 67
diff changeset
89 void
kono
parents: 67
diff changeset
90 sreal::shift_right (int s)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 {
111
kono
parents: 67
diff changeset
92 gcc_checking_assert (s > 0);
kono
parents: 67
diff changeset
93 gcc_checking_assert (s <= SREAL_BITS);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 /* Exponent should never be so large because shift_right is used only by
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 sreal_add and sreal_sub ant thus the number cannot be shifted out from
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 exponent range. */
111
kono
parents: 67
diff changeset
97 gcc_checking_assert (m_exp + s <= SREAL_MAX_EXP);
kono
parents: 67
diff changeset
98
kono
parents: 67
diff changeset
99 m_exp += s;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100
111
kono
parents: 67
diff changeset
101 m_sig += (int64_t) 1 << (s - 1);
kono
parents: 67
diff changeset
102 m_sig >>= s;
kono
parents: 67
diff changeset
103 }
kono
parents: 67
diff changeset
104
kono
parents: 67
diff changeset
105 /* Return integer value of *this. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106
111
kono
parents: 67
diff changeset
107 int64_t
kono
parents: 67
diff changeset
108 sreal::to_int () const
kono
parents: 67
diff changeset
109 {
kono
parents: 67
diff changeset
110 int64_t sign = SREAL_SIGN (m_sig);
kono
parents: 67
diff changeset
111
kono
parents: 67
diff changeset
112 if (m_exp <= -SREAL_BITS)
kono
parents: 67
diff changeset
113 return 0;
kono
parents: 67
diff changeset
114 if (m_exp >= SREAL_PART_BITS)
kono
parents: 67
diff changeset
115 return sign * INTTYPE_MAXIMUM (int64_t);
kono
parents: 67
diff changeset
116 if (m_exp > 0)
kono
parents: 67
diff changeset
117 return sign * (SREAL_ABS (m_sig) << m_exp);
kono
parents: 67
diff changeset
118 if (m_exp < 0)
kono
parents: 67
diff changeset
119 return m_sig >> -m_exp;
kono
parents: 67
diff changeset
120 return m_sig;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122
111
kono
parents: 67
diff changeset
123 /* Return value of *this as double.
kono
parents: 67
diff changeset
124 This should be used for debug output only. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125
111
kono
parents: 67
diff changeset
126 double
kono
parents: 67
diff changeset
127 sreal::to_double () const
kono
parents: 67
diff changeset
128 {
kono
parents: 67
diff changeset
129 double val = m_sig;
kono
parents: 67
diff changeset
130 if (m_exp)
kono
parents: 67
diff changeset
131 val = ldexp (val, m_exp);
kono
parents: 67
diff changeset
132 return val;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134
111
kono
parents: 67
diff changeset
135 /* Return *this + other. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136
111
kono
parents: 67
diff changeset
137 sreal
kono
parents: 67
diff changeset
138 sreal::operator+ (const sreal &other) const
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 int dexp;
111
kono
parents: 67
diff changeset
141 sreal tmp, r;
kono
parents: 67
diff changeset
142
kono
parents: 67
diff changeset
143 const sreal *a_p = this, *b_p = &other, *bb;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144
111
kono
parents: 67
diff changeset
145 if (a_p->m_exp < b_p->m_exp)
kono
parents: 67
diff changeset
146 std::swap (a_p, b_p);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
111
kono
parents: 67
diff changeset
148 dexp = a_p->m_exp - b_p->m_exp;
kono
parents: 67
diff changeset
149 r.m_exp = a_p->m_exp;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 if (dexp > SREAL_BITS)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 {
111
kono
parents: 67
diff changeset
152 r.m_sig = a_p->m_sig;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 if (dexp == 0)
111
kono
parents: 67
diff changeset
157 bb = b_p;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 {
111
kono
parents: 67
diff changeset
160 tmp = *b_p;
kono
parents: 67
diff changeset
161 tmp.shift_right (dexp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 bb = &tmp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
164
111
kono
parents: 67
diff changeset
165 r.m_sig = a_p->m_sig + bb->m_sig;
kono
parents: 67
diff changeset
166 r.normalize ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169
111
kono
parents: 67
diff changeset
170
kono
parents: 67
diff changeset
171 /* Return *this - other. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172
111
kono
parents: 67
diff changeset
173 sreal
kono
parents: 67
diff changeset
174 sreal::operator- (const sreal &other) const
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 int dexp;
111
kono
parents: 67
diff changeset
177 sreal tmp, r;
kono
parents: 67
diff changeset
178 const sreal *bb;
kono
parents: 67
diff changeset
179 const sreal *a_p = this, *b_p = &other;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180
111
kono
parents: 67
diff changeset
181 int64_t sign = 1;
kono
parents: 67
diff changeset
182 if (a_p->m_exp < b_p->m_exp)
kono
parents: 67
diff changeset
183 {
kono
parents: 67
diff changeset
184 sign = -1;
kono
parents: 67
diff changeset
185 std::swap (a_p, b_p);
kono
parents: 67
diff changeset
186 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
111
kono
parents: 67
diff changeset
188 dexp = a_p->m_exp - b_p->m_exp;
kono
parents: 67
diff changeset
189 r.m_exp = a_p->m_exp;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 if (dexp > SREAL_BITS)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 {
111
kono
parents: 67
diff changeset
192 r.m_sig = sign * a_p->m_sig;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 if (dexp == 0)
111
kono
parents: 67
diff changeset
196 bb = b_p;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 {
111
kono
parents: 67
diff changeset
199 tmp = *b_p;
kono
parents: 67
diff changeset
200 tmp.shift_right (dexp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 bb = &tmp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
203
111
kono
parents: 67
diff changeset
204 r.m_sig = sign * (a_p->m_sig - bb->m_sig);
kono
parents: 67
diff changeset
205 r.normalize ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
208
111
kono
parents: 67
diff changeset
209 /* Return *this * other. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
210
111
kono
parents: 67
diff changeset
211 sreal
kono
parents: 67
diff changeset
212 sreal::operator* (const sreal &other) const
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 {
111
kono
parents: 67
diff changeset
214 sreal r;
kono
parents: 67
diff changeset
215 if (absu_hwi (m_sig) < SREAL_MIN_SIG || absu_hwi (other.m_sig) < SREAL_MIN_SIG)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 {
111
kono
parents: 67
diff changeset
217 r.m_sig = 0;
kono
parents: 67
diff changeset
218 r.m_exp = -SREAL_MAX_EXP;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 {
111
kono
parents: 67
diff changeset
222 r.m_sig = m_sig * other.m_sig;
kono
parents: 67
diff changeset
223 r.m_exp = m_exp + other.m_exp;
kono
parents: 67
diff changeset
224 r.normalize ();
kono
parents: 67
diff changeset
225 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
226
111
kono
parents: 67
diff changeset
227 return r;
kono
parents: 67
diff changeset
228 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229
111
kono
parents: 67
diff changeset
230 /* Return *this / other. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
231
111
kono
parents: 67
diff changeset
232 sreal
kono
parents: 67
diff changeset
233 sreal::operator/ (const sreal &other) const
kono
parents: 67
diff changeset
234 {
kono
parents: 67
diff changeset
235 gcc_checking_assert (other.m_sig != 0);
kono
parents: 67
diff changeset
236 sreal r;
kono
parents: 67
diff changeset
237 r.m_sig
kono
parents: 67
diff changeset
238 = SREAL_SIGN (m_sig) * (SREAL_ABS (m_sig) << SREAL_PART_BITS) / other.m_sig;
kono
parents: 67
diff changeset
239 r.m_exp = m_exp - other.m_exp - SREAL_PART_BITS;
kono
parents: 67
diff changeset
240 r.normalize ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
243
111
kono
parents: 67
diff changeset
244 /* Stream sreal value to OB. */
kono
parents: 67
diff changeset
245
kono
parents: 67
diff changeset
246 void
kono
parents: 67
diff changeset
247 sreal::stream_out (struct output_block *ob)
kono
parents: 67
diff changeset
248 {
kono
parents: 67
diff changeset
249 streamer_write_hwi (ob, m_sig);
kono
parents: 67
diff changeset
250 streamer_write_hwi (ob, m_exp);
kono
parents: 67
diff changeset
251 }
kono
parents: 67
diff changeset
252
kono
parents: 67
diff changeset
253 /* Read sreal value from IB. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
254
111
kono
parents: 67
diff changeset
255 sreal
kono
parents: 67
diff changeset
256 sreal::stream_in (struct lto_input_block *ib)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 {
111
kono
parents: 67
diff changeset
258 sreal val;
kono
parents: 67
diff changeset
259 val.m_sig = streamer_read_hwi (ib);
kono
parents: 67
diff changeset
260 val.m_exp = streamer_read_hwi (ib);
kono
parents: 67
diff changeset
261 return val;
kono
parents: 67
diff changeset
262 }
kono
parents: 67
diff changeset
263
kono
parents: 67
diff changeset
264 #if CHECKING_P
kono
parents: 67
diff changeset
265
kono
parents: 67
diff changeset
266 namespace selftest {
kono
parents: 67
diff changeset
267
kono
parents: 67
diff changeset
268 /* Selftests for sreals. */
kono
parents: 67
diff changeset
269
kono
parents: 67
diff changeset
270 /* Verify basic sreal operations. */
kono
parents: 67
diff changeset
271
kono
parents: 67
diff changeset
272 static void
kono
parents: 67
diff changeset
273 sreal_verify_basics (void)
kono
parents: 67
diff changeset
274 {
kono
parents: 67
diff changeset
275 sreal minimum = INT_MIN;
kono
parents: 67
diff changeset
276 sreal maximum = INT_MAX;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277
111
kono
parents: 67
diff changeset
278 sreal seven = 7;
kono
parents: 67
diff changeset
279 sreal minus_two = -2;
kono
parents: 67
diff changeset
280 sreal minus_nine = -9;
kono
parents: 67
diff changeset
281
kono
parents: 67
diff changeset
282 ASSERT_EQ (INT_MIN, minimum.to_int ());
kono
parents: 67
diff changeset
283 ASSERT_EQ (INT_MAX, maximum.to_int ());
kono
parents: 67
diff changeset
284
kono
parents: 67
diff changeset
285 ASSERT_FALSE (minus_two < minus_two);
kono
parents: 67
diff changeset
286 ASSERT_FALSE (seven < seven);
kono
parents: 67
diff changeset
287 ASSERT_TRUE (seven > minus_two);
kono
parents: 67
diff changeset
288 ASSERT_TRUE (minus_two < seven);
kono
parents: 67
diff changeset
289 ASSERT_TRUE (minus_two != seven);
kono
parents: 67
diff changeset
290 ASSERT_EQ (minus_two, -2);
kono
parents: 67
diff changeset
291 ASSERT_EQ (seven, 7);
kono
parents: 67
diff changeset
292 ASSERT_EQ ((seven << 10) >> 10, 7);
kono
parents: 67
diff changeset
293 ASSERT_EQ (seven + minus_nine, -2);
kono
parents: 67
diff changeset
294 }
kono
parents: 67
diff changeset
295
kono
parents: 67
diff changeset
296 /* Helper function that performs basic arithmetics and comparison
kono
parents: 67
diff changeset
297 of given arguments A and B. */
kono
parents: 67
diff changeset
298
kono
parents: 67
diff changeset
299 static void
kono
parents: 67
diff changeset
300 verify_aritmetics (int64_t a, int64_t b)
kono
parents: 67
diff changeset
301 {
kono
parents: 67
diff changeset
302 ASSERT_EQ (a, -(-(sreal (a))).to_int ());
kono
parents: 67
diff changeset
303 ASSERT_EQ (a < b, sreal (a) < sreal (b));
kono
parents: 67
diff changeset
304 ASSERT_EQ (a <= b, sreal (a) <= sreal (b));
kono
parents: 67
diff changeset
305 ASSERT_EQ (a == b, sreal (a) == sreal (b));
kono
parents: 67
diff changeset
306 ASSERT_EQ (a != b, sreal (a) != sreal (b));
kono
parents: 67
diff changeset
307 ASSERT_EQ (a > b, sreal (a) > sreal (b));
kono
parents: 67
diff changeset
308 ASSERT_EQ (a >= b, sreal (a) >= sreal (b));
kono
parents: 67
diff changeset
309 ASSERT_EQ (a + b, (sreal (a) + sreal (b)).to_int ());
kono
parents: 67
diff changeset
310 ASSERT_EQ (a - b, (sreal (a) - sreal (b)).to_int ());
kono
parents: 67
diff changeset
311 ASSERT_EQ (b + a, (sreal (b) + sreal (a)).to_int ());
kono
parents: 67
diff changeset
312 ASSERT_EQ (b - a, (sreal (b) - sreal (a)).to_int ());
kono
parents: 67
diff changeset
313 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
314
111
kono
parents: 67
diff changeset
315 /* Verify arithmetics for interesting numbers. */
kono
parents: 67
diff changeset
316
kono
parents: 67
diff changeset
317 static void
kono
parents: 67
diff changeset
318 sreal_verify_arithmetics (void)
kono
parents: 67
diff changeset
319 {
kono
parents: 67
diff changeset
320 int values[] = {-14123413, -7777, -17, -10, -2, 0, 17, 139, 1234123};
kono
parents: 67
diff changeset
321 unsigned c = sizeof (values) / sizeof (int);
kono
parents: 67
diff changeset
322
kono
parents: 67
diff changeset
323 for (unsigned i = 0; i < c; i++)
kono
parents: 67
diff changeset
324 for (unsigned j = 0; j < c; j++)
kono
parents: 67
diff changeset
325 {
kono
parents: 67
diff changeset
326 int a = values[i];
kono
parents: 67
diff changeset
327 int b = values[j];
kono
parents: 67
diff changeset
328
kono
parents: 67
diff changeset
329 verify_aritmetics (a, b);
kono
parents: 67
diff changeset
330 }
kono
parents: 67
diff changeset
331 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
332
111
kono
parents: 67
diff changeset
333 /* Helper function that performs various shifting test of a given
kono
parents: 67
diff changeset
334 argument A. */
kono
parents: 67
diff changeset
335
kono
parents: 67
diff changeset
336 static void
kono
parents: 67
diff changeset
337 verify_shifting (int64_t a)
kono
parents: 67
diff changeset
338 {
kono
parents: 67
diff changeset
339 sreal v = a;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
340
111
kono
parents: 67
diff changeset
341 for (unsigned i = 0; i < 16; i++)
kono
parents: 67
diff changeset
342 ASSERT_EQ (a << i, (v << i).to_int());
kono
parents: 67
diff changeset
343
kono
parents: 67
diff changeset
344 a = a << 16;
kono
parents: 67
diff changeset
345 v = v << 16;
kono
parents: 67
diff changeset
346
kono
parents: 67
diff changeset
347 for (unsigned i = 0; i < 16; i++)
kono
parents: 67
diff changeset
348 ASSERT_EQ (a >> i, (v >> i).to_int());
kono
parents: 67
diff changeset
349 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
350
111
kono
parents: 67
diff changeset
351 /* Verify shifting for interesting numbers. */
kono
parents: 67
diff changeset
352
kono
parents: 67
diff changeset
353 static void
kono
parents: 67
diff changeset
354 sreal_verify_shifting (void)
kono
parents: 67
diff changeset
355 {
kono
parents: 67
diff changeset
356 int values[] = {0, 17, 32, 139, 1024, 55555, 1234123};
kono
parents: 67
diff changeset
357 unsigned c = sizeof (values) / sizeof (int);
kono
parents: 67
diff changeset
358
kono
parents: 67
diff changeset
359 for (unsigned i = 0; i < c; i++)
kono
parents: 67
diff changeset
360 verify_shifting (values[i]);
kono
parents: 67
diff changeset
361 }
kono
parents: 67
diff changeset
362
kono
parents: 67
diff changeset
363 /* Verify division by (of) a negative value. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
364
111
kono
parents: 67
diff changeset
365 static void
kono
parents: 67
diff changeset
366 sreal_verify_negative_division (void)
kono
parents: 67
diff changeset
367 {
kono
parents: 67
diff changeset
368 ASSERT_EQ (sreal (1) / sreal (1), sreal (1));
kono
parents: 67
diff changeset
369 ASSERT_EQ (sreal (-1) / sreal (-1), sreal (1));
kono
parents: 67
diff changeset
370 ASSERT_EQ (sreal (-1234567) / sreal (-1234567), sreal (1));
kono
parents: 67
diff changeset
371 ASSERT_EQ (sreal (-1234567) / sreal (1234567), sreal (-1));
kono
parents: 67
diff changeset
372 ASSERT_EQ (sreal (1234567) / sreal (-1234567), sreal (-1));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 }
111
kono
parents: 67
diff changeset
374
kono
parents: 67
diff changeset
375 /* Run all of the selftests within this file. */
kono
parents: 67
diff changeset
376
kono
parents: 67
diff changeset
377 void sreal_c_tests ()
kono
parents: 67
diff changeset
378 {
kono
parents: 67
diff changeset
379 sreal_verify_basics ();
kono
parents: 67
diff changeset
380 sreal_verify_arithmetics ();
kono
parents: 67
diff changeset
381 sreal_verify_shifting ();
kono
parents: 67
diff changeset
382 sreal_verify_negative_division ();
kono
parents: 67
diff changeset
383 }
kono
parents: 67
diff changeset
384
kono
parents: 67
diff changeset
385 } // namespace selftest
kono
parents: 67
diff changeset
386 #endif /* CHECKING_P */