HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SYS_Types.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SYS_Types.h ( SYS Library, C++)
7  *
8  * COMMENTS: Common types used throughout Houdini.
9  */
10 
11 #ifndef __SYS_Types__
12 #define __SYS_Types__
13 
14 #include "SYS_Compiler.h"
15 #include "SYS_Inline.h"
16 
17 /* Include system types */
18 #include <limits> // for std::numeric_limits
19 #include <float.h> // DBL_MAX etc
20 
21 #include <sys/types.h>
22 #ifdef _MSC_VER
23 using pid_t = int; // this is missing from Visual C++'s <sys/types.h>
24 #endif
25 
26 // <sys/types.h> defines macros with the names 'major' and 'minor'.
27 // glibc 2.25+ emits deprecation warnings for this, so eventually the include
28 // of <sys/sysmacros.h> will be removed and the undef'ing won't be necessary.
29 #undef major
30 #undef minor
31 
32 /*
33  * Integer types
34  */
35 using int8 = signed char;
36 using uint8 = unsigned char;
37 using int16 = short;
38 using uint16 = unsigned short;
39 using int32 = int;
40 using uint32 = unsigned int;
41 
42 using uchar = unsigned char;
43 
44 #ifndef MBSD
45 using uint = unsigned int;
46 #endif
47 
48 /*
49  * Unicode code units for Unicode Transformation Formats
50  * Houdini uses UTF8.
51  */
52 using utf8 = char;
53 #ifdef _WIN32
54 using utf16 = wchar_t;
55 #else
56 using utf16 = unsigned short;
57 #endif
58 using utf32 = unsigned int;
59 
60 
61 /*
62  * The SYS_PRI?64 is to mimic the C99 PRI?64 macro.
63  * printf("%" SYS_PRId64, (int64) value);
64  */
65 #if defined(_MSC_VER)
66  #define SYS_PRI64_PREFIX "I64"
67 #elif defined(MBSD)
68  #define SYS_PRI64_PREFIX "ll"
69 #elif defined(AMD64) || defined(ARM64)
70  #define SYS_PRI64_PREFIX "l"
71 #else
72  #define SYS_PRI64_PREFIX "ll"
73 #endif
74 
75 #if defined(SYS_PRI64_PREFIX)
76 #define SYS_PRId64 SYS_PRI64_PREFIX "d"
77 #define SYS_PRIu64 SYS_PRI64_PREFIX "u"
78 #define SYS_PRIx64 SYS_PRI64_PREFIX "x"
79 #define SYS_PRIX64 SYS_PRI64_PREFIX "X"
80 #endif
81 
82 #if SIZEOF_VOID_P == 8
83  #if defined(MBSD)
84  #include <inttypes.h>
85  #define SYS_PRIiPTR PRIiPTR
86  #define SYS_PRIuPTR PRIuPTR
87  #else
88  #define SYS_PRIiPTR SYS_PRId64
89  #define SYS_PRIuPTR SYS_PRIu64
90  #endif
91 #elif SIZEOF_VOID_P == 4
92  #define SYS_PRIiPTR "d"
93  #define SYS_PRIuPTR "u"
94 #else
95  #error Unknown SIZEOF_VOID_P
96 #endif
97 
98 /*
99  * Avoid using uint64.
100  * The extra bit of precision is NOT worth the cost in pain and suffering
101  * induced by use of unsigned.
102  */
103 #if defined(WIN32)
104  using int64 = __int64;
105  using uint64 = unsigned __int64;
106 #elif defined(MBSD)
107  // On MBSD, int64/uint64 are also defined in the system headers so we must
108  // declare these in the same way or else we get conflicts.
109  #include <stdint.h>
110  using int64 = int64_t;
111  using uint64 = uint64_t;
112 #elif defined(AMD64) || defined(ARM64)
113  using int64 = long;
114  using uint64 = unsigned long;
115 #else
116  using int64 = long long;
117  using uint64 = unsigned long long;
118 #endif
119 
120 /*
121  * The problem with int64 is that it implies that it is a fixed 64-bit quantity
122  * that is saved to disk. Therefore, we need another integral type for
123  * indexing our arrays.
124  */
125 using exint = int64;
126 
127 /*
128  * When casting away const, it is good to use const_cast as it tells
129  * the reader what you are doing (as opposed to C-style cast)
130  * However, it requires redundant information that breaks cut-and-paste
131  * code. So this template lets you cast away const without worrying
132  * about the underlying type.
133  */
134 template <typename T>
136 SYSconst_cast(const T *foo)
137 {
138  return const_cast<T *>(foo);
139 }
140 
141 template <typename T>
143 SYSconst_cast(const T &foo)
144 {
145  return const_cast<T &>(foo);
146 }
147 
148 /*
149  * Integer limits
150  */
151 #if defined(GCC3) && defined(AMD64)
152 #define SYS_INT64_C(x) x ## L
153 #define SYS_UINT64_C(x) x ## UL
154 #else
155 #define SYS_INT64_C(x) x ## LL
156 #define SYS_UINT64_C(x) x ## ULL
157 #endif
158 
159 // int8/uint8
160 #define SYS_INT8_MIN std::numeric_limits<int8>::min()
161 #define SYS_INT8_MAX std::numeric_limits<int8>::max()
162 #define SYS_UINT8_MAX std::numeric_limits<uint8>::max()
163 
164 // int16/uint16
165 #define SYS_INT16_MIN std::numeric_limits<int16>::min()
166 #define SYS_INT16_MAX std::numeric_limits<int16>::max()
167 #define SYS_UINT16_MAX std::numeric_limits<uint16>::max()
168 
169 // int32/uint32
170 #define SYS_INT32_MIN std::numeric_limits<int32>::min()
171 #define SYS_INT32_MAX std::numeric_limits<int32>::max()
172 #define SYS_UINT32_MAX std::numeric_limits<uint32>::max()
173 
174 // int64/uint64
175 #define SYS_INT64_MIN std::numeric_limits<int64>::min()
176 #define SYS_INT64_MAX std::numeric_limits<int64>::max()
177 #define SYS_UINT64_MAX std::numeric_limits<uint64>::max()
178 
179 // exint
180 #define SYS_EXINT_MIN SYS_INT64_MIN
181 #define SYS_EXINT_MAX SYS_INT64_MAX
182 
183 
184 /*
185  * These are the precision you should use for writing out
186  * streams to guarantee that you fully save a double or a float.
187  * Note that limits.h defines DBL_DIG and FLT_DIG - they are
188  * defined incorrectly!
189  */
190 #define SYS_DBL_DIG 17
191 #define SYS_FLT_DIG 9
192 
193 /*
194  * Floating Point Types
195  * By specifying the number of bits (i.e. fpreal32), the precision is
196  * automatically determined. The fpreal type defaults to the precision for the
197  * Houdini release and may change accordingly. fpreal should be used unless
198  * there are specific reasons for requiring a certain precision.
199  */
200 using fpreal32 = float;
201 using fpreal64 = double;
202 
203 /* Include half-precision floats after other types are defined */
204 #include "fpreal16.h"
205 
206 /* Floating Point Limits/Tolerance */
207 
208 #define SYS_FTOLERANCE ((fpreal32)0.00001)
209 #define SYS_FTOLERANCE_D (fpreal64(SYS_FTOLERANCE))
210 
211 // The difference between 1 and the least value greater than 1 that can be
212 // represented.
213 #define SYS_FP32_EPSILON std::numeric_limits<fpreal32>::epsilon()
214 #define SYS_FP64_EPSILON std::numeric_limits<fpreal64>::epsilon()
215 #define SYS_FP_EPSILON std::numeric_limits<fpreal>::epsilon()
216 
217 #define SYS_FP16_MIN ((fpreal16)H_REAL16_MIN)
218 #define SYS_FP32_MIN std::numeric_limits<fpreal32>::denorm_min()
219 #define SYS_FP64_MIN std::numeric_limits<fpreal64>::denorm_min()
220 #define SYS_FP16_MAX ((fpreal16)H_REAL16_MAX)
221 #define SYS_FP32_MAX std::numeric_limits<fpreal32>::max()
222 #define SYS_FP64_MAX std::numeric_limits<fpreal64>::max()
223 
224 // Tolerance values we can use for templates. This can surely be expanded
225 template<typename T>
226 struct SYS_Types : public std::numeric_limits<T>
227 {
228  static T tolerance() { return static_cast<T>(0); }
229 };
230 template<>
231 struct SYS_Types<int32> : public std::numeric_limits<int32>
232 {
233  static int32 tolerance() { return 0; }
234 };
235 template<>
236 struct SYS_Types<int64> : public std::numeric_limits<int64>
237 {
238  static int64 tolerance() { return 0; }
239 };
240 template<>
241 struct SYS_Types<fpreal32> : public std::numeric_limits<fpreal32>
242 {
243  static fpreal32 tolerance() { return SYS_FTOLERANCE; }
244 };
245 template<>
246 struct SYS_Types<fpreal64> : public std::numeric_limits<fpreal64>
247 {
248  static fpreal64 tolerance() { return SYS_FTOLERANCE_D; }
249 };
250 
251 
252 /*
253  * fpreal definition
254  *
255  * Default to double unless SIZEOF_FPREAL_IS_4 is defined as 1
256  */
257 #if SIZEOF_FPREAL_IS_4
258  /*
259  * Single precision fpreal
260  */
261  using fpreal = fpreal32;
262 
263  // sizeof(fpreal)
264  #define SYS_SIZEOF_FPREAL 4
265 
266  #define SYS_FPREAL_DIG SYS_FLT_DIG
267  #define SYS_FTOLERANCE_R (fpreal(SYS_FTOLERANCE))
268  #define SYS_FPEPSILON SYS_FP32_EPSILON
269  #define SYS_FPREAL_MIN SYS_FP32_MIN
270  #define SYS_FPREAL_MAX SYS_FP32_MAX
271 
272  // The scan code to use in scanf() for fpreal
273  #define SYS_SCANF_FPREAL "g"
274 #else
275  /*
276  * Double precision fpreal
277  */
278  using fpreal = fpreal64;
279 
280  // sizeof(fpreal)
281  #define SYS_SIZEOF_FPREAL 8
282 
283  #define SYS_FPREAL_DIG SYS_DBL_DIG
284  #define SYS_FTOLERANCE_R (fpreal(SYS_FTOLERANCE))
285  #define SYS_FPEPSILON SYS_FP64_EPSILON
286  #define SYS_FPREAL_MIN SYS_FP64_MIN
287  #define SYS_FPREAL_MAX SYS_FP64_MAX
288 
289  // The scan code to use in scanf() for fpreal
290  #define SYS_SCANF_FPREAL "lg"
291 #endif
292 /* need an extra level to ensure complete macro expansion before stringize */
293 #define SYS_DIG_FMT_INTERNAL(PREC) "%." #PREC "g"
294 #define SYS_DIG_FMT(PREC) SYS_DIG_FMT_INTERNAL(PREC)
295  /* float precision */
296 #define SYS_FLT_DIG_FMT SYS_DIG_FMT(SYS_FLT_DIG)
297  /* double precision */
298 #define SYS_DBL_DIG_FMT SYS_DIG_FMT(SYS_DBL_DIG)
299  /* fpreal precision */
300 #define SYS_FPREAL_DIG_FMT SYS_DIG_FMT(SYS_FPREAL_DIG)
301 
302 /*
303  * When declaring floating point constants, please use the following macros.
304  * If a floating point expression contains a double precision constant, all
305  * arguments will be promoted to double precision then the result cast back to
306  * single precision. To avoid the cost of conversions, please use the correct
307  * types. Try to avoid using 180.0F.
308  */
309 
310 #define CONST_INT8(x) ((int8)x)
311 #define CONST_UINT8(x) ((uint8)x)
312 #define CONST_INT16(x) ((int16)x)
313 #define CONST_UINT16(x) ((uint16)x)
314 #define CONST_INT32(x) ((int32)x)
315 #define CONST_UINT32(x) ((uint32)x)
316 
317 #if defined(WIN32)
318  #define CONST_INT64(x) ((int64)x)
319  #define CONST_UINT64(x) ((uint64)x)
320 #else
321  #define CONST_INT64(x) (x##LL)
322  #define CONST_UINT64(x) (x##LL)
323 #endif
324 
325 #define CONST_FPREAL16(c) ((fpreal16)c)
326 #define CONST_FPREAL32(c) ((fpreal32)c)
327 #define CONST_FPREAL64(c) ((fpreal64)c)
328 #define CONST_FPREAL(c) ((fpreal)c)
329 
330 ///
331 /// SYS_FPRealUnionT for type-safe casting with integral types
332 ///
333 template <typename T>
335 
336 template <>
338 {
339 
340  using int_type = int16;
341  using uint_type = uint16;
343 
344  constexpr SYS_FPRealUnionT<fpreal16>() : fval() {}
345  constexpr SYS_FPRealUnionT<fpreal16>(int_type v) : ival(v) {}
346  constexpr SYS_FPRealUnionT<fpreal16>(uint_type v) : uval(v) {}
348 
349  enum {
350  EXPONENT_BITS = 5,
351  MANTISSA_BITS = 10,
352  EXPONENT_BIAS = 15
353  };
354 
358 
359  struct
360  {
361  uint_type mantissa_val: 10;
362  uint_type exponent_val: 5;
363  uint_type sign_val: 1;
364  };
365 };
366 
367 
368 template <>
370 {
371  using int_type = int32;
372  using uint_type = uint32;
374 
375  SYS_FPRealUnionT<fpreal32>() = default;
376  constexpr SYS_FPRealUnionT<fpreal32>(int_type v) : ival(v) {}
377  constexpr SYS_FPRealUnionT<fpreal32>(uint_type v) : uval(v) {}
379 
380  enum {
381  EXPONENT_BITS = 8,
382  MANTISSA_BITS = 23,
383  EXPONENT_BIAS = 127 };
384 
388 
389  struct
390  {
391  uint_type mantissa_val: 23;
392  uint_type exponent_val: 8;
393  uint_type sign_val: 1;
394  };
395 };
396 
397 template <>
399 {
400  using int_type = int64;
401  using uint_type = uint64;
403 
404  enum {
405  EXPONENT_BITS = 11,
406  MANTISSA_BITS = 52,
407  EXPONENT_BIAS = 1023 };
408 
412 
413  SYS_FPRealUnionT<fpreal64>() = default;
414  constexpr SYS_FPRealUnionT<fpreal64>(int_type v) : ival(v) {}
415  constexpr SYS_FPRealUnionT<fpreal64>(uint_type v) : uval(v) {}
417 
418  struct
419  {
420  uint_type mantissa_val: 52;
421  uint_type exponent_val: 11;
422  uint_type sign_val: 1;
423  };
424 };
425 
430 
431 /// Special enum to disambiguate default constructors from empty constructors.
432 /// This can be handy for constructing static objects that do not need to
433 /// be explicitly constructed but are safe to use if all members are
434 /// initialized to zero upon DSO load (e.g. their storage is defined by the
435 /// .bss section in ELF files).
437 
438 
439 /// Mark a function as doing printf-style formatting, and generate warnings
440 /// if the formatting string doesn't match the types. string_index is the
441 /// parameter index of the format string, and first_to_check is the index of
442 /// the "..." parameter. These indices are both base 1, and "this" counts as
443 /// the first parameter if it's a method.
444 #if (defined(GCC3) && !defined(__clang__)) || SYS_IS_CLANG_GE(3,3)
445  #define SYS_PRINTF_CHECK_ATTRIBUTE(string_index, first_to_check) \
446  __attribute__ ((format (printf, string_index, first_to_check)))
447 #else
448  #define SYS_PRINTF_CHECK_ATTRIBUTE(string_index, first_to_check)
449 #endif
450 
451 
452 /// Struct modifiers
453 ///
454 /// Note that these qualifiers can only appear in struct declarations.
455 ///
456 // @{
457 
458 /// Compiler hint to pack the struct with the specified alignment.
459 /// Note this is not supported on some platforms and is only a hint.
460 #if defined(GCC4)
461 # define SYS_PACKED_STRUCT_HINT_BEGIN(name, n) \
462  struct __attribute__((__packed__, __aligned__(n))) name
463 # define SYS_PACKED_STRUCT_HINT_END
464 #elif defined(_MSC_VER)
465 # define SYS_PACKED_STRUCT_HINT_BEGIN(name, n) \
466  __pragma(pack(push, n)) struct name
467 # define SYS_PACKED_STRUCT_HINT_END __pragma(pack(pop))
468 #else
469 # define SYS_PACKED_STRUCT_HINT_BEGIN(name, n) struct name
470 # define SYS_PACKED_STRUCT_HINT_END
471 #endif
472 // @}
473 
474 #include "SYS_Decimal128.h"
476 
477 #endif
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
unsigned short uint16
Definition: SYS_Types.h:38
int int32
Definition: SYS_Types.h:39
static int32 tolerance()
Definition: SYS_Types.h:233
const GLdouble * v
Definition: glcorearb.h:837
unsigned short utf16
Definition: SYS_Types.h:56
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
int64 exint
Definition: SYS_Types.h:125
#define SYS_FTOLERANCE_D
Definition: SYS_Types.h:209
unsigned long long uint64
Definition: SYS_Types.h:117
float fpreal32
Definition: SYS_Types.h:200
SYS_EmptyConstructor
Definition: SYS_Types.h:436
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
static T tolerance()
Definition: SYS_Types.h:228
unsigned int utf32
Definition: SYS_Types.h:58
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
long long int64
Definition: SYS_Types.h:116
signed char int8
Definition: SYS_Types.h:35
static int64 tolerance()
Definition: SYS_Types.h:238
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
short int16
Definition: SYS_Types.h:37
fpreal64 fpreal
Definition: SYS_Types.h:278
Decimal128 floating point class (IEEE 754-2008)
static fpreal64 tolerance()
Definition: SYS_Types.h:248
unsigned int uint32
Definition: SYS_Types.h:40
#define SYS_FTOLERANCE
Definition: SYS_Types.h:208
static fpreal32 tolerance()
Definition: SYS_Types.h:243
char utf8
Definition: SYS_Types.h:52
unsigned int uint
Definition: SYS_Types.h:45
unsigned char uchar
Definition: SYS_Types.h:42