| | | 1 | | //======================================================================= |
| | | 2 | | // ChronicleDuration.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace Chronicler.Timing; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// An exact signed interval with 64-bit whole seconds and 32 binary fractional bits. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// Whole seconds use mathematical floor. Negative one fractional unit is <c>(-1, uint.MaxValue)</c>. |
| | | 17 | | /// The range is <c>(long.MinValue, 0)</c> through <c>(long.MaxValue, uint.MaxValue)</c>. |
| | | 18 | | /// Arithmetic throws when the final result exceeds this range. |
| | | 19 | | /// </remarks> |
| | | 20 | | public readonly struct ChronicleDuration : IEquatable<ChronicleDuration>, IComparable<ChronicleDuration> |
| | | 21 | | { |
| | | 22 | | /// <summary>Creates an interval from floor seconds and an unsigned fraction.</summary> |
| | | 23 | | /// <param name="wholeSeconds">The mathematical floor of the interval in seconds.</param> |
| | | 24 | | /// <param name="fractionalSecond">The nonnegative remainder in units of 2^-32 seconds.</param> |
| | | 25 | | public ChronicleDuration(long wholeSeconds, uint fractionalSecond) |
| | | 26 | | { |
| | 27567 | 27 | | WholeSeconds = wholeSeconds; |
| | 27567 | 28 | | FractionalSecond = fractionalSecond; |
| | 27567 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary>Gets the zero interval.</summary> |
| | | 32 | | /// <value>Both components are zero, which is also the default value.</value> |
| | 2209 | 33 | | public static ChronicleDuration Zero => default; |
| | | 34 | | |
| | | 35 | | /// <summary>Gets floor seconds for this interval.</summary> |
| | | 36 | | /// <value>The signed mathematical floor, including for negative fractions.</value> |
| | | 37 | | public long WholeSeconds { get; } |
| | | 38 | | |
| | | 39 | | /// <summary>Gets the remainder after floor seconds.</summary> |
| | | 40 | | /// <value>A nonnegative count of 2^-32-second units, from zero through 2^32 − 1.</value> |
| | | 41 | | public uint FractionalSecond { get; } |
| | | 42 | | |
| | | 43 | | /// <summary>Orders this interval relative to another.</summary> |
| | | 44 | | /// <param name="other">The interval to order against.</param> |
| | | 45 | | /// <returns>A negative value for less, zero for equal, and a positive value for greater.</returns> |
| | | 46 | | public int CompareTo(ChronicleDuration other) |
| | | 47 | | { |
| | 42996 | 48 | | int seconds = WholeSeconds.CompareTo(other.WholeSeconds); |
| | 42996 | 49 | | return seconds != 0 ? seconds : FractionalSecond.CompareTo(other.FractionalSecond); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Tests exact interval equality.</summary> |
| | | 53 | | /// <param name="other">The interval to compare by component.</param> |
| | | 54 | | /// <returns>True when both components match.</returns> |
| | | 55 | | public bool Equals(ChronicleDuration other) => |
| | 26377 | 56 | | WholeSeconds == other.WholeSeconds && FractionalSecond == other.FractionalSecond; |
| | | 57 | | |
| | | 58 | | /// <summary>Tests equality with a boxed interval.</summary> |
| | | 59 | | /// <param name="obj">The object to compare. Null or another type compares unequal.</param> |
| | | 60 | | /// <returns>True when the object has this value type and both components match.</returns> |
| | 5 | 61 | | public override bool Equals(object? obj) => obj is ChronicleDuration other && Equals(other); |
| | | 62 | | |
| | | 63 | | /// <summary>Hashes the interval for use in collections.</summary> |
| | | 64 | | /// <returns>A deterministic 32-bit mix of both components.</returns> |
| | 4195 | 65 | | public override int GetHashCode() => unchecked( |
| | 4195 | 66 | | ((int)WholeSeconds ^ (int)(WholeSeconds >> 32)) * 397 ^ (int)FractionalSecond); |
| | | 67 | | |
| | | 68 | | /// <summary>Adds two exact intervals.</summary> |
| | | 69 | | /// <param name="left">The initial interval.</param> |
| | | 70 | | /// <param name="right">The interval to add, which may be negative.</param> |
| | | 71 | | /// <returns>The exact accumulated duration.</returns> |
| | | 72 | | /// <exception cref="OverflowException">The final interval is outside the signed whole-second range.</exception> |
| | | 73 | | public static ChronicleDuration operator +(ChronicleDuration left, ChronicleDuration right) |
| | | 74 | | { |
| | 10964 | 75 | | ulong fraction = (ulong)left.FractionalSecond + right.FractionalSecond; |
| | 10964 | 76 | | long seconds = unchecked(left.WholeSeconds + right.WholeSeconds + (long)(fraction >> 32)); |
| | | 77 | | // Test the final sign after carrying; the intermediate sum may overflow and recover. |
| | 10964 | 78 | | if ((left.WholeSeconds ^ right.WholeSeconds) >= 0 && (left.WholeSeconds ^ seconds) < 0) |
| | 1028 | 79 | | throw new OverflowException("The duration sum is outside the supported range."); |
| | 9936 | 80 | | return new ChronicleDuration(seconds, unchecked((uint)fraction)); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary>Subtracts one interval from another.</summary> |
| | | 84 | | /// <param name="left">The initial interval.</param> |
| | | 85 | | /// <param name="right">The interval to subtract, which may be negative.</param> |
| | | 86 | | /// <returns>The exact remaining duration.</returns> |
| | | 87 | | /// <exception cref="OverflowException">The final interval is outside the signed whole-second range.</exception> |
| | | 88 | | public static ChronicleDuration operator -(ChronicleDuration left, ChronicleDuration right) |
| | | 89 | | { |
| | 8587 | 90 | | ulong fraction = unchecked((ulong)left.FractionalSecond - right.FractionalSecond); |
| | | 91 | | // Fractional underflow sets bit 63; subtract that borrow before testing the final sign. |
| | 8587 | 92 | | long seconds = unchecked(left.WholeSeconds - right.WholeSeconds - (long)(fraction >> 63)); |
| | 8587 | 93 | | if ((left.WholeSeconds ^ right.WholeSeconds) < 0 && (left.WholeSeconds ^ seconds) < 0) |
| | 999 | 94 | | throw new OverflowException("The duration difference is outside the supported range."); |
| | 7588 | 95 | | return new ChronicleDuration(seconds, unchecked((uint)fraction)); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary>Tests whether the left interval exactly equals the right interval.</summary> |
| | | 99 | | /// <param name="left">The first operand.</param> |
| | | 100 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 101 | | /// <returns>True when the left operand exactly equals the right operand.</returns> |
| | 8169 | 102 | | public static bool operator ==(ChronicleDuration left, ChronicleDuration right) => left.Equals(right); |
| | | 103 | | /// <summary>Tests whether the left interval differs from the right interval.</summary> |
| | | 104 | | /// <param name="left">The first operand.</param> |
| | | 105 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 106 | | /// <returns>True when the left operand differs from the right operand.</returns> |
| | 6121 | 107 | | public static bool operator !=(ChronicleDuration left, ChronicleDuration right) => !left.Equals(right); |
| | | 108 | | /// <summary>Tests whether the left interval is less than the right interval.</summary> |
| | | 109 | | /// <param name="left">The first operand.</param> |
| | | 110 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 111 | | /// <returns>True when the left operand is less than the right operand.</returns> |
| | 6121 | 112 | | public static bool operator <(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) < 0; |
| | | 113 | | /// <summary>Tests whether the left interval is less than or equal to the right interval.</summary> |
| | | 114 | | /// <param name="left">The first operand.</param> |
| | | 115 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 116 | | /// <returns>True when the left operand is less than or equal to the right operand.</returns> |
| | 8318 | 117 | | public static bool operator <=(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) <= 0; |
| | | 118 | | /// <summary>Tests whether the left interval is greater than the right interval.</summary> |
| | | 119 | | /// <param name="left">The first operand.</param> |
| | | 120 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 121 | | /// <returns>True when the left operand is greater than the right operand.</returns> |
| | 6121 | 122 | | public static bool operator >(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) > 0; |
| | | 123 | | /// <summary>Tests whether the left interval is greater than or equal to the right interval.</summary> |
| | | 124 | | /// <param name="left">The first operand.</param> |
| | | 125 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 126 | | /// <returns>True when the left operand is greater than or equal to the right operand.</returns> |
| | 6121 | 127 | | public static bool operator >=(ChronicleDuration left, ChronicleDuration right) => left.CompareTo(right) >= 0; |
| | | 128 | | } |