| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | #nullable enable |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | namespace Lattice.Math; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// 定点数 (Q48.16):48位整数 + 16位小数 |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// <para>Lattice 定点数系统使用 64 位有符号整数存储,其中:</para> |
| | | 18 | | /// <list type="bullet"> |
| | | 19 | | /// <item>48 位:整数部分(范围约 ±140万亿)</item> |
| | | 20 | | /// <item>16 位:小数部分(精度约 0.000015)</item> |
| | | 21 | | /// </list> |
| | | 22 | | /// <para>安全乘法范围:±32,768(两个操作数都需在此范围内)</para> |
| | | 23 | | /// <para>参考:FrameSyncEngine / Photon Quantum 设计</para> |
| | | 24 | | /// <para>用途:确定性帧同步,完全替代 float/double</para> |
| | | 25 | | /// </remarks> |
| | | 26 | | /// <example> |
| | | 27 | | /// 基本使用: |
| | | 28 | | /// <code> |
| | | 29 | | /// // 初始化 LUT(游戏启动时) |
| | | 30 | | /// FPLut.InitializeBuiltIn(); |
| | | 31 | | /// |
| | | 32 | | /// // 创建定点数 |
| | | 33 | | /// FP a = 3.5; // 隐式转换 |
| | | 34 | | /// FP b = FP.FromRaw(FP.Raw._1_50); // 使用原始常量 |
| | | 35 | | /// |
| | | 36 | | /// // 数学运算 |
| | | 37 | | /// FP c = a * b; // 乘法(四舍五入) |
| | | 38 | | /// FP d = FP.Sqrt(c); // 平方根 |
| | | 39 | | /// FP e = FP.Sin(a); // 正弦 |
| | | 40 | | /// </code> |
| | | 41 | | /// </example> |
| | | 42 | | [StructLayout(LayoutKind.Explicit, Size = 8)] // 8 bytes, Explicit for cross-platform consistency |
| | | 43 | | public readonly partial struct FP : IEquatable<FP>, IComparable<FP> |
| | | 44 | | { |
| | | 45 | | #region 常量定义 |
| | | 46 | | |
| | | 47 | | /// <summary>小数位数量</summary> |
| | | 48 | | public const int FRACTIONAL_BITS = 16; |
| | | 49 | | |
| | | 50 | | /// <summary>比例因子 = 2^16 = 65536</summary> |
| | | 51 | | public const long ONE = 1L << FRACTIONAL_BITS; |
| | | 52 | | |
| | | 53 | | /// <summary>乘法舍入常量:0.5 LSB = 2^15</summary> |
| | | 54 | | /// <remarks> |
| | | 55 | | /// 用于乘法四舍五入:(a * b + MulRound) >> 16 |
| | | 56 | | /// 相比截断,精度误差减半 |
| | | 57 | | /// </remarks> |
| | | 58 | | internal const long MulRound = 1L << (FRACTIONAL_BITS - 1); // 32768 |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// 安全乘法上限:sqrt(long.MaxValue) ≈ 3,037,000,499 |
| | | 62 | | /// 对应真实值约 46,340 |
| | | 63 | | /// <para>两个 FP 都 ≤ UseableMax 时,乘法不会溢出 long</para> |
| | | 64 | | /// </summary> |
| | 1 | 65 | | public static readonly FP UseableMax = new(3037000499L); |
| | | 66 | | |
| | | 67 | | /// <summary>安全乘法下限(对应真实值约 -46,340)</summary> |
| | 1 | 68 | | public static readonly FP UseableMin = new(-3037000499L); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// 最小精度:1/65536 ≈ 0.000015 |
| | | 72 | | /// <para>注意:这是 Q48.16 能表示的最小单位,不是 float.Epsilon 的概念</para> |
| | | 73 | | /// </summary> |
| | 1 | 74 | | public static readonly FP Epsilon = new(1L); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// 默认近似比较容差(10倍精度,约 0.00015) |
| | | 78 | | /// <para>用于 Approximately 的默认参数</para> |
| | | 79 | | /// </summary> |
| | 1 | 80 | | public static readonly FP EpsilonDefault = new(10L); |
| | | 81 | | |
| | | 82 | | /// <summary>零</summary> |
| | 1889 | 83 | | public static FP Zero => new(0); |
| | | 84 | | |
| | | 85 | | /// <summary>一</summary> |
| | 124 | 86 | | public static FP One => new(ONE); |
| | | 87 | | |
| | | 88 | | /// <summary>二</summary> |
| | 0 | 89 | | public static FP Two => new(ONE * 2); |
| | | 90 | | |
| | | 91 | | /// <summary>一半 (0.5)</summary> |
| | 0 | 92 | | public static FP Half => new(ONE >> 1); |
| | | 93 | | |
| | | 94 | | /// <summary>π (3.14159...)</summary> |
| | 18 | 95 | | public static FP Pi => new(205887L); // 3.1415863037109375 |
| | | 96 | | |
| | | 97 | | /// <summary>2π</summary> |
| | 8 | 98 | | public static FP Pi2 => new(411774L); // 6.283172607421875 |
| | | 99 | | |
| | | 100 | | /// <summary>π/2</summary> |
| | 1014 | 101 | | public static FP PiHalf => new(102943L); // 1.57079315185546875 |
| | | 102 | | |
| | | 103 | | // 快捷常量命名(参考 Quantum 风格) |
| | 2439 | 104 | | public static FP _0 => new(0); |
| | 343 | 105 | | public static FP _1 => new(ONE); |
| | 24470 | 106 | | public static FP _2 => new(ONE * 2); |
| | 25 | 107 | | public static FP _3 => new(ONE * 3); |
| | 6 | 108 | | public static FP _4 => new(ONE * 4); |
| | 16 | 109 | | public static FP _5 => new(ONE * 5); |
| | 16 | 110 | | public static FP _10 => new(ONE * 10); |
| | 24204 | 111 | | public static FP _100 => new(ONE * 100); |
| | 3449 | 112 | | public static FP _1000 => new(ONE * 1000); |
| | | 113 | | |
| | 9 | 114 | | public static FP _0_01 => new(655); // 0.01 (655.36 截断) |
| | 0 | 115 | | public static FP _0_05 => new(3277); // 0.05 (3276.8 四舍五入) |
| | 24211 | 116 | | public static FP _0_10 => new(6554); // 0.1 |
| | 3 | 117 | | public static FP _0_25 => new(16384); // 0.25 (精确) |
| | 139 | 118 | | public static FP _0_50 => new(ONE >> 1); // 0.5 |
| | 1 | 119 | | public static FP _0_75 => new((ONE >> 1) + (ONE >> 2)); // 0.75 |
| | 24202 | 120 | | public static FP _1_50 => new(ONE + (ONE >> 1)); // 1.5 |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Raw 常量:用于编译期确定的场景 |
| | | 124 | | /// <para>命名规范:_整数 或 _整数_小数</para> |
| | | 125 | | /// </summary> |
| | | 126 | | public static class Raw |
| | | 127 | | { |
| | | 128 | | // ==================== 整数常量 ==================== |
| | | 129 | | public const long _0 = 0; |
| | | 130 | | public const long _1 = ONE; |
| | | 131 | | public const long _2 = ONE * 2; |
| | | 132 | | public const long _3 = ONE * 3; |
| | | 133 | | public const long _4 = ONE * 4; |
| | | 134 | | public const long _5 = ONE * 5; |
| | | 135 | | public const long _6 = ONE * 6; |
| | | 136 | | public const long _7 = ONE * 7; |
| | | 137 | | public const long _8 = ONE * 8; |
| | | 138 | | public const long _9 = ONE * 9; |
| | | 139 | | public const long _10 = ONE * 10; |
| | | 140 | | public const long _99 = 6488064L; // 99 |
| | | 141 | | public const long _100 = ONE * 100; |
| | | 142 | | public const long _180 = 11796480L; // 180 |
| | | 143 | | public const long _200 = 13107200L; // 200 |
| | | 144 | | public const long _360 = 23592960L; // 360 |
| | | 145 | | public const long _1000 = ONE * 1000; |
| | | 146 | | public const long _10000 = 655360000L; // 10000 |
| | | 147 | | |
| | | 148 | | // ==================== 小数常量 (0.01 ~ 0.99) ==================== |
| | | 149 | | public const long _0_01 = 655L; // 0.01 |
| | | 150 | | public const long _0_016 = 1049L; // 0.016 ≈ 1/60s (60fps) |
| | | 151 | | public const long _0_02 = 1311L; // 0.02 |
| | | 152 | | public const long _0_03 = 1966L; // 0.03 |
| | | 153 | | public const long _0_04 = 2621L; // 0.04 |
| | | 154 | | public const long _0_05 = 3277L; // 0.05 |
| | | 155 | | public const long _0_10 = 6554L; // 0.1 |
| | | 156 | | public const long _0_20 = 13107L; // 0.2 |
| | | 157 | | public const long _0_25 = 16384L; // 0.25 (精确) |
| | | 158 | | public const long _0_33 = 21845L; // 0.33 (1/3 近似) |
| | | 159 | | public const long _0_50 = ONE >> 1; // 0.5 |
| | | 160 | | public const long _0_66 = 43691L; // 0.66 (2/3 近似) |
| | | 161 | | public const long _0_75 = (ONE >> 1) + (ONE >> 2); // 0.75 |
| | | 162 | | public const long _0_99 = 64881L; // 0.99 |
| | | 163 | | |
| | | 164 | | // ==================== 大于1的常量 ==================== |
| | | 165 | | public const long _1_01 = 66191L; // 1.01 |
| | | 166 | | public const long _1_02 = 66847L; // 1.02 |
| | | 167 | | public const long _1_03 = 67502L; // 1.03 |
| | | 168 | | public const long _1_04 = 68157L; // 1.04 |
| | | 169 | | public const long _1_05 = 68813L; // 1.05 |
| | | 170 | | public const long _1_10 = 72090L; // 1.10 |
| | | 171 | | public const long _1_20 = 78643L; // 1.20 |
| | | 172 | | public const long _1_25 = 81920L; // 1.25 |
| | | 173 | | public const long _1_33 = 87381L; // 1.33 (4/3 近似) |
| | | 174 | | public const long _1_50 = ONE + (ONE >> 1); // 1.5 |
| | | 175 | | public const long _1_75 = 114688L; // 1.75 |
| | | 176 | | public const long _1_99 = 130417L; // 1.99 |
| | | 177 | | |
| | | 178 | | // ==================== 角度弧度常量 ==================== |
| | | 179 | | public const long _PI = 205887; // π ≈ 3.14159 |
| | | 180 | | public const long _2Pi = 411774; // 2π |
| | | 181 | | public const long _PiHalf = 102943; // π/2 |
| | | 182 | | public const long _PiInv = 20861L; // 1/π |
| | | 183 | | public const long _PiOver2Inv = 41722L;// 2/π |
| | | 184 | | public const long Rad_360 = 411775L; // 2π (360度) |
| | | 185 | | public const long Rad_180 = 205887L; // π (180度) |
| | | 186 | | public const long Rad_90 = 102944L; // π/2 (90度) |
| | | 187 | | public const long Rad_45 = 51472L; // π/4 (45度) |
| | | 188 | | public const long Rad_30 = 34315L; // π/6 (30度) |
| | | 189 | | public const long Rad_22_50 = 25736L; // π/8 (22.5度) |
| | | 190 | | |
| | | 191 | | // ==================== 角度转换常量 ==================== |
| | | 192 | | public const long _Deg2Rad = 1144; // π/180 ≈ 0.0174533 |
| | | 193 | | public const long _Rad2Deg = 3754936; // 180/π ≈ 57.2958 |
| | | 194 | | |
| | | 195 | | // ==================== 极小量常量 (EN = Epsilon N) ==================== |
| | | 196 | | public const long EN1 = 6554L; // 0.1 |
| | | 197 | | public const long EN2 = 655L; // 0.01 |
| | | 198 | | public const long EN3 = 66L; // 0.001 (Epsilon) |
| | | 199 | | public const long EN4 = 7L; // 0.0001 |
| | | 200 | | public const long EN5 = 1L; // 0.000015 (最小精度) |
| | | 201 | | |
| | | 202 | | // ==================== 数学常量 ==================== |
| | | 203 | | public const long _E = 178145L; // e ≈ 2.71828 |
| | | 204 | | public const long Log2_E = 94548L; // log2(e) |
| | | 205 | | public const long Log2_10 = 217706L; // log2(10) |
| | | 206 | | |
| | | 207 | | // ==================== 极值常量 ==================== |
| | | 208 | | public const long _MaxValue = 3037000499L; // UseableMax |
| | | 209 | | public const long _MinValue = -3037000499L; // UseableMin |
| | | 210 | | public const long _Epsilon = 1; // 最小精度 |
| | | 211 | | public const long Minus_1 = -65536L; // -1 |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | #endregion |
| | | 215 | | |
| | | 216 | | #region 存储与构造 |
| | | 217 | | |
| | | 218 | | /// <summary>内部原始值 (Q48.16 格式)</summary> |
| | | 219 | | [FieldOffset(0)] |
| | | 220 | | public readonly long RawValue; |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// 内部构造函数,允许同程序集内的类型创建实例 |
| | | 224 | | /// </summary> |
| | | 225 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 443831 | 226 | | internal FP(long raw) => RawValue = raw; |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// 从原始值构造 FP(安全的公开 API) |
| | | 230 | | /// </summary> |
| | | 231 | | /// <example> |
| | | 232 | | /// FP angle = FP.FromRaw(FP.Raw._PI); |
| | | 233 | | /// </example> |
| | | 234 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 72127 | 235 | | public static FP FromRaw(long raw) => new(raw); |
| | | 236 | | |
| | | 237 | | #endregion |
| | | 238 | | |
| | | 239 | | #region 类型转换 |
| | | 240 | | |
| | | 241 | | // ==================== 整数(隐式,确定性安全) ==================== |
| | | 242 | | |
| | | 243 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6599 | 244 | | public static implicit operator FP(int value) => new((long)value << FRACTIONAL_BITS); |
| | | 245 | | |
| | | 246 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1363 | 247 | | public static implicit operator FP(long value) => new(value << FRACTIONAL_BITS); |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// 转换为 int(截断小数部分) |
| | | 251 | | /// <para>如果值超出 int 范围(±2,147,483,647),将抛出 OverflowException</para> |
| | | 252 | | /// </summary> |
| | | 253 | | /// <exception cref="OverflowException">当值超出 int 范围时抛出</exception> |
| | | 254 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 255 | | public static explicit operator int(FP value) |
| | | 256 | | { |
| | 153 | 257 | | long intPart = value.RawValue >> FRACTIONAL_BITS; |
| | 153 | 258 | | if (intPart > int.MaxValue || intPart < int.MinValue) |
| | 4 | 259 | | throw new OverflowException($"FP value {intPart} cannot be converted to int (range: {int.MinValue} to {int.M |
| | 149 | 260 | | return (int)intPart; |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 675 | 264 | | public static explicit operator long(FP value) => value.RawValue >> FRACTIONAL_BITS; |
| | | 265 | | |
| | | 266 | | // ==================== 浮点数(显式 UNSAFE,禁止隐式) ==================== |
| | | 267 | | |
| | | 268 | | #if DEBUG |
| | | 269 | | [Obsolete("警告:FromFloat_UNSAFE 只能在编辑器/配置阶段使用!" + |
| | | 270 | | "模拟/运行时使用会导致非确定性。请使用预计算的 Raw 值,如 FP.FromRaw(FP.Raw._1_50)", true)] |
| | | 271 | | #endif |
| | | 272 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 273 | | public static FP FromFloat_UNSAFE(float f) => new((long)(f * ONE)); |
| | | 274 | | |
| | | 275 | | #if DEBUG |
| | | 276 | | [Obsolete("警告:FromDouble_UNSAFE 只能在编辑器/配置阶段使用!" + |
| | | 277 | | "模拟/运行时使用会导致非确定性。", true)] |
| | | 278 | | #endif |
| | | 279 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 280 | | public static FP FromDouble_UNSAFE(double d) => new((long)(d * ONE)); |
| | | 281 | | |
| | | 282 | | #if DEBUG |
| | | 283 | | [Obsolete("警告:ToFloat_UNSAFE 只能在调试/渲染中使用!" + |
| | | 284 | | "模拟/运行时使用会导致非确定性。", true)] |
| | | 285 | | #endif |
| | | 286 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 287 | | public float ToFloat_UNSAFE() => RawValue / (float)ONE; |
| | | 288 | | |
| | | 289 | | #if DEBUG |
| | | 290 | | [Obsolete("警告:ToDouble_UNSAFE 只能在调试/渲染中使用!", true)] |
| | | 291 | | #endif |
| | | 292 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 293 | | public double ToDouble_UNSAFE() => RawValue / (double)ONE; |
| | | 294 | | |
| | | 295 | | // 显式运算符(会抛出异常,作为最后一道防线) |
| | | 296 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 297 | | public static explicit operator FP(float value) |
| | | 298 | | { |
| | 0 | 299 | | throw new InvalidOperationException( |
| | 0 | 300 | | "禁止隐式从 float 转换为 FP!请使用 FP.FromRaw(FP.Raw.xxx) 或 FP.FromFloat_UNSAFE()(仅在编辑器使用)"); |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 304 | | public static explicit operator FP(double value) |
| | | 305 | | { |
| | 0 | 306 | | throw new InvalidOperationException( |
| | 0 | 307 | | "禁止隐式从 double 转换为 FP!请使用 FP.FromRaw(FP.Raw.xxx) 或 FP.FromDouble_UNSAFE()(仅在编辑器使用)"); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | #endregion |
| | | 311 | | |
| | | 312 | | #region 算术运算 |
| | | 313 | | |
| | | 314 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 56236 | 315 | | public static FP operator +(FP a, FP b) => new(a.RawValue + b.RawValue); |
| | | 316 | | |
| | | 317 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 47085 | 318 | | public static FP operator -(FP a, FP b) => new(a.RawValue - b.RawValue); |
| | | 319 | | |
| | | 320 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2835 | 321 | | public static FP operator -(FP a) => new(-a.RawValue); |
| | | 322 | | |
| | | 323 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5 | 324 | | public static FP operator +(FP a) => a; // 一元正号 |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// 定点数乘法 (a * b / 65536) |
| | | 328 | | /// 使用四舍五入,精度更高 |
| | | 329 | | /// </summary> |
| | | 330 | | /// <remarks> |
| | | 331 | | /// 相比截断版本,精度误差减半(约 0.5 LSB) |
| | | 332 | | /// 性能损失:1 次加法(可忽略) |
| | | 333 | | /// </remarks> |
| | | 334 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 335 | | public static FP operator *(FP a, FP b) |
| | | 336 | | { |
| | 77878 | 337 | | long product = a.RawValue * b.RawValue; |
| | | 338 | | |
| | | 339 | | #if DEBUG |
| | | 340 | | // 检查舍入溢出(当 product 接近 long.MaxValue/MinValue 时) |
| | | 341 | | // 这种情况在正常使用中极不可能发生(需要数值 > 2^63 / 2^16 ≈ 140万亿) |
| | | 342 | | if (product > long.MaxValue - MulRound || product < long.MinValue + MulRound) |
| | | 343 | | { |
| | | 344 | | Debug.Fail($"FP multiplication overflow: {a.RawValue} * {b.RawValue}. Use MultiplyPrecise for overflow check |
| | | 345 | | } |
| | | 346 | | #endif |
| | | 347 | | // 四舍五入:加上 0.5 LSB 后截断 |
| | 77878 | 348 | | return new((product + MulRound) >> FRACTIONAL_BITS); |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// 快速乘法(截断,无舍入开销) |
| | | 353 | | /// </summary> |
| | | 354 | | /// <remarks> |
| | | 355 | | /// 在性能极度敏感且精度要求不高的场景使用 |
| | | 356 | | /// 误差:最大 1 LSB(约 0.000015) |
| | | 357 | | /// </remarks> |
| | | 358 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 359 | | public static FP MultiplyFast(FP a, FP b) |
| | | 360 | | { |
| | 20 | 361 | | return new((a.RawValue * b.RawValue) >> FRACTIONAL_BITS); |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | /// <summary> |
| | | 365 | | /// 高精度乘法(带溢出检测) |
| | | 366 | | /// </summary> |
| | | 367 | | /// <exception cref="OverflowException">乘法溢出时抛出</exception> |
| | | 368 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 369 | | public static FP MultiplyPrecise(FP a, FP b) |
| | | 370 | | { |
| | 6 | 371 | | long product = a.RawValue * b.RawValue; |
| | 6 | 372 | | long rounded = product + MulRound; |
| | | 373 | | |
| | | 374 | | // 检测舍入溢出 |
| | 6 | 375 | | if ((product > 0 && rounded < product) || (product < 0 && rounded > product)) |
| | 0 | 376 | | throw new OverflowException($"FP multiplication overflow: {a} * {b}"); |
| | | 377 | | |
| | 6 | 378 | | return new(rounded >> FRACTIONAL_BITS); |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// 定点数除法 (a * 65536 / b) |
| | | 383 | | /// <para>注意:除零会抛出 <see cref="DivideByZeroException"/></para> |
| | | 384 | | /// </summary> |
| | | 385 | | /// <exception cref="DivideByZeroException">当 b 为零时抛出</exception> |
| | | 386 | | /// <exception cref="OverflowException">当左移操作溢出时抛出</exception> |
| | | 387 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 388 | | public static FP operator /(FP a, FP b) |
| | | 389 | | { |
| | 46860 | 390 | | if (b.RawValue == 0) |
| | 4 | 391 | | throw new DivideByZeroException("FP division by zero"); |
| | | 392 | | |
| | | 393 | | // 检查左移是否会导致溢出:|a| > long.MaxValue >> 16 ≈ 140万亿 |
| | | 394 | | // 对于正常游戏数值(通常 < 100万),永远不会触发 |
| | | 395 | | const long MAX_SAFE_DIVIDEND = long.MaxValue >> FRACTIONAL_BITS; // ≈ 140,737,488,355,327 |
| | 46856 | 396 | | if (a.RawValue > MAX_SAFE_DIVIDEND || a.RawValue < -MAX_SAFE_DIVIDEND) |
| | 1 | 397 | | throw new OverflowException($"FP division overflow: dividend {a.RawValue} is too large for division"); |
| | | 398 | | |
| | 46855 | 399 | | long result = (a.RawValue << FRACTIONAL_BITS) / b.RawValue; |
| | 46855 | 400 | | return new(result); |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | // ==================== 与整数的混合运算(优化) ==================== |
| | | 404 | | |
| | | 405 | | // int 运算符 |
| | | 406 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 107 | 407 | | public static FP operator *(FP a, int b) => new(a.RawValue * b); |
| | | 408 | | |
| | | 409 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 410 | | public static FP operator *(int a, FP b) => new(b.RawValue * a); |
| | | 411 | | |
| | | 412 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 413 | | public static FP operator /(FP a, int b) |
| | | 414 | | { |
| | 64 | 415 | | if (b == 0) |
| | 3 | 416 | | throw new DivideByZeroException("FP division by int zero"); |
| | 61 | 417 | | return new(a.RawValue / b); |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5 | 421 | | public static FP operator +(FP a, int b) => new(a.RawValue + ((long)b << FRACTIONAL_BITS)); |
| | | 422 | | |
| | | 423 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 424 | | public static FP operator +(int a, FP b) => b + a; |
| | | 425 | | |
| | | 426 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 11 | 427 | | public static FP operator -(FP a, int b) => new(a.RawValue - ((long)b << FRACTIONAL_BITS)); |
| | | 428 | | |
| | | 429 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 430 | | public static FP operator -(int a, FP b) => new(((long)a << FRACTIONAL_BITS) - b.RawValue); |
| | | 431 | | |
| | | 432 | | // long 运算符(P1 优化:避免 int 溢出) |
| | | 433 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 434 | | public static FP operator *(FP a, long b) => new(a.RawValue * b); |
| | | 435 | | |
| | | 436 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 437 | | public static FP operator *(long a, FP b) => new(b.RawValue * a); |
| | | 438 | | |
| | | 439 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 440 | | public static FP operator /(FP a, long b) |
| | | 441 | | { |
| | 0 | 442 | | if (b == 0) |
| | 0 | 443 | | throw new DivideByZeroException("FP division by long zero"); |
| | 0 | 444 | | return new(a.RawValue / b); |
| | | 445 | | } |
| | | 446 | | |
| | | 447 | | #endregion |
| | | 448 | | |
| | | 449 | | #region 比较运算 |
| | | 450 | | |
| | | 451 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 452 | | public static bool operator ==(FP a, FP b) => a.RawValue == b.RawValue; |
| | | 453 | | |
| | | 454 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1107 | 455 | | public static bool operator !=(FP a, FP b) => a.RawValue != b.RawValue; |
| | | 456 | | |
| | | 457 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2040 | 458 | | public static bool operator <(FP a, FP b) => a.RawValue < b.RawValue; |
| | | 459 | | |
| | | 460 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2216 | 461 | | public static bool operator >(FP a, FP b) => a.RawValue > b.RawValue; |
| | | 462 | | |
| | | 463 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 757 | 464 | | public static bool operator <=(FP a, FP b) => a.RawValue <= b.RawValue; |
| | | 465 | | |
| | | 466 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 467 | | public static bool operator >=(FP a, FP b) => a.RawValue >= b.RawValue; |
| | | 468 | | |
| | | 469 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 4 | 470 | | public int CompareTo(FP other) => RawValue.CompareTo(other.RawValue); |
| | | 471 | | |
| | | 472 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 75 | 473 | | public bool Equals(FP other) => RawValue == other.RawValue; |
| | | 474 | | |
| | 0 | 475 | | public override bool Equals(object? obj) => obj is FP other && Equals(other); |
| | | 476 | | |
| | 1023 | 477 | | public override int GetHashCode() => RawValue.GetHashCode(); |
| | | 478 | | |
| | | 479 | | #endregion |
| | | 480 | | } |