csharpc

System.Int128

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)
Int128’s and UInt128’s static generic-math members (Log2, PopCount, LeadingZeroCount, TrailingZeroCount, Sign, IsPow2, Abs, CopySign, Clamp, Min/Max and the magnitude members, DivRem, rotates, the Is* predicates, CreateChecked/CreateSaturating/CreateTruncating to and from the other primitives), plus double/float Create* and every integer type’s TryConvert* (src/Csharpc/upstream-types/System.IntegerMembers.txt, the slicer’s host: roots of #391; #393) a new sample recorded from real .NET (1332: every member over values in the lower half, the upper half, both and the extremes, directly and through generic code, and the throwing cases: Log2 of a negative value, Abs(MinValue), CreateChecked overflow, Clamp with min > max, DivRem by zero; on main 91 CSLLVM0002 diagnostics, on #400’s head 90), byte-identical at -O0, -O2, via IL and on wasm32-wasip1, with its neighbouring samples (32 samples in all: the Int128 boxing, parsing, formatting, decimal-conversion and JSON ones, the integer generic-math samples 1330/1331, and the slices’ StringBuilder, Dictionary, Guid, Number and BFloat16 samples): 64 + 32 + 64 runs; tests/slicer.sh 11 of 11 programs that do not call the members are unchanged: the size programs’ LLVM IR is identical to #400’s head (async, compared), every budget holds. CsharpcUpstream.dll 213 504 to 270 336 B. The throughput program 270 648 B (NativeAOT 1 357 648) the throughput program 14.0 s (NativeAOT publish 13.7 s: csharpc 2 % behind, within this shared host’s noise) before: does not compile ('System.Int128.Log2(System.Int128)' has no implementation); after, the throughput program (tools/il-first/bench/int128_generic_math_throughput.cs: T.Log2, T.PopCount, T.LeadingZeroCount, T.Sign, T.IsPow2, T.RotateLeft, CreateTruncating/CreateSaturating over 1 M values of each type, 30 rounds) 270 ms, NativeAOT 358 ms (0.75x); 2.65 G instructions (NativeAOT 3.27 G) none: these members were diagnostics, not shadows From IL, flipped. Int128Support’s operators and conversions and the corelib’s Int128/UInt128 parsing and formatting keep priority; the IL is the fallback. Compiler pieces it needed, all general: (1) the IL front-end lowers ldfld/ldflda/stfld of Int128._lower/_upper (fields the reference pack doesn’t declare) as 64-bit accesses at byte 0 or 8 of the value, which is .NET’s layout of the struct and LLVM’s little-endian layout of the i128 csharpc keeps it as (FunctionCodeGen.BuiltinPrivateFields.cs, which since #405 also covers decimal’s _flags/_hi32/_lo64); (2) the slicer hosts the non-public static helpers that a hosted type’s code reaches (UInt128.LeadingZeroCountAsInt32, ulong’s private TryConvertFromChecked<TOther>), and the IL front-end resolves a reference to such a helper to its host class; (3) a constrained call of a protected interface member (INumberBase<ulong>.TryConvertToChecked<TOther>, which CreateChecked’s IL calls on its TOther) finds the type’s explicit implementation in the host class by the name C# gives it, since the reference pack leaves private implementations out. A helper the corelib’s shadow of the type declares (the same overload, read from the shadow sources: tools/il-first/slicer/ShadowSignatures.cs) stays the shadow’s, so the slice never trades csharpc’s copy for upstream’s code (Math.ThrowMinMaxException, ulong.PowersOf10 stay the corelib’s; Math.BigMul(ulong, uint, out ulong), which no shadow declares, is hosted). Security: the field accesses stay inside the value’s 16 bytes (an offset of 0 or 8 from its address); the hosted IL is upstream’s own arithmetic, with upstream’s overflow checks in CreateChecked. int128_generic_math_throughput: 270 648 / 1 357 648 B; compile cpu 14.0 / 13.7 s; run 270 / 358 ms (0.75x); 2.65 G / 3.27 G instructions