Open
Description
Background and motivation
When using Unsafe to write ref-based loops, it's common to want to iterate while one address is <= another. But there's no Unsafe.IsAddressLessThanOrEqualTo, so instead of being able to write:
do
{
...
}
while (Unsafe.IsAddressLessThanOrEqualTo(ref pos, ref oneVectorFromEnd));
you end up needing to do the mental gymnastics to come up with:
do
{
...
}
while (!Unsafe.IsAddressLessThan(ref oneVectorFromEnd, ref pos));
API Proposal
namespace System.Runtime.CompilerServices;
public static class Unsafe
{
// Existing
public static bool IsAddressGreaterThan<T>([AllowNull] ref T left, [AllowNull] ref T right);
public static bool IsAddressLessThan<T>([AllowNull] ref T left, [AllowNull] ref T right);
// New
+ public static bool IsAddressGreaterThanOrEqualTo<T>([AllowNull] ref T left, [AllowNull] ref T right);
+ public static bool IsAddressLessThanOrEqualTo<T>([AllowNull] ref T left, [AllowNull] ref T right);
}
API Usage
ref int oneVectorFromEnd = ref Unsafe.Subtract(ref end, Vector<int>.Count);
do
{
current.StoreUnsafe(ref pos);
current += increment;
pos = ref Unsafe.Add(ref pos, Vector<int>.Count);
}
while (Unsafe.IsAddressLessThanOrEqualTo(ref pos, ref oneVectorFromEnd));
Alternative Designs
No response
Risks
No response