Skip to content
4 changes: 4 additions & 0 deletions src/Csharpc/CodeGen/ModuleCodeGen.PInvoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ or SpecialType.System_Int32 or SpecialType.System_UInt32 or SpecialType.System_I
// blittable as in .NET. Other shadows may lay their fields out differently from .NET: not blittable.
INamedTypeSymbol { Name: "Guid", TypeKind: TypeKind.Struct, ContainingType: null, ContainingNamespace: { Name: "System", ContainingNamespace.IsGlobalNamespace: true } } s
when CoreLib.TryMap(s) is { TypeKind: TypeKind.Struct } shadow && IsUserType(shadow) => IsBlittable(shadow),
// A CoreLib struct served from the framework's IL (upstream-types/, ADR 0023) has .NET's own fields, so it is as
// blittable as they are: NFloat (one double on 64-bit targets), which Apple's CGPoint/CGSize/CGRect are made of.
INamedTypeSymbol { TypeKind: TypeKind.Struct } s when !IsUserType(s) && CoreLib.TryMap(s) is { TypeKind: TypeKind.Struct } served
&& served.ContainingNamespace.ToDisplayString().StartsWith(Frontend.UpstreamSlice.Prefix, StringComparison.Ordinal) && IsUserType(served) => IsBlittable(served),
_ => false,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# NFloat (Apple's CGFloat: a double on 64-bit targets) served from CoreLib's IL (ADR 0023): .NET's own arithmetic, conversions,
# formatting and parsing; P/Invoke sees it as blittable as its fields (ModuleCodeGen.PInvoke.IsBlittable).
System.Runtime.InteropServices.NFloat
1 change: 1 addition & 0 deletions tests/platform-skips.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ wasi 1540_enum_negative_value_culture_sign .NET on WASI is globalization-invari
wasi 1550_threading_runtime_members needs threads: Thread.Start throws PlatformNotSupportedException on WebAssembly
wasi 1553_interop_gap_members loads a shared library with NativeLibrary.Load: WASI has no dynamic libraries
wasi 1559_driveinfo WASI has no statfs/statvfs: DriveFormat and the drive sizes throw IOException (ENOTSUP)
wasi 1346_nfloat_from_framework NFloat is a float on 32-bit targets (wasm32), a double on 64-bit: its size and range differ from the 64-bit recording
windows 1563_timezoneinfo_from_il live: Windows takes the local zone from the system settings, not TZ; recorded there
macos 1562_special_folders live: macOS takes the user folders from NSFileManager (the real home, not $HOME), Linux from $HOME and XDG; recorded on each
windows 1562_special_folders known gap: Environment.GetFolderPath on Windows (the known folders) is not supported by csharpc yet; an explicit NotSupportedException
Expand Down
34 changes: 34 additions & 0 deletions tests/samples/1346_nfloat_from_framework.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#:property AllowUnsafeBlocks=true
// System.Runtime.InteropServices.NFloat (Apple's CGFloat; a double on 64-bit targets), served from the framework's IL: arithmetic,
// conversions, classification, comparison, formatting and parsing, and NFloat (and structs of it, like CGPoint) across
// unmanaged function pointers, where it is blittable.
using System;
using System.Globalization;
using System.Runtime.InteropServices;

unsafe
{
var inv = CultureInfo.InvariantCulture;
NFloat x = 1.5f, y = (NFloat)2.25;
Console.WriteLine($"{(x + y).ToString(inv)} {(x * y).ToString(inv)} {(y - x).ToString(inv)} {(y / x).ToString(inv)} {(-x).ToString(inv)} {(y % x).ToString(inv)}");
Console.WriteLine($"{x < y} {x >= y} {x == (NFloat)1.5} {x.Equals(y)} {x.CompareTo(y)} {NFloat.Size} {sizeof(NFloat)}");
Console.WriteLine($"{(double)NFloat.MaxValue == double.MaxValue} {NFloat.IsNaN(NFloat.NaN)} {NFloat.IsInfinity(NFloat.PositiveInfinity)} {NFloat.IsNegative(NFloat.NegativeZero)}");
Console.WriteLine($"{(int)(NFloat)7.9} {(long)(NFloat)(-3.5)} {((float)y).ToString(inv)} {((decimal)(NFloat)0.25).ToString(inv)} {((NFloat)(Half)0.5).ToString(inv)}");
Console.WriteLine($"{((NFloat)0.1).ToString(inv)} {((NFloat)1234.5678).ToString("F2", inv)} {((NFloat)1e300).ToString(inv)}");
Console.WriteLine($"{NFloat.Parse("3.25", inv).ToString(inv)} {NFloat.TryParse("oops", inv, out var bad)} {bad.ToString(inv)}");
Console.WriteLine($"{NFloat.Max(x, y).ToString(inv)} {NFloat.Abs((NFloat)(-2)).ToString(inv)} {NFloat.Sqrt((NFloat)16).ToString(inv)} {NFloat.Round((NFloat)2.5).ToString(inv)}");

delegate* unmanaged<NFloat, NFloat, NFloat> hypot = &Native.Hypot;
Console.WriteLine(hypot(3, 4).ToString(inv));
delegate* unmanaged<CGPoint, NFloat, CGPoint> scale = &Native.Scale;
var p = scale(new CGPoint { X = 1.5f, Y = -2 }, 2);
Console.WriteLine($"{p.X.ToString(inv)} {p.Y.ToString(inv)} {sizeof(CGPoint)} {Marshal.SizeOf<CGPoint>()}");
}

struct CGPoint { public NFloat X, Y; }

static class Native
{
[UnmanagedCallersOnly] public static NFloat Hypot(NFloat a, NFloat b) => NFloat.Sqrt(a * a + b * b);
[UnmanagedCallersOnly] public static CGPoint Scale(CGPoint p, NFloat by) => new CGPoint { X = p.X * by, Y = p.Y * by };
}
9 changes: 9 additions & 0 deletions tests/samples/1346_nfloat_from_framework.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
3.75 3.375 0.75 1.5 -1.5 0.75
True False True False -1 8 8
True True True True
7 -3 2.25 0.25 0.5
0.1 1234.57 1E+300
3.25 False 0
2.25 2 4 2
5
3 -4 16 16
Loading