Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SE: Add BitXorOperation #7246

Merged
merged 9 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal sealed partial class Binary
BinaryOperatorKind.And => NumberConstraint.From(CalculateAndMin(left, right), CalculateAndMax(left, right)),
BinaryOperatorKind.Or when left.IsSingleValue && right.IsSingleValue => NumberConstraint.From(left.Min.Value | right.Min.Value),
BinaryOperatorKind.Or => NumberConstraint.From(CalculateOrMin(left, right), CalculateOrMax(left, right)),
BinaryOperatorKind.ExclusiveOr when left.IsSingleValue && right.IsSingleValue => NumberConstraint.From(left.Min.Value ^ right.Min.Value),
BinaryOperatorKind.ExclusiveOr => NumberConstraint.From(CalculateXorMin(left, right), CalculateXorMax(left, right)),
_ => null
};

Expand Down Expand Up @@ -213,6 +215,62 @@ private static NumberConstraint AccountForZero(NumberConstraint constraint)
}
}

private static BigInteger? CalculateXorMin(NumberConstraint left, NumberConstraint right)
{
// Takes advantage of the property a - b <= a ^ b for all a >= 0 and b >= 0
// If ranges overlap => at least 1 value belongs to both ranges => xor can yield 0
if (left.IsPositive && right.IsPositive)
{
if (left.Min > right.Max)
{
return left.Min.Value - right.Max.Value;
}
else if (right.Min > left.Max)
{
return right.Min.Value - left.Max.Value;
}
else
{
return 0;
}
}
else if (left.IsNegative && right.IsNegative)
{
if (right.Min > left.Max)
{
return right.Min.Value - left.Max.Value;
}
else if (left.Min > right.Max)
{
return left.Min.Value - right.Max.Value;
}
else
{
return 0;
}
}
else
{
return null;
}
}

private static BigInteger? CalculateXorMax(NumberConstraint left, NumberConstraint right)
{
if ((left.IsPositive && right.CanBePositive) || (right.IsPositive && left.CanBeNegative))
{
return left.Max.HasValue && right.Max.HasValue ? PositiveMagnitude(left.Max.Value | right.Max.Value) : null;
}
else if ((left.IsPositive && right.IsNegative) || (left.IsNegative && right.IsPositive))
{
return -1;
}
else
{
return null;
}
}

