Skip to content

Commit 5ce2ffc

Browse files
Copilotdadhi
andauthored
Fix net472 float bit conversion compatibility
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/72f15b2a-4d12-499c-a213-bcad43d6e4be Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
1 parent def9eb9 commit 5ce2ffc

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

src/FastExpressionCompiler.LightExpression/FlatExpression.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ private static bool TryGetInlineConstantValue32(object value, Type type, out int
13121312
value32 = unchecked((int)(uint)value);
13131313
return true;
13141314
case TypeCode.Single:
1315-
value32 = BitConverter.SingleToInt32Bits((float)value);
1315+
value32 = ConvertSingleToInt32Bits((float)value);
13161316
return true;
13171317
default:
13181318
value32 = default;
@@ -1342,11 +1342,29 @@ private static object ReadInlineConstantValue32(Type type, int value32)
13421342
TypeCode.UInt16 => (ushort)value32,
13431343
TypeCode.Int32 => value32,
13441344
TypeCode.UInt32 => unchecked((uint)value32),
1345-
TypeCode.Single => BitConverter.Int32BitsToSingle(value32),
1345+
TypeCode.Single => ConvertInt32BitsToSingle(value32),
13461346
_ => Throw.UnsupportedInlineConstantType<object>(type)
13471347
};
13481348
}
13491349

1350+
[StructLayout(LayoutKind.Explicit)]
1351+
private struct SingleInt32Union
1352+
{
1353+
[FieldOffset(0)]
1354+
public float Single;
1355+
1356+
[FieldOffset(0)]
1357+
public int Int32;
1358+
}
1359+
1360+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1361+
private static int ConvertSingleToInt32Bits(float value) =>
1362+
new SingleInt32Union { Single = value }.Int32;
1363+
1364+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1365+
private static float ConvertInt32BitsToSingle(int value) =>
1366+
new SingleInt32Union { Int32 = value }.Single;
1367+
13501368
private ChildList GetChildren(int index)
13511369
{
13521370
ref var node = ref Nodes.GetSurePresentRef(index);

0 commit comments

Comments
 (0)