Skip to content

Commit 038f047

Browse files
Initial fixes based on feedback
1 parent 041e59d commit 038f047

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Numerics;
45
using System.Runtime.CompilerServices;
56
using System.Runtime.Intrinsics;
67
using SixLabors.ImageSharp.Common.Helpers;
@@ -19,8 +20,8 @@ internal partial struct Block8x8F
1920
[MethodImpl(InliningOptions.ShortMethod)]
2021
public void NormalizeColorsAndRoundInPlaceVector128(float maximum)
2122
{
22-
Vector128<float> off = Vector128.Create(MathF.Ceiling(maximum * 0.5F));
2323
Vector128<float> max = Vector128.Create(maximum);
24+
Vector128<float> off = Vector128.Ceiling(max * .5F);
2425

2526
this.V0L = NormalizeAndRoundVector128(this.V0L.AsVector128(), off, max).AsVector4();
2627
this.V0R = NormalizeAndRoundVector128(this.V0R.AsVector128(), off, max).AsVector4();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ internal partial struct Block8x8F
4646
[MethodImpl(InliningOptions.ShortMethod)]
4747
public void NormalizeColorsAndRoundInPlaceVector256(float maximum)
4848
{
49-
Vector256<float> off = Vector256.Create(MathF.Ceiling(maximum * 0.5F));
5049
Vector256<float> max = Vector256.Create(maximum);
50+
Vector256<float> off = Vector256.Ceiling(max * .5F);
5151

5252
this.V256_0 = NormalizeAndRoundVector256(this.V256_0, off, max);
5353
this.V256_1 = NormalizeAndRoundVector256(this.V256_1, off, max);

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,18 +488,30 @@ public void LoadFromInt16Scalar(ref Block8x8 source)
488488
/// <param name="value">Value to compare to.</param>
489489
public bool EqualsToScalar(int value)
490490
{
491-
// TODO: Can we provide a Vector128 implementation for this?
492-
if (Avx2.IsSupported)
491+
if (Vector256.IsHardwareAccelerated)
493492
{
494-
const int equalityMask = unchecked((int)0b1111_1111_1111_1111_1111_1111_1111_1111);
495-
496493
Vector256<int> targetVector = Vector256.Create(value);
497494
ref Vector256<float> blockStride = ref this.V256_0;
498495

499496
for (nuint i = 0; i < RowCount; i++)
500497
{
501-
Vector256<int> areEqual = Avx2.CompareEqual(Avx.ConvertToVector256Int32WithTruncation(Unsafe.Add(ref this.V256_0, i)), targetVector);
502-
if (Avx2.MoveMask(areEqual.AsByte()) != equalityMask)
498+
if (!Vector256.EqualsAll(Vector256.ConvertToInt32(Unsafe.Add(ref this.V256_0, i)), targetVector))
499+
{
500+
return false;
501+
}
502+
}
503+
504+
return true;
505+
}
506+
507+
if (Vector128.IsHardwareAccelerated)
508+
{
509+
Vector128<int> targetVector = Vector128.Create(value);
510+
ref Vector4 blockStride = ref this.V0L;
511+
512+
for (nuint i = 0; i < RowCount * 2; i++)
513+
{
514+
if (!Vector128.EqualsAll(Vector128.ConvertToInt32(Unsafe.Add(ref this.V0L, i).AsVector128()), targetVector))
503515
{
504516
return false;
505517
}

tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ static void RunTest()
462462
// 3. DisableAvx2 - call fallback code of float implementation
463463
FeatureTestRunner.RunWithHwIntrinsicsFeature(
464464
RunTest,
465-
HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX2);
465+
HwIntrinsics.AllowAll | HwIntrinsics.DisableAVX | HwIntrinsics.DisableSSE);
466466
}
467467

468468
[Theory]

0 commit comments

Comments
 (0)