41179d045cdbf1442ad1f31e818124d9ced00705
2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
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
24 #include "kernel/yosys.h"
25 #include "libs/bigint/BigIntegerLibrary.hh"
29 static void extend_u0(RTLIL::Const
&arg
, int width
, bool is_signed
)
31 RTLIL::State padding
= RTLIL::State::S0
;
33 if (arg
.bits
.size() > 0 && is_signed
)
34 padding
= arg
.bits
.back();
36 while (int(arg
.bits
.size()) < width
)
37 arg
.bits
.push_back(padding
);
39 arg
.bits
.resize(width
);
42 static BigInteger
const2big(const RTLIL::Const
&val
, bool as_signed
, int &undef_bit_pos
)
44 BigInteger result
= 0, this_bit
= 1;
45 for (size_t i
= 0; i
< val
.bits
.size(); i
++) {
46 if (val
.bits
[i
] == RTLIL::State::S1
) {
47 if (as_signed
&& i
+1 == val
.bits
.size())
52 else if (val
.bits
[i
] != RTLIL::State::S0
) {
53 if (undef_bit_pos
< 0)
61 static RTLIL::Const
big2const(const BigInteger
&val
, int result_len
, int undef_bit_pos
)
63 if (undef_bit_pos
>= 0)
64 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
66 BigUnsigned mag
= val
.getMagnitude();
67 RTLIL::Const
result(0, result_len
);
71 if (val
.getSign() < 0)
74 for (int i
= 0; i
< result_len
; i
++)
75 result
.bits
[i
] = mag
.getBit(i
) ? RTLIL::State::S0
: RTLIL::State::S1
;
79 for (int i
= 0; i
< result_len
; i
++)
80 result
.bits
[i
] = mag
.getBit(i
) ? RTLIL::State::S1
: RTLIL::State::S0
;
85 if (undef_bit_pos
>= 0)
86 for (int i
= undef_bit_pos
; i
< result_len
; i
++)
87 result
.bits
[i
] = RTLIL::State::Sx
;
93 static RTLIL::State
logic_and(RTLIL::State a
, RTLIL::State b
)
95 if (a
== RTLIL::State::S0
) return RTLIL::State::S0
;
96 if (b
== RTLIL::State::S0
) return RTLIL::State::S0
;
97 if (a
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
98 if (b
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
99 return RTLIL::State::S1
;
102 static RTLIL::State
logic_or(RTLIL::State a
, RTLIL::State b
)
104 if (a
== RTLIL::State::S1
) return RTLIL::State::S1
;
105 if (b
== RTLIL::State::S1
) return RTLIL::State::S1
;
106 if (a
!= RTLIL::State::S0
) return RTLIL::State::Sx
;
107 if (b
!= RTLIL::State::S0
) return RTLIL::State::Sx
;
108 return RTLIL::State::S0
;
111 static RTLIL::State
logic_xor(RTLIL::State a
, RTLIL::State b
)
113 if (a
!= RTLIL::State::S0
&& a
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
114 if (b
!= RTLIL::State::S0
&& b
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
115 return a
!= b
? RTLIL::State::S1
: RTLIL::State::S0
;
118 static RTLIL::State
logic_xnor(RTLIL::State a
, RTLIL::State b
)
120 if (a
!= RTLIL::State::S0
&& a
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
121 if (b
!= RTLIL::State::S0
&& b
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
122 return a
== b
? RTLIL::State::S1
: RTLIL::State::S0
;
125 RTLIL::Const
RTLIL::const_not(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
128 result_len
= arg1
.bits
.size();
130 RTLIL::Const arg1_ext
= arg1
;
131 extend_u0(arg1_ext
, result_len
, signed1
);
133 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
134 for (size_t i
= 0; i
< size_t(result_len
); i
++) {
135 if (i
>= arg1_ext
.bits
.size())
136 result
.bits
[i
] = RTLIL::State::S0
;
137 else if (arg1_ext
.bits
[i
] == RTLIL::State::S0
)
138 result
.bits
[i
] = RTLIL::State::S1
;
139 else if (arg1_ext
.bits
[i
] == RTLIL::State::S1
)
140 result
.bits
[i
] = RTLIL::State::S0
;
146 static RTLIL::Const
logic_wrapper(RTLIL::State(*logic_func
)(RTLIL::State
, RTLIL::State
),
147 RTLIL::Const arg1
, RTLIL::Const arg2
, bool signed1
, bool signed2
, int result_len
= -1)
150 result_len
= std::max(arg1
.bits
.size(), arg2
.bits
.size());
152 extend_u0(arg1
, result_len
, signed1
);
153 extend_u0(arg2
, result_len
, signed2
);
155 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
156 for (size_t i
= 0; i
< size_t(result_len
); i
++) {
157 RTLIL::State a
= i
< arg1
.bits
.size() ? arg1
.bits
[i
] : RTLIL::State::S0
;
158 RTLIL::State b
= i
< arg2
.bits
.size() ? arg2
.bits
[i
] : RTLIL::State::S0
;
159 result
.bits
[i
] = logic_func(a
, b
);
165 RTLIL::Const
RTLIL::const_and(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
167 return logic_wrapper(logic_and
, arg1
, arg2
, signed1
, signed2
, result_len
);
170 RTLIL::Const
RTLIL::const_or(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
172 return logic_wrapper(logic_or
, arg1
, arg2
, signed1
, signed2
, result_len
);
175 RTLIL::Const
RTLIL::const_xor(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
177 return logic_wrapper(logic_xor
, arg1
, arg2
, signed1
, signed2
, result_len
);
180 RTLIL::Const
RTLIL::const_xnor(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
182 return logic_wrapper(logic_xnor
, arg1
, arg2
, signed1
, signed2
, result_len
);
185 static RTLIL::Const
logic_reduce_wrapper(RTLIL::State initial
, RTLIL::State(*logic_func
)(RTLIL::State
, RTLIL::State
), const RTLIL::Const
&arg1
, int result_len
)
187 RTLIL::State temp
= initial
;
189 for (size_t i
= 0; i
< arg1
.bits
.size(); i
++)
190 temp
= logic_func(temp
, arg1
.bits
[i
]);
192 RTLIL::Const
result(temp
);
193 while (int(result
.bits
.size()) < result_len
)
194 result
.bits
.push_back(RTLIL::State::S0
);
198 RTLIL::Const
RTLIL::const_reduce_and(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
200 return logic_reduce_wrapper(RTLIL::State::S1
, logic_and
, arg1
, result_len
);
203 RTLIL::Const
RTLIL::const_reduce_or(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
205 return logic_reduce_wrapper(RTLIL::State::S0
, logic_or
, arg1
, result_len
);
208 RTLIL::Const
RTLIL::const_reduce_xor(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
210 return logic_reduce_wrapper(RTLIL::State::S0
, logic_xor
, arg1
, result_len
);
213 RTLIL::Const
RTLIL::const_reduce_xnor(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
215 RTLIL::Const buffer
= logic_reduce_wrapper(RTLIL::State::S0
, logic_xor
, arg1
, result_len
);
216 if (!buffer
.bits
.empty()) {
217 if (buffer
.bits
.front() == RTLIL::State::S0
)
218 buffer
.bits
.front() = RTLIL::State::S1
;
219 else if (buffer
.bits
.front() == RTLIL::State::S1
)
220 buffer
.bits
.front() = RTLIL::State::S0
;
225 RTLIL::Const
RTLIL::const_reduce_bool(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
227 return logic_reduce_wrapper(RTLIL::State::S0
, logic_or
, arg1
, result_len
);
230 RTLIL::Const
RTLIL::const_logic_not(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
232 int undef_bit_pos_a
= -1;
233 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos_a
);
234 RTLIL::Const
result(a
.isZero() ? undef_bit_pos_a
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S1
: RTLIL::State::S0
);
236 while (int(result
.bits
.size()) < result_len
)
237 result
.bits
.push_back(RTLIL::State::S0
);
241 RTLIL::Const
RTLIL::const_logic_and(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
243 int undef_bit_pos_a
= -1, undef_bit_pos_b
= -1;
244 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos_a
);
245 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos_b
);
247 RTLIL::State bit_a
= a
.isZero() ? undef_bit_pos_a
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
248 RTLIL::State bit_b
= b
.isZero() ? undef_bit_pos_b
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
249 RTLIL::Const
result(logic_and(bit_a
, bit_b
));
251 while (int(result
.bits
.size()) < result_len
)
252 result
.bits
.push_back(RTLIL::State::S0
);
256 RTLIL::Const
RTLIL::const_logic_or(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
258 int undef_bit_pos_a
= -1, undef_bit_pos_b
= -1;
259 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos_a
);
260 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos_b
);
262 RTLIL::State bit_a
= a
.isZero() ? undef_bit_pos_a
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
263 RTLIL::State bit_b
= b
.isZero() ? undef_bit_pos_b
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
264 RTLIL::Const
result(logic_or(bit_a
, bit_b
));
266 while (int(result
.bits
.size()) < result_len
)
267 result
.bits
.push_back(RTLIL::State::S0
);
271 static RTLIL::Const
const_shift_worker(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool sign_ext
, int direction
, int result_len
)
273 int undef_bit_pos
= -1;
274 BigInteger offset
= const2big(arg2
, false, undef_bit_pos
) * direction
;
277 result_len
= arg1
.bits
.size();
279 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
280 if (undef_bit_pos
>= 0)
283 for (int i
= 0; i
< result_len
; i
++) {
284 BigInteger pos
= BigInteger(i
) + offset
;
286 result
.bits
[i
] = RTLIL::State::S0
;
287 else if (pos
>= arg1
.bits
.size())
288 result
.bits
[i
] = sign_ext
? arg1
.bits
.back() : RTLIL::State::S0
;
290 result
.bits
[i
] = arg1
.bits
[pos
.toInt()];
296 RTLIL::Const
RTLIL::const_shl(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool, int result_len
)
298 RTLIL::Const arg1_ext
= arg1
;
299 extend_u0(arg1_ext
, result_len
, signed1
);
300 return const_shift_worker(arg1_ext
, arg2
, false, -1, result_len
);
303 RTLIL::Const
RTLIL::const_shr(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool, int result_len
)
305 RTLIL::Const arg1_ext
= arg1
;
306 extend_u0(arg1_ext
, std::max(result_len
, SIZE(arg1
)), signed1
);
307 return const_shift_worker(arg1_ext
, arg2
, false, +1, result_len
);
310 RTLIL::Const
RTLIL::const_sshl(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
313 return const_shl(arg1
, arg2
, signed1
, signed2
, result_len
);
314 return const_shift_worker(arg1
, arg2
, true, -1, result_len
);
317 RTLIL::Const
RTLIL::const_sshr(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
320 return const_shr(arg1
, arg2
, signed1
, signed2
, result_len
);
321 return const_shift_worker(arg1
, arg2
, true, +1, result_len
);
324 static RTLIL::Const
const_shift_shiftx(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool, bool signed2
, int result_len
, RTLIL::State other_bits
)
326 int undef_bit_pos
= -1;
327 BigInteger offset
= const2big(arg2
, signed2
, undef_bit_pos
);
330 result_len
= arg1
.bits
.size();
332 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
333 if (undef_bit_pos
>= 0)
336 for (int i
= 0; i
< result_len
; i
++) {
337 BigInteger pos
= BigInteger(i
) + offset
;
338 if (pos
< 0 || pos
>= arg1
.bits
.size())
339 result
.bits
[i
] = other_bits
;
341 result
.bits
[i
] = arg1
.bits
[pos
.toInt()];
347 RTLIL::Const
RTLIL::const_shift(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
349 return const_shift_shiftx(arg1
, arg2
, signed1
, signed2
, result_len
, RTLIL::State::S0
);
352 RTLIL::Const
RTLIL::const_shiftx(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
354 return const_shift_shiftx(arg1
, arg2
, signed1
, signed2
, result_len
, RTLIL::State::Sx
);
357 RTLIL::Const
RTLIL::const_lt(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
359 int undef_bit_pos
= -1;
360 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) < const2big(arg2
, signed2
, undef_bit_pos
);
361 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
363 while (int(result
.bits
.size()) < result_len
)
364 result
.bits
.push_back(RTLIL::State::S0
);
368 RTLIL::Const
RTLIL::const_le(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
370 int undef_bit_pos
= -1;
371 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) <= const2big(arg2
, signed2
, undef_bit_pos
);
372 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
374 while (int(result
.bits
.size()) < result_len
)
375 result
.bits
.push_back(RTLIL::State::S0
);
379 RTLIL::Const
RTLIL::const_eq(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
381 RTLIL::Const arg1_ext
= arg1
;
382 RTLIL::Const arg2_ext
= arg2
;
383 RTLIL::Const
result(RTLIL::State::S0
, result_len
);
385 int width
= std::max(arg1_ext
.bits
.size(), arg2_ext
.bits
.size());
386 extend_u0(arg1_ext
, width
, signed1
&& signed2
);
387 extend_u0(arg2_ext
, width
, signed1
&& signed2
);
389 RTLIL::State matched_status
= RTLIL::State::S1
;
390 for (size_t i
= 0; i
< arg1_ext
.bits
.size(); i
++) {
391 if (arg1_ext
.bits
.at(i
) == RTLIL::State::S0
&& arg2_ext
.bits
.at(i
) == RTLIL::State::S1
)
393 if (arg1_ext
.bits
.at(i
) == RTLIL::State::S1
&& arg2_ext
.bits
.at(i
) == RTLIL::State::S0
)
395 if (arg1_ext
.bits
.at(i
) > RTLIL::State::S1
|| arg2_ext
.bits
.at(i
) > RTLIL::State::S1
)
396 matched_status
= RTLIL::State::Sx
;
399 result
.bits
.front() = matched_status
;
403 RTLIL::Const
RTLIL::const_ne(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
405 RTLIL::Const result
= RTLIL::const_eq(arg1
, arg2
, signed1
, signed2
, result_len
);
406 if (result
.bits
.front() == RTLIL::State::S0
)
407 result
.bits
.front() = RTLIL::State::S1
;
408 else if (result
.bits
.front() == RTLIL::State::S1
)
409 result
.bits
.front() = RTLIL::State::S0
;
413 RTLIL::Const
RTLIL::const_eqx(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
415 RTLIL::Const arg1_ext
= arg1
;
416 RTLIL::Const arg2_ext
= arg2
;
417 RTLIL::Const
result(RTLIL::State::S0
, result_len
);
419 int width
= std::max(arg1_ext
.bits
.size(), arg2_ext
.bits
.size());
420 extend_u0(arg1_ext
, width
, signed1
&& signed2
);
421 extend_u0(arg2_ext
, width
, signed1
&& signed2
);
423 for (size_t i
= 0; i
< arg1_ext
.bits
.size(); i
++) {
424 if (arg1_ext
.bits
.at(i
) != arg2_ext
.bits
.at(i
))
428 result
.bits
.front() = RTLIL::State::S1
;
432 RTLIL::Const
RTLIL::const_nex(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
434 RTLIL::Const result
= RTLIL::const_eqx(arg1
, arg2
, signed1
, signed2
, result_len
);
435 if (result
.bits
.front() == RTLIL::State::S0
)
436 result
.bits
.front() = RTLIL::State::S1
;
437 else if (result
.bits
.front() == RTLIL::State::S1
)
438 result
.bits
.front() = RTLIL::State::S0
;
442 RTLIL::Const
RTLIL::const_ge(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
444 int undef_bit_pos
= -1;
445 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) >= const2big(arg2
, signed2
, undef_bit_pos
);
446 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
448 while (int(result
.bits
.size()) < result_len
)
449 result
.bits
.push_back(RTLIL::State::S0
);
453 RTLIL::Const
RTLIL::const_gt(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
455 int undef_bit_pos
= -1;
456 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) > const2big(arg2
, signed2
, undef_bit_pos
);
457 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
459 while (int(result
.bits
.size()) < result_len
)
460 result
.bits
.push_back(RTLIL::State::S0
);
464 RTLIL::Const
RTLIL::const_add(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
466 int undef_bit_pos
= -1;
467 BigInteger y
= const2big(arg1
, signed1
, undef_bit_pos
) + const2big(arg2
, signed2
, undef_bit_pos
);
468 return big2const(y
, result_len
>= 0 ? result_len
: std::max(arg1
.bits
.size(), arg2
.bits
.size()), undef_bit_pos
);
471 RTLIL::Const
RTLIL::const_sub(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
473 int undef_bit_pos
= -1;
474 BigInteger y
= const2big(arg1
, signed1
, undef_bit_pos
) - const2big(arg2
, signed2
, undef_bit_pos
);
475 return big2const(y
, result_len
>= 0 ? result_len
: std::max(arg1
.bits
.size(), arg2
.bits
.size()), undef_bit_pos
);
478 RTLIL::Const
RTLIL::const_mul(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
480 int undef_bit_pos
= -1;
481 BigInteger y
= const2big(arg1
, signed1
, undef_bit_pos
) * const2big(arg2
, signed2
, undef_bit_pos
);
482 return big2const(y
, result_len
>= 0 ? result_len
: std::max(arg1
.bits
.size(), arg2
.bits
.size()), std::min(undef_bit_pos
, 0));
485 RTLIL::Const
RTLIL::const_div(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
487 int undef_bit_pos
= -1;
488 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos
);
489 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos
);
491 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
492 bool result_neg
= (a
.getSign() == BigInteger::negative
) != (b
.getSign() == BigInteger::negative
);
493 a
= a
.getSign() == BigInteger::negative
? -a
: a
;
494 b
= b
.getSign() == BigInteger::negative
? -b
: b
;
495 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));
498 RTLIL::Const
RTLIL::const_mod(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
500 int undef_bit_pos
= -1;
501 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos
);
502 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos
);
504 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
505 bool result_neg
= a
.getSign() == BigInteger::negative
;
506 a
= a
.getSign() == BigInteger::negative
? -a
: a
;
507 b
= b
.getSign() == BigInteger::negative
? -b
: b
;
508 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));
511 RTLIL::Const
RTLIL::const_pow(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
513 int undef_bit_pos
= -1;
515 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos
);
516 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos
);
520 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
523 return RTLIL::Const(RTLIL::State::S0
, result_len
);
530 y
= (-b
% 2) == 0 ? 1 : -1;
535 // Power-modulo with 2^result_len as modulus
536 BigInteger modulus
= 1;
537 int modulus_bits
= (result_len
>= 0 ? result_len
: 1024);
538 for (int i
= 0; i
< modulus_bits
; i
++)
541 bool flip_result_sign
= false;
545 flip_result_sign
= true;
550 y
= (y
* a
) % modulus
;
552 a
= (a
* a
) % modulus
;
555 if (flip_result_sign
)
559 return big2const(y
, result_len
>= 0 ? result_len
: std::max(arg1
.bits
.size(), arg2
.bits
.size()), std::min(undef_bit_pos
, 0));
562 RTLIL::Const
RTLIL::const_pos(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
564 RTLIL::Const arg1_ext
= arg1
;
565 extend_u0(arg1_ext
, result_len
, signed1
);
570 RTLIL::Const
RTLIL::const_neg(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
572 RTLIL::Const arg1_ext
= arg1
;
573 RTLIL::Const
zero(RTLIL::State::S0
, 1);
575 return RTLIL::const_sub(zero
, arg1_ext
, true, signed1
, result_len
);