commit b90e39a6292a00884be7d8cf813de44f2ef8b58b
parent d85d296440a0f67ae4337b4e05e27e2dcf172486
Author: [email protected] <[email protected]>
Date: Thu, 14 Mar 2019 23:04:02 +0000
Add more overloads to random_bit_generator, fix for ubuntu, disable cycle counter for arm
Diffstat:
1 file changed, 34 insertions(+), 14 deletions(-)
diff --git a/include/kfr/base/random.hpp b/include/kfr/base/random.hpp
@@ -29,6 +29,10 @@
#include "../simd/shuffle.hpp"
#include "../simd/vec.hpp"
+#ifdef CMT_ARCH_ARM
+#define KFR_DISABLE_READCYCLECOUNTER
+#endif
+
namespace kfr
{
@@ -113,13 +117,15 @@ KFR_INTRINSIC vec<T, N> random_uniform(random_bit_generator& gen)
template <typename T, size_t N, KFR_ENABLE_IF(std::is_same<T, f32>::value)>
KFR_INTRINSIC vec<f32, N> randommantissa(random_bit_generator& gen)
{
- return bitcast<f32>((random_uniform<u32, N>(gen) & 0x7FFFFFu) | 0x3f800000u) + 0.0f;
+ return bitcast<f32>((random_uniform<u32, N>(gen) & u32(0x7FFFFFu)) | u32(0x3f800000u)) + 0.0f;
}
template <typename T, size_t N, KFR_ENABLE_IF(std::is_same<T, f64>::value)>
KFR_INTRINSIC vec<f64, N> randommantissa(random_bit_generator& gen)
{
- return bitcast<f64>((random_uniform<u64, N>(gen) & 0x000FFFFFFFFFFFFFull) | 0x3FF0000000000000ull) + 0.0;
+ return bitcast<f64>((random_uniform<u64, N>(gen) & u64(0x000FFFFFFFFFFFFFull)) |
+ u64(0x3FF0000000000000ull)) +
+ 0.0;
}
template <typename T, size_t N, KFR_ENABLE_IF(is_f_class<T>::value)>
@@ -146,47 +152,52 @@ KFR_INTRINSIC vec<T, N> random_range(random_bit_generator& gen, T min, T max)
namespace internal
{
-template <typename T>
+template <typename T, typename Gen = random_bit_generator>
struct expression_random_uniform : input_expression
{
using value_type = T;
- constexpr expression_random_uniform(const random_bit_generator& gen) CMT_NOEXCEPT : gen(gen) {}
+ constexpr expression_random_uniform(Gen gen) CMT_NOEXCEPT : gen(gen) {}
template <size_t N>
friend vec<T, N> get_elements(const expression_random_uniform& self, cinput_t, size_t, vec_shape<T, N>)
{
return random_uniform<T, N>(self.gen);
}
- mutable random_bit_generator gen;
+ mutable Gen gen;
};
-template <typename T>
+template <typename T, typename Gen = random_bit_generator>
struct expression_random_range : input_expression
{
using value_type = T;
- constexpr expression_random_range(const random_bit_generator& gen, T min, T max) CMT_NOEXCEPT : gen(gen),
- min(min),
- max(max)
- {
- }
+ constexpr expression_random_range(Gen gen, T min, T max) CMT_NOEXCEPT : gen(gen), min(min), max(max) {}
template <size_t N>
friend vec<T, N> get_elements(const expression_random_range& self, cinput_t, size_t, vec_shape<T, N>)
{
return random_range<N, T>(self.gen, self.min, self.max);
}
- mutable random_bit_generator gen;
+ mutable Gen gen;
const T min;
const T max;
};
} // namespace internal
-/// @brief Returns expression that returns pseudo random values
+/// @brief Returns expression that returns pseudo random values. Copies the given generator
template <typename T>
KFR_FUNCTION internal::expression_random_uniform<T> gen_random_uniform(const random_bit_generator& gen)
{
return internal::expression_random_uniform<T>(gen);
}
+/// @brief Returns expression that returns pseudo random values. References the given
+/// generator. Use std::ref(gen) to force this overload
+template <typename T>
+KFR_FUNCTION internal::expression_random_uniform<T, std::reference_wrapper<random_bit_generator>>
+gen_random_uniform(std::reference_wrapper<random_bit_generator> gen)
+{
+ return internal::expression_random_uniform<T, std::reference_wrapper<random_bit_generator>>(gen);
+}
+
#ifndef KFR_DISABLE_READCYCLECOUNTER
/// @brief Returns expression that returns pseudo random values
template <typename T>
@@ -196,7 +207,7 @@ KFR_FUNCTION internal::expression_random_uniform<T> gen_random_uniform()
}
#endif
-/// @brief Returns expression that returns pseudo random values of the given range
+/// @brief Returns expression that returns pseudo random values of the given range. Copies the given generator
template <typename T>
KFR_FUNCTION internal::expression_random_range<T> gen_random_range(const random_bit_generator& gen, T min,
T max)
@@ -204,6 +215,15 @@ KFR_FUNCTION internal::expression_random_range<T> gen_random_range(const random_
return internal::expression_random_range<T>(gen, min, max);
}
+/// @brief Returns expression that returns pseudo random values of the given range. References the given
+/// generator. Use std::ref(gen) to force this overload
+template <typename T>
+KFR_FUNCTION internal::expression_random_range<T, std::reference_wrapper<random_bit_generator>>
+gen_random_range(std::reference_wrapper<random_bit_generator> gen, T min, T max)
+{
+ return internal::expression_random_range<T, std::reference_wrapper<random_bit_generator>>(gen, min, max);
+}
+
#ifndef KFR_DISABLE_READCYCLECOUNTER
/// @brief Returns expression that returns pseudo random values of the given range
template <typename T>