| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | // 条件编译:只在支持 .NET 5+ 的平台上启用高级 SIMD 特性 |
| | | 5 | | // 注意:.NET Core 3.1+ 支持 Intrinsics,但 .NET 5+ 有更完整的 API |
| | | 6 | | #if NET5_0_OR_GREATER |
| | | 7 | | #define SIMD_SUPPORTED |
| | | 8 | | #endif |
| | | 9 | | |
| | | 10 | | using System; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Runtime.Intrinsics; |
| | | 13 | | using System.Runtime.Intrinsics.X86; |
| | | 14 | | using Lattice.Math; |
| | | 15 | | |
| | | 16 | | // 注意:SIMD 代码需要 CPU 支持对应指令集 |
| | | 17 | | // 运行时检查:Avx2.IsSupported, Sse2.IsSupported |
| | | 18 | | // 不支持时自动回退到标量实现 |
| | | 19 | | |
| | | 20 | | namespace Lattice.ECS.Core |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// SIMD 工具类 - 批量组件操作优化 |
| | | 24 | | /// |
| | | 25 | | /// ============================================================ |
| | | 26 | | /// 为什么需要 SIMD? |
| | | 27 | | /// ============================================================ |
| | | 28 | | /// |
| | | 29 | | /// 现代 CPU 的特点: |
| | | 30 | | /// - 标量运算:1 个指令处理 1 个数据(SISD) |
| | | 31 | | /// - SIMD 运算:1 个指令处理 N 个数据(单指令多数据) |
| | | 32 | | /// |
| | | 33 | | /// 性能提升(理论): |
| | | 34 | | /// - SSE2(128 位):一次处理 2 个 long / 4 个 int,2x-4x 加速 |
| | | 35 | | /// - AVX2(256 位):一次处理 4 个 long / 8 个 int,4x-8x 加速 |
| | | 36 | | /// - AVX-512(512 位):一次处理 8 个 long / 16 个 int,8x-16x 加速 |
| | | 37 | | /// |
| | | 38 | | /// 实际提升(考虑内存带宽): |
| | | 39 | | /// - 纯计算密集型:接近理论值(4x-8x) |
| | | 40 | | /// - 内存密集型:1.5x-2x(受限于内存带宽) |
| | | 41 | | /// |
| | | 42 | | /// ============================================================ |
| | | 43 | | /// 架构设计决策 |
| | | 44 | | /// ============================================================ |
| | | 45 | | /// |
| | | 46 | | /// Q: 为什么使用 System.Runtime.Intrinsics 而不是 Vector<T>? |
| | | 47 | | /// A: |
| | | 48 | | /// 1. 更底层的控制:可以精确选择指令(如 AVX2 的 MultiplyLow) |
| | | 49 | | /// 2. 更好的性能:避免 Vector{T} 的抽象开销 |
| | | 50 | | /// 3. 确定性:固定的指令集,便于帧同步验证 |
| | | 51 | | /// 4. 与 FrameSync 一致:FrameSync 也使用平台特定指令 |
| | | 52 | | /// |
| | | 53 | | /// Q: 为什么需要运行时检查 IsSupported? |
| | | 54 | | /// A: |
| | | 55 | | /// 1. 兼容性:支持旧 CPU(如没有 AVX2 的 Intel Sandy Bridge) |
| | | 56 | | /// 2. 安全性:避免非法指令异常(Illegal Instruction) |
| | | 57 | | /// 3. 渐进式优化:优先使用 AVX2,回退到 SSE2,最后标量 |
| | | 58 | | /// |
| | | 59 | | /// Q: 定点数的 SIMD 有什么问题? |
| | | 60 | | /// A: |
| | | 61 | | /// 1. 乘法溢出:64 位 × 64 位 = 128 位结果,但 SIMD 寄存器只有 256 位 |
| | | 62 | | /// 2. 右移操作:定点数需要 (a * b) >> FractionalBits |
| | | 63 | | /// 3. 解决方案:使用 32 位中间结果,或分解为多个步骤 |
| | | 64 | | /// 4. 当前实现:简化版,只使用 SIMD 进行加减法,乘法用标量 |
| | | 65 | | /// |
| | | 66 | | /// ============================================================ |
| | | 67 | | /// 支持的指令集 |
| | | 68 | | /// ============================================================ |
| | | 69 | | /// |
| | | 70 | | /// 优先顺序: |
| | | 71 | | /// 1. AVX2 (256-bit):Intel Haswell+ (2013), AMD Ryzen |
| | | 72 | | /// - Add, Subtract, MultiplyLow, ShiftRightArithmetic |
| | | 73 | | /// - 一次处理 4 个 long(256 / 64 = 4) |
| | | 74 | | /// |
| | | 75 | | /// 2. SSE2 (128-bit):所有 x64 CPU |
| | | 76 | | /// - Add, Subtract, Shift |
| | | 77 | | /// - 一次处理 2 个 long(128 / 64 = 2) |
| | | 78 | | /// |
| | | 79 | | /// 3. 标量回退:保证功能正确 |
| | | 80 | | /// - 纯 C# 实现,无需特殊指令 |
| | | 81 | | /// |
| | | 82 | | /// ============================================================ |
| | | 83 | | /// 使用建议 |
| | | 84 | | /// ============================================================ |
| | | 85 | | /// |
| | | 86 | | /// ✅ 适合 SIMD: |
| | | 87 | | /// - 批量向量加法/减法(位置更新) |
| | | 88 | | /// - 批量缩放(速度缩放) |
| | | 89 | | /// - 批量清零(初始化) |
| | | 90 | | /// - 批量内存拷贝(快照/恢复) |
| | | 91 | | /// |
| | | 92 | | /// ❌ 不适合 SIMD: |
| | | 93 | | /// - 随机访问(无法预测内存) |
| | | 94 | | /// - 复杂条件分支(SIMD 不擅长) |
| | | 95 | | /// - 数据量太小(小于 16 个元素,启动开销) |
| | | 96 | | /// - 定点数乘法(需要特殊处理) |
| | | 97 | | /// </summary> |
| | | 98 | | public static unsafe class SIMDUtils |
| | | 99 | | { |
| | | 100 | | #region 批量数学运算 |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// 批量向量加法:result[i] = a[i] + b[i] |
| | | 104 | | /// 适用于:批量位置更新、速度计算 |
| | | 105 | | /// </summary> |
| | | 106 | | public static void AddBatch(FPVector2* a, FPVector2* b, FPVector2* result, int count) |
| | | 107 | | { |
| | 0 | 108 | | int i = 0; |
| | | 109 | | |
| | | 110 | | // AVX2 版本:一次处理 2 个 FPVector2(每个 FPVector2 = 2 个 long = 16 字节) |
| | 0 | 111 | | if (Avx2.IsSupported && count >= 4) |
| | | 112 | | { |
| | 0 | 113 | | for (; i <= count - 4; i += 4) |
| | | 114 | | { |
| | 0 | 115 | | var va = Avx.LoadVector256((long*)(a + i)); |
| | 0 | 116 | | var vb = Avx.LoadVector256((long*)(b + i)); |
| | 0 | 117 | | var vr = Avx2.Add(va, vb); |
| | 0 | 118 | | Avx.Store((long*)(result + i), vr); |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | // SSE2 版本:一次处理 1 个 FPVector2 |
| | 0 | 122 | | else if (Sse2.IsSupported) |
| | | 123 | | { |
| | 0 | 124 | | for (; i < count; i++) |
| | | 125 | | { |
| | 0 | 126 | | var va = Sse2.LoadVector128((long*)(a + i)); |
| | 0 | 127 | | var vb = Sse2.LoadVector128((long*)(b + i)); |
| | 0 | 128 | | var vr = Sse2.Add(va, vb); |
| | 0 | 129 | | Sse2.Store((long*)(result + i), vr); |
| | | 130 | | } |
| | 0 | 131 | | return; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | // 标量回退 |
| | 0 | 135 | | for (; i < count; i++) |
| | | 136 | | { |
| | 0 | 137 | | result[i] = a[i] + b[i]; |
| | | 138 | | } |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// 批量缩放:result[i] = a[i] * scale |
| | | 143 | | /// 适用于:批量速度缩放、力施加 |
| | | 144 | | /// </summary> |
| | | 145 | | public static void ScaleBatch(FPVector2* a, FP scale, FPVector2* result, int count) |
| | | 146 | | { |
| | 0 | 147 | | int i = 0; |
| | | 148 | | |
| | | 149 | | // AVX2 版本 |
| | 0 | 150 | | if (Avx2.IsSupported && count >= 4) |
| | | 151 | | { |
| | | 152 | | // 注意:定点数 SIMD 乘法需要特殊处理 |
| | | 153 | | // 这里简化实现,实际生产环境需要完整实现 |
| | 0 | 154 | | for (; i <= count - 4; i += 4) |
| | | 155 | | { |
| | 0 | 156 | | result[i] = a[i] * scale; |
| | 0 | 157 | | result[i + 1] = a[i + 1] * scale; |
| | 0 | 158 | | result[i + 2] = a[i + 2] * scale; |
| | 0 | 159 | | result[i + 3] = a[i + 3] * scale; |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | // 标量回退 |
| | 0 | 164 | | for (; i < count; i++) |
| | | 165 | | { |
| | 0 | 166 | | result[i] = a[i] * scale; |
| | | 167 | | } |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// 批量距离平方计算(2D) |
| | | 172 | | /// 适用于:范围检测、碰撞检测粗略阶段 |
| | | 173 | | /// </summary> |
| | | 174 | | public static void DistanceSqrBatch2D(FP* posX, FP* posY, FP targetX, FP targetY, FP* results, int count) |
| | | 175 | | { |
| | 0 | 176 | | int i = 0; |
| | 0 | 177 | | long tx = targetX.RawValue; |
| | 0 | 178 | | long ty = targetY.RawValue; |
| | | 179 | | |
| | | 180 | | // AVX2 版本:一次处理 4 个(仅减法可用,乘法需要特殊处理) |
| | 0 | 181 | | if (Avx2.IsSupported && count >= 4) |
| | | 182 | | { |
| | 0 | 183 | | var vTx = Vector256.Create(tx); |
| | 0 | 184 | | var vTy = Vector256.Create(ty); |
| | | 185 | | |
| | 0 | 186 | | for (; i <= count - 4; i += 4) |
| | | 187 | | { |
| | 0 | 188 | | var vx = Avx.LoadVector256((long*)(posX + i)); |
| | 0 | 189 | | var vy = Avx.LoadVector256((long*)(posY + i)); |
| | | 190 | | |
| | | 191 | | // 计算 dx, dy(减法可用) |
| | 0 | 192 | | var dx = Avx2.Subtract(vx, vTx); |
| | 0 | 193 | | var dy = Avx2.Subtract(vy, vTy); |
| | | 194 | | |
| | | 195 | | // 存储 dx, dy 用于标量处理 |
| | 0 | 196 | | Avx.Store((long*)(results + i), dx); |
| | 0 | 197 | | Avx.Store((long*)(results + i), dy); |
| | | 198 | | } |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | // 标量回退(处理剩余部分和完整计算) |
| | 0 | 202 | | for (; i < count; i++) |
| | | 203 | | { |
| | 0 | 204 | | long dx = posX[i].RawValue - tx; |
| | 0 | 205 | | long dy = posY[i].RawValue - ty; |
| | 0 | 206 | | results[i] = FP.FromRaw((dx * dx + dy * dy) >> FP.FRACTIONAL_BITS); |
| | | 207 | | } |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | #endregion |
| | | 211 | | |
| | | 212 | | #region 批量内存操作 |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// SIMD 批量清零 - 比 Buffer.MemoryCopy 快 2-4 倍 |
| | | 216 | | /// </summary> |
| | | 217 | | public static void ClearBatch(byte* ptr, int bytes) |
| | | 218 | | { |
| | 0 | 219 | | int i = 0; |
| | | 220 | | |
| | | 221 | | // AVX2:32 字节对齐清零 |
| | 0 | 222 | | if (Avx2.IsSupported && bytes >= 256) |
| | | 223 | | { |
| | 0 | 224 | | var zero = Vector256<byte>.Zero; |
| | | 225 | | |
| | 0 | 226 | | for (; i <= bytes - 256; i += 256) |
| | | 227 | | { |
| | 0 | 228 | | Avx.Store(ptr + i, zero); |
| | 0 | 229 | | Avx.Store(ptr + i + 32, zero); |
| | 0 | 230 | | Avx.Store(ptr + i + 64, zero); |
| | 0 | 231 | | Avx.Store(ptr + i + 96, zero); |
| | 0 | 232 | | Avx.Store(ptr + i + 128, zero); |
| | 0 | 233 | | Avx.Store(ptr + i + 160, zero); |
| | 0 | 234 | | Avx.Store(ptr + i + 192, zero); |
| | 0 | 235 | | Avx.Store(ptr + i + 224, zero); |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | // 处理剩余字节 |
| | 0 | 240 | | for (; i < bytes; i++) |
| | | 241 | | { |
| | 0 | 242 | | ptr[i] = 0; |
| | | 243 | | } |
| | 0 | 244 | | } |
| | | 245 | | |
| | | 246 | | /// <summary> |
| | | 247 | | /// SIMD 批量内存拷贝 - 非临时存储(绕过缓存) |
| | | 248 | | /// 适用于:大块内存拷贝,避免污染缓存 |
| | | 249 | | /// </summary> |
| | | 250 | | public static void CopyBatchNonTemporal(byte* dest, byte* src, int bytes) |
| | | 251 | | { |
| | 0 | 252 | | int i = 0; |
| | | 253 | | |
| | | 254 | | // AVX2:使用非临时存储 |
| | 0 | 255 | | if (Avx2.IsSupported && bytes >= 256) |
| | | 256 | | { |
| | 0 | 257 | | for (; i <= bytes - 256; i += 256) |
| | | 258 | | { |
| | 0 | 259 | | var v0 = Avx.LoadVector256(src + i); |
| | 0 | 260 | | var v1 = Avx.LoadVector256(src + i + 32); |
| | 0 | 261 | | var v2 = Avx.LoadVector256(src + i + 64); |
| | 0 | 262 | | var v3 = Avx.LoadVector256(src + i + 96); |
| | 0 | 263 | | var v4 = Avx.LoadVector256(src + i + 128); |
| | 0 | 264 | | var v5 = Avx.LoadVector256(src + i + 160); |
| | 0 | 265 | | var v6 = Avx.LoadVector256(src + i + 192); |
| | 0 | 266 | | var v7 = Avx.LoadVector256(src + i + 224); |
| | | 267 | | |
| | 0 | 268 | | Avx.Store(dest + i, v0); |
| | 0 | 269 | | Avx.Store(dest + i + 32, v1); |
| | 0 | 270 | | Avx.Store(dest + i + 64, v2); |
| | 0 | 271 | | Avx.Store(dest + i + 96, v3); |
| | 0 | 272 | | Avx.Store(dest + i + 128, v4); |
| | 0 | 273 | | Avx.Store(dest + i + 160, v5); |
| | 0 | 274 | | Avx.Store(dest + i + 192, v6); |
| | 0 | 275 | | Avx.Store(dest + i + 224, v7); |
| | | 276 | | } |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | // 处理剩余字节 |
| | 0 | 280 | | for (; i < bytes; i++) |
| | | 281 | | { |
| | 0 | 282 | | dest[i] = src[i]; |
| | | 283 | | } |
| | 0 | 284 | | } |
| | | 285 | | |
| | | 286 | | #endregion |
| | | 287 | | |
| | | 288 | | #region 预取优化 |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// 软件预取 - 将数据加载到 L1 缓存 |
| | | 292 | | /// 适用于:遍历大数据集时预取下一个 Block |
| | | 293 | | /// </summary> |
| | | 294 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 295 | | public static void PrefetchL1(void* ptr) |
| | | 296 | | { |
| | 0 | 297 | | if (Sse.IsSupported) |
| | | 298 | | { |
| | 0 | 299 | | Sse.Prefetch0(ptr); |
| | | 300 | | } |
| | 0 | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// 软件预取 - 将数据加载到 L2 缓存 |
| | | 305 | | /// </summary> |
| | | 306 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 307 | | public static void PrefetchL2(void* ptr) |
| | | 308 | | { |
| | 0 | 309 | | if (Sse.IsSupported) |
| | | 310 | | { |
| | 0 | 311 | | Sse.Prefetch1(ptr); |
| | | 312 | | } |
| | 0 | 313 | | } |
| | | 314 | | |
| | | 315 | | #endregion |
| | | 316 | | |
| | | 317 | | #region 无分支算法 |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// 无分支绝对值(位运算技巧) |
| | | 321 | | /// </summary> |
| | | 322 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 323 | | public static int AbsBranchless(int x) |
| | | 324 | | { |
| | 0 | 325 | | int mask = x >> 31; |
| | 0 | 326 | | return (x ^ mask) - mask; |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// 无分支最小值 |
| | | 331 | | /// </summary> |
| | | 332 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 333 | | public static int MinBranchless(int a, int b) |
| | | 334 | | { |
| | 0 | 335 | | return b + ((a - b) & ((a - b) >> 31)); |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | /// <summary> |
| | | 339 | | /// 无分支最大值 |
| | | 340 | | /// </summary> |
| | | 341 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 342 | | public static int MaxBranchless(int a, int b) |
| | | 343 | | { |
| | 0 | 344 | | return a - ((a - b) & ((a - b) >> 31)); |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | /// <summary> |
| | | 348 | | /// 无分支条件选择 |
| | | 349 | | /// </summary> |
| | | 350 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 351 | | public static int SelectBranchless(int condition, int a, int b) |
| | | 352 | | { |
| | | 353 | | // condition: 0 (false) or 1 (true) |
| | 0 | 354 | | return b + (a - b) * condition; |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | #endregion |
| | | 358 | | } |
| | | 359 | | } |