< Summary

Information
Class: Lattice.Math.FPSimd
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPSimd.cs
Line coverage
63%
Covered lines: 92
Uncovered lines: 53
Coverable lines: 145
Total lines: 613
Line coverage: 63.4%
Branch coverage
60%
Covered branches: 56
Total branches: 92
Branch coverage: 60.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_IsHardwareAccelerated()100%210%
AddBatch(...)100%1212100%
SubtractBatch(...)83.33%121294.11%
MultiplyBatch(...)66.66%6690%
NegateBatch(...)90%101093.75%
AbsBatch(...)91.66%121294.11%
AddBatchVector2(...)83.33%121294.44%
SubtractBatchVector2(...)0%156120%
NegateBatchVector2(...)0%110100%
MultiplyBatchVector2(...)0%2040%
GetAlignedBatchSize(...)100%210%
IsSimdBeneficial(...)0%620%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPSimd.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4#nullable enable
 5
 6using System;
 7using System.Numerics;
 8using System.Runtime.CompilerServices;
 9using System.Runtime.InteropServices;
 10
 11namespace Lattice.Math;
 12
 13/// <summary>
 14/// SIMD 整数运算加速 - 仅使用 Vector&lt;long&gt;/Vector&lt;int&gt;
 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&lt;float&gt;, Vector&lt;double&gt;)被明确禁止</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) &gt;&gt; 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>
 44public static class FPSimd
 45{
 46    #region 常量
 47
 48    /// <summary>
 49    /// Vector&lt;T&gt; 中 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>
 158    public static readonly int VectorLongCount = Vector<long>.Count;
 59
 60    /// <summary>
 61    /// Vector&lt;T&gt; 中 int (32位) 的向量宽度
 62    /// </summary>
 63    /// <remarks>
 64    /// 当前主要用于 32 位定点数运算的潜在扩展,64 位 FP 运算主要使用 <see cref="VectorLongCount"/>。
 65    /// </remarks>
 166    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>
 076    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>小数组(&lt; 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    {
 30120        int count = a.Length;
 30121        if (b.Length != count || result.Length < count)
 2122            throw new ArgumentException("数组长度不匹配");
 123
 124        // 将 FP 数组转换为 long 原始值数组进行 SIMD 处理
 28125        ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FP, long>(a);
 28126        ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FP, long>(b);
 28127        Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result);
 128
 28129        int i = 0;
 130
 131        // SIMD 主循环:每次处理 VectorLongCount 个元素
 28132        if (Vector.IsHardwareAccelerated && count >= VectorLongCount)
 133        {
 20134            int simdLimit = count - (count % VectorLongCount);
 874135            for (; i < simdLimit; i += VectorLongCount)
 136            {
 427137                Vector<long> va = new Vector<long>(aRaw.Slice(i));
 427138                Vector<long> vb = new Vector<long>(bRaw.Slice(i));
 427139                Vector<long> vr = va + vb;
 427140                vr.CopyTo(resultRaw.Slice(i));
 141            }
 142        }
 143
 144        // 标量收尾:处理剩余元素
 96145        for (; i < count; i++)
 146        {
 34147            resultRaw[i] = aRaw[i] + bRaw[i];
 148        }
 28149    }
 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    {
 13181        int count = a.Length;
 13182        if (b.Length != count || result.Length < count)
 0183            throw new ArgumentException("数组长度不匹配");
 184
 13185        ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FP, long>(a);
 13186        ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FP, long>(b);
 13187        Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result);
 188
 13189        int i = 0;
 190
 13191        if (Vector.IsHardwareAccelerated && count >= VectorLongCount)
 192        {
 10193            int simdLimit = count - (count % VectorLongCount);
 196194            for (; i < simdLimit; i += VectorLongCount)
 195            {
 93196                Vector<long> va = new Vector<long>(aRaw.Slice(i));
 93197                Vector<long> vb = new Vector<long>(bRaw.Slice(i));
 93198                Vector<long> vr = va - vb;
 93199                vr.CopyTo(resultRaw.Slice(i));
 200            }
 201        }
 202
 53203        for (; i < count; i++)
 204        {
 20205            resultRaw[i] = aRaw[i] - bRaw[i];
 206        }
 13207    }
 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) &gt;&gt; 16</para>
 222    ///
 223    /// <para><b>⚠️ SIMD 限制说明:</b></para>
 224    /// 当前实现使用标量运算,原因如下:
 225    /// <list type="number">
 226    ///   <item>64位乘法在 SIMD 中会产生 128 位结果,Vector&lt;long&gt; 不直接支持</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    {
 13241        int count = a.Length;
 13242        if (b.Length != count || result.Length < count)
 0243            throw new ArgumentException("数组长度不匹配");
 244
 13245        ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FP, long>(a);
 13246        ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FP, long>(b);
 13247        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&lt;long&gt; 的乘法操作可能产生溢出或平台差异
 786253        for (int i = 0; i < count; i++)
 254        {
 380255            long product = aRaw[i] * bRaw[i];
 380256            resultRaw[i] = (product + MulRound) >> FP.FRACTIONAL_BITS;
 257        }
 13258    }
 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    {
 15289        int count = input.Length;
 15290        if (result.Length < count)
 0291            throw new ArgumentException("输出数组容量不足");
 292
 15293        ReadOnlySpan<long> inputRaw = MemoryMarshal.Cast<FP, long>(input);
 15294        Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result);
 295
 15296        int i = 0;
 297
 15298        if (Vector.IsHardwareAccelerated && count >= VectorLongCount)
 299        {
 11300            Vector<long> zero = Vector<long>.Zero;
 11301            int simdLimit = count - (count % VectorLongCount);
 205302            for (; i < simdLimit; i += VectorLongCount)
 303            {
 97304                Vector<long> v = new Vector<long>(inputRaw.Slice(i));
 97305                Vector<long> vr = zero - v;
 97306                vr.CopyTo(resultRaw.Slice(i));
 307            }
 308        }
 309
 61310        for (; i < count; i++)
 311        {
 23312            resultRaw[i] = -inputRaw[i];
 313        }
 15314    }
 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 &gt;&gt; 63)) - (x &gt;&gt; 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    {
 15346        int count = input.Length;
 15347        if (result.Length < count)
 0348            throw new ArgumentException("输出数组容量不足");
 349
 15350        ReadOnlySpan<long> inputRaw = MemoryMarshal.Cast<FP, long>(input);
 15351        Span<long> resultRaw = MemoryMarshal.Cast<FP, long>(result);
 352
 15353        int i = 0;
 354
 15355        if (Vector.IsHardwareAccelerated && count >= VectorLongCount)
 356        {
 11357            int simdLimit = count - (count % VectorLongCount);
 205358            for (; i < simdLimit; i += VectorLongCount)
 359            {
 97360                Vector<long> v = new Vector<long>(inputRaw.Slice(i));
 361                // 无分支绝对值:(v ^ (v >> 63)) - (v >> 63)
 97362                Vector<long> sign = v >> 63;
 97363                Vector<long> vr = (v ^ sign) - sign;
 97364                vr.CopyTo(resultRaw.Slice(i));
 365            }
 366        }
 367
 61368        for (; i < count; i++)
 369        {
 23370            long v = inputRaw[i];
 23371            resultRaw[i] = v < 0 ? -v : v;
 372        }
 15373    }
 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    {
 7407        int count = a.Length;
 7408        if (b.Length != count || result.Length < count)
 0409            throw new ArgumentException("数组长度不匹配");
 410
 411        // 将 FPVector2 数组转换为 long 数组(每个向量2个long,共 2*count 个long)
 7412        ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FPVector2, long>(a);
 7413        ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FPVector2, long>(b);
 7414        Span<long> resultRaw = MemoryMarshal.Cast<FPVector2, long>(result);
 415
 7416        int totalLongs = count * 2;
 7417        int i = 0;
 418
 7419        if (Vector.IsHardwareAccelerated && totalLongs >= VectorLongCount)
 420        {
 6421            int simdLimit = totalLongs - (totalLongs % VectorLongCount);
 164422            for (; i < simdLimit; i += VectorLongCount)
 423            {
 79424                Vector<long> va = new Vector<long>(aRaw.Slice(i));
 79425                Vector<long> vb = new Vector<long>(bRaw.Slice(i));
 79426                Vector<long> vr = va + vb;
 79427                vr.CopyTo(resultRaw.Slice(i));
 428            }
 429        }
 430
 15431        for (; i < totalLongs; i++)
 432        {
 4433            resultRaw[i] = aRaw[i] + bRaw[i];
 434        }
 7435    }
 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    {
 0457        int count = a.Length;
 0458        if (b.Length != count || result.Length < count)
 0459            throw new ArgumentException("数组长度不匹配");
 460
 0461        ReadOnlySpan<long> aRaw = MemoryMarshal.Cast<FPVector2, long>(a);
 0462        ReadOnlySpan<long> bRaw = MemoryMarshal.Cast<FPVector2, long>(b);
 0463        Span<long> resultRaw = MemoryMarshal.Cast<FPVector2, long>(result);
 464
 0465        int totalLongs = count * 2;
 0466        int i = 0;
 467
 0468        if (Vector.IsHardwareAccelerated && totalLongs >= VectorLongCount)
 469        {
 0470            int simdLimit = totalLongs - (totalLongs % VectorLongCount);
 0471            for (; i < simdLimit; i += VectorLongCount)
 472            {
 0473                Vector<long> va = new Vector<long>(aRaw.Slice(i));
 0474                Vector<long> vb = new Vector<long>(bRaw.Slice(i));
 0475                Vector<long> vr = va - vb;
 0476                vr.CopyTo(resultRaw.Slice(i));
 477            }
 478        }
 479
 0480        for (; i < totalLongs; i++)
 481        {
 0482            resultRaw[i] = aRaw[i] - bRaw[i];
 483        }
 0484    }
 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    {
 0501        int count = input.Length;
 0502        if (result.Length < count)
 0503            throw new ArgumentException("输出数组容量不足");
 504
 0505        ReadOnlySpan<long> inputRaw = MemoryMarshal.Cast<FPVector2, long>(input);
 0506        Span<long> resultRaw = MemoryMarshal.Cast<FPVector2, long>(result);
 507
 0508        int totalLongs = count * 2;
 0509        int i = 0;
 510
 0511        if (Vector.IsHardwareAccelerated && totalLongs >= VectorLongCount)
 512        {
 0513            Vector<long> zero = Vector<long>.Zero;
 0514            int simdLimit = totalLongs - (totalLongs % VectorLongCount);
 0515            for (; i < simdLimit; i += VectorLongCount)
 516            {
 0517                Vector<long> v = new Vector<long>(inputRaw.Slice(i));
 0518                Vector<long> vr = zero - v;
 0519                vr.CopyTo(resultRaw.Slice(i));
 520            }
 521        }
 522
 0523        for (; i < totalLongs; i++)
 524        {
 0525            resultRaw[i] = -inputRaw[i];
 526        }
 0527    }
 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) &gt;&gt; 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    {
 0549        int count = input.Length;
 0550        if (result.Length < count)
 0551            throw new ArgumentException("输出数组容量不足");
 552
 0553        long s = scalar.RawValue;
 554        const long MulRound = 1L << (FP.FRACTIONAL_BITS - 1);
 555
 0556        for (int i = 0; i < count; i++)
 557        {
 0558            FPVector2 v = input[i];
 0559            long x = (v.X.RawValue * s + MulRound) >> FP.FRACTIONAL_BITS;
 0560            long y = (v.Y.RawValue * s + MulRound) >> FP.FRACTIONAL_BITS;
 0561            result[i] = new FPVector2(FP.FromRaw(x), FP.FromRaw(y));
 562        }
 0563    }
 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    {
 0589        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    {
 0609        return Vector.IsHardwareAccelerated && count >= VectorLongCount;
 610    }
 611
 612    #endregion
 613}