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))); } } }
public static void WriteUnsafe(ulong value, [NotNull] byte[] buffer, int offset) { //Contract.Requires(buffer != null && offset >= 0 && offset + 7 < buffer.Length); unsafe { fixed(byte *ptr = &buffer[offset]) { UnsafeHelpers.StoreUInt64BE(ptr, value); } } }
public static unsafe void WriteUnsafe(ulong value, byte *ptr) { //Contract.Requires(ptr != null); UnsafeHelpers.StoreUInt64BE(ptr, value); }