private static BigInteger? NegativeMagnitude(BigInteger value)
{
// For increasing powers of 2 with negative sign, we're looking for the longest chain of 1 from the MSB side
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public void Binary_BitAnd_SingleValue(int left, int right, int expected)
var value = left & right;
Tag("Value", value);
""";
SETestContext.CreateCS(code).Validator.ValidateTag("Value", x => x.Should().HaveOnlyConstraints(ObjectConstraint.NotNull, NumberConstraint.From(expected)));
SETestContext.CreateCS(code).Validator.TagValue("Value").Should().HaveOnlyConstraints(ObjectConstraint.NotNull, NumberConstraint.From(expected));
}

[DataTestMethod]
Expand Down Expand Up @@ -1029,4 +1029,78 @@ public void Binary_BitOr_Range(string expression, int? expectedMin, int? expecte
SETestContext.CreateCS(code, "int i, int j").Validator.TagValue("Value").Should().HaveOnlyConstraints(ObjectConstraint.NotNull);
}
}

[DataTestMethod]
[DataRow(0b0000, 0b0000, 0b0000)]
[DataRow(0b0101, 0b0101, 0b0000)]
[DataRow(0b0101, 0b0001, 0b0100)]
[DataRow(0b1010, 0b0110, 0b1100)]
[DataRow(0b1010, 0b0000, 0b1010)]
[DataRow(0b1111, 0b1111, 0b0000)]
[DataRow(5, -5, -2)]
[DataRow(5, -4, -7)]
[DataRow(-5, -5, 0)]
[DataRow(-5, -4, 7)]
public void Binary_BitXor_SingleValue(int left, int right, int expected)
{
var code = $"""
var left = {left};
var right = {right};
var value = left ^ right;
Tag("Value", value);
""";
SETestContext.CreateCS(code).Validator.TagValue("Value").Should().HaveOnlyConstraints(ObjectConstraint.NotNull, NumberConstraint.From(expected));
}

[DataTestMethod]
[DataRow("i >= 4 && j >= 6", 0, null)]
[DataRow("i >= 4 && j >= -6", null, null)]
[DataRow("i >= 4 && j <= 6", null, null)]
[DataRow("i >= 4 && j <= -6", null, -1)]
[DataRow("i >= -4 && j >= 6", null, null)]
[DataRow("i >= -4 && j >= -6", null, null)]
[DataRow("i >= -4 && j <= 6", null, null)]
[DataRow("i >= -4 && j <= -6", null, null)] // exact range: null, -1
[DataRow("i == 4 && j >= 6", 2, null)]
[DataRow("i == 4 && j >= -6", null, null)] // exact range: -8, null
[DataRow("i == 4 && j <= 6", null, 7)]
[DataRow("i == 4 && j <= -6", null, -1)] // exact range: null, -2
[DataRow("i == -4 && j >= 6", null, null)] // exact range: null, -5
[DataRow("i == -4 && j >= -6", null, null)] // exact range: null, 7
[DataRow("i == -4 && j <= 6", null, null)] // exact range: -8, null
[DataRow("i == -4 && j <= -6", 2, null)] // exact range: 4, null
[DataRow("i <= 4 && j >= 6", null, null)]
[DataRow("i <= 4 && j >= -6", null, null)]
[DataRow("i <= 4 && j <= 6", null, null)]
[DataRow("i <= 4 && j <= -6", null, null)]
[DataRow("i <= -4 && j >= 6", null, null)] // exact range: null, -1
[DataRow("i <= -4 && j >= -6", null, null)]
[DataRow("i <= -4 && j <= 6", null, null)]
[DataRow("i <= -4 && j <= -6", 0, null)]
[DataRow("i >= 4 && j >= 6 && j <= 8", 0, null)]
[DataRow("i >= 4 && j >= 1 && j <= 3", 1, null)] // exact range: 4, null
[DataRow("i >= 4 && i <= 6 && j >= 6", 0, null)]
[DataRow("i >= 4 && i <= 6 && j >= 6 && j <= 8", 0, 15)] // exact range: 0, 14
[DataRow("i >= 4 && i <= 5 && j >= 6 && j <= 8", 1, 15)] // exact range: 2, 13
[DataRow("i >= -3 && i <= -1 && j >= -4 && j <=-2", 0, null)] // exact range: 0, 3
[DataRow("i >= -4 && i <= -3 && j >= -2 && j <=-1", 1, null)] // exact range: 2, 3
public void Binary_BitXor_Range(string expression, int? expectedMin, int? expectedMax)
{
var code = $$"""
if ({{expression}})
{
var value = i ^ j;
Tag("Value", value);
}
""";

if (expectedMin is not null || expectedMax is not null)
{
SETestContext.CreateCS(code, "int i, int j").Validator.TagValue("Value").Should().HaveOnlyConstraints(ObjectConstraint.NotNull, NumberConstraint.From(expectedMin, expectedMax));
}
else
{
SETestContext.CreateCS(code, "int i, int j").Validator.TagValue("Value").Should().HaveOnlyConstraints(ObjectConstraint.NotNull);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void BasicOperators()
_ = i * 2147483600; // Noncompliant

i = 2 ^ j;
_ = i * 2147483600; // FN
_ = i * 2147483600; // Noncompliant

i = 2 % j;
_ = i * 2147483600; // FN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Public Class Sample
__ = i * 2147483600 ' Noncompliant

i = 2 Xor j
__ = i * 2147483600 ' FN
__ = i * 2147483600 ' Noncompliant

i = 2 Mod j
__ = i * 2147483600 ' FN
Expand Down