Skip to content

Commit f8218f9

Browse files
Rewrite how Matrix3x2 and Matrix4x4 are implemented (#80091)
* Rewrite how Matrix3x2 and Matrix4x4 are implemented * Fix a bug in lowerxarch related to merging Sse41.Insert chains
1 parent 7a6f33b commit f8218f9

File tree

9 files changed

+2391
-2360
lines changed

9 files changed

+2391
-2360
lines changed

src/coreclr/jit/lowerxarch.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,6 +2809,9 @@ GenTree* Lowering::LowerHWIntrinsicCreate(GenTreeHWIntrinsic* node)
28092809

28102810
if (comp->compOpportunisticallyDependsOn(InstructionSet_SSE41))
28112811
{
2812+
assert(argCnt <= 4);
2813+
GenTree* insertedNodes[4];
2814+
28122815
for (N = 1; N < argCnt - 1; N++)
28132816
{
28142817
// We will be constructing the following parts:
@@ -2837,10 +2840,12 @@ GenTree* Lowering::LowerHWIntrinsicCreate(GenTreeHWIntrinsic* node)
28372840
idx = comp->gtNewIconNode(N << 4, TYP_INT);
28382841
BlockRange().InsertAfter(tmp2, idx);
28392842

2840-
tmp1 = comp->gtNewSimdHWIntrinsicNode(simdType, tmp1, tmp2, idx, NI_SSE41_Insert, simdBaseJitType,
2843+
tmp3 = comp->gtNewSimdHWIntrinsicNode(simdType, tmp1, tmp2, idx, NI_SSE41_Insert, simdBaseJitType,
28412844
simdSize);
2842-
BlockRange().InsertAfter(idx, tmp1);
2843-
LowerNode(tmp1);
2845+
BlockRange().InsertAfter(idx, tmp3);
2846+
2847+
insertedNodes[N] = tmp3;
2848+
tmp1 = tmp3;
28442849
}
28452850

28462851
// We will be constructing the following parts:
@@ -2868,6 +2873,18 @@ GenTree* Lowering::LowerHWIntrinsicCreate(GenTreeHWIntrinsic* node)
28682873
BlockRange().InsertAfter(tmp2, idx);
28692874

28702875
node->ResetHWIntrinsicId(NI_SSE41_Insert, comp, tmp1, tmp2, idx);
2876+
2877+
for (N = 1; N < argCnt - 1; N++)
2878+
{
2879+
// LowerNode for NI_SSE41_Insert specially handles zeros, constants, and certain mask values
2880+
// to do the minimal number of operations and may merge together two neighboring inserts that
2881+
// don't have any side effects between them. Because of this and because of the interdependence
2882+
// of the inserts we've created above, we need to wait to lower the generated inserts until after
2883+
// we've completed the chain.
2884+
2885+
GenTree* insertedNode = insertedNodes[N];
2886+
LowerNode(insertedNode);
2887+
}
28712888
break;
28722889
}
28732890

src/libraries/System.Numerics.Vectors/tests/Matrix3x2Tests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,15 @@ public void Matrix3x2EqualsTest()
568568
public void Matrix3x2GetHashCodeTest()
569569
{
570570
Matrix3x2 target = GenerateIncrementalMatrixNumber();
571-
int expected = HashCode.Combine(target.M11, target.M12,
572-
target.M21, target.M22,
573-
target.M31, target.M32);
574-
int actual;
575571

576-
actual = target.GetHashCode();
572+
int expected = HashCode.Combine(
573+
new Vector2(target.M11, target.M12),
574+
new Vector2(target.M21, target.M22),
575+
new Vector2(target.M31, target.M32)
576+
);
577+
578+
int actual = target.GetHashCode();
579+
577580
Assert.Equal(expected, actual);
578581
}
579582

src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,29 +1690,13 @@ public void Matrix4x4GetHashCodeTest()
16901690
{
16911691
Matrix4x4 target = GenerateIncrementalMatrixNumber();
16921692

1693-
HashCode hash = default;
1693+
int expected = HashCode.Combine(
1694+
new Vector4(target.M11, target.M12, target.M13, target.M14),
1695+
new Vector4(target.M21, target.M22, target.M23, target.M24),
1696+
new Vector4(target.M31, target.M32, target.M33, target.M34),
1697+
new Vector4(target.M41, target.M42, target.M43, target.M44)
1698+
);
16941699

1695-
hash.Add(target.M11);
1696-
hash.Add(target.M12);
1697-
hash.Add(target.M13);
1698-
hash.Add(target.M14);
1699-
1700-
hash.Add(target.M21);
1701-
hash.Add(target.M22);
1702-
hash.Add(target.M23);
1703-
hash.Add(target.M24);
1704-
1705-
hash.Add(target.M31);
1706-
hash.Add(target.M32);
1707-
hash.Add(target.M33);
1708-
hash.Add(target.M34);
1709-
1710-
hash.Add(target.M41);
1711-
hash.Add(target.M42);
1712-
hash.Add(target.M43);
1713-
hash.Add(target.M44);
1714-
1715-
int expected = hash.ToHashCode();
17161700
int actual = target.GetHashCode();
17171701

17181702
Assert.Equal(expected, actual);

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@
556556
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Parsing.cs" />
557557
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\BitOperations.cs" />
558558
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Matrix3x2.cs" />
559+
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Matrix3x2.Impl.cs" />
559560
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Matrix4x4.cs" />
561+
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Matrix4x4.Impl.cs" />
560562
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Plane.cs" />
561563
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\Quaternion.cs" />
562564
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\TotalOrderIeee754Comparer.cs" />
@@ -2535,4 +2537,4 @@
25352537
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
25362538
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
25372539
</ItemGroup>
2538-
</Project>
2540+
</Project>

0 commit comments

Comments
 (0)