5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2008 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/Types and Definitions
24 * This header file includes basic types and definitions, and various system
25 * specific macros and functions used in SILC Toolkits. Application programmer
26 * may use them when needed.
33 /* The bool macro is deprecated. Use SilcBool instead. */
39 #define bool unsigned char
43 #if SILC_SIZEOF_SHORT > 2
44 #error "size of the short must be 2 bytes"
47 /****d* silcutil/SilcBool
51 * typedef unigned char SilcBool;
55 * Boolean value, and is always 8-bits. Represents value 0 or 1.
58 typedef unsigned char SilcBool;
68 * Boolean true value indicator.
77 /****d* silcutil/FALSE
85 * Boolean false value indicator.
94 /****d* silcutil/SilcUInt8
98 * typedef unsigned char SilcUInt8;
102 * 8-bit unsigned integer.
106 typedef unsigned char SilcUInt8;
109 /****d* silcutil/SilcInt8
113 * typedef signed char SilcInt8;
117 * 8-bit signed integer.
121 typedef signed char SilcInt8;
124 /****d* silcutil/SilcUInt16
128 * typedef unsigned short SilcUInt16;
132 * 16-bit unsigned integer. Guaranteed to be 16-bits.
136 typedef unsigned short SilcUInt16;
139 /****d* silcutil/SilcInt16
143 * typedef signed short SilcInt16;
147 * 16-bit signed integer. Guaranteed to be 16-bits.
151 typedef signed short SilcInt16;
154 /****d* silcutil/SilcUInt32
158 * typedef unsigned long SilcUInt32;
162 * 32-bit unsigned integer. Guaranteed to be 32-bits.
166 #if SILC_SIZEOF_LONG == 4
167 typedef unsigned long SilcUInt32;
168 typedef signed long SilcInt32;
170 #if SILC_SIZEOF_INT == 4
171 typedef unsigned int SilcUInt32;
172 typedef signed int SilcInt32;
174 #if SILC_SIZEOF_LONG_LONG >= 4
176 typedef unsigned long long SilcUInt32;
177 typedef signed long long SilcInt32;
184 /****d* silcutil/SilcInt32
188 * typedef signed long SilcInt32;
192 * 32-bit signed integer. Guaranteed to be 32-bits.
196 /****d* silcutil/SilcUInt64
200 * typedef unsigned long long SilcUInt64;
204 * 64-bit unsigned integer. Guaranteed to be 64-bits on systems that
209 #if SILC_SIZEOF_LONG >= 8
210 typedef unsigned long SilcUInt64;
211 typedef signed long SilcInt64;
213 #if SILC_SIZEOF_LONG_LONG >= 8
215 typedef unsigned long long SilcUInt64;
216 typedef signed long long SilcInt64;
218 typedef unsigned __int64 SilcUInt64;
219 typedef signed __int64 SilcInt64;
222 typedef SilcUInt32 SilcUInt64;
223 typedef SilcInt32 SilcInt64;
228 /****d* silcutil/SilcInt64
232 * typedef signed long long SilcInt64;
236 * 64-bit signed integer. Guaranteed to be 64-bits on systems that
241 /****d* silcutil/SilcFloat32
245 * typedef float SilcFloat32;
249 * 32-bit floating point number.
252 typedef float SilcFloat32;
254 /****d* silcutil/SilcFloat64
258 * typedef double SilcFloat64;
262 * 64-bit floating point number.
265 typedef double SilcFloat64
267 #if SILC_SIZEOF_VOID_P < 4
268 typedef SilcUInt32 * void *;
271 /****d* silcutil/SilcSocket
279 * Platform specific socket. On POSIX compliant systems this is simply
280 * an integer, representing the socket. On other systems it is platform
281 * specific socket context. Access it only through routines that can
282 * handle SilcSocket types.
286 #if defined(SILC_UNIX)
287 typedef int SilcSocket;
288 #elif defined(SILC_WIN32)
289 typedef SOCKET SilcSocket;
290 #elif defined(SILC_SYMBIAN)
291 typedef void * SilcSocket;
295 /****d* silcutil/SilcParam
299 * typedef SilcUInt32 SilcParam;
303 * A generic parameters that describe the type of an parameter or argument.
304 * They can be used to describe function arguments, buffer encoding format,
309 typedef SilcUInt32 SilcParam;
311 #define SILC_PARAM_SINT8 1 /* SilcInt8 */
312 #define SILC_PARAM_UINT8 2 /* SilcUInt8 */
313 #define SILC_PARAM_SINT16 3 /* SilcInt16 */
314 #define SILC_PARAM_UINT16 4 /* SilcUInt16 */
315 #define SILC_PARAM_SINT32 5 /* SilcInt32 */
316 #define SILC_PARAM_UINT32 6 /* SilcUInt32 */
317 #define SILC_PARAM_SINT64 7 /* SilcInt64 */
318 #define SILC_PARAM_UINT64 8 /* SilcUInt64 */
319 #define SILC_PARAM_SICHAR 9 /* signed char * */
320 #define SILC_PARAM_UICHAR 10 /* unsigned char * */
321 #define SILC_PARAM_BUFFER 11 /* SilcBuffer */
322 #define SILC_PARAM_PTR 12 /* void * */
323 #define SILC_PARAM_END 0xfeeefff1 /* End of parameters */
326 /* Internal parameter types, not publicly documented, used mainly by the
327 SILC Buffer Format API (silcbuffmt.h). */
328 #define SILC_PARAM_UI8_STRING 100 /* String (max len 8-bits) */
329 #define SILC_PARAM_UI16_STRING 101 /* String (max len 16-bits) */
330 #define SILC_PARAM_UI32_STRING 102 /* String (max len 32-bits) */
331 #define SILC_PARAM_UI8_NSTRING 103 /* String (max len 8-bits) */
332 #define SILC_PARAM_UI16_NSTRING 104 /* String (max len 16-bits) */
333 #define SILC_PARAM_UI32_NSTRING 105 /* String (max len 32-bits) */
334 #define SILC_PARAM_OFFSET 106
335 #define SILC_PARAM_ADVANCE 107
336 #define SILC_PARAM_FUNC 108
337 #define SILC_PARAM_REGEX 109
338 #define SILC_PARAM_OFFSET_START 110
339 #define SILC_PARAM_OFFSET_END 111
340 #define SILC_PARAM_DELETE 112
341 #define SILC_PARAM_ALLOC 0x00010000 /* Allocate, bitmask */
342 #define SILC_PARAM_REPLACE 0x00020000 /* Replace, bitmask */
346 #if (defined(SILC_I486) || defined(SILC_X86_64)) && defined(__GNUC__)
347 #define SILC_GET_WORD(cp) \
349 SilcUInt32 _result_; \
350 asm volatile ("movl (%1), %0; bswapl %0" \
351 : "=q" (_result_) : "q" (cp)); \
355 #define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
356 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
357 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
358 | ((SilcUInt32)(SilcUInt8)(cp)[3])
359 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
361 /****d* silcutil/SILC_GET16_MSB
365 * #define SILC_GET16_MSB(dest, src)
369 * Return two 8-bit bytes, most significant bytes first.
373 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
374 #define SILC_GET16_MSB(l, cp) \
375 asm volatile ("movw (%1), %w0; rolw $8, %w0" \
376 : "=q" (l) : "q" (cp) : "memory", "cc");
378 #define SILC_GET16_MSB(l, cp) \
380 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8) \
381 | ((SilcUInt32)(SilcUInt8)(cp)[1]); \
383 #endif /* (SILC_I386 || SILC_X86_64) && __GNUC__ */
386 /****d* silcutil/SILC_GET32_MSB
390 * #define SILC_GET32_MSB(dest, src)
394 * Return four 8-bit bytes, most significant bytes first.
398 #if (defined(SILC_I486) || defined(SILC_X86_64)) && defined(__GNUC__)
399 #define SILC_GET32_MSB(l, cp) \
400 asm volatile ("movl (%1), %0; bswapl %0" \
401 : "=q" (l) : "q" (cp) : "memory", "cc");
403 #define SILC_GET32_MSB(l, cp) \
405 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
406 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
407 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
408 | ((SilcUInt32)(SilcUInt8)(cp)[3]); \
410 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
413 /* Same as upper but XOR the result always. Special purpose macro. */
414 #if (defined(SILC_I486) || defined(SILC_X86_64)) && defined(__GNUC__)
415 #define SILC_GET32_X_MSB(l, cp) \
417 register volatile SilcUInt32 _x_; \
418 asm volatile ("movl %1, %3; movl (%2), %0;\n\t" \
419 "bswapl %0; xorl %3, %0" \
420 : "=r" (l) : "0" (l), "r" (cp), "r" (_x_) \
424 #define SILC_GET32_X_MSB(l, cp) \
425 (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
426 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
427 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
428 | ((SilcUInt32)(SilcUInt8)(cp)[3]);
429 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
431 /****d* silcutil/SILC_GET64_MSB
435 * #define SILC_GET64_MSB(dest, src)
439 * Return eight 8-bit bytes, most significant bytes first.
443 #if defined(SILC_X86_64) && defined(__GNUC__)
444 #define SILC_GET64_MSB(l, cp) \
445 asm volatile ("movq (%1), %0; bswapq %0" \
446 : "=r" (l) : "r" (cp) : "memory", "cc");
448 #define SILC_GET64_MSB(l, cp) \
450 (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) | \
451 ((SilcUInt64)SILC_GET_WORD((cp) + 4))); \
453 #endif /* SILC_X86_64 && __GNUC__ */
456 /****d* silcutil/SILC_GET16_LSB
460 * #define SILC_GET16_MSB(dest, src)
464 * Return two 8-bit bytes, least significant bytes first.
468 #if defined(SILC_I386) || defined(SILC_X86_64)
469 #define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
471 #define SILC_GET16_LSB(l, cp) \
473 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
474 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8); \
476 #endif /* SILC_I386 || SILC_X86_64 */
479 /****d* silcutil/SILC_GET32_LSB
483 * #define SILC_GET32_LSB(dest, src)
487 * Return four 8-bit bytes, least significant bytes first.
491 #if defined(SILC_I386) || defined(SILC_X86_64)
492 #define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
494 #define SILC_GET32_LSB(l, cp) \
496 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
497 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
498 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
499 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24); \
501 #endif /* SILC_I386 || SILC_X86_64 */
504 /* Same as upper but XOR the result always. Special purpose macro. */
505 #if defined(SILC_I386) || defined(SILC_X86_64)
506 #define SILC_GET32_X_LSB(l, cp) (l) ^= (*(SilcUInt32 *)(cp))
508 #define SILC_GET32_X_LSB(l, cp) \
509 (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0]) \
510 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
511 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
512 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
513 #endif /* SILC_I386 || SILC_X86_64 */
515 /****d* silcutil/SILC_PUT16_MSB
519 * #define SILC_PUT16_MSB(dest, src)
523 * Put two 8-bit bytes, most significant bytes first.
527 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
528 #define SILC_PUT16_MSB(l, cp) \
529 asm volatile ("rolw $8, %w1; movw %w1, (%0)" \
530 : : "q" (cp), "q" (l) : "memory", "cc");
532 #define SILC_PUT16_MSB(l, cp) \
534 (cp)[0] = (SilcUInt8)((l) >> 8); \
535 (cp)[1] = (SilcUInt8)(l); \
537 #endif /* (SILC_I386 || SILC_X86_64) && __GNUC__ */
540 /****d* silcutil/SILC_PUT32_MSB
544 * #define SILC_PUT32_MSB(dest, src)
548 * Put four 8-bit bytes, most significant bytes first.
552 #if (defined(SILC_I486) || defined(SILC_X86_64)) && defined(__GNUC__)
553 #define SILC_PUT32_MSB(l, cp) \
554 asm volatile ("bswapl %1; movl %1, (%0); bswapl %1" \
555 : : "q" (cp), "q" (l) : "memory", "cc");
557 #define SILC_PUT32_MSB(l, cp) \
559 (cp)[0] = (SilcUInt8)((l) >> 24); \
560 (cp)[1] = (SilcUInt8)((l) >> 16); \
561 (cp)[2] = (SilcUInt8)((l) >> 8); \
562 (cp)[3] = (SilcUInt8)(l); \
564 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
567 /****d* silcutil/SILC_PUT64_MSB
571 * #define SILC_PUT64_MSB(dest, src)
575 * Put eight 8-bit bytes, most significant bytes first.
579 #if defined(SILC_X86_64) && defined(__GNUC__)
580 #define SILC_PUT64_MSB(l, cp) \
581 asm volatile ("bswapq %1; movq %1, (%0); bswapq %1" \
582 : : "r" (cp), "r" (l) : "memory", "cc");
584 #define SILC_PUT64_MSB(l, cp) \
586 SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp)); \
587 SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4); \
589 #endif /* SILC_X86_64 && __GNUC__ */
592 /****d* silcutil/SILC_PUT16_LSB
596 * #define SILC_PUT16_LSB(dest, src)
600 * Put two 8-bit bytes, least significant bytes first.
604 #if defined(SILC_I386) || defined(SILC_X86_64)
605 #define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
607 #define SILC_PUT16_LSB(l, cp) \
609 (cp)[0] = (SilcUInt8)(l); \
610 (cp)[1] = (SilcUInt8)((l) >> 8); \
612 #endif /* SILC_I386 || SILC_X86_64 */
615 /****d* silcutil/SILC_PUT32_LSB
619 * #define SILC_PUT32_LSB(dest, src)
623 * Put four 8-bit bytes, least significant bytes first.
627 #if defined(SILC_I386) || defined(SILC_X86_64)
628 #define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
630 #define SILC_PUT32_LSB(l, cp) \
632 (cp)[0] = (SilcUInt8)(l); \
633 (cp)[1] = (SilcUInt8)((l) >> 8); \
634 (cp)[2] = (SilcUInt8)((l) >> 16); \
635 (cp)[3] = (SilcUInt8)((l) >> 24); \
637 #endif /* SILC_I386 || SILC_X86_64 */
640 /****d* silcutil/SILC_SWAB_16
644 * #define SILC_SWAB_16(integer)
648 * Swabs 16-bit unsigned integer byte order. Returns the new value.
652 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
653 #define SILC_SWAB_16(l) \
655 SilcUInt16 _result_; \
656 asm volatile ("movw %w1, %w0; rolw $8, %w0" \
657 : "=q" (_result_) : "q" (l)); \
661 #define SILC_SWAB_16(l) \
662 ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
663 (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
664 #endif /* (SILC_I386 || SILC_X86_64) && __GNUC__ */
667 /****d* silcutil/SILC_SWAB_32
671 * #define SILC_SWAB_32(integer)
675 * Swabs 32-bit unsigned integer byte order. Returns the new value.
679 #if (defined(SILC_I486) || defined(SILC_X86_64)) && defined(__GNUC__)
680 #define SILC_SWAB_32(l) \
682 SilcUInt32 _result_; \
683 asm volatile ("movl %1, %0; bswapl %0" \
684 : "=q" (_result_) : "q" (l)); \
688 #define SILC_SWAB_32(l) \
689 ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) | \
690 (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8) | \
691 (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8) | \
692 (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
693 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
696 /****d* silcutil/SILC_PTR_TO_32
700 * #define SILC_PTR_TO_32(ptr)
704 * Type casts a pointer's value into a 32-bit integer. Use this to
705 * avoid compiler warnings when type casting pointers to integers
710 #if SILC_SIZEOF_VOID_P < 8
711 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
713 #define SILC_PTR_TO_32(_ptr__) \
714 ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
718 /****d* silcutil/SILC_PTR_TO_64
722 * #define SILC_PTR_TO_64(ptr)
726 * Type casts a pointer's value into a 64-bit integer. Use this to
727 * avoid compiler warnings when type casting pointers to integers
732 #if SILC_SIZEOF_VOID_P < 8
733 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
735 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt64)(_ptr__)))
739 /****d* silcutil/SILC_32_TO_PTR
743 * #define SILC_32_TO_PTR(ptr)
747 * Type casts a 32-bit integer value into a pointer. Use this to
748 * avoid compiler warnings when type casting integers to pointers of
753 #if SILC_SIZEOF_VOID_P < 8
754 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
756 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
760 /****d* silcutil/SILC_64_TO_PTR
764 * #define SILC_64_TO_PTR(ptr)
768 * Type casts a 64-bit integer value into a pointer. Use this to
769 * avoid compiler warnings when type casting integers to pointers of
774 #if SILC_SIZEOF_VOID_P < 8
775 #define SILC_64_TO_PTR(_ival__) \
776 ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
778 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
782 /****d* silcutil/SILC_ASSERT
786 * #define SILC_ASSERT(experssion)
790 * Assert macro that prints error message to stderr and calls abort()
791 * if the `expression' is false (ie. compares equal to zero). If
792 * SILC_DEBUG is not defined this macro has no effect.
796 #if defined(SILC_DEBUG)
797 #define SILC_ASSERT(expr) assert((expr));
799 #define SILC_ASSERT(expr) do { } while(0)
800 #endif /* SILC_DEBUG */
803 /****d* silcutil/SILC_VERIFY
807 * #define SILC_VERIFY(experssion)
811 * Verification macro that prints error message to stderr and calls
812 * abort() if the `expression' is false (ie. compares equal to zero)
813 * on debug builds (SILC_DEBUG defined), and prints error message to
814 * stderr on release builds (SILC_DEBUG undefined) but does not abort().
815 * This macro is always compiled even if debugging (SILC_DEBUG) is not
820 #if defined(SILC_DEBUG)
821 #define SILC_VERIFY(expr) assert((expr));
823 #define SILC_VERIFY(expr) \
824 if (silc_unlikely(!(expr))) { \
825 SILC_LOG_ERROR(("SILC_VERIFY %s:%s:%d", \
826 __FILE__, __FUNCTION__, __LINE__)); \
827 silc_set_errno_reason_nofail(SILC_ERR_ASSERT, "SILC_VERIFY %s:%s:%d", \
828 __FILE__, __FUNCTION__, __LINE__); \
830 #endif /* SILC_DEBUG */
833 /****d* silcutil/silc_rol
837 * static inline SilcUInt32 silc_rol(SilcUInt32 val, int num);
841 * Rotate 32-bit integer's bits to left `num' times. Bits pushed to the
842 * left will appear from the right side of the integer, thus rotating.
843 * Returns the rotated value.
846 static inline SilcUInt32 silc_rol(SilcUInt32 val, int num)
848 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
849 asm volatile ("roll %%cl, %0"
850 : "=r" (val) : "0" (val), "c" (num));
853 return ((val << (SilcUInt32)num) | (val >> (32 - (SilcUInt32)num)));
854 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
857 /****d* silcutil/silc_rolc
861 * static inline SilcUInt32 silc_rolc(SilcUInt32 val, const int num);
865 * Rotate 32-bit integer's bits to left `num' times. Bits pushed to the
866 * left will appear from the right side of the integer, thus rotating.
867 * Returns the rotated value.
870 static inline SilcUInt32 silc_rolc(SilcUInt32 val, const int num)
872 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
873 asm volatile ("roll %2, %0"
874 : "=r" (val) : "0" (val), "I" (num));
877 return ((val << (SilcUInt32)num) | (val >> (32 - (SilcUInt32)num)));
878 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
881 /****d* silcutil/silc_ror
885 * static inline SilcUInt32 silc_ror(SilcUInt32 val, int num);
889 * Rotate 32-bit integer's bits to right `num' times. Bits pushed to the
890 * right will appear from the left side of the integer, thus rotating.
891 * Returns the rotated value.
894 static inline SilcUInt32 silc_ror(SilcUInt32 val, int num)
896 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
897 asm volatile ("rorl %%cl, %0"
898 : "=r" (val) : "0" (val), "c" (num));
901 return ((val >> (SilcUInt32)num) | (val << (32 - (SilcUInt32)num)));
902 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
905 /****d* silcutil/silc_rorc
909 * static inline SilcUInt32 silc_ror(SilcUInt32 val, const int num);
913 * Rotate 32-bit integer's bits to right `num' times. Bits pushed to the
914 * right will appear from the left side of the integer, thus rotating.
915 * Returns the rotated value.
918 static inline SilcUInt32 silc_rorc(SilcUInt32 val, const int num)
920 #if (defined(SILC_I386) || defined(SILC_X86_64)) && defined(__GNUC__)
921 asm volatile ("rorl %2, %0"
922 : "=r" (val) : "0" (val), "I" (num));
925 return ((val >> (SilcUInt32)num) | (val << (32 - (SilcUInt32)num)));
926 #endif /* (SILC_I486 || SILC_X86_64) && __GNUC__ */
929 /****d* silcutil/silc_rol64
933 * static inline SilcUInt64 silc_rol64(SilcUInt64 val, int num);
937 * Rotate 64-bit integer's bits to left `num' times. Bits pushed to the
938 * left will appear from the right side of the integer, thus rotating.
939 * Returns the rotated value.
942 static inline SilcUInt64 silc_rol64(SilcUInt64 val, int num)
944 #if defined(SILC_X86_64) && defined(__GNUC__)
945 asm volatile ("rolq %%cl, %0"
946 : "=r" (val) : "0" (val), "c" (num));
949 return ((val << (SilcUInt64)num) | (val >> (64 - (SilcUInt64)num)));
950 #endif /* SILC_X86_64 && __GNUC__ */
953 /****d* silcutil/silc_rolc64
957 * static inline SilcUInt64 silc_rolc64(SilcUInt64 val, const int num);
961 * Rotate 64-bit integer's bits to left `num' times. Bits pushed to the
962 * left will appear from the right side of the integer, thus rotating.
963 * Returns the rotated value.
966 static inline SilcUInt64 silc_rolc64(SilcUInt64 val, const int num)
968 #if defined(SILC_X86_64) && defined(__GNUC__)
969 asm volatile ("rolq %2, %0"
970 : "=r" (val) : "0" (val), "J" (num));
973 return ((val << (SilcUInt64)num) | (val >> (64 - (SilcUInt64)num)));
974 #endif /* SILC_X86_64 && __GNUC__ */
977 /****d* silcutil/silc_ror64
981 * static inline SilcUInt64 silc_ror64(SilcUInt64 val, int num);
985 * Rotate 64-bit integer's bits to right `num' times. Bits pushed to the
986 * right will appear from the left side of the integer, thus rotating.
987 * Returns the rotated value.
990 static inline SilcUInt64 silc_ror64(SilcUInt64 val, int num)
992 #if defined(SILC_X86_64) && defined(__GNUC__)
993 asm volatile ("rorq %%cl, %0"
994 : "=r" (val) : "0" (val), "c" (num));
997 return ((val >> (SilcUInt64)num) | (val << (64 - (SilcUInt64)num)));
998 #endif /* SILC_X86_64 && __GNUC__ */
1001 /****d* silcutil/silc_rorc64
1005 * static inline SilcUInt64 silc_rorc64(SilcUInt64 val, const int num);
1009 * Rotate 64-bit integer's bits to right `num' times. Bits pushed to the
1010 * right will appear from the left side of the integer, thus rotating.
1011 * Returns the rotated value.
1014 static inline SilcUInt64 silc_rorc64(SilcUInt64 val, const int num)
1016 #if defined(SILC_X86_64) && defined(__GNUC__)
1017 asm volatile ("rorq %2, %0"
1018 : "=r" (val) : "0" (val), "J" (num));
1021 return ((val >> (SilcUInt64)num) | (val << (64 - (SilcUInt64)num)));
1022 #endif /* SILC_X86_64 && __GNUC__ */
1025 /****d* silcutil/silc_offsetof
1029 * #define silc_offsetof(TYPE, MEMBER)
1033 * offsetof() macro replacement. Use this instead of offsetof().
1036 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1038 /****d* silcutil/silc_attribute
1042 * #define silc_attribute(attrlist)
1046 * Compiler attributes. If compiler doesn't support attributes this macro
1047 * doesn't do anything. Currently this works only with GCC compiler.
1048 * See GCC documentation for specified attributes.
1052 * int printf(const char *fmt, ...) silc_attribute((format(printf, 1, 2)));
1055 #if defined(__GNUC__)
1056 #define silc_attribute(attrlist) __attribute__ (attrlist)
1058 #define silc_attribute(attrlist)
1059 #endif /* __GNUC__ */
1061 /****d* silcutil/silc_likely
1065 * #define silc_likely(expression)
1069 * Branch prediction macro. It specifies that it is likely that the branch
1070 * where silc_likely is applied is taken. Compiler will optimize the
1071 * code based on this prediction. Never use this before you have profiled
1076 /****d* silcutil/silc_unlikely
1080 * #define silc_unlikely(expression)
1084 * Branch prediction macro. It specifies that it is unlikely that the
1085 * branch where silc_unlikely is applied is taken. Compiler will optimize
1086 * the code based on this prediction. Never use this before you have
1087 * profiled the code first.
1091 #define silc_likely(expr) __builtin_expect(!!(expr), 1)
1092 #define silc_unlikely(expr) __builtin_expect(!!(expr), 0)
1094 #define silc_likely(expr) (expr)
1095 #define silc_unlikely(expr) (expr)
1096 #endif /* __GNUC__ >= 3 */
1098 /* Prefetch operations. Use these to prefetch data to CPU cache before
1099 reading or writing if you think that the data will be needed soon after
1102 /****d* silcutil/silc_prefetch
1106 * static inline void silc_prefetch(void *addr, int rw, int locality);
1110 * Simple prefetch. Loads memory from specified address to CPU cache.
1111 * The amount of data loaded is CPU dependant (cache line length). The
1112 * `rw' argument defines the reason for prefetch: 0=read, 1=write. The
1113 * `locality' argument defines the locality of the prefetch, 0=non-temporal
1114 * (non-temporal cache, cache closest to CPU, data will not stay long in
1115 * the cache), 1=temporal (L2+ cache), 2=temporal (L2, L3+ cache),
1116 * 3=temporal (fetch to all caches, data stays longer time in cache).
1120 * This produces only a hint for CPU. CPU doesn't have to actually
1121 * prefetch the data. Use silc_prefetch_block to ensure CPU always
1126 static inline silc_attribute((always_inline))
1127 void silc_prefetch(void *addr, int rw, int locality)
1130 __builtin_prefetch(addr, rw, locality);
1131 #endif /* __GNUC__ */
1134 /****d* silcutil/silc_prefetch_block
1138 * static inline void silc_prefetch_block(void *addr,
1139 * int prefetch_length,
1140 * const int cache_line_length)
1144 * Enforced block prefetch. This function loads the specified amount
1145 * `prefetch_length' of memory from the specified address `addr' to CPU
1146 * cache with each loaded cache line being the size of `cache_line_length'.
1147 * If you don't know the cache line size use 64 bytes. Note that, the
1148 * `cache_line_length' is a const int. In this context this mean its
1149 * value must not come from a variable but must be a constant (the code
1150 * won't compile if it comes from a variable).
1152 * The `prefetch_length' must be multiple of twice of the
1153 * `cache_line_length' or 128 if you don't know the cache line size, hence
1154 * the minimum length for `prefetch_length' is 128 bytes when the
1155 * `cache_line_length' is 64 bytes. Shorter cache line length (32 bytes)
1158 * You should use the correct `cache_line_length' value for your CPU or
1159 * the value of the CPU for which you want to optimize your code. Intel
1160 * CPUs usually have cache size of 32 or 64 bytes. The most optimal
1161 * prefetch is achieved if the `cache_line_length' is the actual CPU cache
1162 * line size. Always do performance testing with and without prefetching
1163 * to make sure the prefetch actually helps. If used improperly, it may
1164 * slow down your program.
1166 * The difference to silc_prefetch is that this function always performs
1167 * the prefetch and has the ability to prefetch more than one cache line
1168 * worth of memory, whereas silc_prefetch can prefetch only one cache line
1169 * and may not do the prefetch at all.
1173 static inline silc_attribute((always_inline))
1174 void silc_prefetch_block(void *addr,
1175 int prefetch_length,
1176 const int cache_line_length)
1179 SILC_ASSERT(cache_line_length >= 32);
1180 SILC_ASSERT(cache_line_length % 32 == 0);
1181 SILC_ASSERT(prefetch_length >= cache_line_length);
1182 SILC_ASSERT(prefetch_length % (cache_line_length * 2) == 0);
1185 #if SILC_SIZEOF_VOID_P < 8
1186 #define SILC_PREFETCH_UINT SilcUInt32
1188 #define SILC_PREFETCH_UINT SilcUInt64
1189 #endif /* SILC_SIZEOF_VOID_P < 8 */
1191 #if defined(__GNUC__) && (defined(SILC_I386) || defined(SILC_X86_64))
1193 /* Assembler implementation.
1195 The idea here is to simply enforce the CPU to load the requested amount
1196 of bytes to cache. We simply mov data from the memory to a register.
1197 Each mov will load a full cache line worth of data from the memory.
1199 We expect the `cache_line_length' to be the actual cache line size.
1200 It doesn't matter if it is. If it is smaller the prefetch is a bit
1201 slower as there is redundancy. If it is larger we skip some of the
1202 data and don't prefetch everything.
1204 The loop is unrolled to handle two mov's at once, this why we expect
1205 the `prefetch_length' to be multiple of twice the length of
1206 `cache_line_length`. We also mov the data from end to beginning instead
1207 of from the beginning to assure CPU doesn't prefetch the data before
1208 we actually want to do it.
1210 This technique is described by AMD in:
1211 http://cdrom.amd.com/devconn/events/AMD_block_prefetch_paper.pdf */
1214 SILC_PREFETCH_UINT temp;
1216 #define SILC_PREFETCH_ASM(ip, rp) \
1217 asm volatile ("1: \n\t" \
1218 "mov" ip " -%c4(%2, %" rp "3), %0 \n\t" \
1219 "mov" ip " -%c5(%2, %" rp "3), %0 \n\t" \
1220 "sub" ip " %5, %" rp "3 \n\t" \
1222 : "=&r" (temp), "=r" (prefetch_length) \
1223 : "r" (addr), "1" (prefetch_length), \
1224 "Z" (cache_line_length), \
1225 "Z" (cache_line_length * 2) \
1228 #if defined(SILC_I386)
1229 /* 32-bit prefetch */
1230 SILC_PREFETCH_ASM("l", "");
1232 /* 64-bit prefetch */
1233 SILC_PREFETCH_ASM("q", "q");
1234 #endif /* SILC_I386 */
1238 /* C implementation. Yes, you can do it in C too. In fact, we'll try to
1239 make the compiler generate nearly identical code to the above assembler
1240 code. Note that, the memory access must be volatile, otherwise the
1241 compiler will optimize them away because the temp variable isn't actually
1242 used for anything. This should be as fast as the assembler code above,
1243 unless the compiler decides to start meddling with it (don't use
1244 -funroll-loops with this code). */
1247 register unsigned char *a = addr;
1248 register int len = prefetch_length;
1249 register SILC_PREFETCH_UINT temp;
1252 temp = *(SILC_PREFETCH_UINT volatile *)
1253 (a + (len - cache_line_length));
1254 temp = *(SILC_PREFETCH_UINT volatile *)
1255 (a + (len - (cache_line_length * 2)));
1256 len -= (cache_line_length * 2);
1259 #endif /* __GNUC__ */
1260 #undef SILC_PREFETCH_UINT
1261 #undef SILC_PREFETCH_ASM
1264 #endif /* SILCTYPES_H */