public Uuid128 Increment(ulong value) { unsafe { fixed(Uuid128 *self = &this) { // serialize GUID into High Endian format byte *buf = stackalloc byte[16]; WriteUnsafe((Guid *)self, buf); // Add the low 64 bits (in HE) ulong lo = UnsafeHelpers.LoadUInt64BE(buf + 8); ulong sum = lo + value; if (sum < value) { // overflow occured, we must carry to the high 64 bits (in HE) ulong hi = UnsafeHelpers.LoadUInt64BE(buf); UnsafeHelpers.StoreUInt64BE(buf, unchecked (hi + 1)); } UnsafeHelpers.StoreUInt64BE(buf + 8, sum); // deserialize back to GUID return(new Uuid128(ReadUnsafe(buf))); } } }
internal static unsafe ulong ReadUnsafe(ReadOnlySpan <byte> src) { //Contract.Requires(src.Length >= 0); fixed(byte *ptr = &MemoryMarshal.GetReference(src)) { return(UnsafeHelpers.LoadUInt64BE(ptr)); } }
/// <summary>Split this 128-bit UUID into two 64-bit numbers</summary> /// <param name="a">xxxxxxxx-xxxx-xxxx-....-............</param> /// <param name="b">........-....-....-xxxx-xxxxxxxxxxxx</param> public void Deconstruct(out ulong a, out ulong b) { unsafe { byte *buffer = stackalloc byte[16]; WriteUnsafe(m_packed, buffer); a = UnsafeHelpers.LoadUInt64BE(buffer + 0); b = UnsafeHelpers.LoadUInt64BE(buffer + 8); } }
/// <summary>Creates a 96-bit <see cref="VersionStamp"/>.</summary> /// <returns>Complete stamp, with a user version.</returns> public static VersionStamp Custom(Uuid80 uuid, ushort userVersion, bool incomplete) { unsafe { byte *ptr = stackalloc byte[10]; uuid.WriteToUnsafe(ptr); ulong version = UnsafeHelpers.LoadUInt64BE(ptr); ushort order = UnsafeHelpers.LoadUInt16BE(ptr + 8); return(new VersionStamp(version, order, userVersion, incomplete ? (ushort)(FLAGS_IS_INCOMPLETE | FLAGS_HAS_VERSION) : FLAGS_HAS_VERSION)); } }
/// <summary>Creates a 96-bit <see cref="VersionStamp"/>.</summary> /// <returns>Complete stamp, with a user version.</returns> public static VersionStamp Custom(Uuid80 uuid, bool incomplete) { unsafe { byte *ptr = stackalloc byte[10]; uuid.WriteToUnsafe(ptr); ulong version = UnsafeHelpers.LoadUInt64BE(ptr); ushort order = UnsafeHelpers.LoadUInt16BE(ptr + 8); return(new VersionStamp(version, order, NO_USER_VERSION, incomplete ? FLAGS_IS_INCOMPLETE : FLAGS_NONE)); } }
public static ulong ReadUnsafe([NotNull] byte[] buffer, int offset) { //Contract.Requires(buffer != null && offset >= 0 && offset + 7 < buffer.Length); // buffer contains the bytes in Big Endian unsafe { fixed(byte *ptr = &buffer[offset]) { return(UnsafeHelpers.LoadUInt64BE(ptr)); } } }
internal static unsafe ulong ReadUnsafe([NotNull] byte *src) { //Contract.Requires(src != null); return(UnsafeHelpers.LoadUInt64BE(src)); }