Add private function mju_isZeroByte to check for byte-wise zero.

PiperOrigin-RevId: 810456282
Change-Id: I877d0f9225e7a783ed7ff12ce54f4ed0a7889793
This commit is contained in:
Yuval Tassa
2025-09-23 08:48:43 -07:00
committed by Copybara-Service
parent bb0878417e
commit d71d42a374
8 changed files with 128 additions and 6 deletions
+6
View File
@@ -55,6 +55,12 @@ mujoco_test(
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_test(
iszero_benchmark_test
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
mujoco_test(
solveLD_benchmark_test
MAIN_TARGET benchmark::benchmark_main
+76
View File
@@ -0,0 +1,76 @@
// Copyright 2025 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include "benchmark/benchmark.h"
#include <absl/base/attributes.h>
#include <mujoco/mujoco.h>
#include "src/engine/engine_util_misc.h"
namespace mujoco {
namespace {
// Run benchmark for the isZero functions, for mjtNum directly or bytewise.
ABSL_ATTRIBUTE_NO_TAIL_CALL static void IzZeroBenchmark(
benchmark::State& state, int n, bool bytewise) {
std::vector<mjtNum> data(n, 0);
data[n-1] = 1;
if (bytewise) {
for (auto s : state) {
int iszero =
mju_isZeroByte((const unsigned char*)data.data(), n * sizeof(mjtNum));
benchmark::DoNotOptimize(iszero);
}
} else {
for (auto s : state) {
int iszero = mju_isZero(data.data(), n);
benchmark::DoNotOptimize(iszero);
}
}
state.counters["items/s"] =
benchmark::Counter(state.iterations() * n, benchmark::Counter::kIsRate);
}
// Define benchmarks.
void BM_isZeroByte_1e2(benchmark::State& state) {
IzZeroBenchmark(state, 100, true);
}
BENCHMARK(BM_isZeroByte_1e2);
void BM_isZeroByte_1e5(benchmark::State& state) {
IzZeroBenchmark(state, 100000, true);
}
BENCHMARK(BM_isZeroByte_1e5);
void BM_isZero_1e2(benchmark::State& state) {
IzZeroBenchmark(state, 100, false);
}
BENCHMARK(BM_isZero_1e2);
void BM_isZero_1e5(benchmark::State& state) {
IzZeroBenchmark(state, 100000, false);
}
BENCHMARK(BM_isZero_1e5);
} // namespace
} // namespace mujoco
int main(int argc, char** argv) {
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
return 0;
}
+11
View File
@@ -349,6 +349,17 @@ TEST_F(UtilMiscTest, MjuSparseLower2SymMapPartial) {
EXPECT_THAT(AsVector(mat_res, res_nnz), ElementsAre(1, 2, 0, 2, 3, 0, 6));
}
TEST_F(UtilMiscTest, MjuIsZero) {
mjtNum vec[1] = {1};
EXPECT_EQ(mju_isZero(vec, 1), 0);
EXPECT_EQ(mju_isZero(vec, 0), 1);
vec[0] = 0;
EXPECT_EQ(mju_isZero(vec, 1), 1);
vec[0] = -0.0;
EXPECT_EQ(mju_isZero(vec, 1), 1);
EXPECT_EQ(mju_isZeroByte((const unsigned char*)vec, sizeof(mjtNum)), 0);
}
// --------------------------------- Interpolation -----------------------------
using InterpolationTest = MujocoTest;