123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #ifndef BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
- #define BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
- #include <boost/assert.hpp>
- #include <boost/type_traits.hpp>
- #include <boost/compute/command_queue.hpp>
- #include <boost/compute/function.hpp>
- #include <boost/compute/detail/literal.hpp>
- #include <boost/compute/types/fundamental.hpp>
- namespace boost {
- namespace compute {
- template<class RealType = float>
- class uniform_real_distribution
- {
- public:
- typedef RealType result_type;
-
-
-
- uniform_real_distribution(RealType a = 0.f, RealType b = 1.f)
- : m_a(a),
- m_b(b)
- {
- BOOST_ASSERT(a < b);
- }
-
- ~uniform_real_distribution()
- {
- }
-
- result_type a() const
- {
- return m_a;
- }
-
- result_type b() const
- {
- return m_b;
- }
-
-
- template<class OutputIterator, class Generator>
- void generate(OutputIterator first,
- OutputIterator last,
- Generator &generator,
- command_queue &queue)
- {
- BOOST_COMPUTE_FUNCTION(RealType, scale_random, (const uint_ x),
- {
- return nextafter(LO + (convert_RealType(x) / MAX_RANDOM) * (HI - LO), (RealType) LO);
- });
- scale_random.define("LO", detail::make_literal(m_a));
- scale_random.define("HI", detail::make_literal(m_b));
- scale_random.define("MAX_RANDOM", "UINT_MAX");
- scale_random.define(
- "convert_RealType", std::string("convert_") + type_name<RealType>()
- );
- scale_random.define("RealType", type_name<RealType>());
- generator.generate(
- first, last, scale_random, queue
- );
- }
-
- template<class OutputIterator, class Generator>
- void fill(OutputIterator first,
- OutputIterator last,
- Generator &g,
- command_queue &queue)
- {
- generate(first, last, g, queue);
- }
- private:
- RealType m_a;
- RealType m_b;
- BOOST_STATIC_ASSERT_MSG(
- boost::is_floating_point<RealType>::value,
- "Template argument must be a floating point type"
- );
- };
- }
- }
- #endif
|