| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | |
| | | 8 | | namespace FixedMathSharp; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a Q(64-SHIFT_AMOUNT).SHIFT_AMOUNT fixed-point number. |
| | | 12 | | /// Provides high precision for fixed-point arithmetic where SHIFT_AMOUNT bits |
| | | 13 | | /// are used for the fractional part and (64 - SHIFT_AMOUNT) bits for the integer part. |
| | | 14 | | /// The precision is determined by SHIFT_AMOUNT, which defines the resolution of fractional values. |
| | | 15 | | /// </summary> |
| | | 16 | | [Serializable] |
| | | 17 | | [MemoryPackable] |
| | | 18 | | public partial struct Fixed64 : IEquatable<Fixed64>, IComparable<Fixed64>, IEqualityComparer<Fixed64> |
| | | 19 | | { |
| | | 20 | | #region Static Readonly Fields |
| | | 21 | | |
| | | 22 | | public static readonly Fixed64 MAX_VALUE = new Fixed64(FixedMath.MAX_VALUE_L); |
| | | 23 | | public static readonly Fixed64 MIN_VALUE = new Fixed64(FixedMath.MIN_VALUE_L); |
| | | 24 | | |
| | | 25 | | public static readonly Fixed64 One = new Fixed64(FixedMath.ONE_L); |
| | | 26 | | public static readonly Fixed64 Two = One * 2; |
| | | 27 | | public static readonly Fixed64 Three = One * 3; |
| | | 28 | | public static readonly Fixed64 Half = One / 2; |
| | | 29 | | public static readonly Fixed64 Quarter = One / 4; |
| | | 30 | | public static readonly Fixed64 Eighth = One / 8; |
| | | 31 | | public static readonly Fixed64 Zero = new Fixed64(0); |
| | | 32 | | |
| | | 33 | | /// <inheritdoc cref="FixedMath.EPSILON_L" /> |
| | | 34 | | public static readonly Fixed64 Epsilon = new Fixed64(FixedMath.EPSILON_L); |
| | | 35 | | /// <inheritdoc cref="FixedMath.PRECISION_L" /> |
| | | 36 | | public static readonly Fixed64 Precision = new Fixed64(FixedMath.PRECISION_L); |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region Fields |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// The underlying raw long value representing the fixed-point number. |
| | | 44 | | /// </summary> |
| | | 45 | | [JsonInclude] |
| | | 46 | | [MemoryPackInclude] |
| | | 47 | | public long m_rawValue; |
| | | 48 | | |
| | | 49 | | #endregion |
| | | 50 | | |
| | | 51 | | #region Constructors |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Internal constructor for a Fixed64 from a raw long value. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="rawValue">Raw long value representing the fixed-point number.</param> |
| | 35263 | 57 | | internal Fixed64(long rawValue) => m_rawValue = rawValue; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Constructs a Fixed64 from an integer, with the fractional part set to zero. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="value">Integer value to convert to </param> |
| | 9174 | 63 | | public Fixed64(int value) : this((long)value << FixedMath.SHIFT_AMOUNT_I) { } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Constructs a Fixed64 from a double-precision floating-point value. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="value">Double value to convert to </param> |
| | 2781 | 69 | | public Fixed64(double value) : this((long)Math.Round((double)value * FixedMath.ONE_L)) { } |
| | | 70 | | |
| | | 71 | | #endregion |
| | | 72 | | |
| | | 73 | | #region Methods (Instance) |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Offsets the current Fixed64 by an integer value. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="x">The integer value to add.</param> |
| | | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 80 | | public void Offset(int x) |
| | 1 | 81 | | { |
| | 1 | 82 | | m_rawValue += (long)x << FixedMath.SHIFT_AMOUNT_I; |
| | 1 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Returns the raw value as a string. |
| | | 87 | | /// </summary> |
| | | 88 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 89 | | public string RawToString() |
| | 1 | 90 | | { |
| | 1 | 91 | | return m_rawValue.ToString(); |
| | 1 | 92 | | } |
| | | 93 | | |
| | | 94 | | #endregion |
| | | 95 | | |
| | | 96 | | #region Fixed64 Operations |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Creates a Fixed64 from a fractional number. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="numerator">The numerator of the fraction.</param> |
| | | 102 | | /// <param name="denominator">The denominator of the fraction.</param> |
| | | 103 | | /// <returns>A Fixed64 representing the fraction.</returns> |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | public static Fixed64 Fraction(double numerator, double denominator) |
| | 1 | 106 | | { |
| | 1 | 107 | | return new Fixed64(numerator / denominator); |
| | 1 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// x++ (post-increment) |
| | | 112 | | /// </summary> |
| | | 113 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 114 | | public static Fixed64 PostIncrement(ref Fixed64 a) |
| | 1 | 115 | | { |
| | 1 | 116 | | Fixed64 originalValue = a; |
| | 1 | 117 | | a.m_rawValue += One.m_rawValue; |
| | 1 | 118 | | return originalValue; |
| | 1 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// x-- (post-decrement) |
| | | 123 | | /// </summary> |
| | | 124 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 125 | | public static Fixed64 PostDecrement(ref Fixed64 a) |
| | 1 | 126 | | { |
| | 1 | 127 | | Fixed64 originalValue = a; |
| | 1 | 128 | | a.m_rawValue -= One.m_rawValue; |
| | 1 | 129 | | return originalValue; |
| | 1 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Counts the leading zeros in a 64-bit unsigned integer. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="x">The number to count leading zeros for.</param> |
| | | 136 | | /// <returns>The number of leading zeros.</returns> |
| | | 137 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 138 | | internal static int CountLeadingZeroes(ulong x) |
| | 633 | 139 | | { |
| | 633 | 140 | | int result = 0; |
| | 24003 | 141 | | while ((x & 0xF000000000000000) == 0) { result += 4; x <<= 4; } |
| | 6028 | 142 | | while ((x & 0x8000000000000000) == 0) { result += 1; x <<= 1; } |
| | 633 | 143 | | return result; |
| | 633 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Returns a number indicating the sign of a Fix64 number. |
| | | 148 | | /// Returns 1 if the value is positive, 0 if is 0, and -1 if it is negative. |
| | | 149 | | /// </summary> |
| | | 150 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 151 | | public static int Sign(Fixed64 value) |
| | 10 | 152 | | { |
| | | 153 | | // Return the sign of the value, optimizing for branchless comparison |
| | 10 | 154 | | return value.m_rawValue < 0 ? -1 : (value.m_rawValue > 0 ? 1 : 0); |
| | 10 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Returns true if the number has no decimal part (i.e., if the number is equivalent to an integer) and False other |
| | | 159 | | /// </summary> |
| | | 160 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 161 | | public static bool IsInteger(Fixed64 value) |
| | 7 | 162 | | { |
| | 7 | 163 | | return ((ulong)value.m_rawValue & FixedMath.MAX_SHIFTED_AMOUNT_UI) == 0; |
| | 7 | 164 | | } |
| | | 165 | | |
| | | 166 | | #endregion |
| | | 167 | | |
| | | 168 | | #region Explicit and Implicit Conversions |
| | | 169 | | |
| | | 170 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 171 | | public static explicit operator Fixed64(long value) |
| | 1 | 172 | | { |
| | 1 | 173 | | return FromRaw(value << FixedMath.SHIFT_AMOUNT_I); |
| | 1 | 174 | | } |
| | | 175 | | |
| | | 176 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 177 | | public static explicit operator long(Fixed64 value) |
| | 1 | 178 | | { |
| | 1 | 179 | | return value.m_rawValue >> FixedMath.SHIFT_AMOUNT_I; |
| | 1 | 180 | | } |
| | | 181 | | |
| | | 182 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 183 | | public static explicit operator Fixed64(int value) |
| | 2191 | 184 | | { |
| | 2191 | 185 | | return new Fixed64(value); |
| | 2191 | 186 | | } |
| | | 187 | | |
| | | 188 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 189 | | public static explicit operator int(Fixed64 value) |
| | 14 | 190 | | { |
| | 14 | 191 | | return (int)(value.m_rawValue >> FixedMath.SHIFT_AMOUNT_I); |
| | 14 | 192 | | } |
| | | 193 | | |
| | | 194 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 195 | | public static explicit operator Fixed64(float value) |
| | 3 | 196 | | { |
| | 3 | 197 | | return new Fixed64((double)value); |
| | 3 | 198 | | } |
| | | 199 | | |
| | | 200 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 201 | | public static explicit operator float(Fixed64 value) |
| | 1 | 202 | | { |
| | 1 | 203 | | return value.m_rawValue * FixedMath.SCALE_FACTOR_F; |
| | 1 | 204 | | } |
| | | 205 | | |
| | | 206 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 207 | | public static explicit operator Fixed64(double value) |
| | 95 | 208 | | { |
| | 95 | 209 | | return new Fixed64(value); |
| | 95 | 210 | | } |
| | | 211 | | |
| | | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 213 | | public static explicit operator double(Fixed64 value) |
| | 1494 | 214 | | { |
| | 1494 | 215 | | return value.m_rawValue * FixedMath.SCALE_FACTOR_D; |
| | 1494 | 216 | | } |
| | | 217 | | |
| | | 218 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 219 | | public static explicit operator Fixed64(decimal value) |
| | 1 | 220 | | { |
| | 1 | 221 | | return new Fixed64((double)value); |
| | 1 | 222 | | } |
| | | 223 | | |
| | | 224 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 225 | | public static explicit operator decimal(Fixed64 value) |
| | 1 | 226 | | { |
| | 1 | 227 | | return value.m_rawValue * FixedMath.SCALE_FACTOR_M; |
| | 1 | 228 | | } |
| | | 229 | | |
| | | 230 | | #endregion |
| | | 231 | | |
| | | 232 | | #region Arithmetic Operators |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Adds two Fixed64 numbers, with saturating behavior in case of overflow. |
| | | 236 | | /// </summary> |
| | | 237 | | public static Fixed64 operator +(Fixed64 x, Fixed64 y) |
| | 3879 | 238 | | { |
| | 3879 | 239 | | long xl = x.m_rawValue; |
| | 3879 | 240 | | long yl = y.m_rawValue; |
| | 3879 | 241 | | long sum = xl + yl; |
| | | 242 | | // Check for overflow, if signs of operands are equal and signs of sum and x are different |
| | 3879 | 243 | | if (((~(xl ^ yl) & (xl ^ sum)) & FixedMath.MIN_VALUE_L) != 0) |
| | 1 | 244 | | sum = xl > 0 ? FixedMath.MAX_VALUE_L : FixedMath.MIN_VALUE_L; |
| | 3879 | 245 | | return new Fixed64(sum); |
| | 3879 | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Adds an int to x |
| | | 250 | | /// </summary> |
| | | 251 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 252 | | public static Fixed64 operator +(Fixed64 x, int y) |
| | 12 | 253 | | { |
| | 12 | 254 | | return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) + y); |
| | 12 | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Adds an Fixed64 to x |
| | | 259 | | /// </summary> |
| | | 260 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 261 | | public static Fixed64 operator +(int x, Fixed64 y) |
| | 1 | 262 | | { |
| | 1 | 263 | | return y + x; |
| | 1 | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Adds a float to x |
| | | 268 | | /// </summary> |
| | | 269 | | public static Fixed64 operator +(Fixed64 x, float y) |
| | 12 | 270 | | { |
| | 12 | 271 | | return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) + y); |
| | 12 | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Adds a Fixed64 to x |
| | | 276 | | /// </summary> |
| | | 277 | | public static Fixed64 operator +(float x, Fixed64 y) |
| | 1 | 278 | | { |
| | 1 | 279 | | return y + x; |
| | 1 | 280 | | } |
| | | 281 | | |
| | | 282 | | /// <summary> |
| | | 283 | | /// Subtracts one Fixed64 number from another, with saturating behavior in case of overflow. |
| | | 284 | | /// </summary> |
| | | 285 | | public static Fixed64 operator -(Fixed64 x, Fixed64 y) |
| | 2478 | 286 | | { |
| | 2478 | 287 | | long xl = x.m_rawValue; |
| | 2478 | 288 | | long yl = y.m_rawValue; |
| | 2478 | 289 | | long diff = xl - yl; |
| | | 290 | | // Check for overflow, if signs of operands are different and signs of sum and x are different |
| | 2478 | 291 | | if ((((xl ^ yl) & (xl ^ diff)) & FixedMath.MIN_VALUE_L) != 0) |
| | 1 | 292 | | diff = xl < 0 ? FixedMath.MIN_VALUE_L : FixedMath.MAX_VALUE_L; |
| | 2478 | 293 | | return new Fixed64(diff); |
| | 2478 | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Subtracts an int from x |
| | | 298 | | /// </summary> |
| | | 299 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 300 | | public static Fixed64 operator -(Fixed64 x, int y) |
| | 6 | 301 | | { |
| | 6 | 302 | | return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) - y); |
| | 6 | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Subtracts a Fixed64 from x |
| | | 307 | | /// </summary> |
| | | 308 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 309 | | public static Fixed64 operator -(int x, Fixed64 y) |
| | 6 | 310 | | { |
| | 6 | 311 | | return new Fixed64(x - (y.m_rawValue * FixedMath.SCALE_FACTOR_D)); |
| | 6 | 312 | | } |
| | | 313 | | |
| | | 314 | | /// <summary> |
| | | 315 | | /// Subtracts a float from x |
| | | 316 | | /// </summary> |
| | | 317 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 318 | | public static Fixed64 operator -(Fixed64 x, float y) |
| | 6 | 319 | | { |
| | 6 | 320 | | return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) - y); |
| | 6 | 321 | | } |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// Subtracts a Fixed64 from x |
| | | 325 | | /// </summary> |
| | | 326 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 327 | | public static Fixed64 operator -(float x, Fixed64 y) |
| | 6 | 328 | | { |
| | 6 | 329 | | return new Fixed64(x - (y.m_rawValue * FixedMath.SCALE_FACTOR_D)); |
| | 6 | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Multiplies two Fixed64 numbers, handling overflow and rounding. |
| | | 334 | | /// </summary> |
| | | 335 | | public static Fixed64 operator *(Fixed64 x, Fixed64 y) |
| | 8301 | 336 | | { |
| | 8301 | 337 | | long xl = x.m_rawValue; |
| | 8301 | 338 | | long yl = y.m_rawValue; |
| | | 339 | | |
| | | 340 | | // Split both numbers into high and low parts |
| | 8301 | 341 | | ulong xlo = (ulong)(xl & FixedMath.MAX_SHIFTED_AMOUNT_UI); |
| | 8301 | 342 | | long xhi = xl >> FixedMath.SHIFT_AMOUNT_I; |
| | 8301 | 343 | | ulong ylo = (ulong)(yl & FixedMath.MAX_SHIFTED_AMOUNT_UI); |
| | 8301 | 344 | | long yhi = yl >> FixedMath.SHIFT_AMOUNT_I; |
| | | 345 | | |
| | | 346 | | // Perform partial products |
| | 8301 | 347 | | ulong lolo = xlo * ylo; // low bits * low bits |
| | 8301 | 348 | | long lohi = (long)xlo * yhi; // low bits * high bits |
| | 8301 | 349 | | long hilo = xhi * (long)ylo; // high bits * low bits |
| | 8301 | 350 | | long hihi = xhi * yhi; // high bits * high bits |
| | | 351 | | |
| | | 352 | | // Combine results, starting with the low part |
| | 8301 | 353 | | ulong loResult = lolo >> FixedMath.SHIFT_AMOUNT_I; |
| | 8301 | 354 | | long hiResult = hihi << FixedMath.SHIFT_AMOUNT_I; |
| | | 355 | | |
| | | 356 | | // Adjust rounding for the fractional part of the lolo term |
| | 8301 | 357 | | if ((lolo & (1UL << (FixedMath.SHIFT_AMOUNT_I - 1))) != 0) |
| | 951 | 358 | | loResult++; // Apply rounding up if the dropped bit is 1 (round half-up) |
| | | 359 | | |
| | 8301 | 360 | | bool overflow = false; |
| | 8301 | 361 | | long sum = FixedMath.AddOverflowHelper((long)loResult, lohi, ref overflow); |
| | 8301 | 362 | | sum = FixedMath.AddOverflowHelper(sum, hilo, ref overflow); |
| | 8301 | 363 | | sum = FixedMath.AddOverflowHelper(sum, hiResult, ref overflow); |
| | | 364 | | |
| | | 365 | | // Overflow handling |
| | 8301 | 366 | | bool opSignsEqual = ((xl ^ yl) & FixedMath.MIN_VALUE_L) == 0; |
| | | 367 | | |
| | | 368 | | // Positive overflow check |
| | 8301 | 369 | | if (opSignsEqual) |
| | 7844 | 370 | | { |
| | 7844 | 371 | | if (sum < 0 || (overflow && xl > 0)) |
| | 1 | 372 | | return MAX_VALUE; |
| | 7843 | 373 | | } |
| | | 374 | | else |
| | 457 | 375 | | { |
| | 457 | 376 | | if (sum > 0) |
| | 1 | 377 | | return MIN_VALUE; |
| | 456 | 378 | | } |
| | | 379 | | |
| | | 380 | | // Final overflow check: if the high 32 bits are non-zero or non-sign-extended, it's an overflow |
| | 8299 | 381 | | long topCarry = hihi >> FixedMath.SHIFT_AMOUNT_I; |
| | 8299 | 382 | | if (topCarry != 0 && topCarry != -1) |
| | 0 | 383 | | return opSignsEqual ? MAX_VALUE : MIN_VALUE; |
| | | 384 | | |
| | | 385 | | // Negative overflow check |
| | 8299 | 386 | | if (!opSignsEqual) |
| | 456 | 387 | | { |
| | 456 | 388 | | long posOp = xl > yl ? xl : yl; |
| | 456 | 389 | | long negOp = xl < yl ? xl : yl; |
| | | 390 | | |
| | 456 | 391 | | if (sum > negOp && negOp < -FixedMath.ONE_L && posOp > FixedMath.ONE_L) |
| | 0 | 392 | | return MIN_VALUE; |
| | 456 | 393 | | } |
| | | 394 | | |
| | 8299 | 395 | | return new Fixed64(sum); |
| | 8301 | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Multiplies a Fixed64 by an integer. |
| | | 400 | | /// </summary> |
| | | 401 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 402 | | public static Fixed64 operator *(Fixed64 x, int y) |
| | 157 | 403 | | { |
| | 157 | 404 | | return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) * y); |
| | 157 | 405 | | } |
| | | 406 | | |
| | | 407 | | /// <summary> |
| | | 408 | | /// Multiplies an integer by a |
| | | 409 | | /// </summary> |
| | | 410 | | public static Fixed64 operator *(int x, Fixed64 y) |
| | 29 | 411 | | { |
| | 29 | 412 | | return y * x; |
| | 29 | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Divides one Fixed64 number by another, handling division by zero and overflow. |
| | | 417 | | /// </summary> |
| | | 418 | | public static Fixed64 operator /(Fixed64 x, Fixed64 y) |
| | 868 | 419 | | { |
| | 868 | 420 | | long xl = x.m_rawValue; |
| | 868 | 421 | | long yl = y.m_rawValue; |
| | | 422 | | |
| | 868 | 423 | | if (yl == 0) |
| | 1 | 424 | | throw new DivideByZeroException($"Attempted to divide {x} by zero."); |
| | | 425 | | |
| | 867 | 426 | | ulong remainder = (ulong)(xl < 0 ? -xl : xl); |
| | 867 | 427 | | ulong divider = (ulong)(yl < 0 ? -yl : yl); |
| | 867 | 428 | | ulong quotient = 0UL; |
| | 867 | 429 | | int bitPos = FixedMath.SHIFT_AMOUNT_I + 1; |
| | | 430 | | |
| | | 431 | | // If the divider is divisible by 2^n, take advantage of it. |
| | 6297 | 432 | | while ((divider & 0xF) == 0 && bitPos >= 4) |
| | 5430 | 433 | | { |
| | 5430 | 434 | | divider >>= 4; |
| | 5430 | 435 | | bitPos -= 4; |
| | 5430 | 436 | | } |
| | | 437 | | |
| | 1496 | 438 | | while (remainder != 0 && bitPos >= 0) |
| | 631 | 439 | | { |
| | 631 | 440 | | int shift = CountLeadingZeroes(remainder); |
| | 631 | 441 | | if (shift > bitPos) |
| | 509 | 442 | | shift = bitPos; |
| | | 443 | | |
| | 631 | 444 | | remainder <<= shift; |
| | 631 | 445 | | bitPos -= shift; |
| | | 446 | | |
| | 631 | 447 | | ulong div = remainder / divider; |
| | 631 | 448 | | remainder %= divider; |
| | 631 | 449 | | quotient += div << bitPos; |
| | | 450 | | |
| | | 451 | | // Detect overflow |
| | 631 | 452 | | if ((div & ~(0xFFFFFFFFFFFFFFFF >> bitPos)) != 0) |
| | 2 | 453 | | return ((xl ^ yl) & FixedMath.MIN_VALUE_L) == 0 ? MAX_VALUE : MIN_VALUE; |
| | | 454 | | |
| | 629 | 455 | | remainder <<= 1; |
| | 629 | 456 | | --bitPos; |
| | 629 | 457 | | } |
| | | 458 | | |
| | | 459 | | // Rounding logic: "Round half to even" or "Banker's rounding" |
| | 865 | 460 | | if ((quotient & 0x1) != 0) |
| | 143 | 461 | | quotient += 1; |
| | | 462 | | |
| | 865 | 463 | | long result = (long)(quotient >> 1); |
| | 865 | 464 | | if (((xl ^ yl) & FixedMath.MIN_VALUE_L) != 0) |
| | 41 | 465 | | result = -result; |
| | | 466 | | |
| | 865 | 467 | | return new Fixed64(result); |
| | 867 | 468 | | } |
| | | 469 | | |
| | | 470 | | /// <summary> |
| | | 471 | | /// Divides a Fixed64 by an integer. |
| | | 472 | | /// </summary> |
| | | 473 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 474 | | public static Fixed64 operator /(Fixed64 x, int y) |
| | 124 | 475 | | { |
| | 124 | 476 | | return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) / y); |
| | 124 | 477 | | } |
| | | 478 | | |
| | | 479 | | /// <summary> |
| | | 480 | | /// Computes the remainder of division of one Fixed64 number by another. |
| | | 481 | | /// </summary> |
| | | 482 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 483 | | public static Fixed64 operator %(Fixed64 x, Fixed64 y) |
| | 172 | 484 | | { |
| | 172 | 485 | | if (x.m_rawValue == FixedMath.MIN_VALUE_L && y.m_rawValue == -1) |
| | 1 | 486 | | return Zero; |
| | 171 | 487 | | return new Fixed64(x.m_rawValue % y.m_rawValue); |
| | 172 | 488 | | } |
| | | 489 | | |
| | | 490 | | /// <summary> |
| | | 491 | | /// Unary negation operator. |
| | | 492 | | /// </summary> |
| | | 493 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 494 | | public static Fixed64 operator -(Fixed64 x) |
| | 1199 | 495 | | { |
| | 1199 | 496 | | return x.m_rawValue == FixedMath.MIN_VALUE_L ? MAX_VALUE : new Fixed64(-x.m_rawValue); |
| | 1199 | 497 | | } |
| | | 498 | | |
| | | 499 | | /// <summary> |
| | | 500 | | /// Pre-increment operator (++x). |
| | | 501 | | /// </summary> |
| | | 502 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 503 | | public static Fixed64 operator ++(Fixed64 a) |
| | 1 | 504 | | { |
| | 1 | 505 | | a.m_rawValue += One.m_rawValue; |
| | 1 | 506 | | return a; |
| | 1 | 507 | | } |
| | | 508 | | |
| | | 509 | | /// <summary> |
| | | 510 | | /// Pre-decrement operator (--x). |
| | | 511 | | /// </summary> |
| | | 512 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 513 | | public static Fixed64 operator --(Fixed64 a) |
| | 1 | 514 | | { |
| | 1 | 515 | | a.m_rawValue -= One.m_rawValue; |
| | 1 | 516 | | return a; |
| | 1 | 517 | | } |
| | | 518 | | |
| | | 519 | | /// <summary> |
| | | 520 | | /// Bitwise left shift operator. |
| | | 521 | | /// </summary> |
| | | 522 | | /// <param name="a">Operand to shift.</param> |
| | | 523 | | /// <param name="shift">Number of bits to shift.</param> |
| | | 524 | | /// <returns>The shifted value.</returns> |
| | | 525 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 526 | | public static Fixed64 operator <<(Fixed64 a, int shift) |
| | 1 | 527 | | { |
| | 1 | 528 | | return new Fixed64(a.m_rawValue << shift); |
| | 1 | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <summary> |
| | | 532 | | /// Bitwise right shift operator. |
| | | 533 | | /// </summary> |
| | | 534 | | /// <param name="a">Operand to shift.</param> |
| | | 535 | | /// <param name="shift">Number of bits to shift.</param> |
| | | 536 | | /// <returns>The shifted value.</returns> |
| | | 537 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 538 | | public static Fixed64 operator >>(Fixed64 a, int shift) |
| | 1 | 539 | | { |
| | 1 | 540 | | return new Fixed64(a.m_rawValue >> shift); |
| | 1 | 541 | | } |
| | | 542 | | |
| | | 543 | | #endregion |
| | | 544 | | |
| | | 545 | | #region Comparison Operators |
| | | 546 | | |
| | | 547 | | /// <summary> |
| | | 548 | | /// Determines whether one Fixed64 is greater than another. |
| | | 549 | | /// </summary> |
| | | 550 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 551 | | public static bool operator >(Fixed64 x, Fixed64 y) |
| | 1365 | 552 | | { |
| | 1365 | 553 | | return x.m_rawValue > y.m_rawValue; |
| | 1365 | 554 | | } |
| | | 555 | | |
| | | 556 | | /// <summary> |
| | | 557 | | /// Determines whether one Fixed64 is less than another. |
| | | 558 | | /// </summary> |
| | | 559 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 560 | | public static bool operator <(Fixed64 x, Fixed64 y) |
| | 11572 | 561 | | { |
| | 11572 | 562 | | return x.m_rawValue < y.m_rawValue; |
| | 11572 | 563 | | } |
| | | 564 | | |
| | | 565 | | /// <summary> |
| | | 566 | | /// Determines whether one Fixed64 is greater than or equal to another. |
| | | 567 | | /// </summary> |
| | | 568 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 569 | | public static bool operator >=(Fixed64 x, Fixed64 y) |
| | 14538 | 570 | | { |
| | 14538 | 571 | | return x.m_rawValue >= y.m_rawValue; |
| | 14538 | 572 | | } |
| | | 573 | | |
| | | 574 | | /// <summary> |
| | | 575 | | /// Determines whether one Fixed64 is less than or equal to another. |
| | | 576 | | /// </summary> |
| | | 577 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 578 | | public static bool operator <=(Fixed64 x, Fixed64 y) |
| | 4970 | 579 | | { |
| | 4970 | 580 | | return x.m_rawValue <= y.m_rawValue; |
| | 4970 | 581 | | } |
| | | 582 | | |
| | | 583 | | /// <summary> |
| | | 584 | | /// Determines whether two Fixed64 instances are equal. |
| | | 585 | | /// </summary> |
| | | 586 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 587 | | public static bool operator ==(Fixed64 left, Fixed64 right) |
| | 4389 | 588 | | { |
| | 4389 | 589 | | return left.Equals(right); |
| | 4389 | 590 | | } |
| | | 591 | | |
| | | 592 | | /// <summary> |
| | | 593 | | /// Determines whether two Fixed64 instances are not equal. |
| | | 594 | | /// </summary> |
| | | 595 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 596 | | public static bool operator !=(Fixed64 left, Fixed64 right) |
| | 383 | 597 | | { |
| | 383 | 598 | | return !left.Equals(right); |
| | 383 | 599 | | } |
| | | 600 | | |
| | | 601 | | #endregion |
| | | 602 | | |
| | | 603 | | #region Conversion |
| | | 604 | | |
| | | 605 | | /// <summary> |
| | | 606 | | /// Returns the string representation of this Fixed64 instance. |
| | | 607 | | /// </summary> |
| | | 608 | | /// <remarks> |
| | | 609 | | /// Up to 10 decimal places. |
| | | 610 | | /// </remarks> |
| | | 611 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 612 | | public override string ToString() |
| | 1007 | 613 | | { |
| | 1007 | 614 | | return ((double)this).ToString(CultureInfo.InvariantCulture); |
| | 1007 | 615 | | } |
| | | 616 | | |
| | | 617 | | /// <summary> |
| | | 618 | | /// Converts the numeric value of the current Fixed64 object to its equivalent string representation. |
| | | 619 | | /// </summary> |
| | | 620 | | /// <param name="format">A format specification that governs how the current Fixed64 object is converted.</param> |
| | | 621 | | /// <returns>The string representation of the value of the current Fixed64 object.</returns> |
| | | 622 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 623 | | public string ToString(string format) |
| | 1 | 624 | | { |
| | 1 | 625 | | return ((double)this).ToString(format, CultureInfo.InvariantCulture); |
| | 1 | 626 | | } |
| | | 627 | | |
| | | 628 | | /// <summary> |
| | | 629 | | /// Parses a string to create a Fixed64 instance. |
| | | 630 | | /// </summary> |
| | | 631 | | /// <param name="s">The string representation of the </param> |
| | | 632 | | /// <returns>The parsed Fixed64 value.</returns> |
| | | 633 | | public static Fixed64 Parse(string s) |
| | 5 | 634 | | { |
| | 7 | 635 | | if (string.IsNullOrEmpty(s)) throw new ArgumentNullException(nameof(s)); |
| | | 636 | | |
| | | 637 | | // Check if the value is negative |
| | 3 | 638 | | bool isNegative = false; |
| | 3 | 639 | | if (s[0] == '-') |
| | 1 | 640 | | { |
| | 1 | 641 | | isNegative = true; |
| | 1 | 642 | | s = s.Substring(1); |
| | 1 | 643 | | } |
| | | 644 | | |
| | 3 | 645 | | if (!long.TryParse(s, out long rawValue)) |
| | 1 | 646 | | throw new FormatException($"Invalid format: {s}"); |
| | | 647 | | |
| | | 648 | | // If the value was negative, negate the result |
| | 2 | 649 | | if (isNegative) |
| | 1 | 650 | | rawValue = -rawValue; |
| | | 651 | | |
| | 2 | 652 | | return FromRaw(rawValue); |
| | 2 | 653 | | } |
| | | 654 | | |
| | | 655 | | /// <summary> |
| | | 656 | | /// Tries to parse a string to create a Fixed64 instance. |
| | | 657 | | /// </summary> |
| | | 658 | | /// <param name="s">The string representation of the </param> |
| | | 659 | | /// <param name="result">The parsed Fixed64 value.</param> |
| | | 660 | | /// <returns>True if parsing succeeded; otherwise, false.</returns> |
| | | 661 | | public static bool TryParse(string s, out Fixed64 result) |
| | 4 | 662 | | { |
| | 4 | 663 | | result = Zero; |
| | 5 | 664 | | if (string.IsNullOrEmpty(s)) return false; |
| | | 665 | | |
| | | 666 | | // Check if the value is negative |
| | 3 | 667 | | bool isNegative = false; |
| | 3 | 668 | | if (s[0] == '-') |
| | 1 | 669 | | { |
| | 1 | 670 | | isNegative = true; |
| | 1 | 671 | | s = s.Substring(1); |
| | 1 | 672 | | } |
| | | 673 | | |
| | 4 | 674 | | if (!long.TryParse(s, out long rawValue)) return false; |
| | | 675 | | |
| | | 676 | | // If the value was negative, negate the result |
| | 2 | 677 | | if (isNegative) |
| | 1 | 678 | | rawValue = -rawValue; |
| | | 679 | | |
| | 2 | 680 | | result = FromRaw(rawValue); |
| | 2 | 681 | | return true; |
| | 4 | 682 | | } |
| | | 683 | | |
| | | 684 | | /// <summary> |
| | | 685 | | /// Creates a Fixed64 from a raw long value. |
| | | 686 | | /// </summary> |
| | | 687 | | /// <param name="rawValue">The raw long value.</param> |
| | | 688 | | /// <returns>A Fixed64 representing the raw value.</returns> |
| | | 689 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 690 | | public static Fixed64 FromRaw(long rawValue) |
| | 14374 | 691 | | { |
| | 14374 | 692 | | return new Fixed64(rawValue); |
| | 14374 | 693 | | } |
| | | 694 | | |
| | | 695 | | /// <summary> |
| | | 696 | | /// Converts a Fixed64s RawValue (Int64) into a double |
| | | 697 | | /// </summary> |
| | | 698 | | /// <param name="f1"></param> |
| | | 699 | | /// <returns></returns> |
| | | 700 | | public static double ToDouble(long f1) |
| | 1 | 701 | | { |
| | 1 | 702 | | return f1 * FixedMath.SCALE_FACTOR_D; |
| | 1 | 703 | | } |
| | | 704 | | |
| | | 705 | | /// <summary> |
| | | 706 | | /// Converts a Fixed64s RawValue (Int64) into a float |
| | | 707 | | /// </summary> |
| | | 708 | | /// <param name="f1"></param> |
| | | 709 | | /// <returns></returns> |
| | | 710 | | public static float ToFloat(long f1) |
| | 1 | 711 | | { |
| | 1 | 712 | | return f1 * FixedMath.SCALE_FACTOR_F; |
| | 1 | 713 | | } |
| | | 714 | | |
| | | 715 | | /// <summary> |
| | | 716 | | /// Converts a Fixed64s RawValue (Int64) into a decimal |
| | | 717 | | /// </summary> |
| | | 718 | | /// <param name="f1"></param> |
| | | 719 | | /// <returns></returns> |
| | | 720 | | public static decimal ToDecimal(long f1) |
| | 1 | 721 | | { |
| | 1 | 722 | | return f1 * FixedMath.SCALE_FACTOR_M; |
| | 1 | 723 | | } |
| | | 724 | | |
| | | 725 | | #endregion |
| | | 726 | | |
| | | 727 | | #region Equality, HashCode, Comparable Overrides |
| | | 728 | | |
| | | 729 | | /// <summary> |
| | | 730 | | /// Determines whether this instance equals another object. |
| | | 731 | | /// </summary> |
| | | 732 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 733 | | public override bool Equals(object? obj) |
| | 6 | 734 | | { |
| | 6 | 735 | | return obj is Fixed64 other && Equals(other); |
| | 6 | 736 | | } |
| | | 737 | | |
| | | 738 | | /// <summary> |
| | | 739 | | /// Determines whether this instance equals another |
| | | 740 | | /// </summary> |
| | | 741 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 742 | | public bool Equals(Fixed64 other) |
| | 7211 | 743 | | { |
| | 7211 | 744 | | return m_rawValue == other.m_rawValue; |
| | 7211 | 745 | | } |
| | | 746 | | |
| | | 747 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 748 | | public bool Equals(Fixed64 x, Fixed64 y) |
| | 5 | 749 | | { |
| | 5 | 750 | | return x.Equals(y); |
| | 5 | 751 | | } |
| | | 752 | | |
| | | 753 | | /// <summary> |
| | | 754 | | /// Returns the hash code for this Fixed64 instance. |
| | | 755 | | /// </summary> |
| | | 756 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 757 | | public override int GetHashCode() |
| | 131 | 758 | | { |
| | 131 | 759 | | return m_rawValue.GetHashCode(); |
| | 131 | 760 | | } |
| | | 761 | | |
| | | 762 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 763 | | public int GetHashCode(Fixed64 obj) |
| | 1 | 764 | | { |
| | 1 | 765 | | return obj.GetHashCode(); |
| | 1 | 766 | | } |
| | | 767 | | |
| | | 768 | | /// <summary> |
| | | 769 | | /// Compares this instance to another |
| | | 770 | | /// </summary> |
| | | 771 | | /// <param name="other">The Fixed64 to compare with.</param> |
| | | 772 | | /// <returns>-1 if less than, 0 if equal, 1 if greater than other.</returns> |
| | | 773 | | public int CompareTo(Fixed64 other) |
| | 36 | 774 | | { |
| | 36 | 775 | | return m_rawValue.CompareTo(other.m_rawValue); |
| | 36 | 776 | | } |
| | | 777 | | |
| | | 778 | | #endregion |
| | | 779 | | } |