1 /*
2 ---------------------------------------------------------------------------
3 Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
4 
5 The redistribution and use of this software (with or without changes)
6 is allowed without the payment of fees or royalties provided that:
7 
8   source code distributions include the above copyright notice, this
9   list of conditions and the following disclaimer;
10 
11   binary distributions include the above copyright notice, this list
12   of conditions and the following disclaimer in their documentation.
13 
14 This software is provided 'as is' with no explicit or implied warranties
15 in respect of its operation, including, but not limited to, correctness
16 and fitness for purpose.
17 ---------------------------------------------------------------------------
18 Issue Date: 20/12/2007
19 
20  The unsigned integer types defined here are of the form uint_<nn>t where
21  <nn> is the length of the type; for example, the unsigned 32-bit type is
22  'uint32_t'.  These are NOT the same as the 'C99 integer types' that are
23  defined in the inttypes.h and stdint.h headers since attempts to use these
24  types have shown that support for them is still highly variable.  However,
25  since the latter are of the form uint<nn>_t, a regular expression search
26  and replace (in VC++ search on 'uint_{:z}t' and replace with 'uint\1_t')
27  can be used to convert the types used here to the C99 standard types.
28 */
29 
30 #ifndef _BRG_TYPES_H
31 #define _BRG_TYPES_H
32 
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36 
37 #include <limits.h>
38 #include <stdint.h>
39 
40 #if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
41 #  include <stddef.h>
42 #  define ptrint_t intptr_t
43 #elif defined( __ECOS__ )
44 #  define intptr_t unsigned int
45 #  define ptrint_t intptr_t
46 #elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )
47 #  define ptrint_t intptr_t
48 #else
49 #  define ptrint_t int
50 #endif
51 
52 #ifndef BRG_UI32
53 #  define BRG_UI32
54 #  if UINT_MAX == 4294967295u
55 #    define li_32(h) 0x##h##u
56 #  elif ULONG_MAX == 4294967295u
57 #    define li_32(h) 0x##h##ul
58 #  elif defined( _CRAY )
59 #    error This code needs 32-bit data types, which Cray machines do not provide
60 #  else
61 #    error Please define uint32_t as a 32-bit unsigned integer type in brg_types.h
62 #  endif
63 #endif
64 
65 #ifndef BRG_UI64
66 #  if defined( __BORLANDC__ ) && !defined( __MSDOS__ )
67 #    define BRG_UI64
68 #    define li_64(h) 0x##h##ui64
69 #  elif defined( _MSC_VER ) && ( _MSC_VER < 1300 )    /* 1300 == VC++ 7.0 */
70 #    define BRG_UI64
71 #    define li_64(h) 0x##h##ui64
72 #  elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful
73 #    define BRG_UI64
74 #    define li_64(h) 0x##h##ull
75 #  elif defined( __MVS__ )
76 #    define BRG_UI64
77 #    define li_64(h) 0x##h##ull
78 #  elif defined( UINT_MAX ) && UINT_MAX > 4294967295u
79 #    if UINT_MAX == 18446744073709551615u
80 #      define BRG_UI64
81 #      define li_64(h) 0x##h##u
82 #    endif
83 #  elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u
84 #    if ULONG_MAX == 18446744073709551615ul
85 #      define BRG_UI64
86 #      define li_64(h) 0x##h##ul
87 #    endif
88 #  elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u
89 #    if ULLONG_MAX == 18446744073709551615ull
90 #      define BRG_UI64
91 #      define li_64(h) 0x##h##ull
92 #    endif
93 #  elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u
94 #    if ULONG_LONG_MAX == 18446744073709551615ull
95 #      define BRG_UI64
96 #      define li_64(h) 0x##h##ull
97 #    endif
98 #  elif defined( UINT64_MAX ) && UINT64_MAX > 4294967295u
99 #    if UINT64_MAX == __UINT64_C(18446744073709551615)
100 #      define BRG_UI64
101 #      define li_64(h) __UINT64_C(0x##h)
102 #    endif
103 #  endif
104 #endif
105 
106 #if !defined( BRG_UI64 )
107 #  if defined( NEED_UINT_64T )
108 #    error Please define uint64_t as an unsigned 64 bit type in brg_types.h
109 #  endif
110 #endif
111 
112 #ifndef RETURN_VALUES
113 #  define RETURN_VALUES
114 #  if defined( DLL_EXPORT )
115 #    if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
116 #      define VOID_RETURN    __declspec( dllexport ) void __stdcall
117 #      define INT_RETURN     __declspec( dllexport ) int  __stdcall
118 #    elif defined( __GNUC__ )
119 #      define VOID_RETURN    __declspec( __dllexport__ ) void
120 #      define INT_RETURN     __declspec( __dllexport__ ) int
121 #    else
122 #      error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
123 #    endif
124 #  elif defined( DLL_IMPORT )
125 #    if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
126 #      define VOID_RETURN    __declspec( dllimport ) void __stdcall
127 #      define INT_RETURN     __declspec( dllimport ) int  __stdcall
128 #    elif defined( __GNUC__ )
129 #      define VOID_RETURN    __declspec( __dllimport__ ) void
130 #      define INT_RETURN     __declspec( __dllimport__ ) int
131 #    else
132 #      error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
133 #    endif
134 #  elif defined( __WATCOMC__ )
135 #    define VOID_RETURN  void __cdecl
136 #    define INT_RETURN   int  __cdecl
137 #  else
138 #    define VOID_RETURN  void
139 #    define INT_RETURN   int
140 #  endif
141 #endif
142 
143 /*	These defines are used to detect and set the memory alignment of pointers.
144     Note that offsets are in bytes.
145 
146     ALIGN_OFFSET(x,n)			return the positive or zero offset of
147                                 the memory addressed by the pointer 'x'
148                                 from an address that is aligned on an
149                                 'n' byte boundary ('n' is a power of 2)
150 
151     ALIGN_FLOOR(x,n)			return a pointer that points to memory
152                                 that is aligned on an 'n' byte boundary
153                                 and is not higher than the memory address
154                                 pointed to by 'x' ('n' is a power of 2)
155 
156     ALIGN_CEIL(x,n)				return a pointer that points to memory
157                                 that is aligned on an 'n' byte boundary
158                                 and is not lower than the memory address
159                                 pointed to by 'x' ('n' is a power of 2)
160 */
161 
162 #define ALIGN_OFFSET(x,n)	(((ptrint_t)(x)) & ((n) - 1))
163 #define ALIGN_FLOOR(x,n)	((uint8_t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))
164 #define ALIGN_CEIL(x,n)		((uint8_t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))
165 
166 /*  These defines are used to declare buffers in a way that allows
167     faster operations on longer variables to be used.  In all these
168     defines 'size' must be a power of 2 and >= 8. NOTE that the
169     buffer size is in bytes but the type length is in bits
170 
171     UNIT_TYPEDEF(x,size)        declares a variable 'x' of length
172                                 'size' bits
173 
174     BUFR_TYPEDEF(x,size,bsize)  declares a buffer 'x' of length 'bsize'
175                                 bytes defined as an array of variables
176                                 each of 'size' bits (bsize must be a
177                                 multiple of size / 8)
178 
179     UNIT_CAST(x,size)           casts a variable to a type of
180                                 length 'size' bits
181 
182     UPTR_CAST(x,size)           casts a pointer to a pointer to a
183                                 varaiable of length 'size' bits
184 */
185 
186 #define UI_TYPE(size)               uint##size##_t
187 #define UNIT_TYPEDEF(x,size)        typedef UI_TYPE(size) x
188 #define BUFR_TYPEDEF(x,size,bsize)  typedef UI_TYPE(size) x[bsize / (size >> 3)]
189 #define UNIT_CAST(x,size)           ((UI_TYPE(size) )(x))
190 #define UPTR_CAST(x,size)           ((UI_TYPE(size)*)(x))
191 
192 #if defined(__cplusplus)
193 }
194 #endif
195 
196 #endif
197