KleidiAI Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 94.7% 36 / 0 / 38
Functions: 66.7% 2 / 0 / 3
Branches: 52.9% 18 / 0 / 34

test/nextgen/reference/clamp.cpp
Line Branch Exec Source
1 //
2 // SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6
7 #include "test/nextgen/reference/clamp.hpp"
8
9 #include <algorithm>
10 #include <cstddef>
11 #include <functional>
12 #include <numeric>
13 #include <tuple>
14 #include <utility>
15
16 #include "test/common/assert.hpp"
17 #include "test/common/buffer.hpp"
18 #include "test/common/data_type.hpp"
19 #include "test/common/memory.hpp"
20 #include "test/common/numeric_limits.hpp"
21 #include "test/common/round.hpp"
22 #include "test/common/span.hpp"
23 #include "test/reference/clamp.hpp"
24
25 namespace kai::test {
26
27 namespace {
28
29 template <typename T>
30 200 std::tuple<Buffer, Buffer> dynamic_clamp(float ratio, Span<const size_t> shape, Span<const std::byte> data) {
31
1/4
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
200 KAI_TEST_ASSERT(ratio > 0.0F);
32
1/4
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
200 KAI_TEST_ASSERT(ratio <= 1.0F);
33
34 200 const size_t num_dims = shape.size();
35 200 const size_t width = shape.at(num_dims - 1);
36 200 const size_t height = std::accumulate(shape.begin(), shape.end() - 1, 1, std::multiplies<>());
37
38 // Finds the input range.
39 200 T src_min = numeric_highest<T>;
40 200 T src_max = numeric_lowest<T>;
41
42
2/2
✓ Branch 0 taken 15452 times.
✓ Branch 1 taken 200 times.
15652 for (size_t row = 0; row < height; ++row) {
43
2/2
✓ Branch 0 taken 1246953 times.
✓ Branch 1 taken 15452 times.
1262405 for (size_t col = 0; col < width; ++col) {
44 1246953 const T value = read_2d<T>(data, width, row, col);
45
46 1246953 src_min = std::min(src_min, value);
47 1246953 src_max = std::max(src_max, value);
48 1246953 }
49 15452 }
50
51 // Determines the output range.
52 200 src_min = std::max(src_min, numeric_lowest<T>);
53 200 src_max = std::min(src_max, numeric_highest<T>);
54
55 200 const T range = src_max - src_min;
56 200 const T reduction = static_cast<T>(static_cast<float>(range) * (1.0F - ratio) / 2);
57
58 200 const T dst_min = src_min + reduction;
59 200 const T dst_max = src_max - reduction;
60
61 200 Buffer limits(sizeof(ClampLimits<T>), 0);
62
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 *reinterpret_cast<ClampLimits<T>*>(limits.data()) = {dst_min, dst_max};
63
64 // Clamps the data.
65
2/4
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
200 Buffer dst(height * round_up_division(width * size_in_bits<T>, 8), 0);
66
67
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 15452 times.
15652 for (size_t row = 0; row < height; ++row) {
68
2/2
✓ Branch 0 taken 1246953 times.
✓ Branch 1 taken 15452 times.
1262405 for (size_t col = 0; col < width; ++col) {
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1246953 times.
1246953 const T value = read_2d<T>(data, width, row, col);
70
1/2
✓ Branch 0 taken 1246953 times.
✗ Branch 1 not taken.
1246953 const T dst_value = std::clamp(value, dst_min, dst_max);
71
2/4
✓ Branch 0 taken 1246953 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1246953 times.
✗ Branch 3 not taken.
1246953 write_2d<T>(dst.view(), width, row, col, dst_value);
72 1246953 }
73 15452 }
74
75 200 return {std::move(limits), std::move(dst)};
76 200 }
77
78 } // namespace
79
80 200 DynamicClampFn make_dynamic_clamp(DataType dtype) {
81
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 switch (dtype) {
82 case DataType::FP32:
83 200 return dynamic_clamp<float>;
84
85 default:
86 KAI_TEST_ERROR("Not implemented.");
87 }
88 }
89
90 } // namespace kai::test
91