audio_utils primitives: Add float clamping memcopy

For security reason, float buffers provided by application must be
clamped to FLOAT_NOMINAL_RANGE_HEADROOM.
With the new all float pipeline, float are no longer clamped by their
conversion to fixed point.

This patch adds a function to efficiently clamp a buffer during a
memcopy.

Test: adb shell /system/bin/primitives_benchmark
Test: adb shell /data/nativetest/primitives_tests/primitives_tests --gtest_filter=*Clamping*
Bug: 68099072
Change-Id: I030b247ea29cb94c62d1206c31960f45da2446e6
Signed-off-by: Kevin Rocard <krocard@google.com>
diff --git a/audio_utils/tests/primitives_benchmark.cpp b/audio_utils/tests/primitives_benchmark.cpp
new file mode 100644
index 0000000..25da434
--- /dev/null
+++ b/audio_utils/tests/primitives_benchmark.cpp
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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 <cstddef>
+#include <random>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include <audio_utils/primitives.h>
+
+static void BM_MemcpyToFloatFromFloatWithClamping(benchmark::State& state) {
+    const size_t count = state.range(0);
+    const float srcMax = state.range(1);
+    const float absMax = 1.413;
+
+    std::vector<float> src(count);
+    std::vector<float> dst(count);
+    std::vector<float> expected(count);
+
+    // Initialize src buffer with deterministic pseudo-random values
+    std::minstd_rand gen(count);
+    std::uniform_real_distribution<> dis(-srcMax, srcMax);
+    for (size_t i = 0; i < count; i++) {
+        src[i] = dis(gen);
+        expected[i] = fmin(absMax, fmax(-absMax, src[i]));
+    }
+
+    // Run the test
+    while (state.KeepRunning()) {
+        benchmark::DoNotOptimize(src.data());
+        benchmark::DoNotOptimize(dst.data());
+        memcpy_to_float_from_float_with_clamping(dst.data(), src.data(), count, 1.413);
+        benchmark::ClobberMemory();
+    }
+
+    if (expected != dst) {
+        state.SkipWithError("Incorrect clamping!");
+    }
+    state.SetComplexityN(state.range(0));
+}
+
+BENCHMARK(BM_MemcpyToFloatFromFloatWithClamping)->RangeMultiplier(2)->Ranges({{10, 8<<12}, {1, 2}});
+
+static void BM_MemcpyFloat(benchmark::State& state) {
+    const size_t count = state.range(0);
+
+    std::vector<float> src(count);
+    std::vector<float> dst(count);
+
+    // Initialize src buffer with deterministic pseudo-random values
+    std::minstd_rand gen(count);
+    std::uniform_real_distribution<> dis;
+    for (size_t i = 0; i < count; i++) {
+        src[i] = dis(gen);
+    }
+
+    // Run the test
+    while (state.KeepRunning()) {
+        benchmark::DoNotOptimize(src.data());
+        benchmark::DoNotOptimize(dst.data());
+        memcpy(dst.data(), src.data(), count * sizeof(float));
+        benchmark::ClobberMemory();
+    }
+
+    if (src != dst) {
+        state.SkipWithError("Incorrect memcpy!");
+    }
+    state.SetComplexityN(state.range(0));
+}
+
+BENCHMARK(BM_MemcpyFloat)->RangeMultiplier(2)->Ranges({{10, 8<<12}});
+
+BENCHMARK_MAIN();