Project Alice
Loading...
Searching...
No Matches
u01fixedpt.h
Go to the documentation of this file.
1/*
2Copyright 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#ifndef _random123_ufixed01_dot_h_
33#define _random123_ufixed01_dot_h_
34
36
111/* Hex floats were standardized by C in 1999, but weren't standardized
112 by C++ until 2011. So, we're obliged to write out our constants in
113 decimal, even though they're most naturally expressed in binary.
114 We cross our fingers and hope that the compiler does the compile-time
115 constant arithmetic properly.
116*/
117#define R123_0x1p_31f (1.f / (1024.f * 1024.f * 1024.f * 2.f))
118#define R123_0x1p_24f (128.f * R123_0x1p_31f)
119#define R123_0x1p_23f (256.f * R123_0x1p_31f)
120#define R123_0x1p_32 (1. / (1024. * 1024. * 1024. * 4.))
121#define R123_0x1p_63 (2. * R123_0x1p_32 * R123_0x1p_32)
122#define R123_0x1p_53 (1024. * R123_0x1p_63)
123#define R123_0x1p_52 (2048. * R123_0x1p_63)
124
127#ifndef R123_USE_U01_DOUBLE
128#define R123_USE_U01_DOUBLE 1
129#endif
130
131#ifdef __cplusplus
132extern "C" {
133#endif
134
135/* narrowing conversions: uint32_t to float */
137 /* N.B. we ignore the high bit, so output is not monotonic */
138 return ((i & 0x7fffffc0) + (i & 0x40)) * R123_0x1p_31f; /* 0x1.p-31f */
139}
140
141R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_closed_open_32_float(uint32_t i) { return (i >> 8) * R123_0x1p_24f; /* 0x1.0p-24f; */ }
142
143R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_open_closed_32_float(uint32_t i) { return (1 + (i >> 8)) * R123_0x1p_24f; /* *0x1.0p-24f; */ }
144
145R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_open_open_32_float(uint32_t i) { return (0.5f + (i >> 9)) * R123_0x1p_23f; /* 0x1.p-23f; */ }
146
147#if R123_USE_U01_DOUBLE
148/* narrowing conversions: uint64_t to double */
150 /* N.B. we ignore the high bit, so output is not monotonic */
151 return ((i & R123_64BIT(0x7ffffffffffffe00)) + (i & 0x200)) * R123_0x1p_63; /* 0x1.p-63; */
152}
153
154R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_open_64_double(uint64_t i) { return (i >> 11) * R123_0x1p_53; /* 0x1.0p-53; */ }
155
156R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_closed_64_double(uint64_t i) { return (1 + (i >> 11)) * R123_0x1p_53; /* 0x1.0p-53; */ }
157
158R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_open_64_double(uint64_t i) { return (0.5 + (i >> 12)) * R123_0x1p_52; /* 0x1.0p-52; */ }
159
160/* widening conversions: u32 to double */
162 /* j = i+(i&1) takes on 2^31+1 possible values with a 'trapezoid' distribution:
163 p_j = 1 0 2 0 2 .... 2 0 2 0 1
164 j = 0 1 2 3 4 .... 2^32
165 by converting to double *before* doing the add, we don't wrap the high bit.
166 */
167 return (((double)(i & 1)) + i) * R123_0x1p_32; /* 0x1.p-32; */
168}
169
170R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_open_32_double(uint32_t i) { return i * R123_0x1p_32; /* 0x1.p-32; */ }
171
172R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_closed_32_double(uint32_t i) { return (1. + i) * R123_0x1p_32; /* 0x1.p-32; */ }
173
174R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_open_32_double(uint32_t i) { return (0.5 + i) * R123_0x1p_32; /* 0x1.p-32; */ }
175#endif /* R123_USE_U01_DOUBLE */
176
177#ifdef __cplusplus
178}
179#endif
180
182#endif
#define R123_CUDA_DEVICE
#define R123_STATIC_INLINE
uint uint32_t
ulong uint64_t
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_open_32_double(uint32_t i)
Definition: u01fixedpt.h:170
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_open_64_double(uint64_t i)
Definition: u01fixedpt.h:154
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_open_32_double(uint32_t i)
Definition: u01fixedpt.h:174
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_closed_32_double(uint32_t i)
Definition: u01fixedpt.h:161
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_closed_32_double(uint32_t i)
Definition: u01fixedpt.h:172
R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_open_closed_32_float(uint32_t i)
Definition: u01fixedpt.h:143
R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_open_open_32_float(uint32_t i)
Definition: u01fixedpt.h:145
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_open_64_double(uint64_t i)
Definition: u01fixedpt.h:158
R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_closed_closed_32_float(uint32_t i)
Definition: u01fixedpt.h:136
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_closed_64_double(uint64_t i)
Definition: u01fixedpt.h:156
R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_closed_64_double(uint64_t i)
Definition: u01fixedpt.h:149
R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_closed_open_32_float(uint32_t i)
Definition: u01fixedpt.h:141