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