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