blob: 1a804d3290be8c41304130acd2a6591e698fe150 [file] [log] [blame]
Georgios Pinitas587708b2018-12-31 15:43:52 +00001/*
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +00002 * Copyright (c) 2019-2021 Arm Limited.
Georgios Pinitas587708b2018-12-31 15:43:52 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_MISC_RANDOM_H
25#define ARM_COMPUTE_MISC_RANDOM_H
Georgios Pinitas587708b2018-12-31 15:43:52 +000026
27#include "arm_compute/core/Error.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Giorgio Arena6aeb2172020-12-15 15:45:43 +000029#include "utils/Utils.h"
Georgios Pinitas587708b2018-12-31 15:43:52 +000030
31#include <random>
32#include <type_traits>
33
34namespace arm_compute
35{
36namespace utils
37{
38namespace random
39{
40/** Uniform distribution within a given number of sub-ranges
41 *
42 * @tparam T Distribution primitive type
43 */
44template <typename T>
45class RangedUniformDistribution
46{
47public:
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +000048 static constexpr bool is_fp_16bit = std::is_same<T, half>::value || std::is_same<T, bfloat16>::value;
49 static constexpr bool is_integral = std::is_integral<T>::value && !is_fp_16bit;
Giorgio Arena6aeb2172020-12-15 15:45:43 +000050
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051 using fp_dist = typename std::conditional<is_fp_16bit,
52 arm_compute::utils::uniform_real_distribution_16bit<T>,
53 std::uniform_real_distribution<T>>::type;
Giorgio Arena6aeb2172020-12-15 15:45:43 +000054 using DT = typename std::conditional<is_integral, std::uniform_int_distribution<T>, fp_dist>::type;
Georgios Pinitas587708b2018-12-31 15:43:52 +000055 using result_type = T;
56 using range_pair = std::pair<result_type, result_type>;
57
Georgios Pinitas587708b2018-12-31 15:43:52 +000058 /** Constructor
59 *
60 * @param[in] low lowest value in the range (inclusive)
61 * @param[in] high highest value in the range (inclusive for uniform_int_distribution, exclusive for uniform_real_distribution)
62 * @param[in] exclude_ranges Ranges to exclude from the generator
63 */
64 RangedUniformDistribution(result_type low, result_type high, const std::vector<range_pair> &exclude_ranges)
65 : _distributions(), _selector()
66 {
67 result_type clow = low;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 for (const auto &erange : exclude_ranges)
Georgios Pinitas587708b2018-12-31 15:43:52 +000069 {
Giorgio Arena6aeb2172020-12-15 15:45:43 +000070 result_type epsilon = is_integral ? result_type(1) : result_type(std::numeric_limits<T>::epsilon());
Georgios Pinitas587708b2018-12-31 15:43:52 +000071
72 ARM_COMPUTE_ERROR_ON(clow > erange.first || clow >= erange.second);
73
74 _distributions.emplace_back(DT(clow, erange.first - epsilon));
75 clow = erange.second + epsilon;
76 }
77 ARM_COMPUTE_ERROR_ON(clow > high);
78 _distributions.emplace_back(DT(clow, high));
79 _selector = std::uniform_int_distribution<uint32_t>(0, _distributions.size() - 1);
80 }
81 /** Generate random number
82 *
83 * @tparam URNG Random number generator object type
84 *
85 * @param[in] g A uniform random number generator object, used as the source of randomness.
86 *
87 * @return A new random number.
88 */
89 template <class URNG>
90 result_type operator()(URNG &g)
91 {
92 unsigned int rand_select = _selector(g);
93 return _distributions[rand_select](g);
94 }
95
96private:
97 std::vector<DT> _distributions;
98 std::uniform_int_distribution<uint32_t> _selector;
99};
100} // namespace random
101} // namespace utils
102} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000103#endif /* ARM_COMPUTE_MISC_RANDOM_H */