diff --git a/src/FastExpressionCompiler/FastExpressionCompiler.cs b/src/FastExpressionCompiler/FastExpressionCompiler.cs index 776f9e2cf..60af28dd1 100644 --- a/src/FastExpressionCompiler/FastExpressionCompiler.cs +++ b/src/FastExpressionCompiler/FastExpressionCompiler.cs @@ -6970,7 +6970,7 @@ private static bool TryEmitArithmeticOperation(Type leftType, Type rightType, Ex ExpressionType.SubtractChecked => exprType.IsUnsigned() ? OpCodes.Sub_Ovf_Un : OpCodes.Sub_Ovf, ExpressionType.Multiply => OpCodes.Mul, ExpressionType.MultiplyChecked => exprType.IsUnsigned() ? OpCodes.Mul_Ovf_Un : OpCodes.Mul_Ovf, - ExpressionType.Divide => OpCodes.Div, + ExpressionType.Divide => exprType.IsUnsigned() ? OpCodes.Div_Un : OpCodes.Div, ExpressionType.Modulo => exprType.IsUnsigned() ? OpCodes.Rem_Un : OpCodes.Rem, ExpressionType.And => OpCodes.And, ExpressionType.Or => OpCodes.Or, diff --git a/test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/README.md b/test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/README.md new file mode 100644 index 000000000..0a3f41232 --- /dev/null +++ b/test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/README.md @@ -0,0 +1,34 @@ +# Use unsigned division for uint and ulong expressions + +Emit div.un instead of div for unsigned arithmetic. Values above the signed range currently produce incorrect quotients or overflow. The existing arithmetic dispatch and closure layout are unchanged. + +One production line. Three regression methods per expression flavor cover uint/ulong, signed-boundary values, divide-by-zero, nullable operands, and compound assignment. + +Independent extraction from #558, based directly on `dadhi/master` `6702eab6`. It requires neither #558 nor either of the other standalone fixes. All six merge orders of the three standalone fixes were checked without conflicts. #558 is unchanged. + +## Validation + +Focused regressions: **6 methods passed per runtime**, covering Standard and LightExpression on .NET 8, .NET 10, and .NET Framework 4.7.2. Loading the untouched master compiler with the same regression assembly reproduces the failure on .NET 8. All three Release solution builds pass with zero warnings/errors. + +Full .NET 8/.NET 10 native runners were also executed. Each reports the same existing failure in `LightExpressionTests.Binary_operators_with_methods_and_nullable_operands_are_the_same_as_system` (two nullable MultiplyChecked rows produce OverflowException). The identical failure reproduces on untouched master; no new full-run failures were observed. No tests are skipped or weakened to obtain this result. + +## Performance and allocations + +Windows ARM64, Release, SDK 10.0.301. Before and after compiler assemblies are loaded in separate AssemblyLoadContexts in the same process. Nine alternating-order samples per phase, common operation counts, approximately 35 ms per side, warmed delegates, tiering disabled, and the process pinned to one available logical CPU. GC runs outside samples and a no-GC region isolates allocation throughput. Compilation reuses prebuilt expression trees; execution reuses compiled delegates. This measures CompileFast and delegate invocation, not tree construction. Ratios for previously incorrect/invalid cases are intentionally not claimed. + +Allocation counts are unchanged on every shared workload measured, for both compilation and invocation. The raw timing samples and all unchanged controls are retained: small timing differences and code-placement/host variation do not establish universal throughput parity. These are correctness fixes with no new runtime helper calls or storage allocations; no broad performance improvement is claimed. + +| Runtime / flavor / workload | Compile ns, master → fix | Invoke ns, master → fix | Compile B/op | Invoke B/op | +| --- | ---: | ---: | ---: | ---: | +| net8.0 / Standard / uint_divide | 1131.1 → 1170.4 | 2.10 → 1.89 | 880 → 880 | 0 → 0 | +| net8.0 / Standard / ulong_divide | 1317.4 → 1287.6 | 2.63 → 2.26 | 880 → 880 | 0 → 0 | +| net8.0 / Light / uint_divide | 1108.9 → 1091.1 | 2.16 → 1.90 | 880 → 880 | 0 → 0 | +| net8.0 / Light / ulong_divide | 1028.7 → 1119.7 | 1.78 → 1.63 | 880 → 880 | 0 → 0 | +| net10.0 / Standard / uint_divide | 1244.6 → 1207.0 | 2.18 → 1.93 | 880 → 880 | 0 → 0 | +| net10.0 / Standard / ulong_divide | 1336.8 → 1241.1 | 1.87 → 1.56 | 880 → 880 | 0 → 0 | +| net10.0 / Light / uint_divide | 1085.7 → 1034.5 | 2.02 → 1.86 | 880 → 880 | 0 → 0 | +| net10.0 / Light / ulong_divide | 1142.4 → 1061.5 | 1.92 → 1.55 | 880 → 880 | 0 → 0 | + +No affected row has a paired-sample bootstrap interval wholly above parity. These descriptive intervals, stored with the raw data, are based on nine temporal samples and are not a universal performance guarantee. + +Raw samples, harness sources, source hashes and validation logs are committed in `test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/evidence.zip`. Earlier separate-process timing attempts had unstable unchanged controls; their raw results are retained as exploratory evidence and are not the final paired comparison. diff --git a/test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/evidence.zip b/test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/evidence.zip new file mode 100644 index 000000000..d4125b1e5 Binary files /dev/null and b/test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/evidence.zip differ diff --git a/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs b/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs index ae61cdc2c..7521b155a 100644 --- a/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs +++ b/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs @@ -123,7 +123,9 @@ void Run(Func run, string name = null) var unitTests = Task.Run(() => { + Run(new UnsignedDivisionRegressionTests().Run); Run(new ArithmeticOperationsTests().Run); + Run(new LightExpression.UnitTests.UnsignedDivisionRegressionTests().Run); Run(new LightExpression.UnitTests.ArithmeticOperationsTests().Run); Run(new AssignTests().Run); Run(new LightExpression.UnitTests.AssignTests().Run); diff --git a/test/FastExpressionCompiler.TestsRunner/Program.cs b/test/FastExpressionCompiler.TestsRunner/Program.cs index 0f60c49fd..be31c1f28 100644 --- a/test/FastExpressionCompiler.TestsRunner/Program.cs +++ b/test/FastExpressionCompiler.TestsRunner/Program.cs @@ -119,7 +119,9 @@ void Run(Func run, string name = null) var unitTests = Task.Run(() => { + Run(new UnsignedDivisionRegressionTests().Run); Run(new ArithmeticOperationsTests().Run); + Run(new LightExpression.UnitTests.UnsignedDivisionRegressionTests().Run); Run(new LightExpression.UnitTests.ArithmeticOperationsTests().Run); Run(new AssignTests().Run); Run(new LightExpression.UnitTests.AssignTests().Run); diff --git a/test/FastExpressionCompiler.UnitTests/UnsignedDivisionRegressionTests.cs b/test/FastExpressionCompiler.UnitTests/UnsignedDivisionRegressionTests.cs new file mode 100644 index 000000000..799d16de2 --- /dev/null +++ b/test/FastExpressionCompiler.UnitTests/UnsignedDivisionRegressionTests.cs @@ -0,0 +1,56 @@ +using System; +using System.Reflection; +using SysExpr = System.Linq.Expressions.Expression; + +#if LIGHT_EXPRESSION +namespace FastExpressionCompiler.LightExpression.UnitTests; +#else +namespace FastExpressionCompiler.UnitTests; +#endif + +public struct UnsignedDivisionRegressionTests : ITest +{ + public int Run() + { + Unsigned_division_uses_unsigned_operands(); + Lifted_unsigned_division_uses_unsigned_operands(); + Unsigned_division_assignment_uses_unsigned_operands(); + return 3; + } + + public void Unsigned_division_uses_unsigned_operands() => Check(false, false); + public void Lifted_unsigned_division_uses_unsigned_operands() => Check(true, false); + public void Unsigned_division_assignment_uses_unsigned_operands() => Check(false, true); + + private static void Check(bool nullable, bool assign) + { + Type[] types = { typeof(uint), typeof(ulong) }; + object[][] values = { new object[] { 0u, 1u, 2u, (uint)int.MaxValue + 1, uint.MaxValue }, + new object[] { 0UL, 1UL, 2UL, (ulong)long.MaxValue + 1, ulong.MaxValue } }; + for (var i = 0; i < types.Length; ++i) + { + var type = nullable ? typeof(Nullable<>).MakeGenericType(types[i]) : types[i]; + var left = SysExpr.Parameter(type); + var right = SysExpr.Parameter(type); + var body = assign ? SysExpr.DivideAssign(left, right) : SysExpr.Divide(left, right); + var expression = SysExpr.Lambda(body, left, right); + var system = expression.Compile(); + var fast = expression.FromSysExpression().CompileFast(true); + Asserts.IsNotNull(fast); + foreach (var a in values[i]) + foreach (var b in values[i]) Asserts.AreEqual(Invoke(system, a, b), Invoke(fast, a, b)); + if (nullable) + foreach (var value in values[i]) + { + Asserts.AreEqual(Invoke(system, value, null), Invoke(fast, value, null)); + Asserts.AreEqual(Invoke(system, null, value), Invoke(fast, null, value)); + } + } + } + + private static object Invoke(Delegate function, object a, object b) + { + try { return function.DynamicInvoke(new[] { a, b }); } + catch (TargetInvocationException e) { return e.InnerException.GetType(); } + } +}