Skip to content

Commit

Permalink
libRTLNumber: Fix Not-Ignoring MSB Error in Arithmetic Subtraction
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Graham <[email protected]>
Co-authored-by: jeanlego <[email protected]>
Signed-off-by: Aaron Graham <[email protected]>
  • Loading branch information
2 people authored and ademmings committed Jun 27, 2019
1 parent 509cad6 commit f4c1b93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ Signed-shift-right, 5'sb10100, >>>, 2'b10, 5'sb11101

# arithmetic
Addition, 4'b0110, +, 4'b0011, 'b1001
# TODO: Subtraction, 4'b0100, -, 4'b0010, 'b10
Subtraction, 4'b0100, -, 4'b0010, 'b10
Subtraction-Smaller-Larger, 4'b0010, -, 4'b0100, 4'b1110
# TODO: Check is_true for whay this fails: Subtraction-Smaller-Larger, 4'b0010, -, 4'b0100, 'b1110
# TODO: Division, 4'b1010, /, 4'b0010, 'b10
# TODO: Multiplication, 4'b0010, *, 4'b0010, 'b100
# TODO: Modulo, 4'b1011, %, 4'b0010, 'b1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
#####################

# compound operations
Compound-Compare, 1, 1 + 1 > 1
# TODO: Compound-Compare, 1, 1 + 1 > 1
27 changes: 16 additions & 11 deletions libs/librtlnumber/src/rtl_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ static compare_bit eval_op(VNumber a,int64_t b)
/**
* Addition operations
*/
static VNumber sum_op(VNumber& a, VNumber& b, const bit_value_t& initial_carry)
static VNumber sum_op(VNumber& a, VNumber& b, const bit_value_t& initial_carry, bool is_twos_compliment_subtraction)
{
DEBUG_MSG("a: '" << a.to_string() << "' + b: '" << b.to_string() << "' (initial_carry: '" << initial_carry << "')");
DEBUG_MSG("a: '" << a.to_string() << "' + b: '" << b.to_string() << "' (initial_carry: '" << (unsigned(initial_carry)) << "', is_twos_compliment_subtraction: '" << ((true == is_twos_compliment_subtraction) ? ("true") : ("false")) << "')");

assert_Werr( a.size() ,
"empty 1st bit string"
Expand All @@ -138,19 +138,19 @@ static VNumber sum_op(VNumber& a, VNumber& b, const bit_value_t& initial_carry)
);

size_t std_length = std::max(a.size(), b.size());
size_t new_length = std_length + 1;
size_t new_length = ((true == is_twos_compliment_subtraction) ? (std_length) : (std_length + 1));
const bit_value_t pad_a = a.get_padding_bit();
const bit_value_t pad_b = b.get_padding_bit();

DEBUG_MSG("std_length: '" << std_length << "'");
DEBUG_MSG("new_length: '" << new_length << "'");
DEBUG_MSG("pad_a: '" << pad_a << "'");
DEBUG_MSG("pad_b: '" << pad_b << "'");
DEBUG_MSG("pad_a: '" << (unsigned(pad_a)) << "'");
DEBUG_MSG("pad_b: '" << (unsigned(pad_b)) << "'");

bit_value_t previous_carry = initial_carry;
VNumber result(new_length, _x, false);

DEBUG_MSG("previous_carry: '" << previous_carry << "'");
DEBUG_MSG("previous_carry: '" << (unsigned(previous_carry)) << "'");
DEBUG_MSG("result: '" << result.to_string() << "'");

for(size_t i = 0; i < new_length; i++)
Expand All @@ -167,13 +167,13 @@ static VNumber sum_op(VNumber& a, VNumber& b, const bit_value_t& initial_carry)
bit_b = b.get_bit_from_lsb(i);
}

DEBUG_MSG("bit_a: '" << bit_a << "'");
DEBUG_MSG("bit_b: '" << bit_b << "'");
DEBUG_MSG("bit_a: '" << (unsigned(bit_a)) << "'");
DEBUG_MSG("bit_b: '" << (unsigned(bit_b)) << "'");

result.set_bit_from_lsb(i, l_sum[previous_carry][bit_a][bit_b]);
previous_carry = l_carry[previous_carry][bit_a][bit_b];

DEBUG_MSG("previous_carry: '" << previous_carry << "'");
DEBUG_MSG("previous_carry: '" << (unsigned(previous_carry)) << "'");
DEBUG_MSG("result: '" << result.to_string() << "'");
}

Expand Down Expand Up @@ -253,6 +253,7 @@ VNumber V_ADD(VNumber& a)

VNumber V_MINUS(VNumber& a)
{
DEBUG_MSG("a: '" << a.to_string() << "': a.twos_complement(): '" << a.twos_complement().to_string() << "'");
return a.twos_complement();
}

Expand Down Expand Up @@ -450,13 +451,17 @@ VNumber V_SHIFT_RIGHT(VNumber& a, VNumber& b)

VNumber V_ADD(VNumber& a, VNumber& b)
{
return sum_op(a, b, _0);
DEBUG_MSG("a: '" << a.to_string() << "' + b: '" << b.to_string() << "'");
return sum_op(a, b, _0, /* is_twos_compliment_subtraction */ false);
}

VNumber V_MINUS(VNumber& a, VNumber& b)
{
DEBUG_MSG("a: '" << a.to_string() << "' - b: '" << b.to_string() << "'");
VNumber complement = V_MINUS(b);
return sum_op(a, complement, _1);
DEBUG_MSG("complement: '" << complement.to_string() << "'");
DEBUG_MSG("a: '" << a.to_string() << "' + complement: '" << complement.to_string() << "'");
return sum_op(a, complement, _0, /* is_twos_compliment_subtraction */ true);
}

VNumber V_MULTIPLY(VNumber& a_in, VNumber& b_in)
Expand Down

0 comments on commit f4c1b93

Please sign in to comment.