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