aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/calc.cc
diff options
context:
space:
mode:
authorXiretza <xiretza@xiretza.xyz>2020-04-21 12:51:58 +0200
committerXiretza <xiretza@xiretza.xyz>2020-05-28 22:59:04 +0200
commitedd8ff2c0778d97808869488cc7394151456c4ca (patch)
tree797418b87588ae7a69992b7f107dfd5cdfdec08d /kernel/calc.cc
parent17163cf43a6b6eec9aac44f6a4463dda54b8ed68 (diff)
downloadyosys-edd8ff2c0778d97808869488cc7394151456c4ca.tar.gz
yosys-edd8ff2c0778d97808869488cc7394151456c4ca.tar.bz2
yosys-edd8ff2c0778d97808869488cc7394151456c4ca.zip
Add flooring division operator
The $div and $mod cells use truncating division semantics (rounding towards 0), as defined by e.g. Verilog. Another rounding mode, flooring (rounding towards negative infinity), can be used in e.g. VHDL. The new $divfloor cell provides this flooring division. This commit also fixes the handling of $div in opt_expr, which was previously optimized as if it was $divfloor.
Diffstat (limited to 'kernel/calc.cc')
-rw-r--r--kernel/calc.cc22
1 files changed, 22 insertions, 0 deletions
diff --git a/kernel/calc.cc b/kernel/calc.cc
index 38a529128..ae18809d3 100644
--- a/kernel/calc.cc
+++ b/kernel/calc.cc
@@ -517,6 +517,28 @@ RTLIL::Const RTLIL::const_mod(const RTLIL::Const &arg1, const RTLIL::Const &arg2
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));
}
+RTLIL::Const RTLIL::const_divfloor(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
+{
+ int undef_bit_pos = -1;
+ BigInteger a = const2big(arg1, signed1, undef_bit_pos);
+ BigInteger b = const2big(arg2, signed2, undef_bit_pos);
+ if (b.isZero())
+ return RTLIL::Const(RTLIL::State::Sx, result_len);
+
+ bool result_pos = (a.getSign() == BigInteger::negative) == (b.getSign() == BigInteger::negative);
+ a = a.getSign() == BigInteger::negative ? -a : a;
+ b = b.getSign() == BigInteger::negative ? -b : b;
+ BigInteger result;
+
+ if (result_pos || a == 0) {
+ result = a / b;
+ } else {
+ // bigint division with negative numbers is wonky, make sure we only negate at the very end
+ result = -((a + b - 1) / b);
+ }
+ return big2const(result, result_len >= 0 ? result_len : max(arg1.bits.size(), arg2.bits.size()), min(undef_bit_pos, 0));
+}
+
RTLIL::Const RTLIL::const_modfloor(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
{
int undef_bit_pos = -1;