2 ---------------------------------------------------------------------------
3 Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
5 The redistribution and use of this software (with or without changes)
6 is allowed without the payment of fees or royalties provided that:
8 source code distributions include the above copyright notice, this
9 list of conditions and the following disclaimer;
11 binary distributions include the above copyright notice, this list
12 of conditions and the following disclaimer in their documentation.
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
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.
33 #if defined(__cplusplus)
40 #if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
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
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
61 # error Please define uint32_t as a 32-bit unsigned integer type in brg_types.h
66 # if defined( __BORLANDC__ ) && !defined( __MSDOS__ )
68 # define li_64(h) 0x##h##ui64
69 # elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */
71 # define li_64(h) 0x##h##ui64
72 # elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful
74 # define li_64(h) 0x##h##ull
75 # elif defined( __MVS__ )
77 # define li_64(h) 0x##h##ull
78 # elif defined( UINT_MAX ) && UINT_MAX > 4294967295u
79 # if UINT_MAX == 18446744073709551615u
81 # define li_64(h) 0x##h##u
83 # elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u
84 # if ULONG_MAX == 18446744073709551615ul
86 # define li_64(h) 0x##h##ul
88 # elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u
89 # if ULLONG_MAX == 18446744073709551615ull
91 # define li_64(h) 0x##h##ull
93 # elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u
94 # if ULONG_LONG_MAX == 18446744073709551615ull
96 # define li_64(h) 0x##h##ull
101 #if !defined( BRG_UI64 )
102 # if defined( NEED_UINT_64T )
103 # error Please define uint64_t as an unsigned 64 bit type in brg_types.h
107 #ifndef RETURN_VALUES
108 # define RETURN_VALUES
109 # if defined( DLL_EXPORT )
110 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
111 # define VOID_RETURN __declspec( dllexport ) void __stdcall
112 # define INT_RETURN __declspec( dllexport ) int __stdcall
113 # elif defined( __GNUC__ )
114 # define VOID_RETURN __declspec( __dllexport__ ) void
115 # define INT_RETURN __declspec( __dllexport__ ) int
117 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
119 # elif defined( DLL_IMPORT )
120 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
121 # define VOID_RETURN __declspec( dllimport ) void __stdcall
122 # define INT_RETURN __declspec( dllimport ) int __stdcall
123 # elif defined( __GNUC__ )
124 # define VOID_RETURN __declspec( __dllimport__ ) void
125 # define INT_RETURN __declspec( __dllimport__ ) int
127 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
129 # elif defined( __WATCOMC__ )
130 # define VOID_RETURN void __cdecl
131 # define INT_RETURN int __cdecl
133 # define VOID_RETURN void
134 # define INT_RETURN int
138 /* These defines are used to detect and set the memory alignment of pointers.
139 Note that offsets are in bytes.
141 ALIGN_OFFSET(x,n) return the positive or zero offset of
142 the memory addressed by the pointer 'x'
143 from an address that is aligned on an
144 'n' byte boundary ('n' is a power of 2)
146 ALIGN_FLOOR(x,n) return a pointer that points to memory
147 that is aligned on an 'n' byte boundary
148 and is not higher than the memory address
149 pointed to by 'x' ('n' is a power of 2)
151 ALIGN_CEIL(x,n) return a pointer that points to memory
152 that is aligned on an 'n' byte boundary
153 and is not lower than the memory address
154 pointed to by 'x' ('n' is a power of 2)
157 #define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1))
158 #define ALIGN_FLOOR(x,n) ((uint8_t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))
159 #define ALIGN_CEIL(x,n) ((uint8_t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))
161 /* These defines are used to declare buffers in a way that allows
162 faster operations on longer variables to be used. In all these
163 defines 'size' must be a power of 2 and >= 8. NOTE that the
164 buffer size is in bytes but the type length is in bits
166 UNIT_TYPEDEF(x,size) declares a variable 'x' of length
169 BUFR_TYPEDEF(x,size,bsize) declares a buffer 'x' of length 'bsize'
170 bytes defined as an array of variables
171 each of 'size' bits (bsize must be a
172 multiple of size / 8)
174 UNIT_CAST(x,size) casts a variable to a type of
177 UPTR_CAST(x,size) casts a pointer to a pointer to a
178 varaiable of length 'size' bits
181 #define UI_TYPE(size) uint##size##_t
182 #define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x
183 #define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)]
184 #define UNIT_CAST(x,size) ((UI_TYPE(size) )(x))
185 #define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x))
187 #if defined(__cplusplus)