Skip to content

Commit

Permalink
Add missing test case for BitCast (#97983)
Browse files Browse the repository at this point in the history
BitCast lacked a test for the case of 2 structs with target requiring higher alignment than source, which could be problematic on platforms without unaligned loads.
  • Loading branch information
MichalPetryka authored Feb 10, 2024
1 parent 0ce3c32 commit af53dab
Showing 1 changed file with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1260,17 +1260,23 @@ public static unsafe void BitCast()
Half h = Unsafe.ReadUnaligned<Half>(ref Unsafe.As<S2, byte>(ref s2));
float s = Unsafe.ReadUnaligned<float>(ref Unsafe.As<S4, byte>(ref s4));
double d = Unsafe.ReadUnaligned<double>(ref Unsafe.As<S8, byte>(ref s8));
(float, float) tf = Unsafe.ReadUnaligned<(float, float)>(ref Unsafe.As<S8, byte>(ref s8));
(uint, uint) ti = Unsafe.ReadUnaligned<(uint, uint)>(ref Unsafe.As<S8, byte>(ref s8));

Assert.Equal(h, Unsafe.BitCast<S2, Half>(s2));
Assert.Equal(s, Unsafe.BitCast<S4, float>(s4));
Assert.Equal(d, Unsafe.BitCast<S8, double>(s8));
Assert.Equal(tf, Unsafe.BitCast<S8, (float, float)>(s8));
Assert.Equal(ti, Unsafe.BitCast<S8, (uint, uint)>(s8));

*(S2*)misalignedPtr = s2;
Assert.Equal(h, Unsafe.BitCast<S2, Half>(*(S2*)misalignedPtr));
*(S4*)misalignedPtr = s4;
Assert.Equal(s, Unsafe.BitCast<S4, float>(*(S4*)misalignedPtr));
*(S8*)misalignedPtr = s8;
Assert.Equal(d, Unsafe.BitCast<S8, double>(*(S8*)misalignedPtr));
Assert.Equal(tf, Unsafe.BitCast<S8, (float, float)>(*(S8*)misalignedPtr));
Assert.Equal(ti, Unsafe.BitCast<S8, (uint, uint)>(*(S8*)misalignedPtr));

NativeMemory.AlignedFree(misalignedPtr - 1);
}
Expand Down

0 comments on commit af53dab

Please # to comment.