Implement faster mju_isZeroByte.

PiperOrigin-RevId: 814148714
Change-Id: Iaf5ed9d3502eaaaee8d46a986f2db00811ae0707
This commit is contained in:
Kyle Bayes
2025-10-02 02:53:02 -07:00
committed by Copybara-Service
parent 401bf431b8
commit 91257896c0
4 changed files with 42 additions and 21 deletions
+2 -20
View File
@@ -1332,26 +1332,8 @@ int mju_isZero(const mjtNum* vec, int n) {
// return 1 if all elements are 0
int mju_isZeroByte(const unsigned char* vec, int n) {
size_t i = 0;
// unroll using 8-byte chunks
const uint64_t* vec64 = (const uint64_t*)vec;
size_t n64 = n / sizeof(uint64_t);
for (; i < n64; ++i) {
if (vec64[i]) {
return 0;
}
}
// remaining bytes
i *= sizeof(uint64_t);
for (; i < n; ++i) {
if (vec[i]) {
return 0;
}
}
return 1;
if (!n || *vec) return !n;
return memcmp(vec, vec + 1, n - 1) == 0;
}