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 #if 0 /* pcg */ 39 #include <stdint.h> 40 #endif /* 0 */ 41 42 #if defined( _MSC_VER ) && ( _MSC_VER >= 1300 ) 43 # include <stddef.h> 44 # define ptrint_t intptr_t 45 #elif defined( __ECOS__ ) 46 # define intptr_t unsigned int 47 # define ptrint_t intptr_t 48 #elif defined( __GNUC__ ) && ( __GNUC__ >= 3 ) /* pcg */ 49 # if defined( __VxWorks__ ) 50 /* Wind River's toolchain doesn't include stdint.h */ 51 # define ptrint_t int 52 # else 53 # include <stdint.h> 54 # define ptrint_t intptr_t 55 # endif 56 #else 57 # define ptrint_t int 58 #endif 59 60 typedef unsigned char uint8_t; /* pcg */ 61 typedef unsigned short uint16_t; /* GCM - pcg */ 62 63 #ifndef BRG_UI32 64 # define BRG_UI32 65 # if UINT_MAX == 4294967295u 66 # define li_32(h) 0x##h##u 67 typedef unsigned int uint32_t; /* AES - pcg */ 68 # elif ULONG_MAX == 4294967295u 69 # define li_32(h) 0x##h##ul 70 typedef unsigned long uint32_t; /* AES - pcg */ 71 # elif defined( _CRAY ) 72 /* USE_AES is undefined on Crays, however we define a dummy data type 73 to get the code to compile - pcg */ 74 /*#error Crays don't support 32-bit data types, this code won't compile on a Cray*/ 75 typedef unsigned int uint32_t; 76 # else 77 # error Please define uint32_t as a 32-bit unsigned integer type in brg_types.h 78 # endif 79 #endif 80 81 #ifndef BRG_UI64 82 # if defined( __BORLANDC__ ) && !defined( __MSDOS__ ) 83 # define BRG_UI64 84 # define li_64(h) 0x##h##ui64 85 typedef unsigned __int64 uint64_t; /* AES-GCM - pcg */ 86 # elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */ 87 # define BRG_UI64 88 # define li_64(h) 0x##h##ui64 89 typedef unsigned __int64 uint64_t; /* AES-GCM - pcg */ 90 # elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful 91 # define BRG_UI64 92 # define li_64(h) 0x##h##ull 93 typedef unsigned long long uint64_t; /* AES-GCM - pcg */ 94 # elif defined( __MVS__ ) 95 # define BRG_UI64 96 # define li_64(h) 0x##h##ull 97 typedef unsigned long long uint64_t; /* AES-GCM - pcg */ 98 # elif defined( UINT_MAX ) && UINT_MAX > 4294967295u 99 # if UINT_MAX == 18446744073709551615u 100 # define BRG_UI64 101 # define li_64(h) 0x##h##u 102 typedef unsigned int uint64_t; /* AES-GCM - pcg */ 103 # endif 104 # elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u 105 # if ULONG_MAX == 18446744073709551615ul 106 # define BRG_UI64 107 # define li_64(h) 0x##h##ul 108 #ifndef _UINT64_T /* Apple define their own version - pcg */ 109 typedef unsigned long uint64_t; /* AES-GCM - pcg */ 110 #endif /* !_UINT64_T */ 111 # endif 112 # elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u 113 # if ULLONG_MAX == 18446744073709551615ull 114 # define BRG_UI64 115 # define li_64(h) 0x##h##ull 116 typedef unsigned long long uint64_t; /* AES-GCM - pcg */ 117 # endif 118 # elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u 119 # if ULONG_LONG_MAX == 18446744073709551615ull 120 # define BRG_UI64 121 # define li_64(h) 0x##h##ull 122 typedef unsigned long long uint64_t; /* AES-GCM - pcg */ 123 # endif 124 # endif 125 #endif 126 127 #if !defined( BRG_UI64 ) 128 # if defined( NEED_UINT_64T ) 129 # error Please define uint64_t as an unsigned 64 bit type in brg_types.h 130 # endif 131 #endif 132 133 #ifndef RETURN_VALUES 134 # define RETURN_VALUES 135 # if defined( DLL_EXPORT ) 136 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) 137 # define VOID_RETURN __declspec( dllexport ) void __stdcall 138 # define INT_RETURN __declspec( dllexport ) int __stdcall 139 # elif defined( __GNUC__ ) 140 # define VOID_RETURN __declspec( __dllexport__ ) void 141 # define INT_RETURN __declspec( __dllexport__ ) int 142 # else 143 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 144 # endif 145 # elif defined( DLL_IMPORT ) 146 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) 147 # define VOID_RETURN __declspec( dllimport ) void __stdcall 148 # define INT_RETURN __declspec( dllimport ) int __stdcall 149 # elif defined( __GNUC__ ) 150 # define VOID_RETURN __declspec( __dllimport__ ) void 151 # define INT_RETURN __declspec( __dllimport__ ) int 152 # else 153 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 154 # endif 155 # elif defined( __WATCOMC__ ) 156 # define VOID_RETURN void __cdecl 157 # define INT_RETURN int __cdecl 158 # else 159 # define VOID_RETURN void 160 # define INT_RETURN int 161 # endif 162 #endif 163 164 /* These defines are used to detect and set the memory alignment of pointers. 165 Note that offsets are in bytes. 166 167 ALIGN_OFFSET(x,n) return the positive or zero offset of 168 the memory addressed by the pointer 'x' 169 from an address that is aligned on an 170 'n' byte boundary ('n' is a power of 2) 171 172 ALIGN_FLOOR(x,n) return a pointer that points to memory 173 that is aligned on an 'n' byte boundary 174 and is not higher than the memory address 175 pointed to by 'x' ('n' is a power of 2) 176 177 ALIGN_CEIL(x,n) return a pointer that points to memory 178 that is aligned on an 'n' byte boundary 179 and is not lower than the memory address 180 pointed to by 'x' ('n' is a power of 2) 181 */ 182 183 #define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1)) 184 #define ALIGN_FLOOR(x,n) ((uint8_t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1))) 185 #define ALIGN_CEIL(x,n) ((uint8_t*)(x) + (-((ptrint_t)(x)) & ((n) - 1))) 186 187 /* These defines are used to declare buffers in a way that allows 188 faster operations on longer variables to be used. In all these 189 defines 'size' must be a power of 2 and >= 8. NOTE that the 190 buffer size is in bytes but the type length is in bits 191 192 UNIT_TYPEDEF(x,size) declares a variable 'x' of length 193 'size' bits 194 195 BUFR_TYPEDEF(x,size,bsize) declares a buffer 'x' of length 'bsize' 196 bytes defined as an array of variables 197 each of 'size' bits (bsize must be a 198 multiple of size / 8) 199 200 UNIT_CAST(x,size) casts a variable to a type of 201 length 'size' bits 202 203 UPTR_CAST(x,size) casts a pointer to a pointer to a 204 varaiable of length 'size' bits 205 */ 206 207 #define UI_TYPE(size) uint##size##_t 208 #define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x 209 #define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)] 210 #define UNIT_CAST(x,size) ((UI_TYPE(size) )(x)) 211 #define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x)) 212 213 #if defined(__cplusplus) 214 } 215 #endif 216 217 #endif 218