| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | #nullable enable |
| | | 5 | | |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace Lattice.Math |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// FP 数学常量定义 |
| | | 12 | | /// <para>所有魔数集中管理,附带推导说明</para> |
| | | 13 | | /// </summary> |
| | | 14 | | internal static class FPMathConstants |
| | | 15 | | { |
| | | 16 | | #region LUT 索引常量 |
| | | 17 | | |
| | | 18 | | /// <summary>Sin/Cos LUT 大小:4096 条目</summary> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// 计算:2π / 4096 ≈ 0.00153 弧度精度 |
| | | 21 | | /// 内存:4096 × 8 bytes = 32 KB |
| | | 22 | | /// </remarks> |
| | | 23 | | public const int SinCosLutSize = 4096; |
| | | 24 | | |
| | | 25 | | /// <summary>Sin/Cos 索引掩码:12 位 (log2(4096))</summary> |
| | | 26 | | public const int SinCosLutMask = SinCosLutSize - 1; |
| | | 27 | | |
| | | 28 | | /// <summary>Sin/Cos 索引位移:16 - 12 = 4</summary> |
| | | 29 | | public const int SinCosIndexShift = 4; |
| | | 30 | | |
| | | 31 | | /// <summary>Sqrt LUT 大小:65536 条目</summary> |
| | | 32 | | /// <remarks> |
| | | 33 | | /// 覆盖输入范围:0 到 65535(即 0.0 到 0.9999 in Q16.16) |
| | | 34 | | /// 内存:65536 × 4 bytes = 256 KB |
| | | 35 | | /// </remarks> |
| | | 36 | | public const int SqrtLutSize = 65536; |
| | | 37 | | |
| | | 38 | | /// <summary>Sqrt 索引掩码:16 位</summary> |
| | | 39 | | public const int SqrtLutMask = SqrtLutSize - 1; |
| | | 40 | | |
| | | 41 | | /// <summary>Acos LUT 大小:65537 条目(包含 -1 到 1)</summary> |
| | | 42 | | public const int AcosLutSize = 65537; |
| | | 43 | | |
| | | 44 | | /// <summary>Acos 索引偏移:32768(将 [-1, 1] 映射到 [0, 65536])</summary> |
| | | 45 | | public const int AcosIndexOffset = 32768; |
| | | 46 | | |
| | | 47 | | #endregion |
| | | 48 | | |
| | | 49 | | #region 归一化常量 |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// 归一化倒数计算的魔数:2^44 |
| | | 53 | | /// </summary> |
| | | 54 | | /// <remarks> |
| | | 55 | | /// 推导: |
| | | 56 | | /// - 我们需要计算 1/sqrt(x) ≈ 2^k / sqrt(mantissa) |
| | | 57 | | /// - k = 44/2 = 22 (sqrt 精度) |
| | | 58 | | /// - 加上 Q16.16 格式需要 16 位 |
| | | 59 | | /// - 加上额外 6 位精度 |
| | | 60 | | /// - 总计:22 + 16 + 6 = 44 位 |
| | | 61 | | /// |
| | | 62 | | /// 值:17,592,186,044,416 |
| | | 63 | | /// </remarks> |
| | | 64 | | public const long ReciprocalNormalizationFactor = 1L << 44; |
| | | 65 | | |
| | | 66 | | /// <summary>2^22 = 4,194,304(sqrt 精度基准)</summary> |
| | | 67 | | public const long SqrtPrecisionBase = 1L << 22; |
| | | 68 | | |
| | | 69 | | /// <summary>2^38 = 274,877,906,944(sqrt 中间计算)</summary> |
| | | 70 | | public const long SqrtIntermediateShift = 1L << 38; |
| | | 71 | | |
| | | 72 | | #endregion |
| | | 73 | | |
| | | 74 | | #region 乘法常量 |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// 乘法舍入常量:0.5 LSB = 2^15 = 32768 |
| | | 78 | | /// </summary> |
| | | 79 | | /// <remarks> |
| | | 80 | | /// 用于四舍五入乘法:(a * b + MulRound) >> 16 |
| | | 81 | | /// 相比截断,精度误差减半 |
| | | 82 | | /// </remarks> |
| | | 83 | | public const long MulRound = 1L << (FP.FRACTIONAL_BITS - 1); |
| | | 84 | | |
| | | 85 | | /// <summary>最大安全被除数:long.MaxValue >> 16</summary> |
| | | 86 | | /// <remarks> |
| | | 87 | | /// 约 140,737,488,355,327(140万亿) |
| | | 88 | | /// 超过此值的 FP 除法会溢出 |
| | | 89 | | /// </remarks> |
| | | 90 | | public const long MaxSafeDividend = long.MaxValue >> FP.FRACTIONAL_BITS; |
| | | 91 | | |
| | | 92 | | #endregion |
| | | 93 | | |
| | | 94 | | #region 角度/弧度转换 |
| | | 95 | | |
| | | 96 | | /// <summary>π 的 FP 表示:205887</summary> |
| | | 97 | | public const long PiRaw = 205887L; |
| | | 98 | | |
| | | 99 | | /// <summary>2π 的 FP 表示:411774</summary> |
| | | 100 | | public const long TwoPiRaw = 411774L; |
| | | 101 | | |
| | | 102 | | /// <summary>π/2 的 FP 表示:102943</summary> |
| | | 103 | | public const long PiHalfRaw = 102943L; |
| | | 104 | | |
| | | 105 | | /// <summary>角度转弧度:π/180 ≈ 1144</summary> |
| | | 106 | | public const long Deg2RadRaw = 1144L; |
| | | 107 | | |
| | | 108 | | /// <summary>弧度转角度:180/π ≈ 3,754,936</summary> |
| | | 109 | | public const long Rad2DegRaw = 3754936L; |
| | | 110 | | |
| | | 111 | | #endregion |
| | | 112 | | |
| | | 113 | | #region 验证方法 |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// 验证所有常量计算正确 |
| | | 117 | | /// </summary> |
| | | 118 | | [Conditional("DEBUG")] |
| | | 119 | | public static void Validate() |
| | | 120 | | { |
| | | 121 | | // 验证 LUT 大小是 2 的幂 |
| | | 122 | | Debug.Assert((SinCosLutSize & SinCosLutMask) == 0, "SinCosLutSize must be power of 2"); |
| | | 123 | | Debug.Assert((SqrtLutSize & SqrtLutMask) == 0, "SqrtLutSize must be power of 2"); |
| | | 124 | | |
| | | 125 | | // 验证掩码计算 |
| | | 126 | | Debug.Assert(SinCosLutMask == 4095, "SinCosLutMask = 4096 - 1"); |
| | | 127 | | Debug.Assert(SqrtLutMask == 65535, "SqrtLutMask = 65536 - 1"); |
| | | 128 | | |
| | | 129 | | // 验证归一化因子 |
| | | 130 | | Debug.Assert(ReciprocalNormalizationFactor == 17592186044416L, "ReciprocalNormalizationFactor = 2^44"); |
| | | 131 | | |
| | | 132 | | // 验证舍入常量 |
| | | 133 | | Debug.Assert(MulRound == 32768L, "MulRound = 2^15"); |
| | | 134 | | |
| | | 135 | | // 验证角度常量 |
| | | 136 | | Debug.Assert(PiRaw == (long)(System.Math.PI * FP.ONE), "PiRaw mismatch"); |
| | | 137 | | Debug.Assert(TwoPiRaw == PiRaw * 2, "TwoPiRaw mismatch"); |
| | | 138 | | Debug.Assert(PiHalfRaw == PiRaw / 2, "PiHalfRaw mismatch"); |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | #endregion |
| | | 142 | | } |
| | | 143 | | } |