helper methods to round to nearest power of 2

This commit is contained in:
Michael Becker 2019-11-24 04:42:11 -05:00
parent 3b82e4ba74
commit 3879cdc740
No known key found for this signature in database
GPG Key ID: 389DFF5D73781A12

View File

@ -870,5 +870,31 @@ namespace UniversalEditor
}
}
#endregion
public static long RoundToNearestPowerOf2(this long input)
{
long v = input;
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
[CLSCompliant(false)]
public static ulong RoundToNearestPowerOf2(this ulong input)
{
ulong v = input;
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
}
}