|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Numerics; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +#if SUPPORTS_RUNTIME_INTRINSICS |
| 9 | +using System.Runtime.Intrinsics; |
| 10 | +using System.Runtime.Intrinsics.X86; |
| 11 | +using static SixLabors.ImageSharp.SimdUtils; |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters |
| 15 | +{ |
| 16 | + internal abstract partial class JpegColorConverter |
| 17 | + { |
| 18 | + internal sealed class FromCmykAvx2 : Avx2JpegColorConverter |
| 19 | + { |
| 20 | + public FromCmykAvx2(int precision) |
| 21 | + : base(JpegColorSpace.Cmyk, precision) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + protected override void ConvertCoreVectorized(in ComponentValues values, Span<Vector4> result) |
| 26 | + { |
| 27 | +#if SUPPORTS_RUNTIME_INTRINSICS |
| 28 | + ref Vector256<float> cBase = |
| 29 | + ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component0)); |
| 30 | + ref Vector256<float> mBase = |
| 31 | + ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component1)); |
| 32 | + ref Vector256<float> yBase = |
| 33 | + ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component2)); |
| 34 | + ref Vector256<float> kBase = |
| 35 | + ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(values.Component3)); |
| 36 | + |
| 37 | + ref Vector256<float> resultBase = |
| 38 | + ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(result)); |
| 39 | + |
| 40 | + // Used for the color conversion |
| 41 | + var scale = Vector256.Create(1 / this.MaximumValue); |
| 42 | + var one = Vector256.Create(1F); |
| 43 | + |
| 44 | + // Used for packing |
| 45 | + ref byte control = ref MemoryMarshal.GetReference(HwIntrinsics.PermuteMaskEvenOdd8x32); |
| 46 | + Vector256<int> vcontrol = Unsafe.As<byte, Vector256<int>>(ref control); |
| 47 | + |
| 48 | + int n = result.Length / 8; |
| 49 | + for (int i = 0; i < n; i++) |
| 50 | + { |
| 51 | + Vector256<float> k = Avx2.PermuteVar8x32(Unsafe.Add(ref kBase, i), vcontrol); |
| 52 | + Vector256<float> c = Avx2.PermuteVar8x32(Unsafe.Add(ref cBase, i), vcontrol); |
| 53 | + Vector256<float> m = Avx2.PermuteVar8x32(Unsafe.Add(ref mBase, i), vcontrol); |
| 54 | + Vector256<float> y = Avx2.PermuteVar8x32(Unsafe.Add(ref yBase, i), vcontrol); |
| 55 | + |
| 56 | + k = Avx.Multiply(k, scale); |
| 57 | + |
| 58 | + c = Avx.Multiply(Avx.Multiply(c, k), scale); |
| 59 | + m = Avx.Multiply(Avx.Multiply(m, k), scale); |
| 60 | + y = Avx.Multiply(Avx.Multiply(y, k), scale); |
| 61 | + |
| 62 | + Vector256<float> cmLo = Avx.UnpackLow(c, m); |
| 63 | + Vector256<float> yoLo = Avx.UnpackLow(y, one); |
| 64 | + Vector256<float> cmHi = Avx.UnpackHigh(c, m); |
| 65 | + Vector256<float> yoHi = Avx.UnpackHigh(y, one); |
| 66 | + |
| 67 | + ref Vector256<float> destination = ref Unsafe.Add(ref resultBase, i * 4); |
| 68 | + |
| 69 | + destination = Avx.Shuffle(cmLo, yoLo, 0b01_00_01_00); |
| 70 | + Unsafe.Add(ref destination, 1) = Avx.Shuffle(cmLo, yoLo, 0b11_10_11_10); |
| 71 | + Unsafe.Add(ref destination, 2) = Avx.Shuffle(cmHi, yoHi, 0b01_00_01_00); |
| 72 | + Unsafe.Add(ref destination, 3) = Avx.Shuffle(cmHi, yoHi, 0b11_10_11_10); |
| 73 | + } |
| 74 | +#endif |
| 75 | + } |
| 76 | + |
| 77 | + protected override void ConvertCore(in ComponentValues values, Span<Vector4> result) => |
| 78 | + FromCmykBasic.ConvertCore(values, result, this.MaximumValue); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments