/src/FreeImage/Source/OpenEXR/Imath/ImathLimits.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 267 lines · 144 code · 29 blank · 94 comment · 1 complexity · ddfc0cc0dfee8f7be1178a3e4115c7b5 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, 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. #ifndef INCLUDED_IMATHLIMITS_H
  35. #define INCLUDED_IMATHLIMITS_H
  36. //----------------------------------------------------------------
  37. //
  38. // Limitations of the basic C++ numerical data types
  39. //
  40. //----------------------------------------------------------------
  41. #include <float.h>
  42. #include <limits.h>
  43. //------------------------------------------
  44. // In Windows, min and max are macros. Yay.
  45. //------------------------------------------
  46. #if defined _WIN32 || defined _WIN64
  47. #ifdef min
  48. #undef min
  49. #endif
  50. #ifdef max
  51. #undef max
  52. #endif
  53. #endif
  54. namespace Imath {
  55. //-----------------------------------------------------------------
  56. //
  57. // Template class limits<T> returns information about the limits
  58. // of numerical data type T:
  59. //
  60. // min() largest possible negative value of type T
  61. //
  62. // max() largest possible positive value of type T
  63. //
  64. // smallest() smallest possible positive value of type T
  65. // (for float and double: smallest normalized
  66. // positive value)
  67. //
  68. // epsilon() smallest possible e of type T, for which
  69. // 1 + e != 1
  70. //
  71. // isIntegral() returns true if T is an integral type
  72. //
  73. // isSigned() returns true if T is signed
  74. //
  75. // Class limits<T> is useful to implement template classes or
  76. // functions which depend on the limits of a numerical type
  77. // which is not known in advance; for example:
  78. //
  79. // template <class T> max (T x[], int n)
  80. // {
  81. // T m = limits<T>::min();
  82. //
  83. // for (int i = 0; i < n; i++)
  84. // if (m < x[i])
  85. // m = x[i];
  86. //
  87. // return m;
  88. // }
  89. //
  90. // Class limits<T> has been implemented for the following types:
  91. //
  92. // char, signed char, unsigned char
  93. // short, unsigned short
  94. // int, unsigned int
  95. // long, unsigned long
  96. // float
  97. // double
  98. // long double
  99. //
  100. // Class limits<T> has only static member functions, all of which
  101. // are implemented as inlines. No objects of type limits<T> are
  102. // ever created.
  103. //
  104. //-----------------------------------------------------------------
  105. template <class T> struct limits
  106. {
  107. static T min();
  108. static T max();
  109. static T smallest();
  110. static T epsilon();
  111. static bool isIntegral();
  112. static bool isSigned();
  113. };
  114. //---------------
  115. // Implementation
  116. //---------------
  117. template <>
  118. struct limits <char>
  119. {
  120. static char min() {return CHAR_MIN;}
  121. static char max() {return CHAR_MAX;}
  122. static char smallest() {return 1;}
  123. static char epsilon() {return 1;}
  124. static bool isIntegral() {return true;}
  125. static bool isSigned() {return (char) ~0 < 0;}
  126. };
  127. template <>
  128. struct limits <signed char>
  129. {
  130. static signed char min() {return SCHAR_MIN;}
  131. static signed char max() {return SCHAR_MAX;}
  132. static signed char smallest() {return 1;}
  133. static signed char epsilon() {return 1;}
  134. static bool isIntegral() {return true;}
  135. static bool isSigned() {return true;}
  136. };
  137. template <>
  138. struct limits <unsigned char>
  139. {
  140. static unsigned char min() {return 0;}
  141. static unsigned char max() {return UCHAR_MAX;}
  142. static unsigned char smallest() {return 1;}
  143. static unsigned char epsilon() {return 1;}
  144. static bool isIntegral() {return true;}
  145. static bool isSigned() {return false;}
  146. };
  147. template <>
  148. struct limits <short>
  149. {
  150. static short min() {return SHRT_MIN;}
  151. static short max() {return SHRT_MAX;}
  152. static short smallest() {return 1;}
  153. static short epsilon() {return 1;}
  154. static bool isIntegral() {return true;}
  155. static bool isSigned() {return true;}
  156. };
  157. template <>
  158. struct limits <unsigned short>
  159. {
  160. static unsigned short min() {return 0;}
  161. static unsigned short max() {return USHRT_MAX;}
  162. static unsigned short smallest() {return 1;}
  163. static unsigned short epsilon() {return 1;}
  164. static bool isIntegral() {return true;}
  165. static bool isSigned() {return false;}
  166. };
  167. template <>
  168. struct limits <int>
  169. {
  170. static int min() {return INT_MIN;}
  171. static int max() {return INT_MAX;}
  172. static int smallest() {return 1;}
  173. static int epsilon() {return 1;}
  174. static bool isIntegral() {return true;}
  175. static bool isSigned() {return true;}
  176. };
  177. template <>
  178. struct limits <unsigned int>
  179. {
  180. static unsigned int min() {return 0;}
  181. static unsigned int max() {return UINT_MAX;}
  182. static unsigned int smallest() {return 1;}
  183. static unsigned int epsilon() {return 1;}
  184. static bool isIntegral() {return true;}
  185. static bool isSigned() {return false;}
  186. };
  187. template <>
  188. struct limits <long>
  189. {
  190. static long min() {return LONG_MIN;}
  191. static long max() {return LONG_MAX;}
  192. static long smallest() {return 1;}
  193. static long epsilon() {return 1;}
  194. static bool isIntegral() {return true;}
  195. static bool isSigned() {return true;}
  196. };
  197. template <>
  198. struct limits <unsigned long>
  199. {
  200. static unsigned long min() {return 0;}
  201. static unsigned long max() {return ULONG_MAX;}
  202. static unsigned long smallest() {return 1;}
  203. static unsigned long epsilon() {return 1;}
  204. static bool isIntegral() {return true;}
  205. static bool isSigned() {return false;}
  206. };
  207. template <>
  208. struct limits <float>
  209. {
  210. static float min() {return -FLT_MAX;}
  211. static float max() {return FLT_MAX;}
  212. static float smallest() {return FLT_MIN;}
  213. static float epsilon() {return FLT_EPSILON;}
  214. static bool isIntegral() {return false;}
  215. static bool isSigned() {return true;}
  216. };
  217. template <>
  218. struct limits <double>
  219. {
  220. static double min() {return -DBL_MAX;}
  221. static double max() {return DBL_MAX;}
  222. static double smallest() {return DBL_MIN;}
  223. static double epsilon() {return DBL_EPSILON;}
  224. static bool isIntegral() {return false;}
  225. static bool isSigned() {return true;}
  226. };
  227. template <>
  228. struct limits <long double>
  229. {
  230. static long double min() {return -LDBL_MAX;}
  231. static long double max() {return LDBL_MAX;}
  232. static long double smallest() {return LDBL_MIN;}
  233. static long double epsilon() {return LDBL_EPSILON;}
  234. static bool isIntegral() {return false;}
  235. static bool isSigned() {return true;}
  236. };
  237. } // namespace Imath
  238. #endif