csharpc

System.Numerics IEEE types (BFloat16, Decimal32, Decimal64, Decimal128)

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)
System.Numerics.BFloat16, Decimal32, Decimal64, Decimal128 (src/Csharpc/upstream-types/System.Numerics.IeeeTypes.txt, type roots; System.Numerics.InterfaceDefaults.txt, the generic-math interfaces’ static default members hosted for any type; #405) the existing 1290 (BFloat16), 1291 (Decimal32/64/128) and 1300 (decimal negative zero) unchanged. New samples recorded from real .NET: 1333 (generic math through T, Parse/TryParse/TryFormat in UTF-16 and UTF-8, conversions to and from every primitive including Int128/UInt128 and decimal, NaN, infinities, subnormals, ±0, MaxValue, canonical and non-canonical encodings through Unsafe.BitCast, decimal to BFloat16 rounding once); 1338 (INumberBase’s default members on user types: UTF-8 Parse/TryParse/TryParsePartial, IUtf8SpanParsable, CreateChecked/Saturating/Truncating, MultiplyAddEstimate; before: 13 invalid-IR externals); 1339 (de-DE formatting and parsing in UTF-16 and UTF-8; skipped on wasi, no named cultures there); 1340 (unary -, checked -, +, ~ through T on user structs and the IL types; before: 19 “operator not supported” diagnostics, then the reference pack’s body-less op_CheckedUnaryNegation default); 1341 (checked(-x) with T = a signed primitive threw OverflowException for any value: the subtraction was checked as unsigned, from the unsubstituted T). 1333 on main: does not compile. All byte-identical at -O0, -O2 and via IL, and on wasm32-wasip1 (except 1339), with 34 neighbouring generic-math, decimal, Half, Int128 and number-formatting samples; tests/slicer.sh 11 of 11 1290: 436 400 / 328 048 B; 1291: 939 656 / 456 960 B (upstream’s generic number formatting and parsing, instantiated for char and byte, against the shadows’ narrower code). Size programs: json 1 571 032 to 1 643 496 B (it names the four types only in typeof, but a struct’s method table lowers ToString and the dispatched interface slots without a box: #420), budget raised to measured + 3 %; the others are unchanged 1290: 11.7 / 9.4 s; 1291: 23.1 / 14.0 s 1290: 20 / 311 ms; 1291: 4 / 5 ms. Throughput program (tools/il-first/bench/ieee_types_throughput.cs: arithmetic, Round, CreateSaturating, UTF-8 format and parse round trips, UTF-16 E3 formatting over 16 K values of each type, 20 rounds): 551 ms; the shadows do not compile it (INumberBase<BFloat16Impl>.TryConvertToSaturating<long> and the like have no implementation) 1 830 (corelib/Numerics/BFloat16.cs, Decimal32.cs, Decimal64.cs, Decimal128.cs, IeeeDecimal.cs, Number.IeeeDecimal.cs); added 79 (DecimalContract.cs: decimal’s IsNegative, High/Low/Mid, DecDivMod1E9, VarBFloat16FromDec), 34 (NumberFormatInfo’s *TChar symbols with a NUL-terminated UTF-8 cache as upstream), 19 (CompareInfo.IsPrefixUtf8) From IL, flipped; shadows deleted. Compiler pieces it needed, all general: (1) the IL front-end lowers decimal’s private _flags/_hi32/_lo64 like Int128’s halves (FunctionCodeGen.BuiltinPrivateFields.cs); (2) the slicer hosts a generic interface’s static default members as generic methods over the interface’s type parameters (!n becomes !!n in the member’s own context only), and a call that Roslyn resolves to the reference pack’s body-less default member (it returns the member itself) goes to the hosted one (HostedDefaultInterfaceMember); user number types get INumberBase’s defaults this way too; (3) an interface member over a CoreLib type the program sees as its slice replacement maps to the replacement’s interface (InterfaceMemberOverShadow); (4) the same hosting serves every generic-math interface’s static defaults (the checked operators, INumber’s Clamp/Min/Max/Sign family, IFloatingPoint’s rounding, IBinaryInteger’s, and the rest: System.Numerics.InterfaceDefaults.txt); (5) unary operators through T resolve the static abstract operator like binary ones, and LibraryOperator calls a concrete operator that is compiled code; (6) IBinaryInteger<T>.GetByteCount() on a built-in integer is its size, a constant. decimal’s TryConvert* members are hosted (host:System.Decimal::...). Security: the IL’s parsing and formatting use Unsafe/span code with upstream’s own bounds invariants (buffer sizes from GetMaxCharCount-style bounds, ValueListBuilder growth); csharpc’s additions are span-checked (IsPrefixUtf8 stack buffers sized to the input’s UTF-16 bound, falling back to an array above 256 chars), the UTF-8 symbol cache keeps upstream’s terminating NUL outside the span, and the private-field accesses stay inside the value (offsets 0, 4, 8 of a 16-byte decimal) ieee_types_throughput: 843 520 / 1 480 584 B; compile cpu 19.7 / 12.9 s; run 554 / 784 ms (0.71x); 4.75 G / 6.55 G instructions