val[len] = mask0 + mask1 + carry;
len++;
if (overflow)
- *overflow = false;
+ *overflow = (sgn == UNSIGNED && carry);
}
else if (overflow)
{
val[len] = mask0 - mask1 - borrow;
len++;
if (overflow)
- *overflow = false;
+ *overflow = (sgn == UNSIGNED && borrow);
}
else if (overflow)
{
test_comparisons <VALUE_TYPE> ();
}
+/* Test overflow conditions. */
+
+static void
+test_overflow ()
+{
+ static int precs[] = { 31, 32, 33, 63, 64, 65, 127, 128 };
+ static int offsets[] = { 16, 1, 0 };
+ for (unsigned int i = 0; i < ARRAY_SIZE (precs); ++i)
+ for (unsigned int j = 0; j < ARRAY_SIZE (offsets); ++j)
+ {
+ int prec = precs[i];
+ int offset = offsets[j];
+ bool overflow;
+ wide_int sum, diff;
+
+ sum = wi::add (wi::max_value (prec, UNSIGNED) - offset, 1,
+ UNSIGNED, &overflow);
+ ASSERT_EQ (sum, -offset);
+ ASSERT_EQ (overflow, offset == 0);
+
+ sum = wi::add (1, wi::max_value (prec, UNSIGNED) - offset,
+ UNSIGNED, &overflow);
+ ASSERT_EQ (sum, -offset);
+ ASSERT_EQ (overflow, offset == 0);
+
+ diff = wi::sub (wi::max_value (prec, UNSIGNED) - offset,
+ wi::max_value (prec, UNSIGNED),
+ UNSIGNED, &overflow);
+ ASSERT_EQ (diff, -offset);
+ ASSERT_EQ (overflow, offset != 0);
+
+ diff = wi::sub (wi::max_value (prec, UNSIGNED) - offset,
+ wi::max_value (prec, UNSIGNED) - 1,
+ UNSIGNED, &overflow);
+ ASSERT_EQ (diff, 1 - offset);
+ ASSERT_EQ (overflow, offset > 1);
+ }
+}
+
/* Run all of the selftests within this file, for all value types. */
void
wide_int_cc_tests ()
{
- run_all_wide_int_tests <wide_int> ();
- run_all_wide_int_tests <offset_int> ();
- run_all_wide_int_tests <widest_int> ();
+ run_all_wide_int_tests <wide_int> ();
+ run_all_wide_int_tests <offset_int> ();
+ run_all_wide_int_tests <widest_int> ();
+ test_overflow ();
}
} // namespace selftest