Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add Unsafe.IsAddressGreaterThan and IsAddressLessThan
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks committed Jan 24, 2018
1 parent 2836df3 commit 83f35ef
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static void InitBlock(ref byte startAddress, byte value, uint byteCount)
public unsafe static void InitBlock(void* startAddress, byte value, uint byteCount) { }
public static void InitBlockUnaligned(ref byte startAddress, byte value, uint byteCount) { }
public unsafe static void InitBlockUnaligned(void* startAddress, byte value, uint byteCount) { }
public static bool IsAddressGreaterThan<T>(ref T left, ref T right) { throw null; }
public static bool IsAddressLessThan<T>(ref T left, ref T right) { throw null; }
public unsafe static T Read<T>(void* source) { throw null; }
public unsafe static T ReadUnaligned<T>(void* source) { throw null; }
public static T ReadUnaligned<T>(ref byte source) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,26 @@
ret
} // end of method Unsafe::AreSame

.method public hidebysig static bool IsAddressGreaterThan<T>(!!T& left, !!T& right) cil managed aggressiveinlining
{
.custom instance void System.Runtime.Versioning.NonVersionableAttribute::.ctor() = ( 01 00 00 00 )
.maxstack 2
ldarg.0
ldarg.1
cgt.un
ret
} // end of method Unsafe::IsAddressGreaterThan

.method public hidebysig static bool IsAddressLessThan<T>(!!T& left, !!T& right) cil managed aggressiveinlining
{
.custom instance void System.Runtime.Versioning.NonVersionableAttribute::.ctor() = ( 01 00 00 00 )
.maxstack 2
ldarg.0
ldarg.1
clt.un
ret
} // end of method Unsafe::IsAddressLessThan

} // end of class System.Runtime.CompilerServices.Unsafe

.class private auto ansi sealed beforefieldinit System.Runtime.Versioning.NonVersionableAttribute
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions src/System.Runtime.CompilerServices.Unsafe/tests/UnsafeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,44 @@ public static void RefAreSame()
Assert.False(Unsafe.AreSame(ref a[0], ref a[1]));
}

[Fact]
public static unsafe void RefIsAddressGreaterThan()
{
int[] a = new int[2];

Assert.False(Unsafe.IsAddressGreaterThan(ref a[0], ref a[0]));
Assert.False(Unsafe.IsAddressGreaterThan(ref a[0], ref a[1]));
Assert.True(Unsafe.IsAddressGreaterThan(ref a[1], ref a[0]));
Assert.False(Unsafe.IsAddressGreaterThan(ref a[1], ref a[1]));

// The following tests ensure that we're using unsigned comparison logic

Assert.False(Unsafe.IsAddressGreaterThan(ref Unsafe.AsRef<byte>((void*)(1)), ref Unsafe.AsRef<byte>((void*)(-1))));
Assert.True(Unsafe.IsAddressGreaterThan(ref Unsafe.AsRef<byte>((void*)(-1)), ref Unsafe.AsRef<byte>((void*)(1))));
Assert.True(Unsafe.IsAddressGreaterThan(ref Unsafe.AsRef<byte>((void*)(Int32.MinValue)), ref Unsafe.AsRef<byte>((void*)(Int32.MaxValue))));
Assert.False(Unsafe.IsAddressGreaterThan(ref Unsafe.AsRef<byte>((void*)(Int32.MaxValue)), ref Unsafe.AsRef<byte>((void*)(Int32.MinValue))));
Assert.False(Unsafe.IsAddressGreaterThan(ref Unsafe.AsRef<byte>(null), ref Unsafe.AsRef<byte>(null)));
}

[Fact]
public static unsafe void RefIsAddressLessThan()
{
int[] a = new int[2];

Assert.False(Unsafe.IsAddressLessThan(ref a[0], ref a[0]));
Assert.True(Unsafe.IsAddressLessThan(ref a[0], ref a[1]));
Assert.False(Unsafe.IsAddressLessThan(ref a[1], ref a[0]));
Assert.False(Unsafe.IsAddressLessThan(ref a[1], ref a[1]));

// The following tests ensure that we're using unsigned comparison logic

Assert.True(Unsafe.IsAddressLessThan(ref Unsafe.AsRef<byte>((void*)(1)), ref Unsafe.AsRef<byte>((void*)(-1))));
Assert.False(Unsafe.IsAddressLessThan(ref Unsafe.AsRef<byte>((void*)(-1)), ref Unsafe.AsRef<byte>((void*)(1))));
Assert.False(Unsafe.IsAddressLessThan(ref Unsafe.AsRef<byte>((void*)(Int32.MinValue)), ref Unsafe.AsRef<byte>((void*)(Int32.MaxValue))));
Assert.True(Unsafe.IsAddressLessThan(ref Unsafe.AsRef<byte>((void*)(Int32.MaxValue)), ref Unsafe.AsRef<byte>((void*)(Int32.MinValue))));
Assert.False(Unsafe.IsAddressLessThan(ref Unsafe.AsRef<byte>(null), ref Unsafe.AsRef<byte>(null)));
}

[Fact]
public static unsafe void ReadUnaligned_ByRef_Int32()
{
Expand Down

0 comments on commit 83f35ef

Please sign in to comment.