Skip to content

Commit 54998f0

Browse files
committed
fix: circular static constructor error when operators are used during cctor
1 parent 2df922b commit 54998f0

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/FixedMathSharp/Core/FixedMath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public static Fixed64 Abs(Fixed64 value)
160160
{
161161
// For the minimum value, return the max to avoid overflow
162162
if (value.m_rawValue == MIN_VALUE_L)
163-
return Fixed64.MAX_VALUE;
163+
return new Fixed64(MAX_VALUE_L);
164164

165165
// Use branchless absolute value calculation
166166
long mask = value.m_rawValue >> 63; // If negative, mask will be all 1s; if positive, all 0s

src/FixedMathSharp/Core/FixedTrigonometry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public static Fixed64 Pow2(Fixed64 x)
159159
return neg ? Fixed64.One / Fixed64.Two : Fixed64.Two;
160160

161161
if (x >= LOG_2_MAX)
162-
return neg ? Fixed64.One / Fixed64.MAX_VALUE : Fixed64.MAX_VALUE;
162+
return neg ? Fixed64.One / new Fixed64(MAX_VALUE_L) : new Fixed64(MAX_VALUE_L);
163163

164164
/*
165165
* Taylor series expansion for exp(x)

src/FixedMathSharp/Numerics/Fixed64.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public static explicit operator decimal(Fixed64 value)
420420
if (!negative)
421421
{
422422
if (roundedOverflow || magnitude > long.MaxValue)
423-
return MAX_VALUE;
423+
return new Fixed64(FixedMath.MAX_VALUE_L);
424424

425425
return new Fixed64((long)magnitude);
426426
}
@@ -430,10 +430,10 @@ public static explicit operator decimal(Fixed64 value)
430430
const ulong minValueMagnitude = 0x8000000000000000UL;
431431

432432
if (roundedOverflow || magnitude > minValueMagnitude)
433-
return MIN_VALUE;
433+
return new Fixed64(FixedMath.MIN_VALUE_L);
434434

435435
if (magnitude == minValueMagnitude)
436-
return MIN_VALUE;
436+
return new Fixed64(FixedMath.MIN_VALUE_L);
437437

438438
return new Fixed64(-(long)magnitude);
439439
}
@@ -577,7 +577,9 @@ private static ulong ShiftRightRoundedToEven(ulong hi, ulong lo, int shift, out
577577

578578
// Detect overflow
579579
if ((div & ~(0xFFFFFFFFFFFFFFFF >> bitPos)) != 0)
580-
return ((xl ^ yl) & FixedMath.MIN_VALUE_L) == 0 ? MAX_VALUE : MIN_VALUE;
580+
return ((xl ^ yl) & FixedMath.MIN_VALUE_L) == 0
581+
? new Fixed64(FixedMath.MAX_VALUE_L)
582+
: new Fixed64(FixedMath.MIN_VALUE_L);
581583

582584
remainder <<= 1;
583585
--bitPos;
@@ -629,7 +631,9 @@ private static ulong ShiftRightRoundedToEven(ulong hi, ulong lo, int shift, out
629631
[MethodImpl(MethodImplOptions.AggressiveInlining)]
630632
public static Fixed64 operator -(Fixed64 x)
631633
{
632-
return x.m_rawValue == FixedMath.MIN_VALUE_L ? MAX_VALUE : new Fixed64(-x.m_rawValue);
634+
return x.m_rawValue == FixedMath.MIN_VALUE_L
635+
? new Fixed64(FixedMath.MAX_VALUE_L)
636+
: new Fixed64(-x.m_rawValue);
633637
}
634638

635639
/// <summary>

0 commit comments

Comments
 (0)