Debian Codegraph @

thirdparty/jolt_physics/Jolt/Math/BVec16.inl

raw
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 JPH_NAMESPACE_BEGIN
6
7 BVec16::BVec16(uint8 inB0, uint8 inB1, uint8 inB2, uint8 inB3, uint8 inB4, uint8 inB5, uint8 inB6, uint8 inB7, uint8 inB8, uint8 inB9, uint8 inB10, uint8 inB11, uint8 inB12, uint8 inB13, uint8 inB14, uint8 inB15)
8 {
9 #if defined(JPH_USE_SSE)
10 mValue = _mm_set_epi8(char(inB15), char(inB14), char(inB13), char(inB12), char(inB11), char(inB10), char(inB9), char(inB8), char(inB7), char(inB6), char(inB5), char(inB4), char(inB3), char(inB2), char(inB1), char(inB0));
11 #elif defined(JPH_USE_NEON)
12 uint8x8_t v1 = vcreate_u8(uint64(inB0) | (uint64(inB1) << 8) | (uint64(inB2) << 16) | (uint64(inB3) << 24) | (uint64(inB4) << 32) | (uint64(inB5) << 40) | (uint64(inB6) << 48) | (uint64(inB7) << 56));
13 uint8x8_t v2 = vcreate_u8(uint64(inB8) | (uint64(inB9) << 8) | (uint64(inB10) << 16) | (uint64(inB11) << 24) | (uint64(inB12) << 32) | (uint64(inB13) << 40) | (uint64(inB14) << 48) | (uint64(inB15) << 56));
14 mValue = vcombine_u8(v1, v2);
15 #else
16 mU8[0] = inB0;
17 mU8[1] = inB1;
18 mU8[2] = inB2;
19 mU8[3] = inB3;
20 mU8[4] = inB4;
21 mU8[5] = inB5;
22 mU8[6] = inB6;
23 mU8[7] = inB7;
24 mU8[8] = inB8;
25 mU8[9] = inB9;
26 mU8[10] = inB10;
27 mU8[11] = inB11;
28 mU8[12] = inB12;
29 mU8[13] = inB13;
30 mU8[14] = inB14;
31 mU8[15] = inB15;
32 #endif
33 }
34
35 BVec16::BVec16(uint64 inV0, uint64 inV1)
36 {
37 mU64[0] = inV0;
38 mU64[1] = inV1;
39 }
40
41 bool BVec16::operator == (BVec16Arg inV2) const
42 {
43 return sEquals(*this, inV2).TestAllTrue();
44 }
45
46 BVec16 BVec16::sZero()
47 {
48 #if defined(JPH_USE_SSE)
49 return _mm_setzero_si128();
50 #elif defined(JPH_USE_NEON)
51 return vdupq_n_u8(0);
52 #else
53 return BVec16(0, 0);
54 #endif
55 }
56
57 BVec16 BVec16::sReplicate(uint8 inV)
58 {
59 #if defined(JPH_USE_SSE)
60 return _mm_set1_epi8(char(inV));
61 #elif defined(JPH_USE_NEON)
62 return vdupq_n_u8(inV);
63 #else
64 uint64 v(inV);
65 v |= v << 8;
66 v |= v << 16;
67 v |= v << 32;
68 return BVec16(v, v);
69 #endif
70 }
71
72 BVec16 BVec16::sLoadByte16(const uint8 *inV)
73 {
74 #if defined(JPH_USE_SSE)
75 return _mm_loadu_si128(reinterpret_cast<const __m128i *>(inV));
76 #elif defined(JPH_USE_NEON)
77 return vld1q_u8(inV);
78 #else
79 return BVec16(inV[0], inV[1], inV[2], inV[3], inV[4], inV[5], inV[6], inV[7], inV[8], inV[9], inV[10], inV[11], inV[12], inV[13], inV[14], inV[15]);
80 #endif
81 }
82
83 BVec16 BVec16::sEquals(BVec16Arg inV1, BVec16Arg inV2)
84 {
85 #if defined(JPH_USE_SSE)
86 return _mm_cmpeq_epi8(inV1.mValue, inV2.mValue);
87 #elif defined(JPH_USE_NEON)
88 return vceqq_u8(inV1.mValue, inV2.mValue);
89 #else
90 auto equals = [](uint64 inV1, uint64 inV2) {
91 uint64 r = inV1 ^ ~inV2; // Bits that are equal are 1
92 r &= r << 1; // Combine bit 0 through 1
93 r &= r << 2; // Combine bit 0 through 3
94 r &= r << 4; // Combine bit 0 through 7
95 r &= 0x8080808080808080UL; // Keep only the highest bit of each byte
96 return r;
97 };
98 return BVec16(equals(inV1.mU64[0], inV2.mU64[0]), equals(inV1.mU64[1], inV2.mU64[1]));
99 #endif
100 }
101
102 BVec16 BVec16::sOr(BVec16Arg inV1, BVec16Arg inV2)
103 {
104 #if defined(JPH_USE_SSE)
105 return _mm_or_si128(inV1.mValue, inV2.mValue);
106 #elif defined(JPH_USE_NEON)
107 return vorrq_u8(inV1.mValue, inV2.mValue);
108 #else
109 return BVec16(inV1.mU64[0] | inV2.mU64[0], inV1.mU64[1] | inV2.mU64[1]);
110 #endif
111 }
112
113 BVec16 BVec16::sXor(BVec16Arg inV1, BVec16Arg inV2)
114 {
115 #if defined(JPH_USE_SSE)
116 return _mm_xor_si128(inV1.mValue, inV2.mValue);
117 #elif defined(JPH_USE_NEON)
118 return veorq_u8(inV1.mValue, inV2.mValue);
119 #else
120 return BVec16(inV1.mU64[0] ^ inV2.mU64[0], inV1.mU64[1] ^ inV2.mU64[1]);
121 #endif
122 }
123
124 BVec16 BVec16::sAnd(BVec16Arg inV1, BVec16Arg inV2)
125 {
126 #if defined(JPH_USE_SSE)
127 return _mm_and_si128(inV1.mValue, inV2.mValue);
128 #elif defined(JPH_USE_NEON)
129 return vandq_u8(inV1.mValue, inV2.mValue);
130 #else
131 return BVec16(inV1.mU64[0] & inV2.mU64[0], inV1.mU64[1] & inV2.mU64[1]);
132 #endif
133 }
134
135
136 BVec16 BVec16::sNot(BVec16Arg inV1)
137 {
138 #if defined(JPH_USE_SSE)
139 return sXor(inV1, sReplicate(0xff));
140 #elif defined(JPH_USE_NEON)
141 return vmvnq_u8(inV1.mValue);
142 #else
143 return BVec16(~inV1.mU64[0], ~inV1.mU64[1]);
144 #endif
145 }
146
147 int BVec16::GetTrues() const
148 {
149 #if defined(JPH_USE_SSE)
150 return _mm_movemask_epi8(mValue);
151 #else
152 int result = 0;
153 for (int i = 0; i < 16; ++i)
154 result |= int(mU8[i] >> 7) << i;
155 return result;
156 #endif
157 }
158
159 bool BVec16::TestAnyTrue() const
160 {
161 #if defined(JPH_USE_SSE)
162 return _mm_movemask_epi8(mValue) != 0;
163 #else
164 return ((mU64[0] | mU64[1]) & 0x8080808080808080UL) != 0;
165 #endif
166 }
167
168 bool BVec16::TestAllTrue() const
169 {
170 #if defined(JPH_USE_SSE)
171 return _mm_movemask_epi8(mValue) == 0b1111111111111111;
172 #else
173 return ((mU64[0] & mU64[1]) & 0x8080808080808080UL) == 0x8080808080808080UL;
174 #endif
175 }
176
177 JPH_NAMESPACE_END