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: 30/09/2017 19 */ 20 21 #ifndef _BRG_TYPES_H 22 #define _BRG_TYPES_H 23 24 #if defined(__cplusplus) 25 extern "C" { 26 #endif 27 28 #include <limits.h> 29 #include <stdint.h> 30 31 #if defined( _MSC_VER ) && ( _MSC_VER >= 1300 ) 32 # include <stddef.h> 33 # define ptrint_t intptr_t 34 #elif defined( __ECOS__ ) 35 # define intptr_t unsigned int 36 # define ptrint_t intptr_t 37 #elif defined( __GNUC__ ) && ( __GNUC__ >= 3 ) && !(defined( __HAIKU__ ) || defined( __VxWorks__ )) 38 # define ptrint_t intptr_t 39 #else 40 # define ptrint_t int 41 #endif 42 43 /* define unsigned 8-bit type if not available in stdint.h */ 44 #if !defined(UINT8_MAX) 45 typedef unsigned char uint8_t; 46 #endif 47 48 /* define unsigned 16-bit type if not available in stdint.h */ 49 #if !defined(UINT16_MAX) 50 typedef unsigned short uint16_t; 51 #endif 52 53 /* define unsigned 32-bit type if not available in stdint.h and define the 54 macro li_32(h) which converts a sequence of eight hexadecimal characters 55 into a 32 bit constant 56 */ 57 #if defined(UINT_MAX) && UINT_MAX == 4294967295u 58 # define li_32(h) 0x##h##u 59 # if !defined(UINT32_MAX) 60 typedef unsigned int uint32_t; 61 # endif 62 #elif defined(ULONG_MAX) && ULONG_MAX == 4294967295u 63 # define li_32(h) 0x##h##ul 64 # if !defined(UINT32_MAX) 65 typedef unsigned long uint32_t; 66 # endif 67 #elif defined( _CRAY ) 68 # error This code needs 32-bit data types, which Cray machines do not provide 69 #else 70 # error Please define uint32_t as a 32-bit unsigned integer type in brg_types.h 71 #endif 72 73 /* define unsigned 64-bit type if not available in stdint.h and define the 74 macro li_64(h) which converts a sequence of eight hexadecimal characters 75 into a 64 bit constant 76 */ 77 #if defined( __BORLANDC__ ) && !defined( __MSDOS__ ) 78 # define li_64(h) 0x##h##ui64 79 # if !defined(UINT64_MAX) 80 typedef unsigned __int64 uint64_t; 81 # endif 82 #elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */ 83 # define li_64(h) 0x##h##ui64 84 # if !defined(UINT64_MAX) 85 typedef unsigned __int64 uint64_t; 86 # endif 87 #elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful 88 # define li_64(h) 0x##h##ull 89 # if !defined(UINT64_MAX) 90 typedef unsigned long long uint64_t; 91 # endif 92 #elif defined( __MVS__ ) 93 # define li_64(h) 0x##h##ull 94 # if !defined(UINT64_MAX) 95 typedef unsigned long long uint64_t; 96 # endif 97 #elif defined( UINT_MAX ) && UINT_MAX > 4294967295u 98 # if UINT_MAX == 18446744073709551615u 99 # define li_64(h) 0x##h##u 100 # if !defined(UINT64_MAX) 101 typedef unsigned int uint64_t; 102 # endif 103 # endif 104 #elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u 105 # if ULONG_MAX == 18446744073709551615ul 106 # define li_64(h) 0x##h##ul 107 # if !defined(UINT64_MAX) && !defined(_UINT64_T) 108 typedef unsigned long uint64_t; 109 # endif 110 # endif 111 #elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u 112 # if ULLONG_MAX == 18446744073709551615ull 113 # define li_64(h) 0x##h##ull 114 # if !defined(UINT64_MAX) && !defined( __HAIKU__ ) 115 typedef unsigned long long uint64_t; 116 # endif 117 # endif 118 #elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u 119 # if ULONG_LONG_MAX == 18446744073709551615ull 120 # define li_64(h) 0x##h##ull 121 # if !defined(UINT64_MAX) 122 typedef unsigned long long uint64_t; 123 # endif 124 # endif 125 #endif 126 127 #if !defined( li_64 ) 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 variable 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