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 # endif 99 #endif 100 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 104 # endif 105 #endif 106 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 116 # else 117 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 118 # endif 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 126 # else 127 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 128 # endif 129 # elif defined( __WATCOMC__ ) 130 # define VOID_RETURN void __cdecl 131 # define INT_RETURN int __cdecl 132 # else 133 # define VOID_RETURN void 134 # define INT_RETURN int 135 # endif 136 #endif 137 138 /* These defines are used to detect and set the memory alignment of pointers. 139 Note that offsets are in bytes. 140 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) 145 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) 150 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) 155 */ 156 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))) 160 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 165 166 UNIT_TYPEDEF(x,size) declares a variable 'x' of length 167 'size' bits 168 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) 173 174 UNIT_CAST(x,size) casts a variable to a type of 175 length 'size' bits 176 177 UPTR_CAST(x,size) casts a pointer to a pointer to a 178 varaiable of length 'size' bits 179 */ 180 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)) 186 187 #if defined(__cplusplus) 188 } 189 #endif 190 191 #endif 192