1 /////////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas 4 // Digital Ltd. LLC 5 // 6 // All rights reserved. 7 // 8 // Redistribution and use in source and binary forms, with or without 9 // modification, are permitted provided that the following conditions are 10 // met: 11 // * Redistributions of source code must retain the above copyright 12 // notice, this list of conditions and the following disclaimer. 13 // * Redistributions in binary form must reproduce the above 14 // copyright notice, this list of conditions and the following disclaimer 15 // in the documentation and/or other materials provided with the 16 // distribution. 17 // * Neither the name of Industrial Light & Magic nor the names of 18 // its contributors may be used to endorse or promote products derived 19 // from this software without specific prior written permission. 20 // 21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 // 33 /////////////////////////////////////////////////////////////////////////// 34 35 36 37 #ifndef INCLUDED_IMATHLIMITS_H 38 #define INCLUDED_IMATHLIMITS_H 39 40 //---------------------------------------------------------------- 41 // 42 // Limitations of the basic C++ numerical data types 43 // 44 //---------------------------------------------------------------- 45 46 #include <OpenEXR/ImathNamespace.h> 47 #include <float.h> 48 #include <limits.h> 49 50 //------------------------------------------ 51 // In Windows, min and max are macros. Yay. 52 //------------------------------------------ 53 54 #if defined _WIN32 || defined _WIN64 55 #ifdef min 56 #undef min 57 #endif 58 #ifdef max 59 #undef max 60 #endif 61 #endif 62 63 #ifndef IMATH_HOSTDEVICE 64 #ifdef __CUDACC__ 65 #define IMATH_HOSTDEVICE __host__ __device__ 66 #else 67 #define IMATH_HOSTDEVICE 68 #endif 69 #endif 70 71 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER 72 73 74 //----------------------------------------------------------------- 75 // 76 // Template class limits<T> returns information about the limits 77 // of numerical data type T: 78 // 79 // min() largest possible negative value of type T 80 // 81 // max() largest possible positive value of type T 82 // 83 // smallest() smallest possible positive value of type T 84 // (for float and double: smallest normalized 85 // positive value) 86 // 87 // epsilon() smallest possible e of type T, for which 88 // 1 + e != 1 89 // 90 // isIntegral() returns true if T is an integral type 91 // 92 // isSigned() returns true if T is signed 93 // 94 // Class limits<T> is useful to implement template classes or 95 // functions which depend on the limits of a numerical type 96 // which is not known in advance; for example: 97 // 98 // template <class T> max (T x[], int n) 99 // { 100 // T m = limits<T>::min(); 101 // 102 // for (int i = 0; i < n; i++) 103 // if (m < x[i]) 104 // m = x[i]; 105 // 106 // return m; 107 // } 108 // 109 // Class limits<T> has been implemented for the following types: 110 // 111 // char, signed char, unsigned char 112 // short, unsigned short 113 // int, unsigned int 114 // long, unsigned long 115 // float 116 // double 117 // long double 118 // 119 // Class limits<T> has only static member functions, all of which 120 // are implemented as inlines. No objects of type limits<T> are 121 // ever created. 122 // 123 //----------------------------------------------------------------- 124 125 126 template <class T> struct limits 127 { 128 IMATH_HOSTDEVICE static constexpr T min(); 129 IMATH_HOSTDEVICE static constexpr T max(); 130 IMATH_HOSTDEVICE static constexpr T smallest(); 131 IMATH_HOSTDEVICE static constexpr T epsilon(); 132 IMATH_HOSTDEVICE static constexpr bool isIntegral(); 133 IMATH_HOSTDEVICE static constexpr bool isSigned(); 134 }; 135 136 137 //--------------- 138 // Implementation 139 //--------------- 140 141 template <> 142 struct limits <char> 143 { 144 IMATH_HOSTDEVICE static constexpr char min() {return CHAR_MIN;} 145 IMATH_HOSTDEVICE static constexpr char max() {return CHAR_MAX;} 146 IMATH_HOSTDEVICE static constexpr char smallest() {return 1;} 147 IMATH_HOSTDEVICE static constexpr char epsilon() {return 1;} 148 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 149 IMATH_HOSTDEVICE static constexpr bool isSigned() {return (char) ~0 < 0;} 150 }; 151 152 template <> 153 struct limits <signed char> 154 { 155 IMATH_HOSTDEVICE static constexpr signed char min() {return SCHAR_MIN;} 156 IMATH_HOSTDEVICE static constexpr signed char max() {return SCHAR_MAX;} 157 IMATH_HOSTDEVICE static constexpr signed char smallest() {return 1;} 158 IMATH_HOSTDEVICE static constexpr signed char epsilon() {return 1;} 159 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 160 IMATH_HOSTDEVICE static constexpr bool isSigned() {return true;} 161 }; 162 163 template <> 164 struct limits <unsigned char> 165 { 166 IMATH_HOSTDEVICE static constexpr unsigned char min() {return 0;} 167 IMATH_HOSTDEVICE static constexpr unsigned char max() {return UCHAR_MAX;} 168 IMATH_HOSTDEVICE static constexpr unsigned char smallest() {return 1;} 169 IMATH_HOSTDEVICE static constexpr unsigned char epsilon() {return 1;} 170 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 171 IMATH_HOSTDEVICE static constexpr bool isSigned() {return false;} 172 }; 173 174 template <> 175 struct limits <short> 176 { 177 IMATH_HOSTDEVICE static constexpr short min() {return SHRT_MIN;} 178 IMATH_HOSTDEVICE static constexpr short max() {return SHRT_MAX;} 179 IMATH_HOSTDEVICE static constexpr short smallest() {return 1;} 180 IMATH_HOSTDEVICE static constexpr short epsilon() {return 1;} 181 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 182 IMATH_HOSTDEVICE static constexpr bool isSigned() {return true;} 183 }; 184 185 template <> 186 struct limits <unsigned short> 187 { 188 IMATH_HOSTDEVICE static constexpr unsigned short min() {return 0;} 189 IMATH_HOSTDEVICE static constexpr unsigned short max() {return USHRT_MAX;} 190 IMATH_HOSTDEVICE static constexpr unsigned short smallest() {return 1;} 191 IMATH_HOSTDEVICE static constexpr unsigned short epsilon() {return 1;} 192 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 193 IMATH_HOSTDEVICE static constexpr bool isSigned() {return false;} 194 }; 195 196 template <> 197 struct limits <int> 198 { 199 IMATH_HOSTDEVICE static constexpr int min() {return INT_MIN;} 200 IMATH_HOSTDEVICE static constexpr int max() {return INT_MAX;} 201 IMATH_HOSTDEVICE static constexpr int smallest() {return 1;} 202 IMATH_HOSTDEVICE static constexpr int epsilon() {return 1;} 203 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 204 IMATH_HOSTDEVICE static constexpr bool isSigned() {return true;} 205 }; 206 207 template <> 208 struct limits <unsigned int> 209 { 210 IMATH_HOSTDEVICE static constexpr unsigned int min() {return 0;} 211 IMATH_HOSTDEVICE static constexpr unsigned int max() {return UINT_MAX;} 212 IMATH_HOSTDEVICE static constexpr unsigned int smallest() {return 1;} 213 IMATH_HOSTDEVICE static constexpr unsigned int epsilon() {return 1;} 214 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 215 IMATH_HOSTDEVICE static constexpr bool isSigned() {return false;} 216 }; 217 218 template <> 219 struct limits <long> 220 { 221 IMATH_HOSTDEVICE static constexpr long min() {return LONG_MIN;} 222 IMATH_HOSTDEVICE static constexpr long max() {return LONG_MAX;} 223 IMATH_HOSTDEVICE static constexpr long smallest() {return 1;} 224 IMATH_HOSTDEVICE static constexpr long epsilon() {return 1;} 225 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 226 IMATH_HOSTDEVICE static constexpr bool isSigned() {return true;} 227 }; 228 229 template <> 230 struct limits <unsigned long> 231 { 232 IMATH_HOSTDEVICE static constexpr unsigned long min() {return 0;} 233 IMATH_HOSTDEVICE static constexpr unsigned long max() {return ULONG_MAX;} 234 IMATH_HOSTDEVICE static constexpr unsigned long smallest() {return 1;} 235 IMATH_HOSTDEVICE static constexpr unsigned long epsilon() {return 1;} 236 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return true;} 237 IMATH_HOSTDEVICE static constexpr bool isSigned() {return false;} 238 }; 239 240 template <> 241 struct limits <float> 242 { 243 IMATH_HOSTDEVICE static constexpr float min() {return -FLT_MAX;} 244 IMATH_HOSTDEVICE static constexpr float max() {return FLT_MAX;} 245 IMATH_HOSTDEVICE static constexpr float smallest() {return FLT_MIN;} 246 IMATH_HOSTDEVICE static constexpr float epsilon() {return FLT_EPSILON;} 247 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return false;} 248 IMATH_HOSTDEVICE static constexpr bool isSigned() {return true;} 249 }; 250 251 template <> 252 struct limits <double> 253 { 254 IMATH_HOSTDEVICE static constexpr double min() {return -DBL_MAX;} 255 IMATH_HOSTDEVICE static constexpr double max() {return DBL_MAX;} 256 IMATH_HOSTDEVICE static constexpr double smallest() {return DBL_MIN;} 257 IMATH_HOSTDEVICE static constexpr double epsilon() {return DBL_EPSILON;} 258 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return false;} 259 IMATH_HOSTDEVICE static constexpr bool isSigned() {return true;} 260 }; 261 262 template <> 263 struct limits <long double> 264 { 265 IMATH_HOSTDEVICE static constexpr long double min() {return -LDBL_MAX;} 266 IMATH_HOSTDEVICE static constexpr long double max() {return LDBL_MAX;} 267 IMATH_HOSTDEVICE static constexpr long double smallest() {return LDBL_MIN;} 268 IMATH_HOSTDEVICE static constexpr long double epsilon() {return LDBL_EPSILON;} 269 IMATH_HOSTDEVICE static constexpr bool isIntegral() {return false;} 270 IMATH_HOSTDEVICE static constexpr bool isSigned() {return true;} 271 }; 272 273 274 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT 275 276 #endif // INCLUDED_IMATHLIMITS_H 277