Skip to content

Commit cd3aa18

Browse files
Rename and refactor method
1 parent aebe375 commit cd3aa18

File tree

6 files changed

+38
-40
lines changed

6 files changed

+38
-40
lines changed

src/ImageSharp/Common/Helpers/SimdUtils.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static Vector4 PseudoRound(this Vector4 v)
3838
/// </summary>
3939
/// <param name="v">The vector</param>
4040
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41-
internal static Vector<float> FastRound(this Vector<float> v)
41+
internal static Vector<float> RoundToNearestInteger(this Vector<float> v)
4242
{
4343
if (Avx512F.IsSupported && Vector<float>.Count == Vector512<float>.Count)
4444
{
@@ -59,13 +59,11 @@ internal static Vector<float> FastRound(this Vector<float> v)
5959
}
6060

6161
// https://github.com/g-truc/glm/blob/master/glm/simd/common.h#L11
62-
Vector<int> magic0 = new(int.MinValue); // 0x80000000
63-
Vector<float> sgn0 = Vector.AsVectorSingle(magic0);
64-
Vector<float> and0 = Vector.BitwiseAnd(sgn0, v);
65-
Vector<float> or0 = Vector.BitwiseOr(and0, new Vector<float>(8388608.0f));
66-
Vector<float> add0 = Vector.Add(v, or0);
62+
Vector<float> sign = v & new Vector<float>(-0F);
63+
Vector<float> val_2p23_f32 = sign | new Vector<float>(8388608F);
6764

68-
return Vector.Subtract(add0, or0);
65+
val_2p23_f32 = (v + val_2p23_f32) - val_2p23_f32;
66+
return val_2p23_f32 | sign;
6967
}
7068

7169
[Conditional("DEBUG")]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,6 @@ private static Vector<float> NormalizeAndRound(Vector<float> row, Vector<float>
588588
row += off;
589589
row = Vector.Max(row, Vector<float>.Zero);
590590
row = Vector.Min(row, max);
591-
return row.FastRound();
591+
return row.RoundToNearestInteger();
592592
}
593593
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ protected override void ConvertToRgbInplaceVectorized(in ComponentValues values)
5555
Vector<float> g = y + (cb * gCbMult) + (cr * gCrMult);
5656
Vector<float> b = y + (cb * bCbMult);
5757

58-
r = r.FastRound();
59-
g = g.FastRound();
60-
b = b.FastRound();
58+
r = r.RoundToNearestInteger();
59+
g = g.RoundToNearestInteger();
60+
b = b.RoundToNearestInteger();
6161
r *= scale;
6262
g *= scale;
6363
b *= scale;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ protected override void ConvertToRgbInplaceVectorized(in ComponentValues values)
5959
Vector<float> g = y + (cb * gCbMult) + (cr * gCrMult);
6060
Vector<float> b = y + (cb * bCbMult);
6161

62-
r = (max - r.FastRound()) * scaledK;
63-
g = (max - g.FastRound()) * scaledK;
64-
b = (max - b.FastRound()) * scaledK;
62+
r = (max - r.RoundToNearestInteger()) * scaledK;
63+
g = (max - g.RoundToNearestInteger()) * scaledK;
64+
b = (max - b.RoundToNearestInteger()) * scaledK;
6565

6666
c0 = r;
6767
c1 = g;

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ public void SimdUtils_FastRound_Vector8()
6767
ref Block8x8F b = ref this.block;
6868

6969
ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V0L);
70-
row0 = row0.FastRound();
70+
row0 = row0.RoundToNearestInteger();
7171
ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V1L);
72-
row1 = row1.FastRound();
72+
row1 = row1.RoundToNearestInteger();
7373
ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L);
74-
row2 = row2.FastRound();
74+
row2 = row2.RoundToNearestInteger();
7575
ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L);
76-
row3 = row3.FastRound();
76+
row3 = row3.RoundToNearestInteger();
7777
ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L);
78-
row4 = row4.FastRound();
78+
row4 = row4.RoundToNearestInteger();
7979
ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L);
80-
row5 = row5.FastRound();
80+
row5 = row5.RoundToNearestInteger();
8181
ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L);
82-
row6 = row6.FastRound();
82+
row6 = row6.RoundToNearestInteger();
8383
ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L);
84-
row7 = row7.FastRound();
84+
row7 = row7.RoundToNearestInteger();
8585
}
8686

8787
[Benchmark]
@@ -90,21 +90,21 @@ public void SimdUtils_FastRound_Vector8_ForceAligned()
9090
ref Block8x8F b = ref Unsafe.AsRef<Block8x8F>(this.alignedPtr);
9191

9292
ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V0L);
93-
row0 = row0.FastRound();
93+
row0 = row0.RoundToNearestInteger();
9494
ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V1L);
95-
row1 = row1.FastRound();
95+
row1 = row1.RoundToNearestInteger();
9696
ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L);
97-
row2 = row2.FastRound();
97+
row2 = row2.RoundToNearestInteger();
9898
ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L);
99-
row3 = row3.FastRound();
99+
row3 = row3.RoundToNearestInteger();
100100
ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L);
101-
row4 = row4.FastRound();
101+
row4 = row4.RoundToNearestInteger();
102102
ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L);
103-
row5 = row5.FastRound();
103+
row5 = row5.RoundToNearestInteger();
104104
ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L);
105-
row6 = row6.FastRound();
105+
row6 = row6.RoundToNearestInteger();
106106
ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L);
107-
row7 = row7.FastRound();
107+
row7 = row7.RoundToNearestInteger();
108108
}
109109

110110
[Benchmark]
@@ -117,20 +117,20 @@ public void SimdUtils_FastRound_Vector8_Grouped()
117117
ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V2L);
118118
ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V3L);
119119

120-
row0 = row0.FastRound();
121-
row1 = row1.FastRound();
122-
row2 = row2.FastRound();
123-
row3 = row3.FastRound();
120+
row0 = row0.RoundToNearestInteger();
121+
row1 = row1.RoundToNearestInteger();
122+
row2 = row2.RoundToNearestInteger();
123+
row3 = row3.RoundToNearestInteger();
124124

125125
row0 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V4L);
126126
row1 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V5L);
127127
row2 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V6L);
128128
row3 = ref Unsafe.As<Vector4, Vector<float>>(ref b.V7L);
129129

130-
row0 = row0.FastRound();
131-
row1 = row1.FastRound();
132-
row2 = row2.FastRound();
133-
row3 = row3.FastRound();
130+
row0 = row0.RoundToNearestInteger();
131+
row1 = row1.RoundToNearestInteger();
132+
row2 = row2.RoundToNearestInteger();
133+
row3 = row3.RoundToNearestInteger();
134134
}
135135

136136
[Benchmark]

tests/ImageSharp.Tests/Common/SimdUtilsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static Vector<float> CreateRandomTestVector(int seed, float min, float m
7373
public void FastRound()
7474
{
7575
Vector<float> v = CreateExactTestVector1();
76-
Vector<float> r = v.FastRound();
76+
Vector<float> r = v.RoundToNearestInteger();
7777

7878
this.Output.WriteLine(r.ToString());
7979

@@ -90,7 +90,7 @@ public void FastRound()
9090
public void FastRound_RandomValues(int seed, float scale)
9191
{
9292
Vector<float> v = CreateRandomTestVector(seed, -scale * 0.5f, scale * 0.5f);
93-
Vector<float> r = v.FastRound();
93+
Vector<float> r = v.RoundToNearestInteger();
9494

9595
this.Output.WriteLine(v.ToString());
9696
this.Output.WriteLine(r.ToString());

0 commit comments

Comments
 (0)