88#include " Transform.h"
99
1010#include < cmath>
11+ #include < type_traits>
12+
13+ // MSVC declares its NEON intrinsics in <arm64_neon.h> and does not define
14+ // __aarch64__, so it uses the scalar code
15+ #if defined(__aarch64__)
16+ #include < arm_neon.h>
17+ #endif
1118
1219#include < glog/logging.h>
1320#include < react/debug/react_native_assert.h>
1421#include < react/utils/FloatComparison.h>
1522
1623namespace facebook ::react {
1724
25+ namespace {
26+
27+ template <typename T>
28+ inline bool matricesEqualImpl (
29+ const std::array<T, 16 >& a,
30+ const std::array<T, 16 >& b) noexcept {
31+ #if defined(__aarch64__)
32+ if constexpr (std::is_same_v<T, float >) {
33+ uint32x4_t c0 = vceqq_f32 (vld1q_f32 (a.data () + 0 ), vld1q_f32 (b.data () + 0 ));
34+ uint32x4_t c1 = vceqq_f32 (vld1q_f32 (a.data () + 4 ), vld1q_f32 (b.data () + 4 ));
35+ uint32x4_t c2 = vceqq_f32 (vld1q_f32 (a.data () + 8 ), vld1q_f32 (b.data () + 8 ));
36+ uint32x4_t c3 =
37+ vceqq_f32 (vld1q_f32 (a.data () + 12 ), vld1q_f32 (b.data () + 12 ));
38+ uint32x4_t all = vandq_u32 (vandq_u32 (c0, c1), vandq_u32 (c2, c3));
39+ return vminvq_u32 (all) != 0 ;
40+ } else if constexpr (std::is_same_v<T, double >) {
41+ uint64x2_t acc = vceqq_f64 (vld1q_f64 (a.data ()), vld1q_f64 (b.data ()));
42+ for (size_t i = 2 ; i < 16 ; i += 2 ) {
43+ acc = vandq_u64 (
44+ acc, vceqq_f64 (vld1q_f64 (a.data () + i), vld1q_f64 (b.data () + i)));
45+ }
46+ return vminvq_u32 (vreinterpretq_u32_u64 (acc)) != 0 ;
47+ }
48+ #endif
49+ for (size_t i = 0 ; i < 16 ; i++) {
50+ if (a[i] != b[i]) {
51+ return false ;
52+ }
53+ }
54+ return true ;
55+ }
56+
57+ template <typename T>
58+ inline void multiplyMatrix4x4 (
59+ const std::array<T, 16 >& lhs,
60+ const std::array<T, 16 >& rhs,
61+ std::array<T, 16 >& out) noexcept {
62+ #if defined(__aarch64__)
63+ if constexpr (std::is_same_v<T, float >) {
64+ const float32x4_t c0 = vld1q_f32 (lhs.data () + 0 );
65+ const float32x4_t c1 = vld1q_f32 (lhs.data () + 4 );
66+ const float32x4_t c2 = vld1q_f32 (lhs.data () + 8 );
67+ const float32x4_t c3 = vld1q_f32 (lhs.data () + 12 );
68+
69+ for (size_t col = 0 ; col < 16 ; col += 4 ) {
70+ const float32x4_t r = vld1q_f32 (rhs.data () + col);
71+ float32x4_t res = vmulq_laneq_f32 (c0, r, 0 );
72+ res = vfmaq_laneq_f32 (res, c1, r, 1 );
73+ res = vfmaq_laneq_f32 (res, c2, r, 2 );
74+ res = vfmaq_laneq_f32 (res, c3, r, 3 );
75+ vst1q_f32 (out.data () + col, res);
76+ }
77+ return ;
78+ } else if constexpr (std::is_same_v<T, double >) {
79+ const float64x2_t c0Lo = vld1q_f64 (lhs.data () + 0 );
80+ const float64x2_t c0Hi = vld1q_f64 (lhs.data () + 2 );
81+ const float64x2_t c1Lo = vld1q_f64 (lhs.data () + 4 );
82+ const float64x2_t c1Hi = vld1q_f64 (lhs.data () + 6 );
83+ const float64x2_t c2Lo = vld1q_f64 (lhs.data () + 8 );
84+ const float64x2_t c2Hi = vld1q_f64 (lhs.data () + 10 );
85+ const float64x2_t c3Lo = vld1q_f64 (lhs.data () + 12 );
86+ const float64x2_t c3Hi = vld1q_f64 (lhs.data () + 14 );
87+
88+ for (size_t col = 0 ; col < 16 ; col += 4 ) {
89+ const float64x2_t rLo = vld1q_f64 (rhs.data () + col);
90+ const float64x2_t rHi = vld1q_f64 (rhs.data () + col + 2 );
91+ float64x2_t resLo = vmulq_laneq_f64 (c0Lo, rLo, 0 );
92+ resLo = vfmaq_laneq_f64 (resLo, c1Lo, rLo, 1 );
93+ resLo = vfmaq_laneq_f64 (resLo, c2Lo, rHi, 0 );
94+ resLo = vfmaq_laneq_f64 (resLo, c3Lo, rHi, 1 );
95+ float64x2_t resHi = vmulq_laneq_f64 (c0Hi, rLo, 0 );
96+ resHi = vfmaq_laneq_f64 (resHi, c1Hi, rLo, 1 );
97+ resHi = vfmaq_laneq_f64 (resHi, c2Hi, rHi, 0 );
98+ resHi = vfmaq_laneq_f64 (resHi, c3Hi, rHi, 1 );
99+ vst1q_f64 (out.data () + col, resLo);
100+ vst1q_f64 (out.data () + col + 2 , resHi);
101+ }
102+ return ;
103+ }
104+ #endif
105+ const auto lhs00 = lhs[0 ], lhs01 = lhs[1 ], lhs02 = lhs[2 ], lhs03 = lhs[3 ];
106+ const auto lhs10 = lhs[4 ], lhs11 = lhs[5 ], lhs12 = lhs[6 ], lhs13 = lhs[7 ];
107+ const auto lhs20 = lhs[8 ], lhs21 = lhs[9 ], lhs22 = lhs[10 ], lhs23 = lhs[11 ];
108+ const auto lhs30 = lhs[12 ], lhs31 = lhs[13 ], lhs32 = lhs[14 ], lhs33 = lhs[15 ];
109+ for (size_t col = 0 ; col < 16 ; col += 4 ) {
110+ const auto r0 = rhs[col + 0 ];
111+ const auto r1 = rhs[col + 1 ];
112+ const auto r2 = rhs[col + 2 ];
113+ const auto r3 = rhs[col + 3 ];
114+ out[col + 0 ] = r0 * lhs00 + r1 * lhs10 + r2 * lhs20 + r3 * lhs30;
115+ out[col + 1 ] = r0 * lhs01 + r1 * lhs11 + r2 * lhs21 + r3 * lhs31;
116+ out[col + 2 ] = r0 * lhs02 + r1 * lhs12 + r2 * lhs22 + r3 * lhs32;
117+ out[col + 3 ] = r0 * lhs03 + r1 * lhs13 + r2 * lhs23 + r3 * lhs33;
118+ }
119+ }
120+
121+ } // namespace
122+
18123/* static */ Transform Transform::Identity () noexcept {
19124 return {};
20125}
@@ -340,14 +445,15 @@ Transform::Translate(Float x, Float y, Float z) noexcept {
340445}
341446
342447bool Transform::operator ==(const Transform& rhs) const noexcept {
343- for (auto i = 0 ; i < 16 ; i++) {
344- if (matrix[i] != rhs.matrix [i]) {
345- return false ;
346- }
347- }
348448 if (this ->operations .size () != rhs.operations .size ()) {
349449 return false ;
350450 }
451+ if (!matricesEqualImpl (matrix, rhs.matrix )) {
452+ return false ;
453+ }
454+ if (this ->operations .empty ()) {
455+ return true ;
456+ }
351457 for (size_t i = 0 ; i < this ->operations .size (); i++) {
352458 if (this ->operations [i] != rhs.operations [i]) {
353459 return false ;
@@ -357,12 +463,17 @@ bool Transform::operator==(const Transform& rhs) const noexcept {
357463}
358464
359465Transform Transform::operator *(const Transform& rhs) const {
466+ // An identity operand returns the other one unchanged, including infinite
467+ // entries that multiplying would turn into NaN (0 * infinity)
360468 if (*this == Transform::Identity ()) {
361469 return rhs;
362470 }
471+ if (rhs == Transform::Identity ()) {
472+ return *this ;
473+ }
363474
364- const auto & lhs = *this ;
365475 auto result = Transform{};
476+ result.operations .reserve (this ->operations .size () + rhs.operations .size ());
366477 for (const auto & op : this ->operations ) {
367478 if (op.type == TransformOperationType::Identity &&
368479 !result.operations .empty ()) {
@@ -378,59 +489,7 @@ Transform Transform::operator*(const Transform& rhs) const {
378489 result.operations .push_back (op);
379490 }
380491
381- auto lhs00 = lhs.matrix [0 ];
382- auto lhs01 = lhs.matrix [1 ];
383- auto lhs02 = lhs.matrix [2 ];
384- auto lhs03 = lhs.matrix [3 ];
385- auto lhs10 = lhs.matrix [4 ];
386- auto lhs11 = lhs.matrix [5 ];
387- auto lhs12 = lhs.matrix [6 ];
388- auto lhs13 = lhs.matrix [7 ];
389- auto lhs20 = lhs.matrix [8 ];
390- auto lhs21 = lhs.matrix [9 ];
391- auto lhs22 = lhs.matrix [10 ];
392- auto lhs23 = lhs.matrix [11 ];
393- auto lhs30 = lhs.matrix [12 ];
394- auto lhs31 = lhs.matrix [13 ];
395- auto lhs32 = lhs.matrix [14 ];
396- auto lhs33 = lhs.matrix [15 ];
397-
398- auto rhs0 = rhs.matrix [0 ];
399- auto rhs1 = rhs.matrix [1 ];
400- auto rhs2 = rhs.matrix [2 ];
401- auto rhs3 = rhs.matrix [3 ];
402- result.matrix [0 ] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30;
403- result.matrix [1 ] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31;
404- result.matrix [2 ] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32;
405- result.matrix [3 ] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33;
406-
407- rhs0 = rhs.matrix [4 ];
408- rhs1 = rhs.matrix [5 ];
409- rhs2 = rhs.matrix [6 ];
410- rhs3 = rhs.matrix [7 ];
411- result.matrix [4 ] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30;
412- result.matrix [5 ] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31;
413- result.matrix [6 ] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32;
414- result.matrix [7 ] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33;
415-
416- rhs0 = rhs.matrix [8 ];
417- rhs1 = rhs.matrix [9 ];
418- rhs2 = rhs.matrix [10 ];
419- rhs3 = rhs.matrix [11 ];
420- result.matrix [8 ] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30;
421- result.matrix [9 ] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31;
422- result.matrix [10 ] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32;
423- result.matrix [11 ] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33;
424-
425- rhs0 = rhs.matrix [12 ];
426- rhs1 = rhs.matrix [13 ];
427- rhs2 = rhs.matrix [14 ];
428- rhs3 = rhs.matrix [15 ];
429- result.matrix [12 ] = rhs0 * lhs00 + rhs1 * lhs10 + rhs2 * lhs20 + rhs3 * lhs30;
430- result.matrix [13 ] = rhs0 * lhs01 + rhs1 * lhs11 + rhs2 * lhs21 + rhs3 * lhs31;
431- result.matrix [14 ] = rhs0 * lhs02 + rhs1 * lhs12 + rhs2 * lhs22 + rhs3 * lhs32;
432- result.matrix [15 ] = rhs0 * lhs03 + rhs1 * lhs13 + rhs2 * lhs23 + rhs3 * lhs33;
433-
492+ multiplyMatrix4x4 (this ->matrix , rhs.matrix , result.matrix );
434493 return result;
435494}
436495
0 commit comments