| | | 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.Numerics; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | namespace Lattice.Math; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// SIMD 整数运算加速 - 仅使用 Vector<long>/Vector<int> |
| | | 15 | | /// <para>为定点数类型提供批量 SIMD 运算支持,确保跨平台确定性</para> |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// <para><b>⚠️ 重要警告 - 浮点 SIMD 被禁止:</b></para> |
| | | 19 | | /// <para> |
| | | 20 | | /// 本文件<strong>仅使用整数 SIMD</strong>(<see cref="Vector{T}"/> 配合 long/int 类型)。 |
| | | 21 | | /// <strong>浮点 SIMD(Vector<float>, Vector<double>)被明确禁止</strong>,原因如下: |
| | | 22 | | /// </para> |
| | | 23 | | /// <list type="number"> |
| | | 24 | | /// <item><b>跨平台差异:</b>x86/ARM 处理浮点舍入和特殊情况(NaN、无穷大)的方式不同</item> |
| | | 25 | | /// <item><b>编译器差异:</b>不同编译器对浮点表达式的优化策略不同</item> |
| | | 26 | | /// <item><b>确定性要求:</b>帧同步游戏需要所有客户端产生完全一致的计算结果</item> |
| | | 27 | | /// </list> |
| | | 28 | | /// |
| | | 29 | | /// <para><b>定点数 SIMD 运算原理:</b></para> |
| | | 30 | | /// <list type="bullet"> |
| | | 31 | | /// <item><b>格式:</b>FP 类型使用 Q48.16 格式(64位有符号整数,16位小数)</item> |
| | | 32 | | /// <item><b>加法/减法:</b>直接对原始 long 值进行 SIMD 运算,无需额外处理</item> |
| | | 33 | | /// <item><b>乘法:</b>需要处理定点数精度转换 (a * b + 32768) >> 16</item> |
| | | 34 | | /// <item><b>取反/绝对值:</b>使用位运算实现无分支 SIMD 操作</item> |
| | | 35 | | /// </list> |
| | | 36 | | /// |
| | | 37 | | /// <para><b>性能特性:</b></para> |
| | | 38 | | /// <list type="bullet"> |
| | | 39 | | /// <item>128-bit SIMD (SSE2/NEON): 每次处理 2 个 FP 值</item> |
| | | 40 | | /// <item>256-bit SIMD (AVX2): 每次处理 4 个 FP 值</item> |
| | | 41 | | /// <item>512-bit SIMD (AVX-512): 每次处理 8 个 FP 值(未来扩展)</item> |
| | | 42 | | /// </list> |
| | | 43 | | /// </remarks> |
| | | 44 | | public static class FPSimd |
| | | 45 | | { |
| | | 46 | | #region 常量 |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Vector<T> 中 long (64位) 的向量宽度 |
| | | 50 | | /// </summary> |
| | | 51 | | /// <remarks> |
| | | 52 | | /// <list type="bullet"> |
| | | 53 | | /// <item>128-bit SIMD: VectorLongCount = 2</item> |
| | | 54 | | /// <item>256-bit SIMD: VectorLongCount = 4</item> |
| | | 55 | | /// <item>512-bit SIMD: VectorLongCount = 8 (未来扩展)</item> |
| | | 56 | | /// </list> |
| | | 57 | | /// </remarks> |
| | 1 | 58 | | public static readonly int VectorLongCount = Vector<long>.Count; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Vector<T> 中 int (32位) 的向量宽度 |
| | | 62 | | /// </summary> |
| | | 63 | | /// <remarks> |
| | | 64 | | /// 当前主要用于 32 位定点数运算的潜在扩展,64 位 FP 运算主要使用 <see cref="VectorLongCount"/>。 |
| | | 65 | | /// </remarks> |
| | 1 | 66 | | public static readonly int VectorIntCount = Vector<int>.Count; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// 硬件 SIMD 加速支持检测 |
| | | 70 | | /// </summary> |
| | | 71 | | /// <remarks> |
| | | 72 | | /// 当 CPU 支持 SSE2/AVX/AVX2/NEON 等指令集时返回 true。 |
| | | 73 | | /// 即使返回 false,整数 SIMD 操作仍会正确执行(使用软件回退)。 |
| | | 74 | | /// 实际性能提升取决于数据规模和具体 CPU 架构。 |
| | | 75 | | /// </remarks> |
| | 0 | 76 | | public static bool IsHardwareAccelerated => Vector.IsHardwareAccelerated; |
| | | 77 | | |
| | | 78 | | #endregion |
| | | 79 | | |
| | | 80 | | #region FP 批量加法 |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// 批量加法:result[i] = a[i] + b[i] |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="a">输入数组 A</param> |
| | | 86 | | /// <param name="b">输入数组 B</param> |
| | | 87 | | /// <param name="result">输出数组</param> |
| | | 88 | | /// <exception cref="ArgumentException">数组长度不匹配时抛出</exception> |
| | | 89 | | /// <remarks> |
| | | 90 | | /// <para><b>实现说明:</b></para> |
| | | 91 | | /// 使用整数 SIMD 实现确定性加法运算。将 FP 数组重新解释为 long 数组后直接进行向量加法, |
| | | 92 | | /// 充分利用 SIMD 的并行计算能力。 |
| | | 93 | | /// |
| | | 94 | | /// <para><b>性能特性:</b></para> |
| | | 95 | | /// <list type="bullet"> |
| | | 96 | | /// <item>小数组(< VectorLongCount):纯标量运算,无 SIMD 开销</item> |
| | | 97 | | /// <item>大数组:SIMD 加速比约为 VectorLongCount:1</item> |
| | | 98 | | /// <item>内存带宽限制:大规模数据可能受限于内存而非 CPU</item> |
| | | 99 | | /// </list> |
| | | 100 | | /// |
| | | 101 | | /// <para><b>前置条件:</b></para> |
| | | 102 | | /// <list type="bullet"> |
| | | 103 | | /// <item>a.Length == b.Length</item> |
| | | 104 | | /// <item>result.Length >= a.Length</item> |
| | | 105 | | /// </list> |
| | | 106 | | /// </remarks> |
| | | 107 | | /// <example> |
| | | 108 | | /// <code> |
| | | 109 | | /// FP[] a = { FP._1, FP._2, FP._3, FP._4 }; |
| | | 110 | | /// FP[] b = { FP._1, FP._1, FP._1, FP._1 }; |
| | | 111 | | /// FP[] result = new FP[4]; |
| | | 112 | | /// |
| | | 113 | | /// FPSimd.AddBatch(a, b, result); |
| | | 114 | | /// // result: { 2, 3, 4, 5 } |
| | | 115 | | /// </code> |
| | | 116 | | /// </example> |
| | | 117 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 118 | | public static void AddBatch(ReadOnlySpan<FP> a, ReadOnlySpan<FP> b, Span<FP> result) |
| | | 119 | | { |
| | 30 | 120 | | int count = a.Length; |
| | 30 | 121 | | if (b.Length != count || result.Length < count) |
| | 2 | 122 | | throw new ArgumentException("数组长度不匹配"); |
| | | 123 | | |
| | | 124 | | // 将 FP 数组转换为 long 原始值数组进行 SIMD 处理 |
| | 28 | 125 | | ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FP, long>(a); |
| | 28 | 126 | | ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FP, long>(b); |
| | 28 | 127 | | Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result); |
| | | 128 | | |
| | 28 | 129 | | int i = 0; |
| | | 130 | | |
| | | 131 | | // SIMD 主循环:每次处理 VectorLongCount 个元素 |
| | 28 | 132 | | if (Vector.IsHardwareAccelerated && count >= VectorLongCount) |
| | | 133 | | { |
| | 20 | 134 | | int simdLimit = count - (count % VectorLongCount); |
| | 874 | 135 | | for (; i < simdLimit; i += VectorLongCount) |
| | | 136 | | { |
| | 427 | 137 | | Vector<long> va = new Vector<long>(aRaw.Slice(i)); |
| | 427 | 138 | | Vector<long> vb = new Vector<long>(bRaw.Slice(i)); |
| | 427 | 139 | | Vector<long> vr = va + vb; |
| | 427 | 140 | | vr.CopyTo(resultRaw.Slice(i)); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | // 标量收尾:处理剩余元素 |
| | 96 | 145 | | for (; i < count; i++) |
| | | 146 | | { |
| | 34 | 147 | | resultRaw[i] = aRaw[i] + bRaw[i]; |
| | | 148 | | } |
| | 28 | 149 | | } |
| | | 150 | | |
| | | 151 | | #endregion |
| | | 152 | | |
| | | 153 | | #region FP 批量减法 |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// 批量减法:result[i] = a[i] - b[i] |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="a">输入数组 A(被减数)</param> |
| | | 159 | | /// <param name="b">输入数组 B(减数)</param> |
| | | 160 | | /// <param name="result">输出数组</param> |
| | | 161 | | /// <exception cref="ArgumentException">数组长度不匹配时抛出</exception> |
| | | 162 | | /// <remarks> |
| | | 163 | | /// <para><b>实现说明:</b></para> |
| | | 164 | | /// 使用整数 SIMD 实现确定性减法运算。将 FP 数组重新解释为 long 数组后直接进行向量减法。 |
| | | 165 | | /// |
| | | 166 | | /// <para><b>性能特性:</b></para> |
| | | 167 | | /// <list type="bullet"> |
| | | 168 | | /// <item>与 <see cref="AddBatch"/> 性能特征相同</item> |
| | | 169 | | /// <item>标量版本使用简单的 long 减法,无额外开销</item> |
| | | 170 | | /// </list> |
| | | 171 | | /// |
| | | 172 | | /// <para><b>前置条件:</b></para> |
| | | 173 | | /// <list type="bullet"> |
| | | 174 | | /// <item>a.Length == b.Length</item> |
| | | 175 | | /// <item>result.Length >= a.Length</item> |
| | | 176 | | /// </list> |
| | | 177 | | /// </remarks> |
| | | 178 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 179 | | public static void SubtractBatch(ReadOnlySpan<FP> a, ReadOnlySpan<FP> b, Span<FP> result) |
| | | 180 | | { |
| | 13 | 181 | | int count = a.Length; |
| | 13 | 182 | | if (b.Length != count || result.Length < count) |
| | 0 | 183 | | throw new ArgumentException("数组长度不匹配"); |
| | | 184 | | |
| | 13 | 185 | | ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FP, long>(a); |
| | 13 | 186 | | ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FP, long>(b); |
| | 13 | 187 | | Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result); |
| | | 188 | | |
| | 13 | 189 | | int i = 0; |
| | | 190 | | |
| | 13 | 191 | | if (Vector.IsHardwareAccelerated && count >= VectorLongCount) |
| | | 192 | | { |
| | 10 | 193 | | int simdLimit = count - (count % VectorLongCount); |
| | 196 | 194 | | for (; i < simdLimit; i += VectorLongCount) |
| | | 195 | | { |
| | 93 | 196 | | Vector<long> va = new Vector<long>(aRaw.Slice(i)); |
| | 93 | 197 | | Vector<long> vb = new Vector<long>(bRaw.Slice(i)); |
| | 93 | 198 | | Vector<long> vr = va - vb; |
| | 93 | 199 | | vr.CopyTo(resultRaw.Slice(i)); |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | |
| | 53 | 203 | | for (; i < count; i++) |
| | | 204 | | { |
| | 20 | 205 | | resultRaw[i] = aRaw[i] - bRaw[i]; |
| | | 206 | | } |
| | 13 | 207 | | } |
| | | 208 | | |
| | | 209 | | #endregion |
| | | 210 | | |
| | | 211 | | #region FP 批量乘法 |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// 批量乘法:result[i] = a[i] * b[i](四舍五入) |
| | | 215 | | /// </summary> |
| | | 216 | | /// <param name="a">输入数组 A</param> |
| | | 217 | | /// <param name="b">输入数组 B</param> |
| | | 218 | | /// <param name="result">输出数组</param> |
| | | 219 | | /// <exception cref="ArgumentException">数组长度不匹配时抛出</exception> |
| | | 220 | | /// <remarks> |
| | | 221 | | /// <para><b>定点数乘法公式:</b>(a.Raw * b.Raw + 32768) >> 16</para> |
| | | 222 | | /// |
| | | 223 | | /// <para><b>⚠️ SIMD 限制说明:</b></para> |
| | | 224 | | /// 当前实现使用标量运算,原因如下: |
| | | 225 | | /// <list type="number"> |
| | | 226 | | /// <item>64位乘法在 SIMD 中会产生 128 位结果,Vector<long> 不直接支持</item> |
| | | 227 | | /// <item>精度处理需要额外的舍入步骤,SIMD 实现复杂</item> |
| | | 228 | | /// <item>定点数乘法的溢出问题需要特殊处理</item> |
| | | 229 | | /// </list> |
| | | 230 | | /// 未来优化方向:使用 32 位 SIMD 分拆计算(需验证溢出安全)。 |
| | | 231 | | /// |
| | | 232 | | /// <para><b>前置条件:</b></para> |
| | | 233 | | /// <list type="bullet"> |
| | | 234 | | /// <item>a.Length == b.Length</item> |
| | | 235 | | /// <item>result.Length >= a.Length</item> |
| | | 236 | | /// </list> |
| | | 237 | | /// </remarks> |
| | | 238 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 239 | | public static void MultiplyBatch(ReadOnlySpan<FP> a, ReadOnlySpan<FP> b, Span<FP> result) |
| | | 240 | | { |
| | 13 | 241 | | int count = a.Length; |
| | 13 | 242 | | if (b.Length != count || result.Length < count) |
| | 0 | 243 | | throw new ArgumentException("数组长度不匹配"); |
| | | 244 | | |
| | 13 | 245 | | ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FP, long>(a); |
| | 13 | 246 | | ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FP, long>(b); |
| | 13 | 247 | | Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result); |
| | | 248 | | |
| | | 249 | | const long MulRound = 1L << (FP.FRACTIONAL_BITS - 1); // 32768 |
| | | 250 | | |
| | | 251 | | // 标量实现:64位乘法需要特殊处理,暂不使用 SIMD |
| | | 252 | | // 原因:Vector<long> 的乘法操作可能产生溢出或平台差异 |
| | 786 | 253 | | for (int i = 0; i < count; i++) |
| | | 254 | | { |
| | 380 | 255 | | long product = aRaw[i] * bRaw[i]; |
| | 380 | 256 | | resultRaw[i] = (product + MulRound) >> FP.FRACTIONAL_BITS; |
| | | 257 | | } |
| | 13 | 258 | | } |
| | | 259 | | |
| | | 260 | | #endregion |
| | | 261 | | |
| | | 262 | | #region FP 批量取反 |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// 批量取反:result[i] = -input[i] |
| | | 266 | | /// </summary> |
| | | 267 | | /// <param name="input">输入数组</param> |
| | | 268 | | /// <param name="result">输出数组</param> |
| | | 269 | | /// <exception cref="ArgumentException">输出数组容量不足时抛出</exception> |
| | | 270 | | /// <remarks> |
| | | 271 | | /// <para><b>实现说明:</b></para> |
| | | 272 | | /// 使用 SIMD 整数减法实现:0 - value。利用 Vector.Zero 作为被减数, |
| | | 273 | | /// 对输入值进行逐元素取反。 |
| | | 274 | | /// |
| | | 275 | | /// <para><b>性能特性:</b></para> |
| | | 276 | | /// <list type="bullet"> |
| | | 277 | | /// <item>SIMD 版本:利用零向量减法,单指令多数据并行处理</item> |
| | | 278 | | /// <item>标量版本:直接使用一元负号运算符</item> |
| | | 279 | | /// </list> |
| | | 280 | | /// |
| | | 281 | | /// <para><b>前置条件:</b></para> |
| | | 282 | | /// <list type="bullet"> |
| | | 283 | | /// <item>result.Length >= input.Length</item> |
| | | 284 | | /// </list> |
| | | 285 | | /// </remarks> |
| | | 286 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 287 | | public static void NegateBatch(ReadOnlySpan<FP> input, Span<FP> result) |
| | | 288 | | { |
| | 15 | 289 | | int count = input.Length; |
| | 15 | 290 | | if (result.Length < count) |
| | 0 | 291 | | throw new ArgumentException("输出数组容量不足"); |
| | | 292 | | |
| | 15 | 293 | | ReadOnlySpan<long> inputRaw = MemoryMarshal.Cast<FP, long>(input); |
| | 15 | 294 | | Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result); |
| | | 295 | | |
| | 15 | 296 | | int i = 0; |
| | | 297 | | |
| | 15 | 298 | | if (Vector.IsHardwareAccelerated && count >= VectorLongCount) |
| | | 299 | | { |
| | 11 | 300 | | Vector<long> zero = Vector<long>.Zero; |
| | 11 | 301 | | int simdLimit = count - (count % VectorLongCount); |
| | 205 | 302 | | for (; i < simdLimit; i += VectorLongCount) |
| | | 303 | | { |
| | 97 | 304 | | Vector<long> v = new Vector<long>(inputRaw.Slice(i)); |
| | 97 | 305 | | Vector<long> vr = zero - v; |
| | 97 | 306 | | vr.CopyTo(resultRaw.Slice(i)); |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | |
| | 61 | 310 | | for (; i < count; i++) |
| | | 311 | | { |
| | 23 | 312 | | resultRaw[i] = -inputRaw[i]; |
| | | 313 | | } |
| | 15 | 314 | | } |
| | | 315 | | |
| | | 316 | | #endregion |
| | | 317 | | |
| | | 318 | | #region FP 批量绝对值 |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// 批量绝对值:result[i] = Abs(input[i]) |
| | | 322 | | /// </summary> |
| | | 323 | | /// <param name="input">输入数组</param> |
| | | 324 | | /// <param name="result">输出数组</param> |
| | | 325 | | /// <exception cref="ArgumentException">输出数组容量不足时抛出</exception> |
| | | 326 | | /// <remarks> |
| | | 327 | | /// <para><b>实现说明:</b></para> |
| | | 328 | | /// 使用无分支位运算实现 SIMD 绝对值: |
| | | 329 | | /// <code>Abs(x) = (x ^ (x >> 63)) - (x >> 63)</code> |
| | | 330 | | /// |
| | | 331 | | /// 利用算术右移扩展符号位,然后通过异或和减法计算绝对值, |
| | | 332 | | /// 整个过程无分支,适合 SIMD 并行执行。 |
| | | 333 | | /// |
| | | 334 | | /// <para><b>⚠️ 边界情况:</b></para> |
| | | 335 | | /// long.MinValue 的绝对值仍为 long.MinValue(溢出), |
| | | 336 | | /// 但在 FP 正常使用范围内不会发生。 |
| | | 337 | | /// |
| | | 338 | | /// <para><b>前置条件:</b></para> |
| | | 339 | | /// <list type="bullet"> |
| | | 340 | | /// <item>result.Length >= input.Length</item> |
| | | 341 | | /// </list> |
| | | 342 | | /// </remarks> |
| | | 343 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 344 | | public static void AbsBatch(ReadOnlySpan<FP> input, Span<FP> result) |
| | | 345 | | { |
| | 15 | 346 | | int count = input.Length; |
| | 15 | 347 | | if (result.Length < count) |
| | 0 | 348 | | throw new ArgumentException("输出数组容量不足"); |
| | | 349 | | |
| | 15 | 350 | | ReadOnlySpan<long> inputRaw = MemoryMarshal.Cast<FP, long>(input); |
| | 15 | 351 | | Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result); |
| | | 352 | | |
| | 15 | 353 | | int i = 0; |
| | | 354 | | |
| | 15 | 355 | | if (Vector.IsHardwareAccelerated && count >= VectorLongCount) |
| | | 356 | | { |
| | 11 | 357 | | int simdLimit = count - (count % VectorLongCount); |
| | 205 | 358 | | for (; i < simdLimit; i += VectorLongCount) |
| | | 359 | | { |
| | 97 | 360 | | Vector<long> v = new Vector<long>(inputRaw.Slice(i)); |
| | | 361 | | // 无分支绝对值:(v ^ (v >> 63)) - (v >> 63) |
| | 97 | 362 | | Vector<long> sign = v >> 63; |
| | 97 | 363 | | Vector<long> vr = (v ^ sign) - sign; |
| | 97 | 364 | | vr.CopyTo(resultRaw.Slice(i)); |
| | | 365 | | } |
| | | 366 | | } |
| | | 367 | | |
| | 61 | 368 | | for (; i < count; i++) |
| | | 369 | | { |
| | 23 | 370 | | long v = inputRaw[i]; |
| | 23 | 371 | | resultRaw[i] = v < 0 ? -v : v; |
| | | 372 | | } |
| | 15 | 373 | | } |
| | | 374 | | |
| | | 375 | | #endregion |
| | | 376 | | |
| | | 377 | | #region FPVector2 SIMD 运算 |
| | | 378 | | |
| | | 379 | | /// <summary> |
| | | 380 | | /// 批量 FPVector2 加法:result[i] = a[i] + b[i] |
| | | 381 | | /// </summary> |
| | | 382 | | /// <param name="a">输入向量数组 A</param> |
| | | 383 | | /// <param name="b">输入向量数组 B</param> |
| | | 384 | | /// <param name="result">输出向量数组</param> |
| | | 385 | | /// <exception cref="ArgumentException">数组长度不匹配时抛出</exception> |
| | | 386 | | /// <remarks> |
| | | 387 | | /// <para><b>内存布局:</b></para> |
| | | 388 | | /// FPVector2 内存布局为 X(8字节) + Y(8字节) = 16字节。 |
| | | 389 | | /// SIMD 处理时将两个 FPVector2 展开为 4 个 long 进行并行处理。 |
| | | 390 | | /// |
| | | 391 | | /// <para><b>性能特性:</b></para> |
| | | 392 | | /// <list type="bullet"> |
| | | 393 | | /// <item>每个 FPVector2 包含 2 个 FP 值(16 字节)</item> |
| | | 394 | | /// <item>128-bit SIMD: 每次处理 2 个 FPVector2</item> |
| | | 395 | | /// <item>256-bit SIMD: 每次处理 4 个 FPVector2</item> |
| | | 396 | | /// </list> |
| | | 397 | | /// |
| | | 398 | | /// <para><b>前置条件:</b></para> |
| | | 399 | | /// <list type="bullet"> |
| | | 400 | | /// <item>a.Length == b.Length</item> |
| | | 401 | | /// <item>result.Length >= a.Length</item> |
| | | 402 | | /// </list> |
| | | 403 | | /// </remarks> |
| | | 404 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 405 | | public static void AddBatchVector2(ReadOnlySpan<FPVector2> a, ReadOnlySpan<FPVector2> b, Span<FPVector2> result) |
| | | 406 | | { |
| | 7 | 407 | | int count = a.Length; |
| | 7 | 408 | | if (b.Length != count || result.Length < count) |
| | 0 | 409 | | throw new ArgumentException("数组长度不匹配"); |
| | | 410 | | |
| | | 411 | | // 将 FPVector2 数组转换为 long 数组(每个向量2个long,共 2*count 个long) |
| | 7 | 412 | | ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FPVector2, long>(a); |
| | 7 | 413 | | ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FPVector2, long>(b); |
| | 7 | 414 | | Span<long> resultRaw = MemoryMarshal.Cast<FPVector2, long>(result); |
| | | 415 | | |
| | 7 | 416 | | int totalLongs = count * 2; |
| | 7 | 417 | | int i = 0; |
| | | 418 | | |
| | 7 | 419 | | if (Vector.IsHardwareAccelerated && totalLongs >= VectorLongCount) |
| | | 420 | | { |
| | 6 | 421 | | int simdLimit = totalLongs - (totalLongs % VectorLongCount); |
| | 164 | 422 | | for (; i < simdLimit; i += VectorLongCount) |
| | | 423 | | { |
| | 79 | 424 | | Vector<long> va = new Vector<long>(aRaw.Slice(i)); |
| | 79 | 425 | | Vector<long> vb = new Vector<long>(bRaw.Slice(i)); |
| | 79 | 426 | | Vector<long> vr = va + vb; |
| | 79 | 427 | | vr.CopyTo(resultRaw.Slice(i)); |
| | | 428 | | } |
| | | 429 | | } |
| | | 430 | | |
| | 15 | 431 | | for (; i < totalLongs; i++) |
| | | 432 | | { |
| | 4 | 433 | | resultRaw[i] = aRaw[i] + bRaw[i]; |
| | | 434 | | } |
| | 7 | 435 | | } |
| | | 436 | | |
| | | 437 | | /// <summary> |
| | | 438 | | /// 批量 FPVector2 减法:result[i] = a[i] - b[i] |
| | | 439 | | /// </summary> |
| | | 440 | | /// <param name="a">输入向量数组 A(被减数)</param> |
| | | 441 | | /// <param name="b">输入向量数组 B(减数)</param> |
| | | 442 | | /// <param name="result">输出向量数组</param> |
| | | 443 | | /// <exception cref="ArgumentException">数组长度不匹配时抛出</exception> |
| | | 444 | | /// <remarks> |
| | | 445 | | /// <para><b>性能特性:</b></para> |
| | | 446 | | /// 与 <see cref="AddBatchVector2"/> 性能特征相同。 |
| | | 447 | | /// |
| | | 448 | | /// <para><b>前置条件:</b></para> |
| | | 449 | | /// <list type="bullet"> |
| | | 450 | | /// <item>a.Length == b.Length</item> |
| | | 451 | | /// <item>result.Length >= a.Length</item> |
| | | 452 | | /// </list> |
| | | 453 | | /// </remarks> |
| | | 454 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 455 | | public static void SubtractBatchVector2(ReadOnlySpan<FPVector2> a, ReadOnlySpan<FPVector2> b, Span<FPVector2> result |
| | | 456 | | { |
| | 0 | 457 | | int count = a.Length; |
| | 0 | 458 | | if (b.Length != count || result.Length < count) |
| | 0 | 459 | | throw new ArgumentException("数组长度不匹配"); |
| | | 460 | | |
| | 0 | 461 | | ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FPVector2, long>(a); |
| | 0 | 462 | | ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FPVector2, long>(b); |
| | 0 | 463 | | Span<long> resultRaw = MemoryMarshal.Cast<FPVector2, long>(result); |
| | | 464 | | |
| | 0 | 465 | | int totalLongs = count * 2; |
| | 0 | 466 | | int i = 0; |
| | | 467 | | |
| | 0 | 468 | | if (Vector.IsHardwareAccelerated && totalLongs >= VectorLongCount) |
| | | 469 | | { |
| | 0 | 470 | | int simdLimit = totalLongs - (totalLongs % VectorLongCount); |
| | 0 | 471 | | for (; i < simdLimit; i += VectorLongCount) |
| | | 472 | | { |
| | 0 | 473 | | Vector<long> va = new Vector<long>(aRaw.Slice(i)); |
| | 0 | 474 | | Vector<long> vb = new Vector<long>(bRaw.Slice(i)); |
| | 0 | 475 | | Vector<long> vr = va - vb; |
| | 0 | 476 | | vr.CopyTo(resultRaw.Slice(i)); |
| | | 477 | | } |
| | | 478 | | } |
| | | 479 | | |
| | 0 | 480 | | for (; i < totalLongs; i++) |
| | | 481 | | { |
| | 0 | 482 | | resultRaw[i] = aRaw[i] - bRaw[i]; |
| | | 483 | | } |
| | 0 | 484 | | } |
| | | 485 | | |
| | | 486 | | /// <summary> |
| | | 487 | | /// 批量 FPVector2 取反:result[i] = -input[i] |
| | | 488 | | /// </summary> |
| | | 489 | | /// <param name="input">输入向量数组</param> |
| | | 490 | | /// <param name="result">输出向量数组</param> |
| | | 491 | | /// <exception cref="ArgumentException">输出数组容量不足时抛出</exception> |
| | | 492 | | /// <remarks> |
| | | 493 | | /// <para><b>前置条件:</b></para> |
| | | 494 | | /// <list type="bullet"> |
| | | 495 | | /// <item>result.Length >= input.Length</item> |
| | | 496 | | /// </list> |
| | | 497 | | /// </remarks> |
| | | 498 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 499 | | public static void NegateBatchVector2(ReadOnlySpan<FPVector2> input, Span<FPVector2> result) |
| | | 500 | | { |
| | 0 | 501 | | int count = input.Length; |
| | 0 | 502 | | if (result.Length < count) |
| | 0 | 503 | | throw new ArgumentException("输出数组容量不足"); |
| | | 504 | | |
| | 0 | 505 | | ReadOnlySpan<long> inputRaw = MemoryMarshal.Cast<FPVector2, long>(input); |
| | 0 | 506 | | Span<long> resultRaw = MemoryMarshal.Cast<FPVector2, long>(result); |
| | | 507 | | |
| | 0 | 508 | | int totalLongs = count * 2; |
| | 0 | 509 | | int i = 0; |
| | | 510 | | |
| | 0 | 511 | | if (Vector.IsHardwareAccelerated && totalLongs >= VectorLongCount) |
| | | 512 | | { |
| | 0 | 513 | | Vector<long> zero = Vector<long>.Zero; |
| | 0 | 514 | | int simdLimit = totalLongs - (totalLongs % VectorLongCount); |
| | 0 | 515 | | for (; i < simdLimit; i += VectorLongCount) |
| | | 516 | | { |
| | 0 | 517 | | Vector<long> v = new Vector<long>(inputRaw.Slice(i)); |
| | 0 | 518 | | Vector<long> vr = zero - v; |
| | 0 | 519 | | vr.CopyTo(resultRaw.Slice(i)); |
| | | 520 | | } |
| | | 521 | | } |
| | | 522 | | |
| | 0 | 523 | | for (; i < totalLongs; i++) |
| | | 524 | | { |
| | 0 | 525 | | resultRaw[i] = -inputRaw[i]; |
| | | 526 | | } |
| | 0 | 527 | | } |
| | | 528 | | |
| | | 529 | | /// <summary> |
| | | 530 | | /// 批量 FPVector2 标量乘法:result[i] = input[i] * scalar |
| | | 531 | | /// </summary> |
| | | 532 | | /// <param name="input">输入向量数组</param> |
| | | 533 | | /// <param name="scalar">标量乘数</param> |
| | | 534 | | /// <param name="result">输出向量数组</param> |
| | | 535 | | /// <exception cref="ArgumentException">输出数组容量不足时抛出</exception> |
| | | 536 | | /// <remarks> |
| | | 537 | | /// <para><b>⚠️ 实现限制:</b></para> |
| | | 538 | | /// 当前使用标量实现,因为定点数乘法需要精度转换 (a * b + 32768) >> 16。 |
| | | 539 | | /// SIMD 实现需要特殊的 64 位乘法分解,将在后续版本优化。 |
| | | 540 | | /// |
| | | 541 | | /// <para><b>前置条件:</b></para> |
| | | 542 | | /// <list type="bullet"> |
| | | 543 | | /// <item>result.Length >= input.Length</item> |
| | | 544 | | /// </list> |
| | | 545 | | /// </remarks> |
| | | 546 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 547 | | public static void MultiplyBatchVector2(ReadOnlySpan<FPVector2> input, FP scalar, Span<FPVector2> result) |
| | | 548 | | { |
| | 0 | 549 | | int count = input.Length; |
| | 0 | 550 | | if (result.Length < count) |
| | 0 | 551 | | throw new ArgumentException("输出数组容量不足"); |
| | | 552 | | |
| | 0 | 553 | | long s = scalar.RawValue; |
| | | 554 | | const long MulRound = 1L << (FP.FRACTIONAL_BITS - 1); |
| | | 555 | | |
| | 0 | 556 | | for (int i = 0; i < count; i++) |
| | | 557 | | { |
| | 0 | 558 | | FPVector2 v = input[i]; |
| | 0 | 559 | | long x = (v.X.RawValue * s + MulRound) >> FP.FRACTIONAL_BITS; |
| | 0 | 560 | | long y = (v.Y.RawValue * s + MulRound) >> FP.FRACTIONAL_BITS; |
| | 0 | 561 | | result[i] = new FPVector2(FP.FromRaw(x), FP.FromRaw(y)); |
| | | 562 | | } |
| | 0 | 563 | | } |
| | | 564 | | |
| | | 565 | | #endregion |
| | | 566 | | |
| | | 567 | | #region 性能工具方法 |
| | | 568 | | |
| | | 569 | | /// <summary> |
| | | 570 | | /// 获取适合 SIMD 处理的批次大小(向上取整到 VectorLongCount 的倍数) |
| | | 571 | | /// </summary> |
| | | 572 | | /// <param name="minSize">最小需要处理的元素数量</param> |
| | | 573 | | /// <returns>对齐后的批次大小</returns> |
| | | 574 | | /// <remarks> |
| | | 575 | | /// <para><b>使用场景:</b></para> |
| | | 576 | | /// 用于预分配数组或缓冲区,确保能够充分利用 SIMD 处理能力。 |
| | | 577 | | /// |
| | | 578 | | /// <para><b>计算示例:</b></para> |
| | | 579 | | /// <code> |
| | | 580 | | /// // 假设 VectorLongCount = 4(256-bit SIMD) |
| | | 581 | | /// int aligned = FPSimd.GetAlignedBatchSize(5); // 返回 8 |
| | | 582 | | /// int aligned = FPSimd.GetAlignedBatchSize(4); // 返回 4 |
| | | 583 | | /// int aligned = FPSimd.GetAlignedBatchSize(1); // 返回 4 |
| | | 584 | | /// </code> |
| | | 585 | | /// </remarks> |
| | | 586 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 587 | | public static int GetAlignedBatchSize(int minSize) |
| | | 588 | | { |
| | 0 | 589 | | return (minSize + VectorLongCount - 1) & ~(VectorLongCount - 1); |
| | | 590 | | } |
| | | 591 | | |
| | | 592 | | /// <summary> |
| | | 593 | | /// 检查指定数组长度是否适合使用 SIMD 加速 |
| | | 594 | | /// </summary> |
| | | 595 | | /// <param name="count">元素数量</param> |
| | | 596 | | /// <returns>如果适合 SIMD 加速返回 true</returns> |
| | | 597 | | /// <remarks> |
| | | 598 | | /// <para><b>判断标准:</b></para> |
| | | 599 | | /// <list type="bullet"> |
| | | 600 | | /// <item>硬件支持 SIMD(<see cref="IsHardwareAccelerated"/> 为 true)</item> |
| | | 601 | | /// <item>元素数量 >= VectorLongCount</item> |
| | | 602 | | /// </list> |
| | | 603 | | /// |
| | | 604 | | /// 当返回 false 时,批量操作将使用纯标量实现,性能可能不如逐个调用普通方法。 |
| | | 605 | | /// </remarks> |
| | | 606 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 607 | | public static bool IsSimdBeneficial(int count) |
| | | 608 | | { |
| | 0 | 609 | | return Vector.IsHardwareAccelerated && count >= VectorLongCount; |
| | | 610 | | } |
| | | 611 | | |
| | | 612 | | #endregion |
| | | 613 | | } |