Added silc_likely and silc_unlikely GCC branch prediction macros.
[silc.git] / lib / silcutil / silctypes.h
1 /*
2
3   silctypes.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 - 2006 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/SILC Types
21  *
22  * DESCRIPTION
23  *
24  *    This header includes the most basic types used in the SILC source
25  *    tree, such as arithmetic types and their manipulation macros.  This
26  *    file is included in the silcincludes.h and is automatically available
27  *    for application.
28  *
29  ***/
30
31 #ifndef SILCTYPES_H
32 #define SILCTYPES_H
33
34 /****d* silcutil/SILCTypes/TRUE
35  *
36  * NAME
37  *
38  *    #define TRUE ...
39  *
40  * DESCRIPTION
41  *
42  *    Boolean true value indicator.
43  *
44  * SOURCE
45  */
46 #ifndef TRUE
47 #define TRUE 1
48 #endif
49 /***/
50
51 /****d* silcutil/SILCTypes/FALSE
52  *
53  * NAME
54  *
55  *    #define FALSE ...
56  *
57  * DESCRIPTION
58  *
59  *    Boolean false value indicator.
60  *
61  * SOURCE
62  */
63 #ifndef FALSE
64 #define FALSE 0
65 #endif
66 /***/
67
68 /****d* silcutil/SILCTypes/SilcBool
69  *
70  * NAME
71  *
72  *    typedef unigned char SilcBool;
73  *
74  * DESCRIPTION
75  *
76  *    Boolean value, and is always 8-bits.  Represents value 0 or 1.
77  *
78  ***/
79 typedef unsigned char SilcBool;
80
81 /****d* silcutil/SILCTypes/bool
82  *
83  * NAME
84  *
85  *    #define SilcBool ...
86  *
87  * DESCRIPTION
88  *
89  *    Boolean value, and is 8-bits.  Represents value 0 or 1.  In
90  *    C++ code this type is defined by the C++, and this definition is
91  *    not used.
92  *
93  * NOTES
94  *
95  *    This macro is deprecated.  Use SilcBool instead.
96  *
97  * SOURCE
98  */
99 #ifdef SILC_MACOSX
100 #define bool _Bool
101 #endif
102
103 #ifndef __cplusplus
104 #ifndef bool
105 #define bool unsigned char
106 #endif
107 #endif
108 /***/
109
110 /* Our offsetof macro */
111 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
112
113 /* silc_likely and silc_unlikely GCC branch prediction macros. Use only if
114    you have profiled the code first. */
115 #if __GNUC__ >= 3
116 #define silc_likely(expr) __builtin_expect(!!(expr), 1)
117 #define silc_unlikely(expr) __builtin_expect(!!(expr), 0)
118 #else
119 #define silc_likely(expr) (expr)
120 #define silc_unlikely(expr) (expr)
121 #endif /* __GNUC__ >= 3 */
122
123 #if SILC_SIZEOF_SHORT > 2
124 #error "size of the short must be 2 bytes"
125 #endif
126
127 /****d* silcutil/SILCTypes/SilcUInt8
128  *
129  * NAME
130  *
131  *    typedef unsigned char SilcUInt8;
132  *
133  * DESCRIPTION
134  *
135  *    8-bit unsigned integer.
136  *
137  * SOURCE
138  */
139 typedef unsigned char SilcUInt8;
140 /***/
141
142 /****d* silcutil/SILCTypes/SilcInt8
143  *
144  * NAME
145  *
146  *    typedef signed char SilcInt8;
147  *
148  * DESCRIPTION
149  *
150  *    8-bit signed integer.
151  *
152  * SOURCE
153  */
154 typedef signed char SilcInt8;
155 /***/
156
157 /****d* silcutil/SILCTypes/SilcUInt16
158  *
159  * NAME
160  *
161  *    typedef unsigned short SilcUInt16;
162  *
163  * DESCRIPTION
164  *
165  *    16-bit unsigned integer.  Guaranteed to be 16-bits.
166  *
167  * SOURCE
168  */
169 typedef unsigned short SilcUInt16;
170 /***/
171
172 /****d* silcutil/SILCTypes/SilcInt16
173  *
174  * NAME
175  *
176  *    typedef signed short SilcInt16;
177  *
178  * DESCRIPTION
179  *
180  *    16-bit signed integer.  Guaranteed to be 16-bits.
181  *
182  * SOURCE
183  */
184 typedef signed short SilcInt16;
185 /***/
186
187 /****d* silcutil/SILCTypes/SilcUInt32
188  *
189  * NAME
190  *
191  *    typedef unsigned long SilcUInt32;
192  *
193  * DESCRIPTION
194  *
195  *    32-bit unsigned integer.  Guaranteed to be 32-bits.
196  *
197  * SOURCE
198  */
199 #if SILC_SIZEOF_LONG == 4
200 typedef unsigned long SilcUInt32;
201 typedef signed long SilcInt32;
202 #else
203 #if SILC_SIZEOF_INT == 4
204 typedef unsigned int SilcUInt32;
205 typedef signed int SilcInt32;
206 #else
207 #if SILC_SIZEOF_LONG_LONG >= 4
208 #ifndef WIN32
209 typedef unsigned long long SilcUInt32;
210 typedef signed long long SilcInt32;
211 #endif
212 #endif
213 #endif
214 #endif
215 /***/
216
217 /****d* silcutil/SILCTypes/SilcInt32
218  *
219  * NAME
220  *
221  *    typedef signed long SilcInt32;
222  *
223  * DESCRIPTION
224  *
225  *    32-bit signed integer.  Guaranteed to be 32-bits.
226  *
227  ***/
228
229 /****d* silcutil/SILCTypes/SilcUInt64
230  *
231  * NAME
232  *
233  *    typedef unsigned long long SilcUInt64;
234  *
235  * DESCRIPTION
236  *
237  *    64-bit unsigned integer.  Guaranteed to be 64-bits on systems that
238  *    support it.
239  *
240  * SOURCE
241  */
242 #if SILC_SIZEOF_LONG >= 8
243 typedef unsigned long SilcUInt64;
244 typedef signed long SilcInt64;
245 #else
246 #if SILC_SIZEOF_LONG_LONG >= 8
247 #ifndef WIN32
248 typedef unsigned long long SilcUInt64;
249 typedef signed long long SilcInt64;
250 #else
251 typedef unsigned __int64 SilcUInt64;
252 typedef signed __int64 SilcInt64;
253 #endif
254 #else
255 typedef SilcUInt32 SilcUInt64;
256 typedef SilcInt32 SilcInt64;
257 #endif
258 #endif
259 /***/
260
261 /****d* silcutil/SILCTypes/SilcInt64
262  *
263  * NAME
264  *
265  *    typedef signed long long SilcInt64;
266  *
267  * DESCRIPTION
268  *
269  *    64-bit signed integer.  Guaranteed to be 64-bits on systems that
270  *    support it.
271  *
272  ***/
273
274 #if SILC_SIZEOF_VOID_P < 4
275 typedef SilcUInt32 * void *;
276 #endif
277
278 /* Macros */
279
280 #define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24        \
281                     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)            \
282                     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)             \
283                     | ((SilcUInt32)(SilcUInt8)(cp)[3])
284
285 /****d* silcutil/SILCTypes/SILC_GET16_MSB
286  *
287  * NAME
288  *
289  *    #define SILC_GET16_MSB ...
290  *
291  * DESCRIPTION
292  *
293  *    Return two 8-bit bytes, most significant bytes first.
294  *
295  * SOURCE
296  */
297 #define SILC_GET16_MSB(l, cp)                           \
298 do {                                                    \
299   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)           \
300     | ((SilcUInt32)(SilcUInt8)(cp)[1]);                 \
301 } while(0)
302 /***/
303
304 /****d* silcutil/SILCTypes/SILC_GET32_MSB
305  *
306  * NAME
307  *
308  *    #define SILC_GET32_MSB ...
309  *
310  * DESCRIPTION
311  *
312  *    Return four 8-bit bytes, most significant bytes first.
313  *
314  * SOURCE
315  */
316 #define SILC_GET32_MSB(l, cp)                           \
317 do {                                                    \
318   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24          \
319     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)            \
320     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)             \
321     | ((SilcUInt32)(SilcUInt8)(cp)[3]);                 \
322 } while(0)
323 /***/
324
325 /****d* silcutil/SILCTypes/SILC_GET64_MSB
326  *
327  * NAME
328  *
329  *    #define SILC_GET64_MSB ...
330  *
331  * DESCRIPTION
332  *
333  *    Return eight 8-bit bytes, most significant bytes first.
334  *
335  * SOURCE
336  */
337 #define SILC_GET64_MSB(l, cp)                                   \
338 do {                                                            \
339   (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) |            \
340          ((SilcUInt64)SILC_GET_WORD((cp) + 4)));                \
341 } while(0)
342 /***/
343
344 /****d* silcutil/SILCTypes/SILC_GET16_LSB
345  *
346  * NAME
347  *
348  *    #define SILC_GET16_MSB ...
349  *
350  * DESCRIPTION
351  *
352  *    Return two 8-bit bytes, least significant bytes first.
353  *
354  * SOURCE
355  */
356 #if defined(SILC_I486) && defined(__GNUC__)
357 #define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
358 #else
359 #define SILC_GET16_LSB(l, cp)                           \
360 do {                                                    \
361   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])                \
362     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8);            \
363 } while(0)
364 #endif /* SILC_I486 && __GNUC__ */
365 /***/
366
367 /****d* silcutil/SILCTypes/SILC_GET32_LSB
368  *
369  * NAME
370  *
371  *    #define SILC_GET32_LSB ...
372  *
373  * DESCRIPTION
374  *
375  *    Return four 8-bit bytes, least significant bytes first.
376  *
377  * SOURCE
378  */
379 #if defined(SILC_I486) && defined(__GNUC__)
380 #define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
381 #else
382 #define SILC_GET32_LSB(l, cp)                           \
383 do {                                                    \
384   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])                \
385     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)             \
386     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)            \
387     | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24);           \
388 } while(0)
389 #endif /* SILC_I486 && __GNUC__ */
390
391 /* Same as upper but XOR the result always. Special purpose macro. */
392 #if defined(SILC_I486) && defined(__GNUC__)
393 #define SILC_GET32_X_LSB(l, cp) (l) ^= (*(SilcUInt32 *)(cp))
394 #else
395 #define SILC_GET32_X_LSB(l, cp)                         \
396   (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])               \
397     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)             \
398     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)            \
399     | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
400 #endif /* SILC_I486 && __GNUC__ */
401 /***/
402
403 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
404  *
405  * NAME
406  *
407  *    #define SILC_PUT16_MSB ...
408  *
409  * DESCRIPTION
410  *
411  *    Put two 8-bit bytes, most significant bytes first.
412  *
413  * SOURCE
414  */
415 #define SILC_PUT16_MSB(l, cp)                   \
416 do {                                            \
417   (cp)[0] = (SilcUInt8)((l) >> 8);              \
418   (cp)[1] = (SilcUInt8)(l);                     \
419 } while(0)
420 /***/
421
422 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
423  *
424  * NAME
425  *
426  *    #define SILC_PUT32_MSB ...
427  *
428  * DESCRIPTION
429  *
430  *    Put four 8-bit bytes, most significant bytes first.
431  *
432  * SOURCE
433  */
434 #define SILC_PUT32_MSB(l, cp)                   \
435 do {                                            \
436   (cp)[0] = (SilcUInt8)((l) >> 24);             \
437   (cp)[1] = (SilcUInt8)((l) >> 16);             \
438   (cp)[2] = (SilcUInt8)((l) >> 8);              \
439   (cp)[3] = (SilcUInt8)(l);                     \
440 } while(0)
441 /***/
442
443 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
444  *
445  * NAME
446  *
447  *    #define SILC_PUT64_MSB ...
448  *
449  * DESCRIPTION
450  *
451  *    Put eight 8-bit bytes, most significant bytes first.
452  *
453  * SOURCE
454  */
455 #define SILC_PUT64_MSB(l, cp)                                   \
456 do {                                                            \
457   SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp));    \
458   SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4);                    \
459 } while(0)
460 /***/
461
462 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
463  *
464  * NAME
465  *
466  *    #define SILC_PUT16_LSB ...
467  *
468  * DESCRIPTION
469  *
470  *    Put two 8-bit bytes, least significant bytes first.
471  *
472  * SOURCE
473  */
474 #if defined(SILC_I486) && defined(__GNUC__)
475 #define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
476 #else
477 #define SILC_PUT16_LSB(l, cp)                   \
478 do  {                                           \
479   (cp)[0] = (SilcUInt8)(l);                     \
480   (cp)[1] = (SilcUInt8)((l) >> 8);              \
481 } while(0)
482 #endif /* SILC_I486 && __GNUC__ */
483 /***/
484
485 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
486  *
487  * NAME
488  *
489  *    #define SILC_PUT32_LSB ...
490  *
491  * DESCRIPTION
492  *
493  *    Put four 8-bit bytes, least significant bytes first.
494  *
495  * SOURCE
496  */
497 #if defined(SILC_I486) && defined(__GNUC__)
498 #define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
499 #else
500 #define SILC_PUT32_LSB(l, cp)                   \
501 do {                                            \
502   (cp)[0] = (SilcUInt8)(l);                     \
503   (cp)[1] = (SilcUInt8)((l) >> 8);              \
504   (cp)[2] = (SilcUInt8)((l) >> 16);             \
505   (cp)[3] = (SilcUInt8)((l) >> 24);             \
506 } while(0)
507 #endif /* SILC_I486 && __GNUC__ */
508 /***/
509
510 /****d* silcutil/SILCTypes/SILC_SWAB_16
511  *
512  * NAME
513  *
514  *    #define SILC_SWAB_16 ...
515  *
516  * DESCRIPTION
517  *
518  *    Swabs 16-bit unsigned integer byte order.
519  *
520  * SOURCE
521  */
522 #define SILC_SWAB_16(l)                                         \
523   ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
524                (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
525 /***/
526
527 /****d* silcutil/SILCTypes/SILC_SWAB_32
528  *
529  * NAME
530  *
531  *    #define SILC_SWAB_32 ...
532  *
533  * DESCRIPTION
534  *
535  *    Swabs 32-bit unsigned integer byte order.
536  *
537  * SOURCE
538  */
539 #define SILC_SWAB_32(l)                                                 \
540   ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) |   \
541                (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8)  |   \
542                (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8)  |   \
543                (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
544 /***/
545
546 /****d* silcutil/SILCTypes/SILC_PTR_TO_32
547  *
548  * NAME
549  *
550  *    #define SILC_PTR_TO_32 ...
551  *
552  * DESCRIPTION
553  *
554  *    Type casts a pointer's value into a 32-bit integer.  Use this to
555  *    avoid compiler warnings when type casting pointers to integers
556  *    of different size.
557  *
558  * SOURCE
559  */
560 #if SILC_SIZEOF_VOID_P < 8
561 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
562 #else
563 #define SILC_PTR_TO_32(_ptr__)                                          \
564   ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
565 #endif
566 /***/
567
568 /****d* silcutil/SILCTypes/SILC_PTR_TO_64
569  *
570  * NAME
571  *
572  *    #define SILC_PTR_TO_64 ...
573  *
574  * DESCRIPTION
575  *
576  *    Type casts a pointer's value into a 64-bit integer.  Use this to
577  *    avoid compiler warnings when type casting pointers to integers
578  *    of different size.
579  *
580  * SOURCE
581  */
582 #if SILC_SIZEOF_VOID_P < 8
583 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
584 #else
585 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt64)(_ptr__)))
586 #endif
587 /***/
588
589 /****d* silcutil/SILCTypes/SILC_32_TO_PTR
590  *
591  * NAME
592  *
593  *    #define SILC_32_TO_PTR ...
594  *
595  * DESCRIPTION
596  *
597  *    Type casts a 32-bit integer value into a pointer.  Use this to
598  *    avoid compiler warnings when type casting integers to pointers of
599  *    different size.
600  *
601  * SOURCE
602  */
603 #if SILC_SIZEOF_VOID_P < 8
604 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
605 #else
606 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
607 #endif
608 /***/
609
610 /****d* silcutil/SILCTypes/SILC_64_TO_PTR
611  *
612  * NAME
613  *
614  *    #define SILC_64_TO_PTR ...
615  *
616  * DESCRIPTION
617  *
618  *    Type casts a 64-bit integer value into a pointer.  Use this to
619  *    avoid compiler warnings when type casting integers to pointers of
620  *    different size.
621  *
622  * SOURCE
623  */
624 #if SILC_SIZEOF_VOID_P < 8
625 #define SILC_64_TO_PTR(_ival__)                                         \
626   ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
627 #else
628 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
629 #endif
630 /***/
631
632 #endif /* SILCTYPES_H */