Browse Source

Rename and refactor method

pull/2917/head
James Jackson-South 1 year ago
parent
commit
cd3aa18dd4
  1. 12
      src/ImageSharp/Common/Helpers/SimdUtils.cs
  2. 2
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs
  3. 6
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector.cs
  4. 6
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector.cs
  5. 48
      tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Round.cs
  6. 4
      tests/ImageSharp.Tests/Common/SimdUtilsTests.cs

12
src/ImageSharp/Common/Helpers/SimdUtils.cs

@ -38,7 +38,7 @@ internal static partial class SimdUtils
/// </summary> /// </summary>
/// <param name="v">The vector</param> /// <param name="v">The vector</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector<float> FastRound(this Vector<float> v) internal static Vector<float> RoundToNearestInteger(this Vector<float> v)
{ {
if (Avx512F.IsSupported && Vector<float>.Count == Vector512<float>.Count) if (Avx512F.IsSupported && Vector<float>.Count == Vector512<float>.Count)
{ {
@ -59,13 +59,11 @@ internal static partial class SimdUtils
} }
// https://github.com/g-truc/glm/blob/master/glm/simd/common.h#L11 // https://github.com/g-truc/glm/blob/master/glm/simd/common.h#L11
Vector<int> magic0 = new(int.MinValue); // 0x80000000 Vector<float> sign = v & new Vector<float>(-0F);
Vector<float> sgn0 = Vector.AsVectorSingle(magic0); Vector<float> val_2p23_f32 = sign | new Vector<float>(8388608F);
Vector<float> and0 = Vector.BitwiseAnd(sgn0, v);
Vector<float> or0 = Vector.BitwiseOr(and0, new Vector<float>(8388608.0f));
Vector<float> add0 = Vector.Add(v, or0);
return Vector.Subtract(add0, or0); val_2p23_f32 = (v + val_2p23_f32) - val_2p23_f32;
return val_2p23_f32 | sign;
} }
[Conditional("DEBUG")] [Conditional("DEBUG")]

2
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs

@ -588,6 +588,6 @@ internal partial struct Block8x8F : IEquatable<Block8x8F>
row += off; row += off;
row = Vector.Max(row, Vector<float>.Zero); row = Vector.Max(row, Vector<float>.Zero);
row = Vector.Min(row, max); row = Vector.Min(row, max);
return row.FastRound(); return row.RoundToNearestInteger();
} }
} }

6
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector.cs

@ -55,9 +55,9 @@ internal abstract partial class JpegColorConverterBase
Vector<float> g = y + (cb * gCbMult) + (cr * gCrMult); Vector<float> g = y + (cb * gCbMult) + (cr * gCrMult);
Vector<float> b = y + (cb * bCbMult); Vector<float> b = y + (cb * bCbMult);
r = r.FastRound(); r = r.RoundToNearestInteger();
g = g.FastRound(); g = g.RoundToNearestInteger();
b = b.FastRound(); b = b.RoundToNearestInteger();
r *= scale; r *= scale;
g *= scale; g *= scale;
b *= scale; b *= scale;

6
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector.cs

@ -59,9 +59,9 @@ internal abstract partial class JpegColorConverterBase
Vector<float> g = y + (cb * gCbMult) + (cr * gCrMult); Vector<float> g = y + (cb * gCbMult) + (cr * gCrMult);
Vector<float> b = y + (cb * bCbMult); Vector<float> b = y + (cb * bCbMult);
r = (max - r.FastRound()) * scaledK; r = (max - r.RoundToNearestInteger()) * scaledK;
g = (max - g.FastRound()) * scaledK; g = (max - g.RoundToNearestInteger()) * scaledK;
b = (max - b.FastRound()) * scaledK; b = (max - b.RoundToNearestInteger()) * scaledK;
c0 = r; c0 = r;
c1 = g; c1 = g;

48
tests/ImageSharp.Benchmarks/Codecs/Jpeg/BlockOperations/Block8x8F_Round.cs

