| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2026 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 | std::tuple<T, T> find_clamp_range(const void* src, size_t len, std::optional<float> keep_ratio) { |
| 23 | if (!keep_ratio.has_value()) { |
| 24 | return {numeric_lowest<T>, numeric_highest<T>}; |
| 25 | } |
| 26 | T min_value = numeric_highest<T>; |
| 27 | T max_value = numeric_lowest<T>; |
| 28 | |
| 29 | KAI_ASSUME_ALWAYS(keep_ratio.value() > 0.0F); |
| 30 | KAI_ASSUME_ALWAYS(keep_ratio.value() <= 1.0F); |
| 31 | |
| 32 | for (size_t i = 0; i < len; ++i) { |
| 33 | const T value = read_array<T>(src, i); |
| 34 | |
| 35 | min_value = std::min(min_value, value); |
| 36 | max_value = std::max(max_value, value); |
| 37 | } |
| 38 | |
| 39 | min_value = std::max(min_value, numeric_lowest<T>); |
| 40 | max_value = std::min(max_value, numeric_highest<T>); |
| 41 | |
| 42 | const T range = max_value - min_value; |
| 43 | const T reduction = static_cast<T>(static_cast<float>(range) * (1.0F - keep_ratio.value()) / 2); |
| 44 | |
| 45 | const T clamp_min_value = min_value + reduction; |
| 46 | const T clamp_max_value = max_value - reduction; |
| 47 | |
| 48 | return {clamp_min_value, clamp_max_value}; |
| 49 | } |
| 50 | |
| 51 | template std::tuple<float, float> find_clamp_range(const void* src, size_t len, std::optional<float> keep_ratio); |
| 52 | template std::tuple<Float16, Float16> find_clamp_range(const void* src, size_t len, std::optional<float> keep_ratio); |
| 53 | |
| 54 | std::tuple<float, float> find_clamp_range(DataType type, const void* src, size_t len, std::optional<float> keep_ratio) { |
| 55 | if (!keep_ratio.has_value()) { |
| 56 | return {std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max()}; |
| 57 | } |
| 58 | auto max = std::numeric_limits<float>::lowest(); |
| 59 | auto min = std::numeric_limits<float>::max(); |
| 60 | |
| 61 | KAI_ASSUME_ALWAYS(keep_ratio.value() > 0.0F); // Require a positive kept range. |
| 62 | for (size_t i = 0; i < len; i += 1) { |
| 63 | const float value = read_array(type, src, i); |
| 64 | max = std::max(value, max); |
| 65 | min = std::min(value, min); |
| 66 | } |
| 67 | |
| 68 | const float reduction = (max - min) * (1.0F - keep_ratio.value()) / 2.0F; |
| 69 | return {min + reduction, max - reduction}; |
| 70 | } |
| 71 | |
| 72 | template <typename T> |
| 73 | Buffer clamp(const void* src, size_t len, T min_value, T max_value) { |
| 74 | Buffer dst(round_up_division(len * size_in_bits<T>, 8)); |
| 75 | |
| 76 | for (size_t i = 0; i < len; ++i) { |
| 77 | write_array<T>(dst.data(), i, std::clamp(read_array<T>(src, i), min_value, max_value)); |
| 78 | } |
| 79 | |
| 80 | return dst; |
| 81 | } |
| 82 | |
| 83 | template Buffer clamp(const void* src, size_t len, float min_value, float max_value); |
| 84 | template Buffer clamp(const void* src, size_t len, Float16 min_value, Float16 max_value); |
| 85 | |
| 86 | Buffer clamp(DataType type, const void* src, size_t len, float min_value, float max_value) { |
| 87 | Buffer dst(round_up_division(len * data_type_size_in_bits(type), 8)); |
| 88 | |
| 89 | for (size_t i = 0; i < len; ++i) { |
| 90 | write_array(type, dst.data(), i, std::clamp<float>(read_array(type, src, i), min_value, max_value)); |
| 91 | } |
| 92 | |
| 93 | return dst; |
| 94 | } |
| 95 | |
| 96 | } // namespace kai::test |