Skip to content

Commit c38fc81

Browse files
committed
Minor codegen tweaks
1 parent 442e467 commit c38fc81

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/ImageSharp/Common/Helpers/Numerics.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -584,24 +584,22 @@ public static unsafe void CubePowOnXYZ(Span<Vector4> vectors)
584584
[MethodImpl(MethodImplOptions.AggressiveInlining)]
585585
public static unsafe void CubeRootOnXYZ(Span<Vector4> vectors)
586586
{
587-
ref Vector4 vectorsRef = ref MemoryMarshal.GetReference(vectors);
588-
int length = vectors.Length;
589-
590587
#if SUPPORTS_RUNTIME_INTRINSICS
591588
if (Sse41.IsSupported)
592589
{
590+
ref Vector4 vectors4Ref = ref MemoryMarshal.GetReference(vectors);
591+
ref Vector4 vectors4End = ref Unsafe.Add(ref vectors4Ref, vectors.Length);
592+
593593
var v128_0x7FFFFFFF = Vector128.Create(0x7FFFFFFF);
594594
var v128_0x3F8000000 = Vector128.Create(0x3F800000);
595595
var v128_341 = Vector128.Create(341);
596596
var v128_0x80000000 = Vector128.Create(unchecked((int)0x80000000));
597597
var v4_23rds = new Vector4(2 / 3f);
598598
var v4_13rds = new Vector4(1 / 3f);
599599

600-
for (int x = 0; x < length; x++)
600+
while (Unsafe.IsAddressLessThan(ref vectors4Ref, ref vectors4End))
601601
{
602-
ref Vector4 v4 = ref Unsafe.Add(ref vectorsRef, x);
603-
604-
Vector4 vx = v4;
602+
Vector4 vx = vectors4Ref;
605603
float a = vx.W;
606604
Vector128<int> veax = Unsafe.As<Vector4, Vector128<int>>(ref vx);
607605
Vector128<int> vecx = veax;
@@ -626,12 +624,15 @@ public static unsafe void CubeRootOnXYZ(Span<Vector4> vectors)
626624
y4 = (v4_23rds * y4) + (v4_13rds * (vx / (y4 * y4)));
627625
y4.W = a;
628626

629-
v4 = y4;
627+
vectors4Ref = y4;
628+
vectors4Ref = ref Unsafe.Add(ref vectors4Ref, 1);
630629
}
631630

632631
return;
633632
}
634633
#endif
634+
ref Vector4 vectorsRef = ref MemoryMarshal.GetReference(vectors);
635+
int length = vectors.Length;
635636

636637
// Fallback with scalar preprocessing and vectorized approximation steps
637638
for (int x = 0; x < length; x++)

0 commit comments

Comments
 (0)