using System.Numerics; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; namespace SixLabors.ImageSharp.Benchmarks.General.Vectorization { public class UInt32ToSingle { private float[] data; private const int Count = 64; [GlobalSetup] public void Setup() { this.data = new float[Count]; } [Benchmark(Baseline = true)] public void MagicMethod() { ref Vector b = ref Unsafe.As>(ref this.data[0]); int n = Count / Vector.Count; Vector magick = new Vector(32768.0f); Vector scale = new Vector(255f) / new Vector(256f); for (int i = 0; i < n; i++) { // union { float f; uint32_t i; } u; // u.f = 32768.0f + x * (255.0f / 256.0f); // return (uint8_t)u.i; ref Vector d = ref Unsafe.Add(ref b, i); Vector x = d; //x = Vector.Max(x, Vector.Zero); //x = Vector.Min(x, Vector.One); x = (x * scale) + magick; d = x; } } [Benchmark] public void StandardSimd() { int n = Count / Vector.Count; ref Vector b = ref Unsafe.As>(ref this.data[0]); var scale = new Vector(1f / 255f); for (int i = 0; i < n; i++) { ref Vector df = ref Unsafe.Add(ref b, i); Vector du = Unsafe.As, Vector>(ref df); Vector v = Vector.ConvertToSingle(du); v *= scale; df = v; } } } }