Skip to content

Commit ee362f5

Browse files
wasabiiSimonCropp
andauthored
Add BitConverter reinterpret casting. (#436)
* Add BitConverter reinterpret casting. * Switch to a single struct. Can't use generic structs here. --------- Co-authored-by: Simon Cropp <[email protected]>
1 parent 7600a6b commit ee362f5

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

apiCount.include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**API count: 669**
1+
**API count: 669**

api_list.include.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165

166166
#### Double
167167

168+
* `ulong DoubleToUInt64Bits(double)`
168169
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat?view=net-10.0#system-double-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
169170
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat?view=net-10.0#system-double-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
170171
* `bool TryParse(ReadOnlySpan<byte>, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=net-10.0#system-double-tryparse(system-readonlyspan((system-byte))-system-double@))
@@ -373,6 +374,7 @@
373374

374375
#### Int32
375376

377+
* `float Int32BitsToSingle(int)`
376378
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryformat?view=net-10.0#system-int32-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
377379
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryformat?view=net-10.0#system-int32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
378380
* `bool TryParse(ReadOnlySpan<byte>, IFormatProvider?, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-10.0#system-int32-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-int32@))
@@ -639,6 +641,8 @@
639641

640642
#### Single
641643

644+
* `int SingleToInt32Bits(float)`
645+
* `uint SingleToUInt32Bits(float)`
642646
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.single.tryformat?view=net-10.0#system-single-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
643647
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.single.tryformat?view=net-10.0#system-single-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
644648

@@ -861,6 +865,7 @@
861865

862866
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryformat?view=net-10.0#system-uint32-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
863867
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryformat?view=net-10.0#system-uint32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
868+
* `float UInt32BitsToSingle(uint)`
864869
* `bool TryParse(ReadOnlySpan<byte>, IFormatProvider?, uint)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryparse?view=net-10.0#system-uint32-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-uint32@))
865870
* `bool TryParse(ReadOnlySpan<byte>, NumberStyles, IFormatProvider?, uint)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryparse?view=net-10.0#system-uint32-tryparse(system-readonlyspan((system-byte))-system-globalization-numberstyles-system-iformatprovider-system-uint32@))
866871
* `bool TryParse(ReadOnlySpan<byte>, uint)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryparse?view=net-10.0#system-uint32-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-uint32@))
@@ -874,6 +879,7 @@
874879

875880
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat?view=net-10.0#system-uint64-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
876881
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat?view=net-10.0#system-uint64-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
882+
* `double UInt64BitsToDouble(ulong)`
877883
* `bool TryParse(ReadOnlySpan<byte>, IFormatProvider?, ulong)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryparse?view=net-10.0#system-uint64-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-uint64@))
878884
* `bool TryParse(ReadOnlySpan<byte>, NumberStyles, IFormatProvider?, ulong)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryparse?view=net-10.0#system-uint64-tryparse(system-readonlyspan((system-byte))-system-globalization-numberstyles-system-iformatprovider-system-uint64@))
879885
* `bool TryParse(ReadOnlySpan<byte>, ulong)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryparse?view=net-10.0#system-uint64-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-uint64@))

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ The class `Polyfill` includes the following extension methods:
610610

611611
#### Double
612612

613+
* `ulong DoubleToUInt64Bits(double)`
613614
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat?view=net-10.0#system-double-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
614615
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat?view=net-10.0#system-double-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
615616
* `bool TryParse(ReadOnlySpan<byte>, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=net-10.0#system-double-tryparse(system-readonlyspan((system-byte))-system-double@))
@@ -818,6 +819,7 @@ The class `Polyfill` includes the following extension methods:
818819

819820
#### Int32
820821

822+
* `float Int32BitsToSingle(int)`
821823
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryformat?view=net-10.0#system-int32-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
822824
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryformat?view=net-10.0#system-int32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
823825
* `bool TryParse(ReadOnlySpan<byte>, IFormatProvider?, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-10.0#system-int32-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-int32@))
@@ -1084,6 +1086,8 @@ The class `Polyfill` includes the following extension methods:
10841086

10851087
#### Single
10861088

1089+
* `int SingleToInt32Bits(float)`
1090+
* `uint SingleToUInt32Bits(float)`
10871091
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.single.tryformat?view=net-10.0#system-single-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
10881092
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.single.tryformat?view=net-10.0#system-single-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
10891093

@@ -1306,6 +1310,7 @@ The class `Polyfill` includes the following extension methods:
13061310

13071311
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryformat?view=net-10.0#system-uint32-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
13081312
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryformat?view=net-10.0#system-uint32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
1313+
* `float UInt32BitsToSingle(uint)`
13091314
* `bool TryParse(ReadOnlySpan<byte>, IFormatProvider?, uint)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryparse?view=net-10.0#system-uint32-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-uint32@))
13101315
* `bool TryParse(ReadOnlySpan<byte>, NumberStyles, IFormatProvider?, uint)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryparse?view=net-10.0#system-uint32-tryparse(system-readonlyspan((system-byte))-system-globalization-numberstyles-system-iformatprovider-system-uint32@))
13111316
* `bool TryParse(ReadOnlySpan<byte>, uint)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryparse?view=net-10.0#system-uint32-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-uint32@))
@@ -1319,6 +1324,7 @@ The class `Polyfill` includes the following extension methods:
13191324

13201325
* `bool TryFormat(Span<byte>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat?view=net-10.0#system-uint64-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
13211326
* `bool TryFormat(Span<char>, int, ReadOnlySpan<char>, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat?view=net-10.0#system-uint64-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider))
1327+
* `double UInt64BitsToDouble(ulong)`
13221328
* `bool TryParse(ReadOnlySpan<byte>, IFormatProvider?, ulong)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryparse?view=net-10.0#system-uint64-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-uint64@))
13231329
* `bool TryParse(ReadOnlySpan<byte>, NumberStyles, IFormatProvider?, ulong)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryparse?view=net-10.0#system-uint64-tryparse(system-readonlyspan((system-byte))-system-globalization-numberstyles-system-iformatprovider-system-uint64@))
13241330
* `bool TryParse(ReadOnlySpan<byte>, ulong)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryparse?view=net-10.0#system-uint64-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-uint64@))
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
#if FeatureMemory
5+
6+
namespace Polyfills;
7+
8+
using System;
9+
using System.Runtime.CompilerServices;
10+
using System.Runtime.InteropServices;
11+
12+
static partial class Polyfill
13+
{
14+
15+
#if !AllowUnsafeBlocks && !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP2_0_OR_GREATER
16+
17+
[StructLayout(LayoutKind.Explicit)]
18+
struct BitConverter_Int32_Float
19+
{
20+
[FieldOffset(0)]
21+
public int _int;
22+
[FieldOffset(0)]
23+
public float _float;
24+
}
25+
26+
#endif
27+
28+
extension(BitConverter)
29+
{
30+
31+
#if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP2_0_OR_GREATER
32+
33+
/// <summary>
34+
/// Reinterprets the specified 32-bit integer as a single-precision floating-point value.
35+
/// </summary>
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.int32bitstosingle
38+
#if AllowUnsafeBlocks
39+
public static unsafe float Int32BitsToSingle(int value)
40+
{
41+
return Unsafe.AsRef<float>(Unsafe.AsPointer(ref value));
42+
}
43+
#else
44+
public static float Int32BitsToSingle(int value)
45+
{
46+
var i = new BitConverter_Int32_Float();
47+
i._int = value;
48+
return i._float;
49+
}
50+
#endif
51+
52+
#endif
53+
54+
#if !NET6_0_OR_GREATER
55+
56+
/// <summary>
57+
/// Converts the specified 32-bit unsigned integer to a single-precision floating point number.
58+
/// </summary>
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.uint32bitstosingle
61+
public static float UInt32BitsToSingle(uint value)
62+
{
63+
return BitConverter.Int32BitsToSingle((int)value);
64+
}
65+
66+
#endif
67+
68+
#if !NET6_0_OR_GREATER
69+
70+
/// <summary>
71+
/// Converts the specified 64-bit unsigned integer to a double-precision floating point number.
72+
/// </summary>
73+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
74+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.uint64bitstodouble
75+
public static double UInt64BitsToDouble(ulong value)
76+
{
77+
return BitConverter.Int64BitsToDouble((long)value);
78+
}
79+
80+
#endif
81+
82+
#if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP2_0_OR_GREATER
83+
84+
/// <summary>
85+
/// Converts a single-precision floating-point value into an integer.
86+
/// </summary>
87+
/// <param name="value"></param>
88+
/// <returns></returns>
89+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
90+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.singletoint32bits
91+
#if AllowUnsafeBlocks
92+
public static unsafe int SingleToInt32Bits(float value)
93+
{
94+
return Unsafe.AsRef<int>(Unsafe.AsPointer(ref value));
95+
}
96+
#else
97+
public static int SingleToInt32Bits(float value)
98+
{
99+
var i = new BitConverter_Int32_Float();
100+
i._float = value;
101+
return i._int;
102+
}
103+
#endif
104+
105+
#endif
106+
107+
#if !NET6_0_OR_GREATER
108+
109+
/// <summary>
110+
/// Converts the specified single-precision floating point number to a 32-bit unsigned integer.
111+
/// </summary>
112+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
113+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.singletouint32bits
114+
public static uint SingleToUInt32Bits(float value)
115+
{
116+
return (uint)BitConverter.SingleToInt32Bits(value);
117+
}
118+
119+
#endif
120+
121+
#if !NET6_0_OR_GREATER
122+
123+
/// <summary>
124+
/// Converts the specified double-precision floating point number to a 64-bit unsigned integer.
125+
/// </summary>
126+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
127+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.doubletouint64bits
128+
public static ulong DoubleToUInt64Bits(double value)
129+
{
130+
return (ulong)BitConverter.DoubleToInt64Bits(value);
131+
}
132+
133+
#endif
134+
135+
}
136+
137+
}
138+
139+
#endif

0 commit comments

Comments
 (0)