Skip to content

NFloat from the framework's IL, blittable as its fields - #296

Merged
jonathanperis merged 8 commits into
jonathanperis:mainfrom
filipnavara:feat/nfloat-from-il
Nov 27, 2026
Merged

jonathanperis merged 8 commits into
jonathanperis:mainfrom
filipnavara:feat/nfloat-from-il

Conversation

@filipnavara

@filipnavara filipnavara commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Part of #99 ("Interop: … NFloat …").

System.Runtime.InteropServices.NFloat (Apple's CGFloat; a double on 64-bit targets) has neither a shadow nor an implementation, so any program naming it fails:

not supported by csharpc yet: struct 'System.Runtime.InteropServices.NFloat'

That is every .NET for macOS/iOS app: CGPoint, CGSize, CGRect and all of AppKit's geometry are made of it. In eM Client, library structs csharpc lacks were 958 of the first 1,398 compile errors, mostly NFloat.

The change

  • upstream-types.txt gets System.Runtime.InteropServices.NFloat (ADR 0023): the slice serves .NET's own NFloat, so its arithmetic, conversions, formatting and parsing are .NET's. There was no shadow to delete. The slicer cut it on the first try.
  • IsBlittable: a CoreLib struct served by the slice (CoreLib.TryMap → a type under CsharpcCoreLib.Upstream) is as blittable as its fields, which are .NET's own. This is the same reasoning as the existing Guid case, but without a hand-written layout to trust. P/Invoke and unmanaged function pointers need it: delegate* unmanaged<NFloat, NFloat, NFloat> was "P/Invoke parameter of non-blittable struct".

(An earlier prototype of mine shadowed NFloat over a double, about 200 lines. The slice makes that unnecessary.)

Tests

  • New sample 1346_nfloat_from_framework, recorded from .NET:
    • arithmetic, comparison, classification, conversions (including Half and decimal), formatting and parsing, Max/Abs/Sqrt/Round
    • NFloat and a CGPoint-like struct of it through [UnmanagedCallersOnly] function pointers
  • CSHARPC_JOBS=6 tests/run.sh --all-opts 'pinvoke|marshal|unmanaged|function_pointer|interop': 36 passed.
  • tests/conformance.sh: 195/195.
  • tests/size.sh on macos-arm64: byte-identical sizes with and without the type in the slice. Only programs that use NFloat get its code.
  • The sample through the IL front-end (csc build, then csharpc p.dll): same output. --via-il is Linux-only, so this was checked by hand on macOS.

Recorded on macOS with the script fixes from #278.

System.Runtime.InteropServices.NFloat (Apple's CGFloat, a double on
64-bit targets) had neither a shadow nor an implementation: any program
naming it failed ("struct 'System.Runtime.InteropServices.NFloat'"),
which is every .NET for macOS/iOS app (CGPoint, CGSize, CGRect, all of
AppKit's geometry). It now joins upstream-types.txt (ADR 0023): the
slice serves .NET's own NFloat, so its arithmetic, conversions,
formatting and parsing are .NET's.

P/Invoke and unmanaged function pointers also need it blittable:
IsBlittable now accepts a CoreLib struct served by the slice as
blittable as its fields, which are .NET's own (NFloat's one double).

Size budgets are unchanged (tests/size.sh gives identical sizes with
and without the type in the slice). Sample 1346 is recorded from .NET.
# Conflicts:
#	src/Csharpc/upstream-types.txt
wasm32 serves .NET's 32-bit NFloat (a float), as .NET does on any 32-bit
platform, so its size and range differ from the 64-bit recording.
# Conflicts:
#	tests/platform-skips.txt
@jonathanperis

Copy link
Copy Markdown
Owner

I pushed two commits to this branch (maintainer edits) so it can merge:

  • main merged in (a602f60). The only conflict was tests/platform-skips.txt, which is append-only, so both sides were kept. NFloat moves into src/Csharpc/upstream-types/System.Runtime.InteropServices.NFloat.txt, the per-library lists main now uses (ADR 0023).
  • A WASI skip for sample 1346 (a937888). On wasm32, NFloat is a float, so its size and range differ from the 64-bit recording. NativeAOT shows the same difference.

Local checks on a602f60:

  • 1346 prints exactly what .NET prints;
  • the NFloat, nint, interop and marshalling samples: 28/28 at -O0/-O2, and 14/14 through the IL front-end;
  • size.sh and slicer.sh green.

I'll merge once CI is green on this head. Thanks for the PR.

@jonathanperis
jonathanperis merged commit 898ac94 into jonathanperis:main Nov 27, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants