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