5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2006 Pekka Riikonen
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.
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.
20 /****h* silcutil/SILC Types
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
34 /****d* silcutil/SILCTypes/TRUE
42 * Boolean true value indicator.
51 /****d* silcutil/SILCTypes/FALSE
59 * Boolean false value indicator.
68 /****d* silcutil/SILCTypes/SilcBool
72 * typedef unigned char SilcBool;
76 * Boolean value, and is always 8-bits. Represents value 0 or 1.
79 typedef unsigned char SilcBool;
81 /****d* silcutil/SILCTypes/bool
85 * #define SilcBool ...
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
95 * This macro is deprecated. Use SilcBool instead.
105 #define bool unsigned char
110 /* Our offsetof macro */
111 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
113 /* silc_likely and silc_unlikely GCC branch prediction macros. Use only if
114 you have profiled the code first. */
116 #define silc_likely(expr) __builtin_expect(!!(expr), 1)
117 #define silc_unlikely(expr) __builtin_expect(!!(expr), 0)
119 #define silc_likely(expr) (expr)
120 #define silc_unlikely(expr) (expr)
121 #endif /* __GNUC__ >= 3 */
123 #if SILC_SIZEOF_SHORT > 2
124 #error "size of the short must be 2 bytes"
127 /****d* silcutil/SILCTypes/SilcUInt8
131 * typedef unsigned char SilcUInt8;
135 * 8-bit unsigned integer.
139 typedef unsigned char SilcUInt8;
142 /****d* silcutil/SILCTypes/SilcInt8
146 * typedef signed char SilcInt8;
150 * 8-bit signed integer.
154 typedef signed char SilcInt8;
157 /****d* silcutil/SILCTypes/SilcUInt16
161 * typedef unsigned short SilcUInt16;
165 * 16-bit unsigned integer. Guaranteed to be 16-bits.
169 typedef unsigned short SilcUInt16;
172 /****d* silcutil/SILCTypes/SilcInt16
176 * typedef signed short SilcInt16;
180 * 16-bit signed integer. Guaranteed to be 16-bits.
184 typedef signed short SilcInt16;
187 /****d* silcutil/SILCTypes/SilcUInt32
191 * typedef unsigned long SilcUInt32;
195 * 32-bit unsigned integer. Guaranteed to be 32-bits.
199 #if SILC_SIZEOF_LONG == 4
200 typedef unsigned long SilcUInt32;
201 typedef signed long SilcInt32;
203 #if SILC_SIZEOF_INT == 4
204 typedef unsigned int SilcUInt32;
205 typedef signed int SilcInt32;
207 #if SILC_SIZEOF_LONG_LONG >= 4
209 typedef unsigned long long SilcUInt32;
210 typedef signed long long SilcInt32;
217 /****d* silcutil/SILCTypes/SilcInt32
221 * typedef signed long SilcInt32;
225 * 32-bit signed integer. Guaranteed to be 32-bits.
229 /****d* silcutil/SILCTypes/SilcUInt64
233 * typedef unsigned long long SilcUInt64;
237 * 64-bit unsigned integer. Guaranteed to be 64-bits on systems that
242 #if SILC_SIZEOF_LONG >= 8
243 typedef unsigned long SilcUInt64;
244 typedef signed long SilcInt64;
246 #if SILC_SIZEOF_LONG_LONG >= 8
248 typedef unsigned long long SilcUInt64;
249 typedef signed long long SilcInt64;
251 typedef unsigned __int64 SilcUInt64;
252 typedef signed __int64 SilcInt64;
255 typedef SilcUInt32 SilcUInt64;
256 typedef SilcInt32 SilcInt64;
261 /****d* silcutil/SILCTypes/SilcInt64
265 * typedef signed long long SilcInt64;
269 * 64-bit signed integer. Guaranteed to be 64-bits on systems that
274 #if SILC_SIZEOF_VOID_P < 4
275 typedef SilcUInt32 * void *;
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])
285 /****d* silcutil/SILCTypes/SILC_GET16_MSB
289 * #define SILC_GET16_MSB ...
293 * Return two 8-bit bytes, most significant bytes first.
297 #define SILC_GET16_MSB(l, cp) \
299 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8) \
300 | ((SilcUInt32)(SilcUInt8)(cp)[1]); \
304 /****d* silcutil/SILCTypes/SILC_GET32_MSB
308 * #define SILC_GET32_MSB ...
312 * Return four 8-bit bytes, most significant bytes first.
316 #define SILC_GET32_MSB(l, cp) \
318 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
319 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
320 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
321 | ((SilcUInt32)(SilcUInt8)(cp)[3]); \
325 /****d* silcutil/SILCTypes/SILC_GET64_MSB
329 * #define SILC_GET64_MSB ...
333 * Return eight 8-bit bytes, most significant bytes first.
337 #define SILC_GET64_MSB(l, cp) \
339 (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) | \
340 ((SilcUInt64)SILC_GET_WORD((cp) + 4))); \
344 /****d* silcutil/SILCTypes/SILC_GET16_LSB
348 * #define SILC_GET16_MSB ...
352 * Return two 8-bit bytes, least significant bytes first.
356 #if defined(SILC_I486) && defined(__GNUC__)
357 #define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
359 #define SILC_GET16_LSB(l, cp) \
361 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
362 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8); \
364 #endif /* SILC_I486 && __GNUC__ */
367 /****d* silcutil/SILCTypes/SILC_GET32_LSB
371 * #define SILC_GET32_LSB ...
375 * Return four 8-bit bytes, least significant bytes first.
379 #if defined(SILC_I486) && defined(__GNUC__)
380 #define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
382 #define SILC_GET32_LSB(l, cp) \
384 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
385 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
386 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
387 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24); \
389 #endif /* SILC_I486 && __GNUC__ */
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))
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__ */
403 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
407 * #define SILC_PUT16_MSB ...
411 * Put two 8-bit bytes, most significant bytes first.
415 #define SILC_PUT16_MSB(l, cp) \
417 (cp)[0] = (SilcUInt8)((l) >> 8); \
418 (cp)[1] = (SilcUInt8)(l); \
422 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
426 * #define SILC_PUT32_MSB ...
430 * Put four 8-bit bytes, most significant bytes first.
434 #define SILC_PUT32_MSB(l, cp) \
436 (cp)[0] = (SilcUInt8)((l) >> 24); \
437 (cp)[1] = (SilcUInt8)((l) >> 16); \
438 (cp)[2] = (SilcUInt8)((l) >> 8); \
439 (cp)[3] = (SilcUInt8)(l); \
443 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
447 * #define SILC_PUT64_MSB ...
451 * Put eight 8-bit bytes, most significant bytes first.
455 #define SILC_PUT64_MSB(l, cp) \
457 SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp)); \
458 SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4); \
462 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
466 * #define SILC_PUT16_LSB ...
470 * Put two 8-bit bytes, least significant bytes first.
474 #if defined(SILC_I486) && defined(__GNUC__)
475 #define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
477 #define SILC_PUT16_LSB(l, cp) \
479 (cp)[0] = (SilcUInt8)(l); \
480 (cp)[1] = (SilcUInt8)((l) >> 8); \
482 #endif /* SILC_I486 && __GNUC__ */
485 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
489 * #define SILC_PUT32_LSB ...
493 * Put four 8-bit bytes, least significant bytes first.
497 #if defined(SILC_I486) && defined(__GNUC__)
498 #define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
500 #define SILC_PUT32_LSB(l, cp) \
502 (cp)[0] = (SilcUInt8)(l); \
503 (cp)[1] = (SilcUInt8)((l) >> 8); \
504 (cp)[2] = (SilcUInt8)((l) >> 16); \
505 (cp)[3] = (SilcUInt8)((l) >> 24); \
507 #endif /* SILC_I486 && __GNUC__ */
510 /****d* silcutil/SILCTypes/SILC_SWAB_16
514 * #define SILC_SWAB_16 ...
518 * Swabs 16-bit unsigned integer byte order.
522 #define SILC_SWAB_16(l) \
523 ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
524 (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
527 /****d* silcutil/SILCTypes/SILC_SWAB_32
531 * #define SILC_SWAB_32 ...
535 * Swabs 32-bit unsigned integer byte order.
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))
546 /****d* silcutil/SILCTypes/SILC_PTR_TO_32
550 * #define SILC_PTR_TO_32 ...
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
560 #if SILC_SIZEOF_VOID_P < 8
561 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
563 #define SILC_PTR_TO_32(_ptr__) \
564 ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
568 /****d* silcutil/SILCTypes/SILC_PTR_TO_64
572 * #define SILC_PTR_TO_64 ...
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
582 #if SILC_SIZEOF_VOID_P < 8
583 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
585 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt64)(_ptr__)))
589 /****d* silcutil/SILCTypes/SILC_32_TO_PTR
593 * #define SILC_32_TO_PTR ...
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
603 #if SILC_SIZEOF_VOID_P < 8
604 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
606 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
610 /****d* silcutil/SILCTypes/SILC_64_TO_PTR
614 * #define SILC_64_TO_PTR ...
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
624 #if SILC_SIZEOF_VOID_P < 8
625 #define SILC_64_TO_PTR(_ival__) \
626 ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
628 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
632 #endif /* SILCTYPES_H */