Browse Source

Rename and refactor method

pull/2917/head
James Jackson-South 9 months 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>
/// <param name="v">The vector</param>
[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)
{
@ -59,13 +59,11 @@ internal static partial class SimdUtils
}
// https://github.com/g-truc/glm/blob/master/glm/simd/common.h#L11
Vector<int> magic0 = new(int.MinValue); // 0x80000000
Vector<float> sgn0 = Vector.AsVectorSingle(magic0);
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);
Vector<float> sign = v & new Vector<float>(-0F);
Vector<float> val_2p23_f32 = sign | new Vector<float>(8388608F);
return Vector.Subtract(add0, or0);
val_2p23_f32 = (v + val_2p23_f32) - val_2p23_f32;
return val_2p23_f32 | sign;
}
[Conditional("DEBUG")]

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

@ -588,6 +588,6 @@ internal partial struct Block8x8F : IEquatable<Block8x8F>
row += off;
row = Vector.Max(row, Vector<float>.Zero);
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> b = y + (cb * bCbMult);
r = r.FastRound();
g = g.FastRound();
b = b.FastRound();
r = r.RoundToNearestInteger();
g = g.RoundToNearestInteger();
b = b.RoundToNearestInteger();
r *= scale;
g *= 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> b = y + (cb * bCbMult);
r = (max - r.FastRound()) * scaledK;
g = (max - g.FastRound()) * scaledK;
b = (max - b.FastRound()) * scaledK;
r = (max - r.RoundToNearestInteger()) * scaledK;
g = (max - g.RoundToNearestInteger()) * scaledK;
b = (max - b.RoundToNearestInteger()) * scaledK;
c0 = r;
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 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);
row1 = row1.FastRound();
row1 = row1.RoundToNearestInteger();
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);
row3 = row3.FastRound();
row3 = row3.RoundToNearestInteger();
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);
row5 = row5.FastRound();
row5 = row5.RoundToNearestInteger();
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);
row7 = row7.FastRound();
row7 = row7.RoundToNearestInteger();
}
[Benchmark]
@ -90,21 +90,21 @@ public unsafe class Block8x8F_Round
ref Block8x8F b = ref Unsafe.AsRef<Block8x8F>(this.alignedPtr);
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);
row1 = row1.FastRound();
row1 = row1.RoundToNearestInteger();
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);
row3 = row3.FastRound();
row3 = row3.RoundToNearestInteger();
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);
row5 = row5.FastRound();
row5 = row5.RoundToNearestInteger();
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);
row7 = row7.FastRound();
row7 = row7.RoundToNearestInteger();
}
[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> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L);
row0 = row0.FastRound();
row1 = row1.FastRound();
row2 = row2.FastRound();
row3 = row3.FastRound();
row0 = row0.RoundToNearestInteger();
row1 = row1.RoundToNearestInteger();
row2 = row2.RoundToNearestInteger();
row3 = row3.RoundToNearestInteger();
row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L);
row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L);
row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L);
row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L);
row0 = row0.FastRound();
row1 = row1.FastRound();
row2 = row2.FastRound();
row3 = row3.FastRound();
row0 = row0.RoundToNearestInteger();
row1 = row1.RoundToNearestInteger();
row2 = row2.RoundToNearestInteger();
row3 = row3.RoundToNearestInteger();
}
[Benchmark]

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

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

Loading…
Cancel
Save