public static void u64Encode(IBitBufferWriter self, u64 value) { bool one = false; increment(ref value, ref one); u128 map = 0; i32 index = -1; for (var i = u64Lookup.Length - 1; i >= 0; i--) { if (greaterOrEquals(value, one, u64Lookup[i])) { if (index == -1) { // last 1 leads to unique double 11 which is first and only one index = i + 1; map = WriteBit(map, index, true); } map = WriteBit(map, i, true); minus(ref value, ref one, u64Lookup[i]); } } for (var i = 0; i <= index; i++) { // TODO: optimize var bit = ExtractBit(map, i); self.b(bit); } }
private static bool ExtractBit(u128 value, int bitOffset) => (value & (u128.One << bitOffset)) != 0;
static bool INumberBase <int> .TryConvertToChecked <TOther>(int value, [NotNullWhen(true)] out TOther result) { // In order to reduce overall code duplication and improve the inlinabilty of these // methods for the corelib types we have `ConvertFrom` handle the same sign and // `ConvertTo` handle the opposite sign. However, since there is an uneven split // between signed and unsigned types, the one that handles unsigned will also // handle `Decimal`. // // That is, `ConvertFrom` for `int` will handle the other signed types and // `ConvertTo` will handle the unsigned types if (typeof(TOther) == typeof(byte)) { byte actualResult = checked ((byte)value); result = (TOther)(object)actualResult; return(true); } else if (typeof(TOther) == typeof(char)) { char actualResult = checked ((char)value); result = (TOther)(object)actualResult; return(true); } else if (typeof(TOther) == typeof(decimal)) { decimal actualResult = value; result = (TOther)(object)actualResult; return(true); } else if (typeof(TOther) == typeof(ushort)) { ushort actualResult = checked ((ushort)value); result = (TOther)(object)actualResult; return(true); } else if (typeof(TOther) == typeof(uint)) { uint actualResult = checked ((uint)value); result = (TOther)(object)actualResult; return(true); } else if (typeof(TOther) == typeof(ulong)) { ulong actualResult = checked ((ulong)value); result = (TOther)(object)actualResult; return(true); } else if (typeof(TOther) == typeof(UInt128)) { UInt128 actualResult = checked ((UInt128)value); result = (TOther)(object)actualResult; return(true); } else if (typeof(TOther) == typeof(nuint)) { nuint actualResult = checked ((nuint)value); result = (TOther)(object)actualResult; return(true); } else { result = default !;
private static u128 WriteBit(u128 value, int bitOffset, bool on) { u128 onn = on ? u128.One << bitOffset : 0; return((value & ~(u128.One << bitOffset)) | onn); }
private static bool TryConvertFromTruncating <TOther>(TOther value, out ushort result) where TOther : INumberBase <TOther> { // In order to reduce overall code duplication and improve the inlinabilty of these // methods for the corelib types we have `ConvertFrom` handle the same sign and // `ConvertTo` handle the opposite sign. However, since there is an uneven split // between signed and unsigned types, the one that handles unsigned will also // handle `Decimal`. // // That is, `ConvertFrom` for `ushort` will handle the other unsigned types and // `ConvertTo` will handle the signed types if (typeof(TOther) == typeof(byte)) { byte actualValue = (byte)(object)value; result = actualValue; return(true); } else if (typeof(TOther) == typeof(char)) { char actualValue = (char)(object)value; result = actualValue; return(true); } else if (typeof(TOther) == typeof(decimal)) { decimal actualValue = (decimal)(object)value; result = (actualValue >= MaxValue) ? MaxValue : (actualValue <= MinValue) ? MinValue : (ushort)actualValue; return(true); } else if (typeof(TOther) == typeof(uint)) { uint actualValue = (uint)(object)value; result = (ushort)actualValue; return(true); } else if (typeof(TOther) == typeof(ulong)) { ulong actualValue = (ulong)(object)value; result = (ushort)actualValue; return(true); } else if (typeof(TOther) == typeof(UInt128)) { UInt128 actualValue = (UInt128)(object)value; result = (ushort)actualValue; return(true); } else if (typeof(TOther) == typeof(nuint)) { nuint actualValue = (nuint)(object)value; result = (ushort)actualValue; return(true); } else { result = default; return(false); } }