fc978c11a9193e37262b0007149e9790220f20ca
[yosys.git] / kernel / calc.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 // [[CITE]] Power-Modulus Algorithm
21 // Schneier, Bruce (1996). Applied Cryptography: Protocols, Algorithms, and Source Code in C,
22 // Second Edition (2nd ed.). Wiley. ISBN 978-0-471-11709-4, page 244
23
24 #include "kernel/log.h"
25 #include "kernel/rtlil.h"
26 #include "libs/bigint/BigIntegerLibrary.hh"
27 #include <assert.h>
28
29 static void extend(RTLIL::Const &arg, int width, bool is_signed)
30 {
31 RTLIL::State padding = RTLIL::State::S0;
32
33 if (arg.bits.size() > 0 && (is_signed || arg.bits.back() > RTLIL::State::S1))
34 padding = arg.bits.back();
35
36 while (int(arg.bits.size()) < width)
37 arg.bits.push_back(padding);
38 }
39
40 static void extend_u0(RTLIL::Const &arg, int width, bool is_signed)
41 {
42 RTLIL::State padding = RTLIL::State::S0;
43
44 if (arg.bits.size() > 0 && is_signed)
45 padding = arg.bits.back();
46
47 while (int(arg.bits.size()) < width)
48 arg.bits.push_back(padding);
49 }
50
51 static BigInteger const2big(const RTLIL::Const &val, bool as_signed, int &undef_bit_pos)
52 {
53 BigInteger result = 0, this_bit = 1;
54 for (size_t i = 0; i < val.bits.size(); i++) {
55 if (val.bits[i] == RTLIL::State::S1) {
56 if (as_signed && i+1 == val.bits.size())
57 result -= this_bit;
58 else
59 result += this_bit;
60 }
61 else if (val.bits[i] != RTLIL::State::S0) {
62 if (undef_bit_pos < 0)
63 undef_bit_pos = i;
64 }
65 this_bit *= 2;
66 }
67 return result;
68 }
69
70 static RTLIL::Const big2const(const BigInteger &val, int result_len, int undef_bit_pos)
71 {
72 if (undef_bit_pos >= 0)
73 return RTLIL::Const(RTLIL::State::Sx, result_len);
74
75 BigUnsigned mag = val.getMagnitude();
76 RTLIL::Const result(0, result_len);
77
78 if (!mag.isZero())
79 {
80 if (val.getSign() < 0)
81 {
82 mag--;
83 for (int i = 0; i < result_len; i++)
84 result.bits[i] = mag.getBit(i) ? RTLIL::State::S0 : RTLIL::State::S1;
85 }
86 else
87 {
88 for (int i = 0; i < result_len; i++)
89 result.bits[i] = mag.getBit(i) ? RTLIL::State::S1 : RTLIL::State::S0;
90 }
91 }
92
93 #if 0
94 if (undef_bit_pos >= 0)
95 for (int i = undef_bit_pos; i < result_len; i++)
96 result.bits[i] = RTLIL::State::Sx;
97 #endif
98
99 return result;
100 }
101
102 static RTLIL::State logic_and(RTLIL::State a, RTLIL::State b)
103 {
104 if (a == RTLIL::State::S0) return RTLIL::State::S0;
105 if (b == RTLIL::State::S0) return RTLIL::State::S0;
106 if (a != RTLIL::State::S1) return RTLIL::State::Sx;
107 if (b != RTLIL::State::S1) return RTLIL::State::Sx;
108 return RTLIL::State::S1;
109 }
110
111 static RTLIL::State logic_or(RTLIL::State a, RTLIL::State b)
112 {
113 if (a == RTLIL::State::S1) return RTLIL::State::S1;
114 if (b == RTLIL::State::S1) return RTLIL::State::S1;
115 if (a != RTLIL::State::S0) return RTLIL::State::Sx;
116 if (b != RTLIL::State::S0) return RTLIL::State::Sx;
117 return RTLIL::State::S0;
118 }
119
120 static RTLIL::State logic_xor(RTLIL::State a, RTLIL::State b)
121 {
122 if (a != RTLIL::State::S0 && a != RTLIL::State::S1) return RTLIL::State::Sx;
123 if (b != RTLIL::State::S0 && b != RTLIL::State::S1) return RTLIL::State::Sx;
124 return a != b ? RTLIL::State::S1 : RTLIL::State::S0;
125 }
126
127 static RTLIL::State logic_xnor(RTLIL::State a, RTLIL::State b)
128 {
129 if (a != RTLIL::State::S0 && a != RTLIL::State::S1) return RTLIL::State::Sx;
130 if (b != RTLIL::State::S0 && b != RTLIL::State::S1) return RTLIL::State::Sx;
131 return a == b ? RTLIL::State::S1 : RTLIL::State::S0;
132 }
133
134 RTLIL::Const RTLIL::const_not(const RTLIL::Const &arg1, const RTLIL::Const&, bool signed1, bool, int result_len)
135 {
136 if (result_len < 0)
137 result_len = arg1.bits.size();
138
139 RTLIL::Const arg1_ext = arg1;
140 extend_u0(arg1_ext, result_len, signed1);
141
142 RTLIL::Const result(RTLIL::State::Sx, result_len);
143 for (size_t i = 0; i < size_t(result_len); i++) {
144 if (i >= arg1_ext.bits.size())
145 result.bits[i] = RTLIL::State::S0;
146 else if (arg1_ext.bits[i] == RTLIL::State::S0)
147 result.bits[i] = RTLIL::State::S1;
148 else if (arg1_ext.bits[i] == RTLIL::State::S1)
149 result.bits[i] = RTLIL::State::S0;
150 }
151
152 return result;
153 }
154
155 static RTLIL::Const logic_wrapper(RTLIL::State(*logic_func)(RTLIL::State, RTLIL::State),
156 RTLIL::Const arg1, RTLIL::Const arg2, bool signed1, bool signed2, int result_len = -1)
157 {
158 if (result_len < 0)
159 result_len = std::max(arg1.bits.size(), arg2.bits.size());
160
161 extend_u0(arg1, result_len, signed1);
162 extend_u0(arg2, result_len, signed2);
163
164 RTLIL::Const result(RTLIL::State::Sx, result_len);
165 for (size_t i = 0; i < size_t(result_len); i++) {
166 RTLIL::State a = i < arg1.bits.size() ? arg1.bits[i] : RTLIL::State::S0;
167 RTLIL::State b = i < arg2.bits.size() ? arg2.bits[i] : RTLIL::State::S0;
168 result.bits[i] = logic_func(a, b);
169 }
170
171 return result;
172 }
173
174 RTLIL::Const RTLIL::const_and(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
175 {
176 return logic_wrapper(logic_and, arg1, arg2, signed1, signed2, result_len);
177 }
178
179 RTLIL::Const RTLIL::const_or(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
180 {
181 return logic_wrapper(logic_or, arg1, arg2, signed1, signed2, result_len);
182 }
183
184 RTLIL::Const RTLIL::const_xor(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
185 {
186 return logic_wrapper(logic_xor, arg1, arg2, signed1, signed2, result_len);
187 }
188
189 RTLIL::Const RTLIL::const_xnor(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
190 {
191 return logic_wrapper(logic_xnor, arg1, arg2, signed1, signed2, result_len);
192 }
193
194 static RTLIL::Const logic_reduce_wrapper(RTLIL::State initial, RTLIL::State(*logic_func)(RTLIL::State, RTLIL::State), const RTLIL::Const &arg1, int result_len)
195 {
196 RTLIL::State temp = initial;
197
198 for (size_t i = 0; i < arg1.bits.size(); i++)
199 temp = logic_func(temp, arg1.bits[i]);
200
201 RTLIL::Const result(temp);
202 while (int(result.bits.size()) < result_len)
203 result.bits.push_back(RTLIL::State::S0);
204 return result;
205 }
206
207 RTLIL::Const RTLIL::const_reduce_and(const RTLIL::Const &arg1, const RTLIL::Const&, bool, bool, int result_len)
208 {
209 return logic_reduce_wrapper(RTLIL::State::S1, logic_and, arg1, result_len);
210 }
211
212 RTLIL::Const RTLIL::const_reduce_or(const RTLIL::Const &arg1, const RTLIL::Const&, bool, bool, int result_len)
213 {
214 return logic_reduce_wrapper(RTLIL::State::S0, logic_or, arg1, result_len);
215 }
216
217 RTLIL::Const RTLIL::const_reduce_xor(const RTLIL::Const &arg1, const RTLIL::Const&, bool, bool, int result_len)
218 {
219 return logic_reduce_wrapper(RTLIL::State::S0, logic_xor, arg1, result_len);
220 }
221
222 RTLIL::Const RTLIL::const_reduce_xnor(const RTLIL::Const &arg1, const RTLIL::Const&, bool, bool, int result_len)
223 {
224 RTLIL::Const buffer = logic_reduce_wrapper(RTLIL::State::S0, logic_xor, arg1, result_len);
225 if (!buffer.bits.empty()) {
226 if (buffer.bits.front() == RTLIL::State::S0)
227 buffer.bits.front() = RTLIL::State::S1;
228 else if (buffer.bits.front() == RTLIL::State::S1)
229 buffer.bits.front() = RTLIL::State::S0;
230 }
231 return buffer;
232 }
233
234 RTLIL::Const RTLIL::const_reduce_bool(const RTLIL::Const &arg1, const RTLIL::Const&, bool, bool, int result_len)
235 {
236 return logic_reduce_wrapper(RTLIL::State::S0, logic_or, arg1, result_len);
237 }
238
239 RTLIL::Const RTLIL::const_logic_not(const RTLIL::Const &arg1, const RTLIL::Const&, bool signed1, bool, int result_len)
240 {
241 int undef_bit_pos_a = -1;
242 BigInteger a = const2big(arg1, signed1, undef_bit_pos_a);
243 RTLIL::Const result(a.isZero() ? undef_bit_pos_a >= 0 ? RTLIL::State::Sx : RTLIL::State::S1 : RTLIL::State::S0);
244
245 while (int(result.bits.size()) < result_len)
246 result.bits.push_back(RTLIL::State::S0);
247 return result;
248 }
249
250 RTLIL::Const RTLIL::const_logic_and(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
251 {
252 int undef_bit_pos_a = -1, undef_bit_pos_b = -1;
253 BigInteger a = const2big(arg1, signed1, undef_bit_pos_a);
254 BigInteger b = const2big(arg2, signed2, undef_bit_pos_b);
255
256 RTLIL::State bit_a = a.isZero() ? undef_bit_pos_a >= 0 ? RTLIL::State::Sx : RTLIL::State::S0 : RTLIL::State::S1;
257 RTLIL::State bit_b = b.isZero() ? undef_bit_pos_b >= 0 ? RTLIL::State::Sx : RTLIL::State::S0 : RTLIL::State::S1;
258 RTLIL::Const result(logic_and(bit_a, bit_b));
259
260 while (int(result.bits.size()) < result_len)
261 result.bits.push_back(RTLIL::State::S0);
262 return result;
263 }
264
265 RTLIL::Const RTLIL::const_logic_or(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
266 {
267 int undef_bit_pos_a = -1, undef_bit_pos_b = -1;
268 BigInteger a = const2big(arg1, signed1, undef_bit_pos_a);
269 BigInteger b = const2big(arg2, signed2, undef_bit_pos_b);
270
271 RTLIL::State bit_a = a.isZero() ? undef_bit_pos_a >= 0 ? RTLIL::State::Sx : RTLIL::State::S0 : RTLIL::State::S1;
272 RTLIL::State bit_b = b.isZero() ? undef_bit_pos_b >= 0 ? RTLIL::State::Sx : RTLIL::State::S0 : RTLIL::State::S1;
273 RTLIL::Const result(logic_or(bit_a, bit_b));
274
275 while (int(result.bits.size()) < result_len)
276 result.bits.push_back(RTLIL::State::S0);
277 return result;
278 }
279
280 static RTLIL::Const const_shift(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool sign_ext, int direction, int result_len)
281 {
282 int undef_bit_pos = -1;
283 BigInteger offset = const2big(arg2, false, undef_bit_pos) * direction;
284
285 if (result_len < 0)
286 result_len = arg1.bits.size();
287
288 RTLIL::Const result(RTLIL::State::Sx, result_len);
289 if (undef_bit_pos >= 0)
290 return result;
291
292 for (int i = 0; i < result_len; i++) {
293 BigInteger pos = BigInteger(i) + offset;
294 if (pos < 0)
295 result.bits[i] = RTLIL::State::S0;
296 else if (pos >= arg1.bits.size())
297 result.bits[i] = sign_ext ? arg1.bits.back() : RTLIL::State::S0;
298 else
299 result.bits[i] = arg1.bits[pos.toInt()];
300 }
301
302 return result;
303 }
304
305 RTLIL::Const RTLIL::const_shl(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool, int result_len)
306 {
307 RTLIL::Const arg1_ext = arg1;
308 extend(arg1_ext, result_len, signed1);
309 return const_shift(arg1_ext, arg2, false, -1, result_len);
310 }
311
312 RTLIL::Const RTLIL::const_shr(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool, int result_len)
313 {
314 RTLIL::Const arg1_ext = arg1;
315 extend(arg1_ext, result_len, signed1);
316 return const_shift(arg1_ext, arg2, false, +1, result_len);
317 }
318
319 RTLIL::Const RTLIL::const_sshl(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
320 {
321 if (!signed1)
322 return const_shl(arg1, arg2, signed1, signed2, result_len);
323 return const_shift(arg1, arg2, true, -1, result_len);
324 }
325
326 RTLIL::Const RTLIL::const_sshr(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
327 {
328 if (!signed1)
329 return const_shr(arg1, arg2, signed1, signed2, result_len);
330 return const_shift(arg1, arg2, true, +1, result_len);
331 }
332
333 RTLIL::Const RTLIL::const_lt(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
334 {
335 int undef_bit_pos = -1;
336 bool y = const2big(arg1, signed1, undef_bit_pos) < const2big(arg2, signed2, undef_bit_pos);
337 RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0);
338
339 while (int(result.bits.size()) < result_len)
340 result.bits.push_back(RTLIL::State::S0);
341 return result;
342 }
343
344 RTLIL::Const RTLIL::const_le(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
345 {
346 int undef_bit_pos = -1;
347 bool y = const2big(arg1, signed1, undef_bit_pos) <= const2big(arg2, signed2, undef_bit_pos);
348 RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0);
349
350 while (int(result.bits.size()) < result_len)
351 result.bits.push_back(RTLIL::State::S0);
352 return result;
353 }
354
355 RTLIL::Const RTLIL::const_eq(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
356 {
357 RTLIL::Const arg1_ext = arg1;
358 RTLIL::Const arg2_ext = arg2;
359 RTLIL::Const result(RTLIL::State::S0, result_len);
360
361 int width = std::max(arg1_ext.bits.size(), arg2_ext.bits.size());
362 extend_u0(arg1_ext, width, signed1 && signed2);
363 extend_u0(arg2_ext, width, signed1 && signed2);
364
365 RTLIL::State matched_status = RTLIL::State::S1;
366 for (size_t i = 0; i < arg1_ext.bits.size(); i++) {
367 if (arg1_ext.bits.at(i) == RTLIL::State::S0 && arg2_ext.bits.at(i) == RTLIL::State::S1)
368 return result;
369 if (arg1_ext.bits.at(i) == RTLIL::State::S1 && arg2_ext.bits.at(i) == RTLIL::State::S0)
370 return result;
371 if (arg1_ext.bits.at(i) > RTLIL::State::S1 || arg2_ext.bits.at(i) > RTLIL::State::S1)
372 matched_status = RTLIL::State::Sx;
373 }
374
375 result.bits.front() = matched_status;
376 return result;
377 }
378
379 RTLIL::Const RTLIL::const_ne(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
380 {
381 RTLIL::Const result = RTLIL::const_eq(arg1, arg2, signed1, signed2, result_len);
382 if (result.bits.front() == RTLIL::State::S0)
383 result.bits.front() = RTLIL::State::S1;
384 else if (result.bits.front() == RTLIL::State::S1)
385 result.bits.front() = RTLIL::State::S0;
386 return result;
387 }
388
389 RTLIL::Const RTLIL::const_eqx(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
390 {
391 RTLIL::Const arg1_ext = arg1;
392 RTLIL::Const arg2_ext = arg2;
393 RTLIL::Const result(RTLIL::State::S0, result_len);
394
395 int width = std::max(arg1_ext.bits.size(), arg2_ext.bits.size());
396 extend_u0(arg1_ext, width, signed1 && signed2);
397 extend_u0(arg2_ext, width, signed1 && signed2);
398
399 for (size_t i = 0; i < arg1_ext.bits.size(); i++) {
400 if (arg1_ext.bits.at(i) != arg2_ext.bits.at(i))
401 return result;
402 }
403
404 result.bits.front() = RTLIL::State::S1;
405 return result;
406 }
407
408 RTLIL::Const RTLIL::const_nex(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
409 {
410 RTLIL::Const result = RTLIL::const_eqx(arg1, arg2, signed1, signed2, result_len);
411 if (result.bits.front() == RTLIL::State::S0)
412 result.bits.front() = RTLIL::State::S1;
413 else if (result.bits.front() == RTLIL::State::S1)
414 result.bits.front() = RTLIL::State::S0;
415 return result;
416 }
417
418 RTLIL::Const RTLIL::const_ge(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
419 {
420 int undef_bit_pos = -1;
421 bool y = const2big(arg1, signed1, undef_bit_pos) >= const2big(arg2, signed2, undef_bit_pos);
422 RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0);
423
424 while (int(result.bits.size()) < result_len)
425 result.bits.push_back(RTLIL::State::S0);
426 return result;
427 }
428
429 RTLIL::Const RTLIL::const_gt(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
430 {
431 int undef_bit_pos = -1;
432 bool y = const2big(arg1, signed1, undef_bit_pos) > const2big(arg2, signed2, undef_bit_pos);
433 RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0);
434
435 while (int(result.bits.size()) < result_len)
436 result.bits.push_back(RTLIL::State::S0);
437 return result;
438 }
439
440 RTLIL::Const RTLIL::const_add(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
441 {
442 int undef_bit_pos = -1;
443 BigInteger y = const2big(arg1, signed1, undef_bit_pos) + const2big(arg2, signed2, undef_bit_pos);
444 return big2const(y, result_len >= 0 ? result_len : std::max(arg1.bits.size(), arg2.bits.size()), undef_bit_pos);
445 }
446
447 RTLIL::Const RTLIL::const_sub(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
448 {
449 int undef_bit_pos = -1;
450 BigInteger y = const2big(arg1, signed1, undef_bit_pos) - const2big(arg2, signed2, undef_bit_pos);
451 return big2const(y, result_len >= 0 ? result_len : std::max(arg1.bits.size(), arg2.bits.size()), undef_bit_pos);
452 }
453
454 RTLIL::Const RTLIL::const_mul(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
455 {
456 int undef_bit_pos = -1;
457 BigInteger y = const2big(arg1, signed1, undef_bit_pos) * const2big(arg2, signed2, undef_bit_pos);
458 return big2const(y, result_len >= 0 ? result_len : std::max(arg1.bits.size(), arg2.bits.size()), std::min(undef_bit_pos, 0));
459 }
460
461 RTLIL::Const RTLIL::const_div(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
462 {
463 int undef_bit_pos = -1;
464 BigInteger a = const2big(arg1, signed1, undef_bit_pos);
465 BigInteger b = const2big(arg2, signed2, undef_bit_pos);
466 if (b.isZero())
467 return RTLIL::Const(RTLIL::State::Sx, result_len);
468 bool result_neg = (a.getSign() == BigInteger::negative) != (b.getSign() == BigInteger::negative);
469 a = a.getSign() == BigInteger::negative ? -a : a;
470 b = b.getSign() == BigInteger::negative ? -b : b;
471 return big2const(result_neg ? -(a / b) : (a / b), result_len >= 0 ? result_len : std::max(arg1.bits.size(), arg2.bits.size()), std::min(undef_bit_pos, 0));
472 }
473
474 RTLIL::Const RTLIL::const_mod(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
475 {
476 int undef_bit_pos = -1;
477 BigInteger a = const2big(arg1, signed1, undef_bit_pos);
478 BigInteger b = const2big(arg2, signed2, undef_bit_pos);
479 if (b.isZero())
480 return RTLIL::Const(RTLIL::State::Sx, result_len);
481 bool result_neg = a.getSign() == BigInteger::negative;
482 a = a.getSign() == BigInteger::negative ? -a : a;
483 b = b.getSign() == BigInteger::negative ? -b : b;
484 return big2const(result_neg ? -(a % b) : (a % b), result_len >= 0 ? result_len : std::max(arg1.bits.size(), arg2.bits.size()), std::min(undef_bit_pos, 0));
485 }
486
487 RTLIL::Const RTLIL::const_pow(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
488 {
489 int undef_bit_pos = -1;
490
491 BigInteger a = const2big(arg1, signed1, undef_bit_pos);
492 BigInteger b = const2big(arg2, signed2, undef_bit_pos);
493 BigInteger y = 1;
494
495 if (a == 0 && b < 0)
496 return RTLIL::Const(RTLIL::State::Sx, result_len);
497
498 if (a == 0 && b > 0)
499 return RTLIL::Const(RTLIL::State::S0, result_len);
500
501 if (b < 0)
502 {
503 if (a < -1 || a > 1)
504 y = 0;
505 if (a == -1)
506 y = (-b % 2) == 0 ? 1 : -1;
507 }
508
509 if (b > 0)
510 {
511 // Power-modulo with 2^result_len as modulus
512 BigInteger modulus = 1;
513 int modulus_bits = (result_len >= 0 ? result_len : 1024);
514 for (int i = 0; i < modulus_bits; i++)
515 modulus *= 2;
516
517 bool flip_result_sign = false;
518 if (a < 0) {
519 a *= -1;
520 if (b % 2 == 1)
521 flip_result_sign = true;
522 }
523
524 while (b > 0) {
525 if (b % 2 == 1)
526 y = (y * a) % modulus;
527 b = b / 2;
528 a = (a * a) % modulus;
529 }
530
531 if (flip_result_sign)
532 y *= -1;
533 }
534
535 return big2const(y, result_len >= 0 ? result_len : std::max(arg1.bits.size(), arg2.bits.size()), std::min(undef_bit_pos, 0));
536 }
537
538 RTLIL::Const RTLIL::const_pos(const RTLIL::Const &arg1, const RTLIL::Const&, bool signed1, bool, int result_len)
539 {
540 RTLIL::Const arg1_ext = arg1;
541 extend(arg1_ext, result_len, signed1);
542
543 return arg1_ext;
544 }
545
546 RTLIL::Const RTLIL::const_neg(const RTLIL::Const &arg1, const RTLIL::Const&, bool signed1, bool, int result_len)
547 {
548 RTLIL::Const arg1_ext = arg1;
549 extend(arg1_ext, result_len, signed1);
550
551 RTLIL::Const zero(RTLIL::State::S0, 1);
552 return RTLIL::const_sub(zero, arg1_ext, false, signed1, result_len);
553 }
554