Random123
Loading...
Searching...
No Matches
uniform.hpp
Go to the documentation of this file.
1/*
2Copyright 2010-2011, D. E. Shaw Research.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9* Redistributions of source code must retain the above copyright
10 notice, this list of conditions, and the following disclaimer.
11
12* Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions, and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16* Neither the name of D. E. Shaw Research nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32
33#ifndef __r123_uniform_dot_hpp
34#define __r123_uniform_dot_hpp
35
89#include <limits>
90#if R123_USE_CXX11_TYPE_TRAITS
91#include <type_traits>
92#endif
93#if __cplusplus >= 201103L
94#include <array>
95#endif
96
97namespace r123{
103#if R123_USE_CXX11_TYPE_TRAITS
104using std::make_signed;
105using std::make_unsigned;
106#else
107// Sigh... We could try to find another <type_traits>, e.g., from
108// boost or TR1. Or we can do it ourselves in the r123 namespace.
109// It's not clear which will cause less headache...
110template <typename T> struct make_signed{};
111template <typename T> struct make_unsigned{};
112#define R123_MK_SIGNED_UNSIGNED(ST, UT) \
113template<> struct make_signed<ST>{ typedef ST type; }; \
114template<> struct make_signed<UT>{ typedef ST type; }; \
115template<> struct make_unsigned<ST>{ typedef UT type; }; \
116template<> struct make_unsigned<UT>{ typedef UT type; }
117
122#if R123_USE_GNU_UINT128
124#endif
125#undef R123_MK_SIGNED_UNSIGNED
126#endif
127
128#if defined(__CUDACC__) || defined(_LIBCPP_HAS_NO_CONSTEXPR)
129// Amazing! cuda thinks numeric_limits::max() is a __host__ function, so
130// we can't use it in a device function.
131//
132// The LIBCPP_HAS_NO_CONSTEXP test catches situations where the libc++
133// library thinks that the compiler doesn't support constexpr, but we
134// think it does. As a consequence, the library declares
135// numeric_limits::max without constexpr. This workaround should only
136// affect a narrow range of compiler/library pairings.
137//
138// In both cases, we find max() by computing ~(unsigned)0 right-shifted
139// by is_signed.
140template <typename T>
142 typedef typename make_unsigned<T>::type uT;
143 return (~uT(0)) >> std::numeric_limits<T>::is_signed;
144 }
145#else
146template <typename T>
148 return std::numeric_limits<T>::max();
149}
150#endif
156
174template <typename Ftype, typename Itype>
176 typedef typename make_unsigned<Itype>::type Utype;
179#if R123_UNIFORM_FLOAT_STORE
180 volatile Ftype x = Utype(in)*factor; return x+halffactor;
181#else
182 return Utype(in)*factor + halffactor;
183#endif
184}
185
187
205template <typename Ftype, typename Itype>
207 typedef typename make_signed<Itype>::type Stype;
210#if R123_UNIFORM_FLOAT_STORE
211 volatile Ftype x = Stype(in)*factor; return x+halffactor;
212#else
213 return Stype(in)*factor + halffactor;
214#endif
215}
216
218
238template <typename Ftype, typename Itype>
240 typedef typename make_unsigned<Itype>::type Utype;
241 R123_CONSTEXPR int excess = std::numeric_limits<Utype>::digits - std::numeric_limits<Ftype>::digits;
242 if(excess>=0){
243 R123_CONSTEXPR int ex_nowarn = (excess>=0) ? excess : 0;
245 return (1 | (Utype(in)>>ex_nowarn)) * factor;
246 }else
247 return u01<Ftype>(in);
248}
249
250#if R123_USE_CXX11_STD_ARRAY
251
253
258template <typename Ftype, typename CollType>
259static inline
260std::array<Ftype, CollType::static_size> u01all(CollType in)
261{
262 std::array<Ftype, CollType::static_size> ret;
263 auto p = ret.begin();
264 for(auto e : in){
265 *p++ = u01<Ftype>(e);
266 }
267 return ret;
268}
269
271
276template <typename Ftype, typename CollType>
277static inline
278std::array<Ftype, CollType::static_size> uneg11all(CollType in)
279{
280 std::array<Ftype, CollType::static_size> ret;
281 auto p = ret.begin();
282 for(auto e : in){
283 *p++ = uneg11<Ftype>(e);
284 }
285 return ret;
286}
287
289
294template <typename Ftype, typename CollType>
295static inline
296std::array<Ftype, CollType::static_size> u01fixedptall(CollType in)
297{
298 std::array<Ftype, CollType::static_size> ret;
299 auto p = ret.begin();
300 for(auto e : in){
301 *p++ = u01fixedpt<Ftype>(e);
302 }
303 return ret;
304}
305#endif // __cplusplus >= 201103L
306
307} // namespace r123
308
309#endif
310
static std::array< Ftype, CollType::static_size > uneg11all(CollType in)
Apply uneg11 to every item in an r123array, returning a std::array.
Definition uniform.hpp:278
static Ftype u01fixedpt(Itype in)
Return a value in (0,1) chosen from a set of equally spaced fixed-point values.
Definition uniform.hpp:239
static std::array< Ftype, CollType::static_size > u01fixedptall(CollType in)
Apply u01fixedpt to every item in an r123array, returning a std::array.
Definition uniform.hpp:296
static Ftype uneg11(Itype in)
Return a signed value in [-1,1].
Definition uniform.hpp:206
static std::array< Ftype, CollType::static_size > u01all(CollType in)
Apply u01 to every item in an r123array, returning a std::array.
Definition uniform.hpp:260
static Ftype u01(Itype in)
Return a uniform real value in (0, 1].
Definition uniform.hpp:175
Definition aes.h:242