@ -67,21 +67,21 @@ public unsafe class Block8x8F_Round
ref Block8x8F b = ref this.block; ref Block8x8F b = ref this.block;
ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V0L); ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V0L);
row0 = row0.FastRound(); row0 = row0.RoundToNearestInteger();
ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V1L); ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V1L);
row1 = row1.FastRound(); row1 = row1.RoundToNearestInteger();
ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L); ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L);
row2 = row2.FastRound(); row2 = row2.RoundToNearestInteger();
ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L); ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L);
row3 = row3.FastRound(); row3 = row3.RoundToNearestInteger();
ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L); ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L);
row4 = row4.FastRound(); row4 = row4.RoundToNearestInteger();
ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L); ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L);
row5 = row5.FastRound(); row5 = row5.RoundToNearestInteger();
ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L); ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L);
row6 = row6.FastRound(); row6 = row6.RoundToNearestInteger();
ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L); ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L);
row7 = row7.FastRound(); row7 = row7.RoundToNearestInteger();
} }
[Benchmark] [Benchmark]
@ -90,21 +90,21 @@ public unsafe class Block8x8F_Round
ref Block8x8F b = ref Unsafe.AsRef<Block8x8F>(this.alignedPtr); ref Block8x8F b = ref Unsafe.AsRef<Block8x8F>(this.alignedPtr);
ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V0L); ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V0L);
row0 = row0.FastRound(); row0 = row0.RoundToNearestInteger();
ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V1L); ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V1L);
row1 = row1.FastRound(); row1 = row1.RoundToNearestInteger();
ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L); ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L);
row2 = row2.FastRound(); row2 = row2.RoundToNearestInteger();
ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L); ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L);
row3 = row3.FastRound(); row3 = row3.RoundToNearestInteger();
ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L); ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L);
row4 = row4.FastRound(); row4 = row4.RoundToNearestInteger();
ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L); ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L);
row5 = row5.FastRound(); row5 = row5.RoundToNearestInteger();
ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L); ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L);
row6 = row6.FastRound(); row6 = row6.RoundToNearestInteger();
ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L); ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L);
row7 = row7.FastRound(); row7 = row7.RoundToNearestInteger();
} }
[Benchmark] [Benchmark]
@ -117,20 +117,20 @@ public unsafe class Block8x8F_Round
ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L); ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L);
ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L); ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L);
row0 = row0.FastRound(); row0 = row0.RoundToNearestInteger();
row1 = row1.FastRound(); row1 = row1.RoundToNearestInteger();
row2 = row2.FastRound(); row2 = row2.RoundToNearestInteger();
row3 = row3.FastRound(); row3 = row3.RoundToNearestInteger();
row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L); row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L);
row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L); row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L);
row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L); row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L);
row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L); row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L);
row0 = row0.FastRound(); row0 = row0.RoundToNearestInteger();
row1 = row1.FastRound(); row1 = row1.RoundToNearestInteger();
row2 = row2.FastRound(); row2 = row2.RoundToNearestInteger();
row3 = row3.FastRound(); row3 = row3.RoundToNearestInteger();
} }
[Benchmark] [Benchmark]

4
tests/ImageSharp.Tests/Common/SimdUtilsTests.cs

@ -73,7 +73,7 @@ public partial class SimdUtilsTests
public void FastRound() public void FastRound()
{ {
Vector<float> v = CreateExactTestVector1(); Vector<float> v = CreateExactTestVector1();
Vector<float> r = v.FastRound(); Vector<float> r = v.RoundToNearestInteger();
this.Output.WriteLine(r.ToString()); this.Output.WriteLine(r.ToString());
@ -90,7 +90,7 @@ public partial class SimdUtilsTests
public void FastRound_RandomValues(int seed, float scale) public void FastRound_RandomValues(int seed, float scale)
{ {
Vector<float> v = CreateRandomTestVector(seed, -scale * 0.5f, scale * 0.5f); Vector<float> v = CreateRandomTestVector(seed, -scale * 0.5f, scale * 0.5f);
Vector<float> r = v.FastRound(); Vector<float> r = v.RoundToNearestInteger();
this.Output.WriteLine(v.ToString()); this.Output.WriteLine(v.ToString());
this.Output.WriteLine(r.ToString()); this.Output.WriteLine(r.ToString());

Loading…
Cancel
Save