test/reference/clamp.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 2024-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/reference/clamp.hpp" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cstddef> | ||
| 11 | |||
| 12 | #include "kai/kai_common.h" | ||
| 13 | #include "test/common/buffer.hpp" | ||
| 14 | #include "test/common/float16.hpp" | ||
| 15 | #include "test/common/memory.hpp" | ||
| 16 | #include "test/common/numeric_limits.hpp" | ||
| 17 | #include "test/common/round.hpp" | ||
| 18 | |||
| 19 | namespace kai::test { | ||
| 20 | |||
| 21 | template <typename T> | ||
| 22 | 28478 | std::tuple<T, T> find_clamp_range(const void* src, size_t len, float keep_ratio) { | |
| 23 | − | KAI_ASSUME_ALWAYS(keep_ratio > 0.0F); | |
| 24 | − | KAI_ASSUME_ALWAYS(keep_ratio <= 1.0F); | |
| 25 | |||
| 26 | 28478 | T min_value = numeric_highest<T>; | |
| 27 | 28478 | T max_value = numeric_lowest<T>; | |
| 28 | |||
| 29 |
2/4✓ Branch 0 taken 28478 times.
✓ Branch 1 taken 33974785 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
34003263 | for (size_t i = 0; i < len; ++i) { |
| 30 | 33974785 | const T value = read_array<T>(src, i); | |
| 31 | |||
| 32 | 33974785 | min_value = std::min(min_value, value); | |
| 33 | 33974785 | max_value = std::max(max_value, value); | |
| 34 | 33974785 | } | |
| 35 | |||
| 36 | 28478 | min_value = std::max(min_value, numeric_lowest<T>); | |
| 37 | 28478 | max_value = std::min(max_value, numeric_highest<T>); | |
| 38 | |||
| 39 | 28478 | const T range = max_value - min_value; | |
| 40 | 28478 | const T reduction = static_cast<T>(static_cast<float>(range) * (1.0F - keep_ratio) / 2); | |
| 41 | |||
| 42 | 28478 | const T clamp_min_value = min_value + reduction; | |
| 43 | 28478 | const T clamp_max_value = max_value - reduction; | |
| 44 | |||
| 45 | 28478 | return {clamp_min_value, clamp_max_value}; | |
| 46 | 28478 | } | |
| 47 | |||
| 48 | template std::tuple<float, float> find_clamp_range(const void* src, size_t len, float keep_ratio); | ||
| 49 | template std::tuple<Float16, Float16> find_clamp_range(const void* src, size_t len, float keep_ratio); | ||
| 50 | |||
| 51 | 11315 | std::tuple<float, float> find_clamp_range(DataType type, const void* src, size_t len, float keep_ratio) { | |
| 52 | − | KAI_ASSUME_ALWAYS(keep_ratio > 0.0F); // Avoid total clamping. | |
| 53 | 11315 | auto max = std::numeric_limits<float>::min(); | |
| 54 | 11315 | auto min = std::numeric_limits<float>::max(); | |
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 11315 times.
✓ Branch 1 taken 67235158 times.
|
67246473 | for (size_t i = 0; i < len; i += 1) { |
| 57 | 67235158 | const float value = read_array(type, src, i); | |
| 58 | 67235158 | max = std::max(value, max); | |
| 59 | 67235158 | min = std::min(value, min); | |
| 60 | 67235158 | } | |
| 61 | |||
| 62 | 11315 | const float reduction = (max - min) * (1.0F - keep_ratio) / 2.0F; | |
| 63 | 11315 | return {min + reduction, max - reduction}; | |
| 64 | 11315 | } | |
| 65 | |||
| 66 | template <typename T> | ||
| 67 | 28999 | Buffer clamp(const void* src, size_t len, T min_value, T max_value) { | |
| 68 | 28999 | Buffer dst(round_up_division(len * size_in_bits<T>, 8)); | |
| 69 | |||
| 70 |
2/4✓ Branch 0 taken 34904624 times.
✓ Branch 1 taken 28999 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
34933623 | for (size_t i = 0; i < len; ++i) { |
| 71 |
4/16✗ Branch 0 not taken.
✓ Branch 1 taken 34904624 times.
✓ Branch 2 taken 34904624 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 34904624 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 34904624 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
34904624 | write_array<T>(dst.data(), i, std::clamp(read_array<T>(src, i), min_value, max_value)); |
| 72 | 34904624 | } | |
| 73 | |||
| 74 | 28999 | return dst; | |
| 75 | 28999 | } | |
| 76 | |||
| 77 | template Buffer clamp(const void* src, size_t len, float min_value, float max_value); | ||
| 78 | template Buffer clamp(const void* src, size_t len, Float16 min_value, Float16 max_value); | ||
| 79 | |||
| 80 | 11381 | Buffer clamp(DataType type, const void* src, size_t len, float min_value, float max_value) { | |
| 81 | 11381 | Buffer dst(round_up_division(len * data_type_size_in_bits(type), 8)); | |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 67278628 times.
✓ Branch 1 taken 11381 times.
|
67290009 | for (size_t i = 0; i < len; ++i) { |
| 84 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 67278628 times.
✓ Branch 2 taken 67278628 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 67278628 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 67278628 times.
✗ Branch 7 not taken.
|
67278628 | write_array(type, dst.data(), i, std::clamp<float>(read_array(type, src, i), min_value, max_value)); |
| 85 | 67278628 | } | |
| 86 | |||
| 87 | 11381 | return dst; | |
| 88 | 11381 | } | |
| 89 | |||
| 90 | } // namespace kai::test | ||
| 91 |