1 using System;
2
3 namespace cmsx.util
4 {
5 public inline nothrow ulong SignExtend<T>(T value) where System.Meta.IsBasicType<T>()
6 {
7 return cast<ulong>(cast<long>(value << (64 - 8 * sizeof(T))) >> (64 - 8 * sizeof(T)));
8 }
9
10 inline constexpr nothrow bool IsPowerOfTwo(ulong x)
11 {
12 ulong m = 2u;
13 while (m <= x)
14 {
15 if (x == m) return true;
16 ulong s = m << 1u;
17 if (s < m) return false;
18 m = s;
19 }
20 return false;
21 }
22
23 inline constexpr nothrow ulong Log2(ulong x)
24 {
25 ulong log2 = 0u;
26 x = x >> 1u;
27 while (x != 0u)
28 {
29 ++log2;
30 x = x >> 1u;
31 }
32 return log2;
33 }
34 }