From e9fcab3c4ab56da039670dc768e555abfd2ef2c0 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 4 Dec 2026 01:47:58 -0800 Subject: [PATCH] Vectorize Fabric transform multiplication and equality on arm64 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use NEON to multiply and compare Fabric's 4×4 transform matrices on arm64. Keep the scalar implementation on other architectures and with MSVC, which declares its NEON intrinsics in ``. Preserve how matrix equality treats signed zero and NaN. During composition, reserve space for both operation lists before copying them in concatenation order. An identity on either side returns the other transform unchanged. The C++ `TransformTest` suite passes. The new cases check signed zero and NaN in matrix equality, that an identity on either side keeps infinite entries, and the order of composing transforms across three axes. In an optimized C++ benchmark on Apple M6, the median falls from 59.03 to 28.53 retired instructions per equality comparison and from 1,255.42 to 450.21 per composition. `proc_pid_rusage` counts instructions across five interleaved runs. The output checksums match. Signed-off-by: James Ide --- .../react/renderer/graphics/Transform.cpp | 177 ++++++++++++------ .../renderer/graphics/tests/TransformTest.cpp | 29 +++ 2 files changed, 147 insertions(+), 59 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp index 87f17b68c125..0d87fa4758c3 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp @@ -8,6 +8,13 @@ #include "Transform.h" #include +#include + +// MSVC declares its NEON intrinsics in and does not define +// __aarch64__, so it uses the scalar code +#if defined(__aarch64__) +#include +#endif #include #include @@ -15,6 +22,104 @@ namespace facebook::react { +namespace { + +template +inline bool matricesEqualImpl( + const std::array& a, + const std::array& b) noexcept { +#if defined(__aarch64__) + if constexpr (std::is_same_v) { + uint32x4_t c0 = vceqq_f32(vld1q_f32(a.data() + 0), vld1q_f32(b.data() + 0)); + uint32x4_t c1 = vceqq_f32(vld1q_f32(a.data() + 4), vld1q_f32(b.data() + 4)); + uint32x4_t c2 = vceqq_f32(vld1q_f32(a.data() + 8), vld1q_f32(b.data() + 8)); + uint32x4_t c3 = + vceqq_f32(vld1q_f32(a.data() + 12), vld1q_f32(b.data() + 12)); + uint32x4_t all = vandq_u32(vandq_u32(c0, c1), vandq_u32(c2, c3)); + return vminvq_u32(all) != 0; + } else if constexpr (std::is_same_v) { + uint64x2_t acc = vceqq_f64(vld1q_f64(a.data()), vld1q_f64(b.data())); + for (size_t i = 2; i < 16; i += 2) { + acc = vandq_u64( + acc, vceqq_f64(vld1q_f64(a.data() + i), vld1q_f64(b.data() + i))); + } + return vminvq_u32(vreinterpretq_u32_u64(acc)) != 0; + } +#endif + for (size_t i = 0; i < 16; i++) { + if (a[i] != b[i]) { + return false; + } + } + return true; +} + +template +inline void multiplyMatrix4x4( + const std::array& lhs, + const std::array& rhs, + std::array& out) noexcept { +#if defined(__aarch64__) + if constexpr (std::is_same_v) { + const float32x4_t c0 = vld1q_f32(lhs.data() + 0); + const float32x4_t c1 = vld1q_f32(lhs.data() + 4); + const float32x4_t c2 = vld1q_f32(lhs.data() + 8); + const float32x4_t c3 = vld1q_f32(lhs.data() + 12); + + for (size_t col = 0; col < 16; col += 4) { + const float32x4_t r = vld1q_f32(rhs.data() + col); + float32x4_t res = vmulq_laneq_f32(c0, r, 0); + res = vfmaq_laneq_f32(res, c1, r, 1); + res = vfmaq_laneq_f32(res, c2, r, 2); + res = vfmaq_laneq_f32(res, c3, r, 3); + vst1q_f32(out.data() + col, res); + } + return; + } else if constexpr (std::is_same_v) { + const float64x2_t c0Lo = vld1q_f64(lhs.data() + 0); + const float64x2_t c0Hi = vld1q_f64(lhs.data() + 2); + const float64x2_t c1Lo = vld1q_f64(lhs.data() + 4); + const float64x2_t c1Hi = vld1q_f64(lhs.data() + 6); + const float64x2_t c2Lo = vld1q_f64(lhs.data() + 8); + const float64x2_t c2Hi = vld1q_f64(lhs.data() + 10); + const float64x2_t c3Lo = vld1q_f64(lhs.data() + 12); + const float64x2_t c3Hi = vld1q_f64(lhs.data() + 14); + + for (size_t col = 0; col < 16; col += 4) { + const float64x2_t rLo = vld1q_f64(rhs.data() + col); + const float64x2_t rHi = vld1q_f64(rhs.data() + col + 2); + float64x2_t resLo = vmulq_laneq_f64(c0Lo, rLo, 0); + resLo = vfmaq_laneq_f64(resLo, c1Lo, rLo, 1); + resLo = vfmaq_laneq_f64(resLo, c2Lo, rHi, 0); + resLo = vfmaq_laneq_f64(resLo, c3Lo, rHi, 1); + float64x2_t resHi = vmulq_laneq_f64(c0Hi, rLo, 0); + resHi = vfmaq_laneq_f64(resHi, c1Hi, rLo, 1); + resHi = vfmaq_laneq_f64(resHi, c2Hi, rHi, 0); + resHi = vfmaq_laneq_f64(resHi, c3Hi, rHi, 1); + vst1q_f64(out.data() + col, resLo); + vst1q_f64(out.data() + col + 2, resHi); + } + return; + } +#endif + const auto lhs00 = lhs[0], lhs01 = lhs[1], lhs02 = lhs[2], lhs03 = lhs[3]; + const auto lhs10 = lhs[4], lhs11 = lhs[5], lhs12 = lhs[6], lhs13 = lhs[7]; + const auto lhs20 = lhs[8], lhs21 = lhs[9], lhs22 = lhs[10], lhs23 = lhs[11]; + const auto lhs30 = lhs[12], lhs31 = lhs[13], lhs32 = lhs[14], lhs33 = lhs[15]; + for (size_t col = 0; col < 16; col += 4) { + const auto r0 = rhs[col + 0]; + const auto r1 = rhs[col + 1]; + const auto r2 = rhs[col + 2]; + const auto r3 = rhs[col + 3]; + out[col + 0] = r0 * lhs00 + r1 * lhs10 + r2 * lhs20 + r3 * lhs30; + out[col + 1] = r0 * lhs01 + r1 * lhs11 + r2 * lhs21 + r3 * lhs31; + out[col + 2] = r0 * lhs02 + r1 * lhs12 + r2 * lhs22 + r3 * lhs32; + out[col + 3] = r0 * lhs03 + r1 * lhs13 + r2 * lhs23 + r3 * lhs33; + } +} + +} // namespace + /* static */ Transform Transform::Identity() noexcept { return {}; } @@ -340,14 +445,15 @@ Transform::Translate(Float x, Float y, Float z) noexcept { } bool Transform::operator==(const Transform& rhs) const noexcept { - for (auto i = 0; i < 16; i++) { - if (matrix[i] != rhs.matrix[i]) { - return false; - } - } if (this->operations.size() != rhs.operations.size()) { return false; } + if (!matricesEqualImpl(matrix, rhs.matrix)) { + return false; + } + if (this->operations.empty()) { + return true; + } for (size_t i = 0; i < this->operations.size(); i++) { if (this->operations[i] != rhs.operations[i]) { return false; @@ -357,12 +463,17 @@ bool Transform::operator==(const Transform& rhs) const noexcept { } Transform Transform::operator*(const Transform& rhs) const { + // An identity operand returns the other one unchanged, including infinite + // entries that multiplying would turn into NaN (0 * infinity) if (*this == Transform::Identity()) { return rhs; } + if (rhs == Transform::Identity()) { + return *this; + } - const auto& lhs = *this; auto result = Transform{}; + result.operations.reserve(this->operations.size() + rhs.operations.size()); for (const auto& op : this->operations) { if (op.type == TransformOperationType::Identity && !result.operations.empty()) { @@ -378,59 +489,7 @@ Transform Transform::operator*(const Transform& rhs) const { result.operations.push_back(op); } - auto lhs00 = lhs.matrix[0]; - auto lhs01 = lhs.matrix[1]; - auto lhs02 = lhs.matrix[2]; - auto lhs03 = lhs.matrix[3]; - auto lhs10 = lhs.matrix[4]; - auto lhs11 = lhs.matrix[5]; - auto lhs12 = lhs.matrix[6]; - auto lhs13 = lhs.matrix[7]; - auto lhs20 = lhs.matrix[8]; - auto lhs21 = lhs.matrix[9]; - auto lhs22 = lhs.matrix[10]; - auto lhs23 = lhs.matrix[11]; - auto lhs30 = lhs.matrix[12]; - auto lhs31 = lhs.matrix[13]; - auto lhs32 = lhs.matrix[14]; - auto lhs33 = lhs.matrix[15]; - - auto rhs0 = rhs.matrix[0]; - auto rhs1 = rhs.matrix[1]; - auto rhs2 = rhs.matrix[2]; - auto rhs3 = rhs.matrix[3]; - result.matrix[0] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30; - result.matrix[1] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31; - result.matrix[2] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32; - result.matrix[3] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33; - - rhs0 = rhs.matrix[4]; - rhs1 = rhs.matrix[5]; - rhs2 = rhs.matrix[6]; - rhs3 = rhs.matrix[7]; - result.matrix[4] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30; - result.matrix[5] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31; - result.matrix[6] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32; - result.matrix[7] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33; - - rhs0 = rhs.matrix[8]; - rhs1 = rhs.matrix[9]; - rhs2 = rhs.matrix[10]; - rhs3 = rhs.matrix[11]; - result.matrix[8] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30; - result.matrix[9] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31; - result.matrix[10] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32; - result.matrix[11] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33; - - rhs0 = rhs.matrix[12]; - rhs1 = rhs.matrix[13]; - rhs2 = rhs.matrix[14]; - rhs3 = rhs.matrix[15]; - result.matrix[12] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30; - result.matrix[13] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31; - result.matrix[14] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32; - result.matrix[15] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33; - + multiplyMatrix4x4(this->matrix, rhs.matrix, result.matrix); return result; } diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/tests/TransformTest.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/tests/TransformTest.cpp index 9a9719e62793..60e80965b865 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/tests/TransformTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/tests/TransformTest.cpp @@ -9,9 +9,38 @@ #include #include +#include using namespace facebook::react; +TEST(TransformTest, matrixEqualityUsesFloatingPointComparison) { + auto first = Transform::Identity(); + auto second = Transform::Identity(); + second.matrix[4] = -0.0; + EXPECT_EQ(first, second); + + first.matrix[4] = second.matrix[4] = std::numeric_limits::quiet_NaN(); + EXPECT_NE(first, second); +} + +TEST(TransformTest, multiplyingByIdentityKeepsInfiniteEntries) { + auto transform = + Transform::Translate(std::numeric_limits::infinity(), 0, 0); + EXPECT_EQ((transform * Transform::Identity()).matrix, transform.matrix); + EXPECT_EQ((Transform::Identity() * transform).matrix, transform.matrix); +} + +TEST(TransformTest, multipliesColumnsInTransformOrder) { + auto translate = Transform::Translate(10, 20, 30); + auto scale = Transform::Scale(2, 3, 4); + Vector vector{1, 2, 3, 1}; + auto result = (translate * scale) * vector; + EXPECT_EQ(result.x, 12); + EXPECT_EQ(result.y, 26); + EXPECT_EQ(result.z, 42); + EXPECT_EQ(result.w, 1); +} + TEST(TransformTest, transformingSize) { auto size = facebook::react::Size{100, 200}; auto scaledSize = size * Transform::Scale(0.5, 0.5, 1);