System.Math, MathF and the floating-point members of float and double
CoreLib members served from the framework’s IL: their row of the per-library table of ADR 0023 (columns explained there, under “Measurements”).
| Library (switch) | Correctness (samples from IL) | Binary size, IL / shadow | Compile cpu, IL / shadow | Speed, IL / shadow (time) | Lines removed on a flip | Decision | NativeAOT on the same host, csharpc / NativeAOT (tools/il-first/measure-aot.sh: stripped size; compile cpu; run, median of 7; instructions) |
|---|---|---|---|---|---|---|---|
The C# floating-point members of float, double, Math and MathF (src/Csharpc/upstream-types/System.Math.txt, CoreLib: the slicer route, hosted in the types’ host classes; #423) |
273 samples that use floating-point math, conversions, boxing, IConvertible, enums, async code, strings and BigInteger byte-identical to real .NET at -O0 and -O2 (546 runs), 11 of them via IL and 9 on wasm32 (gate after merging the integration branch at d155238f); new 1444. On main 1444 does not compile (double.ConvertToInteger was missing) |
fp_math_throughput 244 320 to 267 216 B (stripped, both on the same base before the number flip; 317 312 B after merging it) |
5.4 to 6.4 s user | 1.566 to 0.506 s (3.1x faster; 0.69 s after the merge, on a loaded host); 18 603 M to 4 874 M instructions (callgrind) | 2 412 (most of corelib/FloatingPointMath.cs); 221 added |
Flipped (#423). Security: rounding with digits and MidpointRounding, ScaleB, ILogB, the saturating ConvertToInteger and the generic-math statics are CoreLib’s own, with its argument checks (ArithmeticException for Math.Sign(NaN), ArgumentOutOfRangeException for more than 15 digits (6 for float), ArgumentException for an undefined MidpointRounding) and its NaN and infinity handling. The libm-backed members (Sin, Pow, Sqrt…) stay csharpc’s intrinsics. JIT intrinsics whose IL only calls itself (ReciprocalEstimate, ReciprocalSqrtEstimate, MultiplyAddEstimate, SinCos; ConvertToIntegerNative for a primitive target) are lowered by the compiler (Intrinsics.cs, FunctionCodeGen.NativeConversions.cs: fdiv 1/x, 1/llvm.sqrt, llvm.fma, libm sin/cos, the x64/arm64 native conversion; on other targets an in-range value converts and NaN or an out-of-range one is an explicit “not supported by csharpc yet”), and the slicer refuses to copy any body that calls itself unconditionally, which would compile to an endless loop. Found on the way, fixed in general: a boxed number’s IConvertible slots are filled member by member as the program dispatches them (they were filled whole once any enum box made IConvertible exist, linking DateTime’s formatter and every calendar: json 1 661 856 to 1 438 440 B, jsonwriter 828 400 to 567 224 B, regexoptions 2 118 168 to 1 970 368 B), and IConvertible.ToType no longer boxes a DateTime to throw. Vector2/3/4, Plane and Quaternion stay corelib shadows (#451). |
fp_math_throughput: 317 064 / 1 365 768 B; compile cpu 7.8 / 12.0 s; run 113 / 195 ms (0.58x); 975 M / 1 860 M instructions. Until #453 Math.Round/double.Round with digits or a MidpointRounding still went to csharpc’s exact-decimal rounding through intrinsics that outranked the hosted IL (594 / 193 ms, 3.08x, 4 874 M instructions); they are removed |
New sample 1444 (the generic-math statics of float and double, Math.Round with digits and every MidpointRounding, BitIncrement/BitDecrement,
IEEERemainder, ILogB/ScaleB, Log(a, b), the saturating and (in range: out of range they are the platform instruction’s) native integer conversions,
with their argument errors).