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
)
46 BigInteger::Sign sign
= BigInteger::positive
;
47 State inv_sign_bit
= RTLIL::State::S1
;
48 size_t num_bits
= val
.bits
.size();
50 if (as_signed
&& num_bits
&& val
.bits
[num_bits
-1] == RTLIL::State::S1
) {
51 inv_sign_bit
= RTLIL::State::S0
;
52 sign
= BigInteger::negative
;
56 for (size_t i
= 0; i
< num_bits
; i
++)
57 if (val
.bits
[i
] == RTLIL::State::S0
|| val
.bits
[i
] == RTLIL::State::S1
)
58 mag
.setBit(i
, val
.bits
[i
] == inv_sign_bit
);
59 else if (undef_bit_pos
< 0)
62 if (sign
== BigInteger::negative
)
65 return BigInteger(mag
, sign
);
68 static RTLIL::Const
big2const(const BigInteger
&val
, int result_len
, int undef_bit_pos
)
70 if (undef_bit_pos
>= 0)
71 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
73 BigUnsigned mag
= val
.getMagnitude();
74 RTLIL::Const
result(0, result_len
);
78 if (val
.getSign() < 0)
81 for (int i
= 0; i
< result_len
; i
++)
82 result
.bits
[i
] = mag
.getBit(i
) ? RTLIL::State::S0
: RTLIL::State::S1
;
86 for (int i
= 0; i
< result_len
; i
++)
87 result
.bits
[i
] = mag
.getBit(i
) ? RTLIL::State::S1
: RTLIL::State::S0
;
92 if (undef_bit_pos
>= 0)
93 for (int i
= undef_bit_pos
; i
< result_len
; i
++)
94 result
.bits
[i
] = RTLIL::State::Sx
;
100 static RTLIL::State
logic_and(RTLIL::State a
, RTLIL::State b
)
102 if (a
== RTLIL::State::S0
) return RTLIL::State::S0
;
103 if (b
== RTLIL::State::S0
) return RTLIL::State::S0
;
104 if (a
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
105 if (b
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
106 return RTLIL::State::S1
;
109 static RTLIL::State
logic_or(RTLIL::State a
, RTLIL::State b
)
111 if (a
== RTLIL::State::S1
) return RTLIL::State::S1
;
112 if (b
== RTLIL::State::S1
) return RTLIL::State::S1
;
113 if (a
!= RTLIL::State::S0
) return RTLIL::State::Sx
;
114 if (b
!= RTLIL::State::S0
) return RTLIL::State::Sx
;
115 return RTLIL::State::S0
;
118 static RTLIL::State
logic_xor(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 static RTLIL::State
logic_xnor(RTLIL::State a
, RTLIL::State b
)
127 if (a
!= RTLIL::State::S0
&& a
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
128 if (b
!= RTLIL::State::S0
&& b
!= RTLIL::State::S1
) return RTLIL::State::Sx
;
129 return a
== b
? RTLIL::State::S1
: RTLIL::State::S0
;
132 RTLIL::Const
RTLIL::const_not(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
135 result_len
= arg1
.bits
.size();
137 RTLIL::Const arg1_ext
= arg1
;
138 extend_u0(arg1_ext
, result_len
, signed1
);
140 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
141 for (size_t i
= 0; i
< size_t(result_len
); i
++) {
142 if (i
>= arg1_ext
.bits
.size())
143 result
.bits
[i
] = RTLIL::State::S0
;
144 else if (arg1_ext
.bits
[i
] == RTLIL::State::S0
)
145 result
.bits
[i
] = RTLIL::State::S1
;
146 else if (arg1_ext
.bits
[i
] == RTLIL::State::S1
)
147 result
.bits
[i
] = RTLIL::State::S0
;
153 static RTLIL::Const
logic_wrapper(RTLIL::State(*logic_func
)(RTLIL::State
, RTLIL::State
),
154 RTLIL::Const arg1
, RTLIL::Const arg2
, bool signed1
, bool signed2
, int result_len
= -1)
157 result_len
= max(arg1
.bits
.size(), arg2
.bits
.size());
159 extend_u0(arg1
, result_len
, signed1
);
160 extend_u0(arg2
, result_len
, signed2
);
162 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
163 for (size_t i
= 0; i
< size_t(result_len
); i
++) {
164 RTLIL::State a
= i
< arg1
.bits
.size() ? arg1
.bits
[i
] : RTLIL::State::S0
;
165 RTLIL::State b
= i
< arg2
.bits
.size() ? arg2
.bits
[i
] : RTLIL::State::S0
;
166 result
.bits
[i
] = logic_func(a
, b
);
172 RTLIL::Const
RTLIL::const_and(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
174 return logic_wrapper(logic_and
, arg1
, arg2
, signed1
, signed2
, result_len
);
177 RTLIL::Const
RTLIL::const_or(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
179 return logic_wrapper(logic_or
, arg1
, arg2
, signed1
, signed2
, result_len
);
182 RTLIL::Const
RTLIL::const_xor(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
184 return logic_wrapper(logic_xor
, arg1
, arg2
, signed1
, signed2
, result_len
);
187 RTLIL::Const
RTLIL::const_xnor(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
189 return logic_wrapper(logic_xnor
, arg1
, arg2
, signed1
, signed2
, result_len
);
192 static RTLIL::Const
logic_reduce_wrapper(RTLIL::State initial
, RTLIL::State(*logic_func
)(RTLIL::State
, RTLIL::State
), const RTLIL::Const
&arg1
, int result_len
)
194 RTLIL::State temp
= initial
;
196 for (size_t i
= 0; i
< arg1
.bits
.size(); i
++)
197 temp
= logic_func(temp
, arg1
.bits
[i
]);
199 RTLIL::Const
result(temp
);
200 while (int(result
.bits
.size()) < result_len
)
201 result
.bits
.push_back(RTLIL::State::S0
);
205 RTLIL::Const
RTLIL::const_reduce_and(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
207 return logic_reduce_wrapper(RTLIL::State::S1
, logic_and
, arg1
, result_len
);
210 RTLIL::Const
RTLIL::const_reduce_or(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
212 return logic_reduce_wrapper(RTLIL::State::S0
, logic_or
, arg1
, result_len
);
215 RTLIL::Const
RTLIL::const_reduce_xor(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
217 return logic_reduce_wrapper(RTLIL::State::S0
, logic_xor
, arg1
, result_len
);
220 RTLIL::Const
RTLIL::const_reduce_xnor(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
222 RTLIL::Const buffer
= logic_reduce_wrapper(RTLIL::State::S0
, logic_xor
, arg1
, result_len
);
223 if (!buffer
.bits
.empty()) {
224 if (buffer
.bits
.front() == RTLIL::State::S0
)
225 buffer
.bits
.front() = RTLIL::State::S1
;
226 else if (buffer
.bits
.front() == RTLIL::State::S1
)
227 buffer
.bits
.front() = RTLIL::State::S0
;
232 RTLIL::Const
RTLIL::const_reduce_bool(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool, bool, int result_len
)
234 return logic_reduce_wrapper(RTLIL::State::S0
, logic_or
, arg1
, result_len
);
237 RTLIL::Const
RTLIL::const_logic_not(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
239 int undef_bit_pos_a
= -1;
240 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos_a
);
241 RTLIL::Const
result(a
.isZero() ? undef_bit_pos_a
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S1
: RTLIL::State::S0
);
243 while (int(result
.bits
.size()) < result_len
)
244 result
.bits
.push_back(RTLIL::State::S0
);
248 RTLIL::Const
RTLIL::const_logic_and(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
250 int undef_bit_pos_a
= -1, undef_bit_pos_b
= -1;
251 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos_a
);
252 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos_b
);
254 RTLIL::State bit_a
= a
.isZero() ? undef_bit_pos_a
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
255 RTLIL::State bit_b
= b
.isZero() ? undef_bit_pos_b
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
256 RTLIL::Const
result(logic_and(bit_a
, bit_b
));
258 while (int(result
.bits
.size()) < result_len
)
259 result
.bits
.push_back(RTLIL::State::S0
);
263 RTLIL::Const
RTLIL::const_logic_or(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
265 int undef_bit_pos_a
= -1, undef_bit_pos_b
= -1;
266 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos_a
);
267 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos_b
);
269 RTLIL::State bit_a
= a
.isZero() ? undef_bit_pos_a
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
270 RTLIL::State bit_b
= b
.isZero() ? undef_bit_pos_b
>= 0 ? RTLIL::State::Sx
: RTLIL::State::S0
: RTLIL::State::S1
;
271 RTLIL::Const
result(logic_or(bit_a
, bit_b
));
273 while (int(result
.bits
.size()) < result_len
)
274 result
.bits
.push_back(RTLIL::State::S0
);
278 static RTLIL::Const
const_shift_worker(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool sign_ext
, int direction
, int result_len
)
280 int undef_bit_pos
= -1;
281 BigInteger offset
= const2big(arg2
, false, undef_bit_pos
) * direction
;
284 result_len
= arg1
.bits
.size();
286 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
287 if (undef_bit_pos
>= 0)
290 for (int i
= 0; i
< result_len
; i
++) {
291 BigInteger pos
= BigInteger(i
) + offset
;
293 result
.bits
[i
] = RTLIL::State::S0
;
294 else if (pos
>= BigInteger(int(arg1
.bits
.size())))
295 result
.bits
[i
] = sign_ext
? arg1
.bits
.back() : RTLIL::State::S0
;
297 result
.bits
[i
] = arg1
.bits
[pos
.toInt()];
303 RTLIL::Const
RTLIL::const_shl(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
, result_len
, signed1
);
307 return const_shift_worker(arg1_ext
, arg2
, false, -1, result_len
);
310 RTLIL::Const
RTLIL::const_shr(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool, int result_len
)
312 RTLIL::Const arg1_ext
= arg1
;
313 extend_u0(arg1_ext
, max(result_len
, GetSize(arg1
)), signed1
);
314 return const_shift_worker(arg1_ext
, arg2
, false, +1, result_len
);
317 RTLIL::Const
RTLIL::const_sshl(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
320 return const_shl(arg1
, arg2
, signed1
, signed2
, result_len
);
321 return const_shift_worker(arg1
, arg2
, true, -1, result_len
);
324 RTLIL::Const
RTLIL::const_sshr(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
327 return const_shr(arg1
, arg2
, signed1
, signed2
, result_len
);
328 return const_shift_worker(arg1
, arg2
, true, +1, result_len
);
331 static RTLIL::Const
const_shift_shiftx(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool, bool signed2
, int result_len
, RTLIL::State other_bits
)
333 int undef_bit_pos
= -1;
334 BigInteger offset
= const2big(arg2
, signed2
, undef_bit_pos
);
337 result_len
= arg1
.bits
.size();
339 RTLIL::Const
result(RTLIL::State::Sx
, result_len
);
340 if (undef_bit_pos
>= 0)
343 for (int i
= 0; i
< result_len
; i
++) {
344 BigInteger pos
= BigInteger(i
) + offset
;
345 if (pos
< 0 || pos
>= BigInteger(int(arg1
.bits
.size())))
346 result
.bits
[i
] = other_bits
;
348 result
.bits
[i
] = arg1
.bits
[pos
.toInt()];
354 RTLIL::Const
RTLIL::const_shift(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
356 return const_shift_shiftx(arg1
, arg2
, signed1
, signed2
, result_len
, RTLIL::State::S0
);
359 RTLIL::Const
RTLIL::const_shiftx(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
361 return const_shift_shiftx(arg1
, arg2
, signed1
, signed2
, result_len
, RTLIL::State::Sx
);
364 RTLIL::Const
RTLIL::const_lt(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
366 int undef_bit_pos
= -1;
367 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) < const2big(arg2
, signed2
, undef_bit_pos
);
368 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
370 while (int(result
.bits
.size()) < result_len
)
371 result
.bits
.push_back(RTLIL::State::S0
);
375 RTLIL::Const
RTLIL::const_le(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
377 int undef_bit_pos
= -1;
378 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) <= const2big(arg2
, signed2
, undef_bit_pos
);
379 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
381 while (int(result
.bits
.size()) < result_len
)
382 result
.bits
.push_back(RTLIL::State::S0
);
386 RTLIL::Const
RTLIL::const_eq(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
388 RTLIL::Const arg1_ext
= arg1
;
389 RTLIL::Const arg2_ext
= arg2
;
390 RTLIL::Const
result(RTLIL::State::S0
, result_len
);
392 int width
= max(arg1_ext
.bits
.size(), arg2_ext
.bits
.size());
393 extend_u0(arg1_ext
, width
, signed1
&& signed2
);
394 extend_u0(arg2_ext
, width
, signed1
&& signed2
);
396 RTLIL::State matched_status
= RTLIL::State::S1
;
397 for (size_t i
= 0; i
< arg1_ext
.bits
.size(); i
++) {
398 if (arg1_ext
.bits
.at(i
) == RTLIL::State::S0
&& arg2_ext
.bits
.at(i
) == RTLIL::State::S1
)
400 if (arg1_ext
.bits
.at(i
) == RTLIL::State::S1
&& arg2_ext
.bits
.at(i
) == RTLIL::State::S0
)
402 if (arg1_ext
.bits
.at(i
) > RTLIL::State::S1
|| arg2_ext
.bits
.at(i
) > RTLIL::State::S1
)
403 matched_status
= RTLIL::State::Sx
;
406 result
.bits
.front() = matched_status
;
410 RTLIL::Const
RTLIL::const_ne(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
412 RTLIL::Const result
= RTLIL::const_eq(arg1
, arg2
, signed1
, signed2
, result_len
);
413 if (result
.bits
.front() == RTLIL::State::S0
)
414 result
.bits
.front() = RTLIL::State::S1
;
415 else if (result
.bits
.front() == RTLIL::State::S1
)
416 result
.bits
.front() = RTLIL::State::S0
;
420 RTLIL::Const
RTLIL::const_eqx(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
422 RTLIL::Const arg1_ext
= arg1
;
423 RTLIL::Const arg2_ext
= arg2
;
424 RTLIL::Const
result(RTLIL::State::S0
, result_len
);
426 int width
= max(arg1_ext
.bits
.size(), arg2_ext
.bits
.size());
427 extend_u0(arg1_ext
, width
, signed1
&& signed2
);
428 extend_u0(arg2_ext
, width
, signed1
&& signed2
);
430 for (size_t i
= 0; i
< arg1_ext
.bits
.size(); i
++) {
431 if (arg1_ext
.bits
.at(i
) != arg2_ext
.bits
.at(i
))
435 result
.bits
.front() = RTLIL::State::S1
;
439 RTLIL::Const
RTLIL::const_nex(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
441 RTLIL::Const result
= RTLIL::const_eqx(arg1
, arg2
, signed1
, signed2
, result_len
);
442 if (result
.bits
.front() == RTLIL::State::S0
)
443 result
.bits
.front() = RTLIL::State::S1
;
444 else if (result
.bits
.front() == RTLIL::State::S1
)
445 result
.bits
.front() = RTLIL::State::S0
;
449 RTLIL::Const
RTLIL::const_ge(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
451 int undef_bit_pos
= -1;
452 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) >= const2big(arg2
, signed2
, undef_bit_pos
);
453 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
455 while (int(result
.bits
.size()) < result_len
)
456 result
.bits
.push_back(RTLIL::State::S0
);
460 RTLIL::Const
RTLIL::const_gt(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
462 int undef_bit_pos
= -1;
463 bool y
= const2big(arg1
, signed1
, undef_bit_pos
) > const2big(arg2
, signed2
, undef_bit_pos
);
464 RTLIL::Const
result(undef_bit_pos
>= 0 ? RTLIL::State::Sx
: y
? RTLIL::State::S1
: RTLIL::State::S0
);
466 while (int(result
.bits
.size()) < result_len
)
467 result
.bits
.push_back(RTLIL::State::S0
);
471 RTLIL::Const
RTLIL::const_add(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
: max(arg1
.bits
.size(), arg2
.bits
.size()), undef_bit_pos
);
478 RTLIL::Const
RTLIL::const_sub(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
: max(arg1
.bits
.size(), arg2
.bits
.size()), undef_bit_pos
);
485 RTLIL::Const
RTLIL::const_mul(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
487 int undef_bit_pos
= -1;
488 BigInteger y
= const2big(arg1
, signed1
, undef_bit_pos
) * const2big(arg2
, signed2
, undef_bit_pos
);
489 return big2const(y
, result_len
>= 0 ? result_len
: max(arg1
.bits
.size(), arg2
.bits
.size()), min(undef_bit_pos
, 0));
492 RTLIL::Const
RTLIL::const_div(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
494 int undef_bit_pos
= -1;
495 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos
);
496 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos
);
498 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
499 bool result_neg
= (a
.getSign() == BigInteger::negative
) != (b
.getSign() == BigInteger::negative
);
500 a
= a
.getSign() == BigInteger::negative
? -a
: a
;
501 b
= b
.getSign() == BigInteger::negative
? -b
: b
;
502 return big2const(result_neg
? -(a
/ b
) : (a
/ b
), result_len
>= 0 ? result_len
: max(arg1
.bits
.size(), arg2
.bits
.size()), min(undef_bit_pos
, 0));
505 RTLIL::Const
RTLIL::const_mod(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
507 int undef_bit_pos
= -1;
508 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos
);
509 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos
);
511 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
512 bool result_neg
= a
.getSign() == BigInteger::negative
;
513 a
= a
.getSign() == BigInteger::negative
? -a
: a
;
514 b
= b
.getSign() == BigInteger::negative
? -b
: b
;
515 return big2const(result_neg
? -(a
% b
) : (a
% b
), result_len
>= 0 ? result_len
: max(arg1
.bits
.size(), arg2
.bits
.size()), min(undef_bit_pos
, 0));
518 RTLIL::Const
RTLIL::const_pow(const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
)
520 int undef_bit_pos
= -1;
522 BigInteger a
= const2big(arg1
, signed1
, undef_bit_pos
);
523 BigInteger b
= const2big(arg2
, signed2
, undef_bit_pos
);
527 return RTLIL::Const(RTLIL::State::Sx
, result_len
);
530 return RTLIL::Const(RTLIL::State::S0
, result_len
);
537 y
= (-b
% 2) == 0 ? 1 : -1;
542 // Power-modulo with 2^result_len as modulus
543 BigInteger modulus
= 1;
544 int modulus_bits
= (result_len
>= 0 ? result_len
: 1024);
545 for (int i
= 0; i
< modulus_bits
; i
++)
548 bool flip_result_sign
= false;
552 flip_result_sign
= true;
557 y
= (y
* a
) % modulus
;
559 a
= (a
* a
) % modulus
;
562 if (flip_result_sign
)
566 return big2const(y
, result_len
>= 0 ? result_len
: max(arg1
.bits
.size(), arg2
.bits
.size()), min(undef_bit_pos
, 0));
569 RTLIL::Const
RTLIL::const_pos(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
571 RTLIL::Const arg1_ext
= arg1
;
572 extend_u0(arg1_ext
, result_len
, signed1
);
577 RTLIL::Const
RTLIL::const_neg(const RTLIL::Const
&arg1
, const RTLIL::Const
&, bool signed1
, bool, int result_len
)
579 RTLIL::Const arg1_ext
= arg1
;
580 RTLIL::Const
zero(RTLIL::State::S0
, 1);
582 return RTLIL::const_sub(zero
, arg1_ext
, true, signed1
, result_len
);