From 5e70320edc15ac5e47fa000e9d60ef7a888d2d68 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 8 Oct 2026 07:52:00 +0900 Subject: [PATCH] =?UTF-8?q?CPU=E3=81=AE=E7=95=B3=E3=81=BF=E8=BE=BC?= =?UTF-8?q?=E3=81=BF=E3=81=AEFFT=E3=81=A72=E6=AE=B5=E3=81=9A=E3=81=A4?= =?UTF-8?q?=E3=81=BE=E3=81=A8=E3=82=81=E3=81=A6=E8=A8=88=E7=AE=97=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・CPUの畳み込みを高速化-M --- .../SpectralConvolution/CpuTileConvolver.cs | 97 ++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs index a6732e8..155093d 100644 --- a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs +++ b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs @@ -649,7 +649,15 @@ internal sealed class CpuTileConvolver : IDisposable ref var data = ref MemoryMarshal.GetReference(line); ref var table = ref MemoryMarshal.GetReference(twiddles); - for (var stage = 0; stage < log2; stage++) + var stage = 0; + if (allowWide && Vector256.IsHardwareAccelerated && log2 >= 2) + { + FirstTwoStages(ref data, ref table, size, log2, direction); + for (stage = 2; stage + 1 < log2; stage += 2) + StagePair(ref data, ref table, size, log2, stage, direction); + } + + for (; stage < log2; stage++) { var half = 1 << stage; var shift = log2 - 1 - stage; @@ -679,6 +687,93 @@ internal sealed class CpuTileConvolver : IDisposable } } + static Vector128 Rotate(Vector128 value, Vector128 real, Vector128 imaginary) + => value * real + Vector128.Shuffle(value, Vector128.Create(1, 0, 3, 2)) * imaginary; + + static Vector256 Rotate(Vector256 value, Vector256 real, Vector256 imaginary) + => value * real + Vector256.Shuffle(value, Vector256.Create(1, 0, 3, 2, 5, 4, 7, 6)) * imaginary; + + static void FirstTwoStages(ref float data, ref Float2 table, int size, int log2, float direction) + { + var first = Unsafe.Add(ref table, 0); + var second = Unsafe.Add(ref table, 1 << (log2 - 2)); + var firstImaginaryPart = first.Y * direction; + var secondImaginaryPart = second.Y * direction; + var firstReal = Vector128.Create(first.X); + var firstImaginary = Vector128.Create(-firstImaginaryPart, firstImaginaryPart, -firstImaginaryPart, firstImaginaryPart); + var secondReal = Vector128.Create(second.X); + var secondImaginary = Vector128.Create(-secondImaginaryPart, secondImaginaryPart, -secondImaginaryPart, secondImaginaryPart); + for (var block = 0; block < size; block += 4) + { + var start = (nuint)(block * Channels); + var value0 = Vector128.LoadUnsafe(ref data, start); + var value1 = Vector128.LoadUnsafe(ref data, start + Channels); + var value2 = Vector128.LoadUnsafe(ref data, start + Channels * 2); + var value3 = Vector128.LoadUnsafe(ref data, start + Channels * 3); + var product1 = Rotate(value1, firstReal, firstImaginary); + var product3 = Rotate(value3, firstReal, firstImaginary); + var sum0 = value0 + product1; + var sum1 = value0 - product1; + var sum2 = value2 + product3; + var sum3 = value2 - product3; + var evenProduct = Rotate(sum2, firstReal, firstImaginary); + var oddProduct = Rotate(sum3, secondReal, secondImaginary); + (sum0 + evenProduct).StoreUnsafe(ref data, start); + (sum1 + oddProduct).StoreUnsafe(ref data, start + Channels); + (sum0 - evenProduct).StoreUnsafe(ref data, start + Channels * 2); + (sum1 - oddProduct).StoreUnsafe(ref data, start + Channels * 3); + } + } + + static void StagePair(ref float data, ref Float2 table, int size, int log2, int stage, float direction) + { + var half = 1 << stage; + var firstShift = log2 - 1 - stage; + var secondShift = log2 - 2 - stage; + var distance = (nuint)(half * Channels); + for (var position = 0; position < half; position += 2) + { + var firstNear = Unsafe.Add(ref table, position << firstShift); + var firstFar = Unsafe.Add(ref table, (position + 1) << firstShift); + var firstNearImaginary = firstNear.Y * direction; + var firstFarImaginary = firstFar.Y * direction; + var firstReal = Vector256.Create(firstNear.X, firstNear.X, firstNear.X, firstNear.X, firstFar.X, firstFar.X, firstFar.X, firstFar.X); + var firstImaginary = Vector256.Create(-firstNearImaginary, firstNearImaginary, -firstNearImaginary, firstNearImaginary, -firstFarImaginary, firstFarImaginary, -firstFarImaginary, firstFarImaginary); + var evenNear = Unsafe.Add(ref table, position << secondShift); + var evenFar = Unsafe.Add(ref table, (position + 1) << secondShift); + var evenNearImaginary = evenNear.Y * direction; + var evenFarImaginary = evenFar.Y * direction; + var evenReal = Vector256.Create(evenNear.X, evenNear.X, evenNear.X, evenNear.X, evenFar.X, evenFar.X, evenFar.X, evenFar.X); + var evenImaginary = Vector256.Create(-evenNearImaginary, evenNearImaginary, -evenNearImaginary, evenNearImaginary, -evenFarImaginary, evenFarImaginary, -evenFarImaginary, evenFarImaginary); + var oddNear = Unsafe.Add(ref table, (position + half) << secondShift); + var oddFar = Unsafe.Add(ref table, (position + half + 1) << secondShift); + var oddNearImaginary = oddNear.Y * direction; + var oddFarImaginary = oddFar.Y * direction; + var oddReal = Vector256.Create(oddNear.X, oddNear.X, oddNear.X, oddNear.X, oddFar.X, oddFar.X, oddFar.X, oddFar.X); + var oddImaginary = Vector256.Create(-oddNearImaginary, oddNearImaginary, -oddNearImaginary, oddNearImaginary, -oddFarImaginary, oddFarImaginary, -oddFarImaginary, oddFarImaginary); + for (var block = 0; block < size; block += half * 4) + { + var start = (nuint)((block + position) * Channels); + var value0 = Vector256.LoadUnsafe(ref data, start); + var value1 = Vector256.LoadUnsafe(ref data, start + distance); + var value2 = Vector256.LoadUnsafe(ref data, start + distance * 2); + var value3 = Vector256.LoadUnsafe(ref data, start + distance * 3); + var product1 = Rotate(value1, firstReal, firstImaginary); + var product3 = Rotate(value3, firstReal, firstImaginary); + var sum0 = value0 + product1; + var sum1 = value0 - product1; + var sum2 = value2 + product3; + var sum3 = value2 - product3; + var evenProduct = Rotate(sum2, evenReal, evenImaginary); + var oddProduct = Rotate(sum3, oddReal, oddImaginary); + (sum0 + evenProduct).StoreUnsafe(ref data, start); + (sum1 + oddProduct).StoreUnsafe(ref data, start + distance); + (sum0 - evenProduct).StoreUnsafe(ref data, start + distance * 2); + (sum1 - oddProduct).StoreUnsafe(ref data, start + distance * 3); + } + } + } + static void StageWithPairedTwiddles(ref float data, ref Float2 table, int size, int half, int shift, float direction, nuint distance) { for (var position = 0; position < half; position += 2)