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