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