Random123
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 */
136R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_closed_closed_32_float(uint32_t i){
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){
142 return (i>>8)*R123_0x1p_24f; /* 0x1.0p-24f; */
143}
144
145R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_open_closed_32_float(uint32_t i){
146 return (1+(i>>8))*R123_0x1p_24f; /* *0x1.0p-24f; */
147}
148
149R123_CUDA_DEVICE R123_STATIC_INLINE float u01fixedpt_open_open_32_float(uint32_t i){
150 return (0.5f+(i>>9))*R123_0x1p_23f; /* 0x1.p-23f; */
151}
152
153#if R123_USE_U01_DOUBLE
154/* narrowing conversions: uint64_t to double */
155R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_closed_64_double(uint64_t i){
156 /* N.B. we ignore the high bit, so output is not monotonic */
157 return ((i&R123_64BIT(0x7ffffffffffffe00)) + (i&0x200))*R123_0x1p_63; /* 0x1.p-63; */
158}
159
160R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_open_64_double(uint64_t i){
161 return (i>>11)*R123_0x1p_53; /* 0x1.0p-53; */
162}
163
164R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_closed_64_double(uint64_t i){
165 return (1+(i>>11))*R123_0x1p_53; /* 0x1.0p-53; */
166}
167
168R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_open_64_double(uint64_t i){
169 return (0.5+(i>>12))*R123_0x1p_52; /* 0x1.0p-52; */
170}
171
172/* widening conversions: u32 to double */
173R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_closed_32_double(uint32_t i){
174 /* j = i+(i&1) takes on 2^31+1 possible values with a 'trapezoid' distribution:
175 p_j = 1 0 2 0 2 .... 2 0 2 0 1
176 j = 0 1 2 3 4 .... 2^32
177 by converting to double *before* doing the add, we don't wrap the high bit.
178 */
179 return (((double)(i&1)) + i)*R123_0x1p_32; /* 0x1.p-32; */
180}
181
182R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_closed_open_32_double(uint32_t i){
183 return i*R123_0x1p_32; /* 0x1.p-32; */
184}
185
186R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_closed_32_double(uint32_t i){
187 return (1.+i)*R123_0x1p_32; /* 0x1.p-32; */
188}
189
190R123_CUDA_DEVICE R123_STATIC_INLINE double u01fixedpt_open_open_32_double(uint32_t i){
191 return (0.5+i)*R123_0x1p_32; /* 0x1.p-32; */
192}
193#endif /* R123_USE_U01_DOUBLE */
194
195#ifdef __cplusplus
196}
197#endif
198
200#endif
static double u01fixedpt_open_closed_64_double(uint64_t i)
Definition: u01fixedpt.h:164
static double u01fixedpt_closed_closed_64_double(uint64_t i)
Definition: u01fixedpt.h:155
static float u01fixedpt_open_closed_32_float(uint32_t i)
Definition: u01fixedpt.h:145
static double u01fixedpt_open_closed_32_double(uint32_t i)
Definition: u01fixedpt.h:186
static float u01fixedpt_open_open_32_float(uint32_t i)
Definition: u01fixedpt.h:149
static double u01fixedpt_closed_open_64_double(uint64_t i)
Definition: u01fixedpt.h:160
static double u01fixedpt_open_open_32_double(uint32_t i)
Definition: u01fixedpt.h:190
static double u01fixedpt_closed_open_32_double(uint32_t i)
Definition: u01fixedpt.h:182
static float u01fixedpt_closed_closed_32_float(uint32_t i)
Definition: u01fixedpt.h:136
static float u01fixedpt_closed_open_32_float(uint32_t i)
Definition: u01fixedpt.h:141
static double u01fixedpt_open_open_64_double(uint64_t i)
Definition: u01fixedpt.h:168
static double u01fixedpt_closed_closed_32_double(uint32_t i)
Definition: u01fixedpt.h:173