| 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 "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 | IMATH_INTERNAL_NAMESPACE_HEADER_ENTER |
| 64 | |
| 65 | |
| 66 | //----------------------------------------------------------------- |
| 67 | // |
| 68 | // Template class limits<T> returns information about the limits |
| 69 | // of numerical data type T: |
| 70 | // |
| 71 | // min() largest possible negative value of type T |
| 72 | // |
| 73 | // max() largest possible positive value of type T |
| 74 | // |
| 75 | // smallest() smallest possible positive value of type T |
| 76 | // (for float and double: smallest normalized |
| 77 | // positive value) |
| 78 | // |
| 79 | // epsilon() smallest possible e of type T, for which |
| 80 | // 1 + e != 1 |
| 81 | // |
| 82 | // isIntegral() returns true if T is an integral type |
| 83 | // |
| 84 | // isSigned() returns true if T is signed |
| 85 | // |
| 86 | // Class limits<T> is useful to implement template classes or |
| 87 | // functions which depend on the limits of a numerical type |
| 88 | // which is not known in advance; for example: |
| 89 | // |
| 90 | // template <class T> max (T x[], int n) |
| 91 | // { |
| 92 | // T m = limits<T>::min(); |
| 93 | // |
| 94 | // for (int i = 0; i < n; i++) |
| 95 | // if (m < x[i]) |
| 96 | // m = x[i]; |
| 97 | // |
| 98 | // return m; |
| 99 | // } |
| 100 | // |
| 101 | // Class limits<T> has been implemented for the following types: |
| 102 | // |
| 103 | // char, signed char, unsigned char |
| 104 | // short, unsigned short |
| 105 | // int, unsigned int |
| 106 | // long, unsigned long |
| 107 | // float |
| 108 | // double |
| 109 | // long double |
| 110 | // |
| 111 | // Class limits<T> has only static member functions, all of which |
| 112 | // are implemented as inlines. No objects of type limits<T> are |
| 113 | // ever created. |
| 114 | // |
| 115 | //----------------------------------------------------------------- |
| 116 | |
| 117 | |
| 118 | template <class T> struct limits |
| 119 | { |
| 120 | static T min(); |
| 121 | static T max(); |
| 122 | static T smallest(); |
| 123 | static T epsilon(); |
| 124 | static bool isIntegral(); |
| 125 | static bool isSigned(); |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | //--------------- |
| 130 | // Implementation |
| 131 | //--------------- |
| 132 | |
| 133 | template <> |
| 134 | struct limits <char> |
| 135 | { |
| 136 | static char min() {return CHAR_MIN;} |
| 137 | static char max() {return CHAR_MAX;} |
| 138 | static char smallest() {return 1;} |
| 139 | static char epsilon() {return 1;} |
| 140 | static bool isIntegral() {return true;} |
| 141 | static bool isSigned() {return (char) ~0 < 0;} |
| 142 | }; |
| 143 | |
| 144 | template <> |
| 145 | struct limits <signed char> |
| 146 | { |
| 147 | static signed char min() {return SCHAR_MIN;} |
| 148 | static signed char max() {return SCHAR_MAX;} |
| 149 | static signed char smallest() {return 1;} |
| 150 | static signed char epsilon() {return 1;} |
| 151 | static bool isIntegral() {return true;} |
| 152 | static bool isSigned() {return true;} |
| 153 | }; |
| 154 | |
| 155 | template <> |
| 156 | struct limits <unsigned char> |
| 157 | { |
| 158 | static unsigned char min() {return 0;} |
| 159 | static unsigned char max() {return UCHAR_MAX;} |
| 160 | static unsigned char smallest() {return 1;} |
| 161 | static unsigned char epsilon() {return 1;} |
| 162 | static bool isIntegral() {return true;} |
| 163 | static bool isSigned() {return false;} |
| 164 | }; |
| 165 | |
| 166 | template <> |
| 167 | struct limits <short> |
| 168 | { |
| 169 | static short min() {return SHRT_MIN;} |
| 170 | static short max() {return SHRT_MAX;} |
| 171 | static short smallest() {return 1;} |
| 172 | static short epsilon() {return 1;} |
| 173 | static bool isIntegral() {return true;} |
| 174 | static bool isSigned() {return true;} |
| 175 | }; |
| 176 | |
| 177 | template <> |
| 178 | struct limits <unsigned short> |
| 179 | { |
| 180 | static unsigned short min() {return 0;} |
| 181 | static unsigned short max() {return USHRT_MAX;} |
| 182 | static unsigned short smallest() {return 1;} |
| 183 | static unsigned short epsilon() {return 1;} |
| 184 | static bool isIntegral() {return true;} |
| 185 | static bool isSigned() {return false;} |
| 186 | }; |
| 187 | |
| 188 | template <> |
| 189 | struct limits <int> |
| 190 | { |
| 191 | static int min() {return INT_MIN;} |
| 192 | static int max() {return INT_MAX;} |
| 193 | static int smallest() {return 1;} |
| 194 | static int epsilon() {return 1;} |
| 195 | static bool isIntegral() {return true;} |
| 196 | static bool isSigned() {return true;} |
| 197 | }; |
| 198 | |
| 199 | template <> |
| 200 | struct limits <unsigned int> |
| 201 | { |
| 202 | static unsigned int min() {return 0;} |
| 203 | static unsigned int max() {return UINT_MAX;} |
| 204 | static unsigned int smallest() {return 1;} |
| 205 | static unsigned int epsilon() {return 1;} |
| 206 | static bool isIntegral() {return true;} |
| 207 | static bool isSigned() {return false;} |
| 208 | }; |
| 209 | |
| 210 | template <> |
| 211 | struct limits <long> |
| 212 | { |
| 213 | static long min() {return LONG_MIN;} |
| 214 | static long max() {return LONG_MAX;} |
| 215 | static long smallest() {return 1;} |
| 216 | static long epsilon() {return 1;} |
| 217 | static bool isIntegral() {return true;} |
| 218 | static bool isSigned() {return true;} |
| 219 | }; |
| 220 | |
| 221 | template <> |
| 222 | struct limits <unsigned long> |
| 223 | { |
| 224 | static unsigned long min() {return 0;} |
| 225 | static unsigned long max() {return ULONG_MAX;} |
| 226 | static unsigned long smallest() {return 1;} |
| 227 | static unsigned long epsilon() {return 1;} |
| 228 | static bool isIntegral() {return true;} |
| 229 | static bool isSigned() {return false;} |
| 230 | }; |
| 231 | |
| 232 | template <> |
| 233 | struct limits <float> |
| 234 | { |
| 235 | static float min() {return -FLT_MAX;} |
| 236 | static float max() {return FLT_MAX;} |
| 237 | static float smallest() {return FLT_MIN;} |
| 238 | static float epsilon() {return FLT_EPSILON;} |
| 239 | static bool isIntegral() {return false;} |
| 240 | static bool isSigned() {return true;} |
| 241 | }; |
| 242 | |
| 243 | template <> |
| 244 | struct limits <double> |
| 245 | { |
| 246 | static double min() {return -DBL_MAX;} |
| 247 | static double max() {return DBL_MAX;} |
| 248 | static double smallest() {return DBL_MIN;} |
| 249 | static double epsilon() {return DBL_EPSILON;} |
| 250 | static bool isIntegral() {return false;} |
| 251 | static bool isSigned() {return true;} |
| 252 | }; |
| 253 | |
| 254 | template <> |
| 255 | struct limits <long double> |
| 256 | { |
| 257 | static long double min() {return -LDBL_MAX;} |
| 258 | static long double max() {return LDBL_MAX;} |
| 259 | static long double smallest() {return LDBL_MIN;} |
| 260 | static long double epsilon() {return LDBL_EPSILON;} |
| 261 | static bool isIntegral() {return false;} |
| 262 | static bool isSigned() {return true;} |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | IMATH_INTERNAL_NAMESPACE_HEADER_EXIT |
| 267 | |
| 268 | #endif // INCLUDED_IMATHLIMITS_H |
| 269 | |