System.IntegerMembers
A library or CoreLib type served from the framework’s IL: its 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 static generic-math members of byte, sbyte, short, ushort, int, uint, long, ulong, nint and nuint that csharpc had no lowering of its own for (Log2, PopCount, LeadingZeroCount, TrailingZeroCount, rotates, DivRem, CopySign, the Is* predicates, MinMagnitude/MaxMagnitude/...Number, CreateChecked/CreateSaturating/CreateTruncating), and Math.Clamp(decimal) (src/Csharpc/upstream-types/System.IntegerMembers.txt, the slicer’s host: roots: the members travel in each type’s host class; the compiler calls one only where no intrinsic, shadow or generic-math path applies; #391, #392) |
two new samples recorded from real .NET: 1330 (every member over each type’s edge values, directly and through generic code, T.Log2(x) with T = int; on main 840 CSLLVM0002 diagnostics) and 1331 (Clamp with min > max for every overload, directly and through generic code, the non-throwing cases, byte/short/ushort/sbyte.Sign; main’s compiler, without the members it cannot lower, prints byte.Sign(5) = 5 and a clamped value for all 15 min > max cases), byte-identical at -O0, -O2 and via IL with 14 neighbouring math, generic-math, decimal, BFloat16 and BitOperations samples (32 + 16 runs) |
programs that do not call the members are unchanged apart from Math.Clamp’s new bounds check (#392): linux-x86_64 hello, exceptions, typeinfo unchanged; async, collections, json, jsonwriter, regex, regexoptions +176 B, linq, net, numbers +160 B (one compare and the cold ThrowMinMaxException<T> call); every budget holds. CsharpcUpstream.dll 185 856 to 213 504 B. The throughput program 140 280 B (NativeAOT 1 341 216) |
the size budgets’ numbers program, 3 runs each on the shared host: main 5.4 / 4.6 / 6.5 s, branch 5.8 / 4.6 / 6.3 s (no change beyond the noise). The throughput program 4.1 s (NativeAOT publish 17.8 s) |
before: does not compile ('int.Log2(int)' has no implementation); after, the throughput program (tools/il-first/bench/integer_generic_math_throughput.cs: T.Log2, T.PopCount, T.Sign, T.IsPow2, CreateTruncating over 1 M values of six types, 40 rounds) 376 ms, NativeAOT 545 ms (0.69x); 3.87 G instructions (NativeAOT 5.47 G) |
none: these members were diagnostics, not shadows. Math.Clamp(int)’s separate intrinsic folded into the other integer overloads’ loop (-7 lines) |
From IL, flipped. Every existing lowering keeps priority (the hot members Sign, Abs, Min/Max, Clamp, IsPow2, PopCount of 32/64-bit types stay LLVM intrinsics); the IL is the fallback. Int128 and UInt128 are not hosted: their IL reads _lower/_upper and private helpers csharpc’s representation of them lacks (#393, follow-up). Compiler pieces it needed, all general: host: roots in the slicer (explicit interface implementations keep their metadata name, generic members their type parameters and constraints); FunctionCodeGen.HostedBuiltinMember maps a call, or a constrained interface call from generic code, to the hosted implementation; Math.ThrowMinMaxException / ThrowNegateTwosCompOverflow and nint/nuint MaxValue/MinValue at the target’s pointer width in the corelib; BitOperations.TrailingZeroCount/IsPow2(nint), Math.Sign(nint), Math.Clamp(nint/nuint) intrinsics at the operand’s own width (wasm32: 4 bytes). Fixed along the way: byte/short.Sign returned CompareTo’s difference (5, not 1), and every Clamp intrinsic now throws .NET’s ArgumentException when min > max (#392, both silent differences). Security: the hosted members are upstream’s own arithmetic over their arguments, with no pointers, buffers or unchecked memory access; CreateChecked keeps upstream’s OverflowException checks; the Clamp bounds check adds a throw where csharpc returned a value. |
integer_generic_math_throughput: 140 280 / 1 341 216 B; compile cpu 4.1 / 17.8 s; run 376 / 545 ms (0.69x); 3.87 G / 5.47 G instructions |