Path: blob/master/3rdparty/openexr/Imath/ImathLimits.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMATHLIMITS_H37#define INCLUDED_IMATHLIMITS_H3839//----------------------------------------------------------------40//41// Limitations of the basic C++ numerical data types42//43//----------------------------------------------------------------4445#include <float.h>46#include <limits.h>4748//------------------------------------------49// In Windows, min and max are macros. Yay.50//------------------------------------------5152#if defined _WIN32 || defined _WIN6453#ifdef min54#undef min55#endif56#ifdef max57#undef max58#endif59#endif6061namespace Imath {626364//-----------------------------------------------------------------65//66// Template class limits<T> returns information about the limits67// of numerical data type T:68//69// min() largest possible negative value of type T70//71// max() largest possible positive value of type T72//73// smallest() smallest possible positive value of type T74// (for float and double: smallest normalized75// positive value)76//77// epsilon() smallest possible e of type T, for which78// 1 + e != 179//80// isIntegral() returns true if T is an integral type81//82// isSigned() returns true if T is signed83//84// Class limits<T> is useful to implement template classes or85// functions which depend on the limits of a numerical type86// which is not known in advance; for example:87//88// template <class T> max (T x[], int n)89// {90// T m = limits<T>::min();91//92// for (int i = 0; i < n; i++)93// if (m < x[i])94// m = x[i];95//96// return m;97// }98//99// Class limits<T> has been implemented for the following types:100//101// char, signed char, unsigned char102// short, unsigned short103// int, unsigned int104// long, unsigned long105// float106// double107// long double108//109// Class limits<T> has only static member functions, all of which110// are implemented as inlines. No objects of type limits<T> are111// ever created.112//113//-----------------------------------------------------------------114115116template <class T> struct limits117{118static T min();119static T max();120static T smallest();121static T epsilon();122static bool isIntegral();123static bool isSigned();124};125126127//---------------128// Implementation129//---------------130131template <>132struct limits <char>133{134static char min() {return CHAR_MIN;}135static char max() {return CHAR_MAX;}136static char smallest() {return 1;}137static char epsilon() {return 1;}138static bool isIntegral() {return true;}139static bool isSigned() {return (char) ~0 < 0;}140};141142template <>143struct limits <signed char>144{145static signed char min() {return SCHAR_MIN;}146static signed char max() {return SCHAR_MAX;}147static signed char smallest() {return 1;}148static signed char epsilon() {return 1;}149static bool isIntegral() {return true;}150static bool isSigned() {return true;}151};152153template <>154struct limits <unsigned char>155{156static unsigned char min() {return 0;}157static unsigned char max() {return UCHAR_MAX;}158static unsigned char smallest() {return 1;}159static unsigned char epsilon() {return 1;}160static bool isIntegral() {return true;}161static bool isSigned() {return false;}162};163164template <>165struct limits <short>166{167static short min() {return SHRT_MIN;}168static short max() {return SHRT_MAX;}169static short smallest() {return 1;}170static short epsilon() {return 1;}171static bool isIntegral() {return true;}172static bool isSigned() {return true;}173};174175template <>176struct limits <unsigned short>177{178static unsigned short min() {return 0;}179static unsigned short max() {return USHRT_MAX;}180static unsigned short smallest() {return 1;}181static unsigned short epsilon() {return 1;}182static bool isIntegral() {return true;}183static bool isSigned() {return false;}184};185186template <>187struct limits <int>188{189static int min() {return INT_MIN;}190static int max() {return INT_MAX;}191static int smallest() {return 1;}192static int epsilon() {return 1;}193static bool isIntegral() {return true;}194static bool isSigned() {return true;}195};196197template <>198struct limits <unsigned int>199{200static unsigned int min() {return 0;}201static unsigned int max() {return UINT_MAX;}202static unsigned int smallest() {return 1;}203static unsigned int epsilon() {return 1;}204static bool isIntegral() {return true;}205static bool isSigned() {return false;}206};207208template <>209struct limits <long>210{211static long min() {return LONG_MIN;}212static long max() {return LONG_MAX;}213static long smallest() {return 1;}214static long epsilon() {return 1;}215static bool isIntegral() {return true;}216static bool isSigned() {return true;}217};218219template <>220struct limits <unsigned long>221{222static unsigned long min() {return 0;}223static unsigned long max() {return ULONG_MAX;}224static unsigned long smallest() {return 1;}225static unsigned long epsilon() {return 1;}226static bool isIntegral() {return true;}227static bool isSigned() {return false;}228};229230template <>231struct limits <float>232{233static float min() {return -FLT_MAX;}234static float max() {return FLT_MAX;}235static float smallest() {return FLT_MIN;}236static float epsilon() {return FLT_EPSILON;}237static bool isIntegral() {return false;}238static bool isSigned() {return true;}239};240241template <>242struct limits <double>243{244static double min() {return -DBL_MAX;}245static double max() {return DBL_MAX;}246static double smallest() {return DBL_MIN;}247static double epsilon() {return DBL_EPSILON;}248static bool isIntegral() {return false;}249static bool isSigned() {return true;}250};251252template <>253struct limits <long double>254{255static long double min() {return -LDBL_MAX;}256static long double max() {return LDBL_MAX;}257static long double smallest() {return LDBL_MIN;}258static long double epsilon() {return LDBL_EPSILON;}259static bool isIntegral() {return false;}260static bool isSigned() {return true;}261};262263264} // namespace Imath265266#endif267268269