OWL
Loading...
Searching...
No Matches
constants.h
1// ======================================================================== //
2// Copyright 2018-2019 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19#include <limits>
20#include <limits.h>
21#ifdef __CUDACC__
22#include <math_constants.h>
23#endif
24
25#ifndef M_PI
26#define M_PI 3.141593f
27#endif
28
29namespace owl {
30 namespace common {
31
32 static struct ZeroTy
33 {
34 __both__ operator double ( ) const { return 0; }
35 __both__ operator float ( ) const { return 0; }
36 __both__ operator long long( ) const { return 0; }
37 __both__ operator unsigned long long( ) const { return 0; }
38 __both__ operator long ( ) const { return 0; }
39 __both__ operator unsigned long ( ) const { return 0; }
40 __both__ operator int ( ) const { return 0; }
41 __both__ operator unsigned int ( ) const { return 0; }
42 __both__ operator short ( ) const { return 0; }
43 __both__ operator unsigned short ( ) const { return 0; }
44 __both__ operator char ( ) const { return 0; }
45 __both__ operator unsigned char ( ) const { return 0; }
46 } zero MAYBE_UNUSED;
47
48 static struct OneTy
49 {
50 __both__ operator double ( ) const { return 1; }
51 __both__ operator float ( ) const { return 1; }
52 __both__ operator long long( ) const { return 1; }
53 __both__ operator unsigned long long( ) const { return 1; }
54 __both__ operator long ( ) const { return 1; }
55 __both__ operator unsigned long ( ) const { return 1; }
56 __both__ operator int ( ) const { return 1; }
57 __both__ operator unsigned int ( ) const { return 1; }
58 __both__ operator short ( ) const { return 1; }
59 __both__ operator unsigned short ( ) const { return 1; }
60 __both__ operator char ( ) const { return 1; }
61 __both__ operator unsigned char ( ) const { return 1; }
62 } one MAYBE_UNUSED;
63
64 static struct NegInfTy
65 {
66#ifdef __CUDA_ARCH__
67 __device__ operator double ( ) const { return -CUDART_INF; }
68 __device__ operator float ( ) const { return -CUDART_INF_F; }
69#else
70 __both__ operator double ( ) const { return -std::numeric_limits<double>::infinity(); }
71 __both__ operator float ( ) const { return -std::numeric_limits<float>::infinity(); }
72 __both__ operator long long( ) const { return std::numeric_limits<long long>::min(); }
73 __both__ operator unsigned long long( ) const { return std::numeric_limits<unsigned long long>::min(); }
74 __both__ operator long ( ) const { return std::numeric_limits<long>::min(); }
75 __both__ operator unsigned long ( ) const { return std::numeric_limits<unsigned long>::min(); }
76 __both__ operator int ( ) const { return std::numeric_limits<int>::min(); }
77 __both__ operator unsigned int ( ) const { return std::numeric_limits<unsigned int>::min(); }
78 __both__ operator short ( ) const { return std::numeric_limits<short>::min(); }
79 __both__ operator unsigned short ( ) const { return std::numeric_limits<unsigned short>::min(); }
80 __both__ operator char ( ) const { return std::numeric_limits<char>::min(); }
81 __both__ operator unsigned char ( ) const { return std::numeric_limits<unsigned char>::min(); }
82#endif
83 } neg_inf MAYBE_UNUSED;
84
85 inline __both__ float infty() {
86#if defined(__CUDA_ARCH__)
87 return CUDART_INF_F;
88#else
89 return std::numeric_limits<float>::infinity();
90#endif
91 }
92
93 static struct PosInfTy
94 {
95#ifdef __CUDA_ARCH__
96 __device__ operator double ( ) const { return CUDART_INF; }
97 __device__ operator float ( ) const { return CUDART_INF_F; }
98#else
99 __both__ operator double ( ) const { return std::numeric_limits<double>::infinity(); }
100 __both__ operator float ( ) const { return std::numeric_limits<float>::infinity(); }
101 __both__ operator long long( ) const { return std::numeric_limits<long long>::max(); }
102 __both__ operator unsigned long long( ) const { return std::numeric_limits<unsigned long long>::max(); }
103 __both__ operator long ( ) const { return std::numeric_limits<long>::max(); }
104 __both__ operator unsigned long ( ) const { return std::numeric_limits<unsigned long>::max(); }
105 __both__ operator int ( ) const { return std::numeric_limits<int>::max(); }
106 __both__ operator unsigned int ( ) const { return std::numeric_limits<unsigned int>::max(); }
107 __both__ operator short ( ) const { return std::numeric_limits<short>::max(); }
108 __both__ operator unsigned short ( ) const { return std::numeric_limits<unsigned short>::max(); }
109 __both__ operator char ( ) const { return std::numeric_limits<char>::max(); }
110 __both__ operator unsigned char ( ) const { return std::numeric_limits<unsigned char>::max(); }
111#endif
112 } inf MAYBE_UNUSED, pos_inf MAYBE_UNUSED;
113
114 static struct NaNTy
115 {
116#ifdef __CUDA_ARCH__
117 __device__ operator double( ) const { return CUDART_NAN; }
118 __device__ operator float ( ) const { return CUDART_NAN_F; }
119#else
120 __both__ operator double( ) const { return std::numeric_limits<double>::quiet_NaN(); }
121 __both__ operator float ( ) const { return std::numeric_limits<float>::quiet_NaN(); }
122#endif
123 } nan MAYBE_UNUSED;
124
125 static struct UlpTy
126 {
127#ifdef __CUDA_ARCH__
128 // todo
129#else
130 __both__ operator double( ) const { return std::numeric_limits<double>::epsilon(); }
131 __both__ operator float ( ) const { return std::numeric_limits<float>::epsilon(); }
132#endif
133 } ulp MAYBE_UNUSED;
134
135
136
137 template<bool is_integer>
139
140 template<> struct limits_traits<true> {
141 template<typename T> static inline __both__ T value_limits_lower(T) { return std::numeric_limits<T>::min(); }
142 template<typename T> static inline __both__ T value_limits_upper(T) { return std::numeric_limits<T>::max(); }
143 };
144 template<> struct limits_traits<false> {
145 template<typename T> static inline __both__ T value_limits_lower(T) { return (T)NegInfTy(); }//{ return -std::numeric_limits<T>::infinity(); }
146 template<typename T> static inline __both__ T value_limits_upper(T) { return (T)PosInfTy(); }//{ return +std::numeric_limits<T>::infinity(); }
147 };
148
150 template<typename T> inline __both__ T empty_bounds_lower()
151 {
152 return limits_traits<std::numeric_limits<T>::is_integer>::value_limits_upper(T());
153 }
154
156 template<typename T> inline __both__ T empty_bounds_upper()
157 {
158 return limits_traits<std::numeric_limits<T>::is_integer>::value_limits_lower(T());
159 }
160
162 template<typename T> inline __both__ T empty_range_lower()
163 {
164 return limits_traits<std::numeric_limits<T>::is_integer>::value_limits_upper(T());
165 }
166
168 template<typename T> inline __both__ T empty_range_upper()
169 {
170 return limits_traits<std::numeric_limits<T>::is_integer>::value_limits_lower(T());
171 }
172
174 template<typename T> inline __both__ T open_range_lower()
175 {
176 return limits_traits<std::numeric_limits<T>::is_integer>::value_limits_lower(T());
177 }
178
180 template<typename T> inline __both__ T open_range_upper()
181 {
182 return limits_traits<std::numeric_limits<T>::is_integer>::value_limits_upper(T());
183 }
184
185 template<> inline __both__ uint32_t empty_bounds_lower<uint32_t>()
186 { return uint32_t(UINT_MAX); }
187 template<> inline __both__ uint32_t empty_bounds_upper<uint32_t>()
188 { return uint32_t(0); }
189 template<> inline __both__ uint32_t open_range_lower<uint32_t>()
190 { return uint32_t(0); }
191 template<> inline __both__ uint32_t open_range_upper<uint32_t>()
192 { return uint32_t(UINT_MAX); }
193
194 template<> inline __both__ int32_t empty_bounds_lower<int32_t>()
195 { return int32_t(INT_MAX); }
196 template<> inline __both__ int32_t empty_bounds_upper<int32_t>()
197 { return int32_t(INT_MIN); }
198 template<> inline __both__ int32_t open_range_lower<int32_t>()
199 { return int32_t(INT_MIN); }
200 template<> inline __both__ int32_t open_range_upper<int32_t>()
201 { return int32_t(INT_MAX); }
202
203 template<> inline __both__ uint64_t empty_bounds_lower<uint64_t>()
204 { return uint64_t(ULONG_MAX); }
205 template<> inline __both__ uint64_t empty_bounds_upper<uint64_t>()
206 { return uint64_t(0); }
207 template<> inline __both__ uint64_t open_range_lower<uint64_t>()
208 { return uint64_t(0); }
209 template<> inline __both__ uint64_t open_range_upper<uint64_t>()
210 { return uint64_t(ULONG_MAX); }
211
212 template<> inline __both__ int64_t empty_bounds_lower<int64_t>()
213 { return int64_t(LONG_MAX); }
214 template<> inline __both__ int64_t empty_bounds_upper<int64_t>()
215 { return int64_t(LONG_MIN); }
216 template<> inline __both__ int64_t open_range_lower<int64_t>()
217 { return int64_t(LONG_MIN); }
218 template<> inline __both__ int64_t open_range_upper<int64_t>()
219 { return int64_t(LONG_MAX); }
220
221
222 template<> inline __both__ uint16_t empty_bounds_lower<uint16_t>()
223 { return uint16_t(USHRT_MAX); }
224 template<> inline __both__ uint16_t empty_bounds_upper<uint16_t>()
225 { return uint16_t(0); }
226 template<> inline __both__ uint16_t open_range_lower<uint16_t>()
227 { return uint16_t(0); }
228 template<> inline __both__ uint16_t open_range_upper<uint16_t>()
229 { return uint16_t(USHRT_MAX); }
230
231 template<> inline __both__ int16_t empty_bounds_lower<int16_t>()
232 { return int16_t(SHRT_MAX); }
233 template<> inline __both__ int16_t empty_bounds_upper<int16_t>()
234 { return int16_t(SHRT_MIN); }
235 template<> inline __both__ int16_t open_range_lower<int16_t>()
236 { return int16_t(SHRT_MIN); }
237 template<> inline __both__ int16_t open_range_upper<int16_t>()
238 { return int16_t(SHRT_MAX); }
239
240 template<> inline __both__ uint8_t empty_bounds_lower<uint8_t>()
241 { return uint8_t(CHAR_MAX); }
242 template<> inline __both__ uint8_t empty_bounds_upper<uint8_t>()
243 { return uint8_t(CHAR_MIN); }
244 template<> inline __both__ uint8_t open_range_lower<uint8_t>()
245 { return uint8_t(CHAR_MIN); }
246 template<> inline __both__ uint8_t open_range_upper<uint8_t>()
247 { return uint8_t(CHAR_MAX); }
248
249 template<> inline __both__ int8_t empty_bounds_lower<int8_t>()
250 { return int8_t(SCHAR_MIN); }
251 template<> inline __both__ int8_t empty_bounds_upper<int8_t>()
252 { return int8_t(SCHAR_MAX); }
253 template<> inline __both__ int8_t open_range_lower<int8_t>()
254 { return int8_t(SCHAR_MAX); }
255 template<> inline __both__ int8_t open_range_upper<int8_t>()
256 { return int8_t(SCHAR_MIN); }
257
258 } // ::owl::common
259} // ::owl
Definition: constants.h:138