diff --git a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs index 6504720..1be0f68 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs @@ -1,3 +1,5 @@ +using ComputeWeave; + namespace SpectralConvolution.Tests; public sealed class CpuTileConvolverTests @@ -424,4 +426,93 @@ public sealed class CpuTileConvolverTests Assert.Equal(0, wrong); } + + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(4)] + [InlineData(5)] + [InlineData(6)] + [InlineData(7)] + [InlineData(8)] + [InlineData(9)] + public void TheVectorButterfliesMatchTheScalarOnesBitForBit(int log2) + { + var size = 1 << log2; + var twiddles = new Float2[Math.Max(size / 2, 1)]; + for (var index = 0; index < twiddles.Length; index++) + { + var angle = -2d * Math.PI * index / size; + twiddles[index] = new Float2((float)Math.Cos(angle), (float)Math.Sin(angle)); + } + + var random = new Random(log2 * 101); + foreach (var direction in new[] { 1f, -1f }) + { + var expected = new float[size * 4]; + for (var index = 0; index < expected.Length; index++) + { + expected[index] = random.Next(7) switch + { + 0 => 0f, + 1 => -0f, + 2 => (float)(random.NextDouble() * 1e-30), + 3 => (float)(random.NextDouble() * 1e6 - 5e5), + _ => (float)(random.NextDouble() * 2 - 1), + }; + } + + var actual = (float[])expected.Clone(); + + CpuTileConvolver.ButterfliesScalar(expected, log2, twiddles, direction); + CpuTileConvolver.Butterflies(actual, log2, twiddles, direction); + + Assert.Equal(expected.Select(BitConverter.SingleToInt32Bits), actual.Select(BitConverter.SingleToInt32Bits)); + } + } + + [Fact] + public void TheButterfliesRefuseALineThatIsTooShort() + { + var twiddles = new Float2[4]; + + Assert.Throws(() => CpuTileConvolver.Butterflies(new float[8 * 4 - 1], 3, twiddles, 1f)); + Assert.Throws(() => CpuTileConvolver.Butterflies(new float[8 * 4], 3, twiddles.AsSpan(0, 3), 1f)); + } + + [Theory] + [InlineData(1f)] + [InlineData(0f)] + [InlineData(1.75f)] + [InlineData(400f)] + public void TheStoredValuesBecomeBytesLikeTheScalarConversion(float gain) + { + float[] edges = + [ + 0f, -0f, 1e-12f, 0.5f / 255f, 0.5f / 255f + 1e-7f, 1f / 255f, 127.5f / 255f, 254.5f / 255f, 254.5f / 255f + 1e-7f, + 0.999f, 1f, 1.0000001f, 2f, 1e9f, -1e-12f, -0.25f, -1e9f, float.NaN, float.PositiveInfinity, float.NegativeInfinity, + float.Epsilon, float.MaxValue, float.MinValue, + ]; + var pixels = edges.Length; + var stored = new float[pixels * 4]; + for (var pixel = 0; pixel < pixels; pixel++) + { + for (var channel = 0; channel < 4; channel++) + stored[pixel * 4 + channel] = edges[(pixel + channel * 5) % edges.Length]; + } + + var output = new byte[stored.Length]; + using var convolver = new CpuTileConvolver(2); + + convolver.RenderStored(stored, pixels, gain, output); + + for (var pixel = 0; pixel < pixels; pixel++) + { + Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 2] * gain), output[pixel * 4]); + Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 1] * gain), output[pixel * 4 + 1]); + Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4] * gain), output[pixel * 4 + 2]); + Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 3] * gain), output[pixel * 4 + 3]); + } + } }