windows-64bit-printf-sanity-20090218
[openafs.git] / src / config / stds.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef OPENAFS_AFS_CONFIG_STDS_H
11 #define OPENAFS_AFS_CONFIG_STDS_H       1
12
13 #include <afs/param.h>
14 #include <sys/types.h>
15
16 #define IN                      /* indicates a parameter is read in */
17 #define OUT                     /* indicates a parameter is sent out (a ptr) */
18 #define INOUT                   /* indicates a parameter is read in and sent out (a ptr) */
19
20 #ifndef MACRO_BEGIN
21 #define MACRO_BEGIN     do {
22 #endif
23 #ifndef MACRO_END
24 #define MACRO_END       } while (0)
25 #endif
26
27 typedef void *opaque;
28
29 #ifndef _ATT4
30 #if defined(__HIGHC__)
31 /*
32  * keep HC from complaining about the use of "old-style" function definitions
33  * with prototypes
34  */
35 pragma Off(Prototype_override_warnings);
36 #endif /* defined(__HIGHC__) */
37 #endif
38
39 /* Now some types to enhance portability.  Always use these on the wire or when
40  * laying out shared structures on disk. */
41
42 /* Imagine that this worked...
43 #if (sizeof(long) != 4) || (sizeof(short) != 2)
44 #error We require size of long and pointers to be equal
45 #endif */
46
47 #define MAX_AFS_INT32 0x7FFFFFFF
48 #define MAX_AFS_UINT32 0xFFFFFFFF
49 #define MAX_AFS_INT64 0x7FFFFFFFFFFFFFFFL
50 #define MAX_AFS_UINT64 0xFFFFFFFFFFFFFFFFL
51
52 typedef short afs_int16;
53 typedef unsigned short afs_uint16;
54 #ifdef  AFS_64BIT_ENV
55 typedef int afs_int32;
56 typedef unsigned int afs_uint32;
57 #if defined(AFS_NT40_ENV) && defined(_MSC_VER)
58 typedef __int64 afs_int64;
59 typedef unsigned __int64 afs_uint64;
60 #else
61 typedef long long afs_int64;
62 typedef unsigned long long afs_uint64;
63 #endif
64 #define ZeroInt64(a)       (a = 0)
65 #define AssignInt64(a, b) *(b) = (a) 
66 #define IncInt64(a) (*(a))++
67 #define IncUInt64(a) (*(a))++
68 #define DecInt64(a) (*(a))--
69 #define DecUInt64(a) (*(a))--
70 #define GTInt64(a,b) ((a) > (b))
71 #define GEInt64(a,b) ((a) >= (b))
72 #define LEInt64(a,b) ((a) <= (b))
73 #define LTInt64(a,b) ((a) < (b))
74 #define AddInt64(a,b,c) *(c) = (afs_int64)(a) + (afs_int64)(b)
75 #define AddUInt64(a,b,c) *(c) = (afs_uint64)(a) + (afs_uint64)(b)
76 #define SubtractInt64(a,b,c) *(c) = (afs_int64)(a) - (afs_int64)(b)
77 #define SubtractUInt64(a,b,c) *(c) = (afs_uint64)(a) - (afs_uint64)(b)
78 #define CompareInt64(a,b) (afs_int64)(a) - (afs_int64)(b)
79 #define CompareUInt64(a,b) (afs_uint64)(a) - (afs_uint64)(b)
80 #define NonZeroInt64(a)                (a)
81 #define Int64ToInt32(a)    (a) & MAX_AFS_UINT32
82 #define FillInt64(t,h,l) (t) = ((afs_int64)(h) << 32) | (l);
83 #define SplitInt64(t,h,l) (h) = ((afs_int64)t) >> 32; (l) = (t) & MAX_AFS_UINT32;
84 #define RoundInt64ToInt32(a)    (a > MAX_AFS_UINT32) ? MAX_AFS_UINT32 : a;
85 #define RoundInt64ToInt31(a)    (a > MAX_AFS_INT32) ? MAX_AFS_INT32 : a;
86 #else /* AFS_64BIT_ENV */
87 typedef long afs_int32;
88 typedef unsigned long afs_uint32;
89
90 struct Int64 {
91     afs_int32 high;
92     afs_uint32 low;
93 };
94 typedef struct Int64 afs_int64;
95
96 struct u_Int64 {
97     afs_uint32 high;
98     afs_uint32 low;
99 };
100 typedef struct u_Int64 afs_uint64;
101 #define ZeroInt64(a) ((a).high = (a).low = 0)
102 #define AssignInt64(a, b) (b)->high = (a).high; (b)->low = (a).low
103 #define IncInt64(a) ((++((a)->low)) ? 0 : (a)->high++ )
104 #define IncUInt64(a) ((++((a)->low)) ? 0 : (a)->high++ )
105 #define DecInt64(a) (((a)->low)-- ? 0 : (a)->high-- )
106 #define DecUInt64(a) (((a)->low)-- ? 0 : (a)->high-- )
107 #define GTInt64(a,b) (((a).high > (b).high) || (((a).high == (b).high) && ((a).low > (b).low)))
108 #define GEInt64(a,b) (((a).high > (b).high) || (((a).high == (b).high) && ((a).low >= (b).low)))
109 #define LEInt64(a,b) (((a).high < (b).high) || (((a).high == (b).high) && ((a).low <= (b).low)))
110 #define LTInt64(a,b) (((a).high < (b).high) || (((a).high == (b).high) && ((a).low < (b).low)))
111 #define CompareInt64(a,b) (((afs_int32)(a).high - (afs_int32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
112 #define AddInt64(a, b, c) {  afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
113 #define SubtractInt64(a, b, c) { afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
114 #define CompareUInt64(a,b) (((afs_uint32)(a).high - (afs_uint32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
115 #define AddUInt64(a, b, c) {  afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
116 #define SubtractUInt64(a, b, c) { afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
117 #define NonZeroInt64(a)   (a).low || (a).high
118 #define Int64ToInt32(a)    (a).low
119 #define FillInt64(t,h,l) (t).high = (h); (t).low = (l);
120 #define SplitInt64(t,h,l) (h) = (t).high; (l) = (t).low;
121 #define RoundInt64ToInt32(a)    (a.high > 0) ? MAX_AFS_UINT32 : a.low;
122 #define RoundInt64ToInt31(a)    (a.high > 0) ? MAX_AFS_INT32 : a.low;
123 #endif /* AFS_64BIT_ENV */
124
125 /* AFS_64BIT_CLIENT should presently be set only for AFS_64BIT_ENV systems */
126
127 #ifdef AFS_64BIT_CLIENT
128 typedef afs_int64 afs_size_t;
129 typedef afs_uint64 afs_offs_t;
130 #else /* AFS_64BIT_CLIENT */
131 typedef afs_int32 afs_size_t;
132 typedef afs_uint32 afs_offs_t;
133 #endif /* AFS_64BIT_CLIENT */
134
135 #ifdef AFS_LARGEFILE_ENV
136 typedef afs_int64 afs_foff_t;
137 typedef afs_uint64 afs_fsize_t;
138 typedef afs_int64 afs_sfsize_t;
139 #define SplitOffsetOrSize(t,h,l) SplitInt64(t,h,l)
140 #else /* !AFS_LARGEFILE_ENV */
141 typedef afs_int32 afs_foff_t;
142 typedef afs_uint32 afs_fsize_t;
143 typedef afs_int32 afs_sfsize_t;
144 #define SplitOffsetOrSize(t,h,l) (h) = 0; (l) = (t);
145 #endif /* !AFS_LARGEFILE_ENV */
146
147 /* Maximum integer sizes.  Also what is expected by %lld, %llu in
148  * afs_snprintf. */
149 #ifdef AFS_64BIT_CLIENT
150 typedef afs_int64 afs_intmax_t;
151 typedef afs_uint64 afs_uintmax_t;
152 #else /* !AFS_64BIT_CLIENT */
153 typedef afs_int32 afs_intmax_t;
154 typedef afs_uint32 afs_uintmax_t;
155 #endif /* !AFS_64BIT_CLIENT */
156
157 /* you still have to include <netinet/in.h> to make these work */
158
159 #define hton32 htonl
160 #define hton16 htons
161 #define ntoh32 ntohl
162 #define ntoh16 ntohs
163
164
165 /* Since there is going to be considerable use of 64 bit integers we provide
166  * some assistence in this matter.  The hyper type is supposed to be compatible
167  * with the afsHyper type: the same macros will work on both. */
168
169 #if     defined(AFS_64BIT_ENV) && 0
170
171 typedef unsigned long afs_hyper_t;
172
173 #define hcmp(a,b)       ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0))
174 #define hsame(a,b)      ((a) == (b))
175 #define hiszero(a)      ((a) == 0)
176 #define hfitsin32(a)    ((a) & 0xffffffff00000000) == 0)
177 #define hset(a,b)       ((a) = (b))
178 #define hzero(a)        ((a) = 0)
179 #define hones(a)        ((a) = ~((unsigned long)0))
180 #define hget32(i,a)     ((i) = (unsigned int)(a))
181 #define hget64(hi,lo,a) ((lo) = ((unsigned int)(a)), (hi) = ((a) & (0xffffffff00000000)))
182 #define hset32(a,i)     ((a) = ((unsigned int)(i)))
183 #define hset64(a,hi,lo) ((a) = (((hi) << 32) | (lo)))
184 #define hgetlo(a)       ((a) & 0xffffffff)
185 #define hgethi(a)       (((unsigned int)(a)) >> 32)
186 #define hadd(a,b)       ((a) += (b))
187 /* XXX */
188 #define hadd32(a,b)     ((a) += (b))
189 #define hshlft(a,n)     ((a)<<(n))
190
191 #else /* AFS_64BIT_ENV */
192
193 typedef struct afs_hyper_t {    /* unsigned 64 bit integers */
194     unsigned int high;
195     unsigned int low;
196 } afs_hyper_t;
197
198 #define hcmp(a,b) ((a).high<(b).high? -1 : ((a).high > (b).high? 1 : \
199     ((a).low <(b).low? -1 : ((a).low > (b).low? 1 : 0))))
200 #define hsame(a,b) ((a).low == (b).low && (a).high == (b).high)
201 #define hiszero(a) ((a).low == 0 && (a).high == 0)
202 #define hfitsin32(a) ((a).high == 0)
203 #define hset(a,b) ((a) = (b))
204 #define hzero(a) ((a).low = 0, (a).high = 0)
205 #define hones(a) ((a).low = 0xffffffff, (a).high = 0xffffffff)
206 #define hget32(i,a) ((i) = (a).low)
207 #define hget64(hi,lo,a) ((lo) = (a).low, (hi) = (a).high)
208 #define hset32(a,i) ((a).high = 0, (a).low = (i))
209 #define hset64(a,hi,lo) ((a).high = (hi), (a).low = (lo))
210 #define hgetlo(a) ((a).low)
211 #define hgethi(a) ((a).high)
212 #define hshlft(a,n)                                                        \
213      { /*Shift Left n bits*/                                               \
214         int s = sizeof((a).low) * 8; /*#bits*/                             \
215         if ((n) <= 0) { /*No shift*/                                       \
216         } else if ((n) >= 2*s) { /*Shift off all bits*/                    \
217            (a).high = (a).low = 0;                                         \
218         } else if ((n) < s) { /*Part of low shifted into high*/            \
219            (a).high = ((a).high<<(n)) | (((a).low>>(s-(n))) & (1<<(n))-1); \
220            (a).low  = (a).low << (n);                                      \
221         } else if ((n) >= s) { /*All of low shifted into high plus some*/  \
222            (a).high = (a).low << ((n)-s);                                  \
223            (a).low=0;                                                      \
224         }                                                                  \
225      }
226
227 /* The algorithm here is to check for two cases that cause a carry.  If the top
228  * two bits are different then if the sum has the top bit off then there must
229  * have been a carry.  If the top bits are both one then there is always a
230  * carry.  We assume 32 bit ints and twos complement arithmetic. */
231
232 #define SIGN 0x80000000
233 #define hadd32(a,i) \
234     (((((a).low ^ (int)(i)) & SIGN) \
235       ? (((((a).low + (int)(i)) & SIGN) == 0) && (a).high++) \
236       : (((a).low & (int)(i) & SIGN) && (a).high++)), \
237      (a).low += (int)(i))
238
239 #define hadd(a,b) (hadd32(a,(b).low), (a).high += (b).high)
240 #endif /* AFS_64BIT_ENV */
241
242 #ifndef KERNEL
243 #ifndef AFS_NT40_ENV
244 #define max(a, b)               ((a) < (b) ? (b) : (a))
245 #define min(a, b)               ((a) > (b) ? (b) : (a))
246 #endif
247 /*#define abs(x)                  ((x) >= 0 ? (x) : -(x))*/
248 #endif
249
250 /* minumum length of string to pass to int_to_base64 */
251 typedef char b64_string_t[8];
252 #ifndef AFS_NT40_ENV
253 #if defined(AFS_HPUX_ENV) || defined(AFS_USR_HPUX_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV))
254 char *int_to_base64();
255 int base64_to_int();
256 #else
257 char *int_to_base64(b64_string_t s, int a);
258 int base64_to_int(char *s);
259 #endif
260 #endif /* AFS_NT40_ENV */
261 /*
262  * The afsUUID data type is built in to RX
263  */
264 struct afsUUID {
265     afs_uint32 time_low;
266     afs_uint16 time_mid;
267     afs_uint16 time_hi_and_version;
268     char clock_seq_hi_and_reserved;
269     char clock_seq_low;
270     char node[6];
271 };
272 typedef struct afsUUID afsUUID;
273
274 /* for now, demand attach fileserver is only support on unix pthreads builds */
275 #if defined(DEMAND_ATTACH_ENABLE) && defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
276 #define AFS_DEMAND_ATTACH_FS 1
277 #endif
278
279 #ifdef AFS_HPUX_ENV
280 #define static_inline static __inline
281 #elif defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
282 #define static_inline static
283 #else
284 #define static_inline static inline
285 #endif
286
287 /* A macro that can be used when printf'ing 64 bit integers, as Unix and 
288  * windows use a different format string
289  */
290 #ifdef AFS_NT40_ENV
291 #define AFS_INT64_FMT "l64d"
292 #else
293 #define AFS_INT64_FMT "lld"
294 #endif
295
296 #endif /* OPENAFS_CONFIG_AFS_STDS_H */