ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libecb/ecb.h
(Generate patch)

Comparing libecb/ecb.h (file contents):
Revision 1.4 by root, Thu May 26 17:26:03 2011 UTC vs.
Revision 1.12 by root, Thu May 26 18:17:45 2011 UTC

1/* 1/*
2 * libecb 2 * libecb
3 * 3 *
4 * Copyright (©) 2009-2011 Marc Alexander Lehmann 4 * Copyright (©) 2009-2011 Marc Alexander Lehmann
5 * Copyright (©) 2011 Emanuele Giaquinta
5 * All rights reserved. 6 * All rights reserved.
6 * 7 *
7 * Redistribution and use in source and binary forms, with or without modifica- 8 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 9 * tion, are permitted provided that the following conditions are met:
9 * 10 *
27 */ 28 */
28 29
29#ifndef ECB_H 30#ifndef ECB_H
30#define ECB_H 31#define ECB_H
31 32
33#include <inttypes.h>
34
35/* many compilers define _GNUC_ to some versions but then only implement
36 * what their idiot authors think are the "more important" extensions,
37 * causing enourmous grief for some better fake benchmark numbers or so.
38 * we try to detect these and simply assume they are not gcc - if they have
39 * an issue with that they should have done it right in the first place.
40 */
41#if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__)
42# define ECB_GCC_VERSION(major,minor) 0
43#else
32#define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 44# define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
45#endif
33 46
34#ifndef __cplusplus 47#ifndef __cplusplus
35# if __STDC_VERSION__ >= 199901L 48# if __STDC_VERSION__ >= 199901L
36# define ECB_INLINE inline 49# define ECB_INLINE inline
37typedef _Bool ecb_bool; 50typedef _Bool ecb_bool;
38# else 51# else
39# define ECB_INLINE inline /* yeah! */ 52# define ECB_INLINE inline /* yeah! */
40typedef int ecb_bool; 53typedef int ecb_bool;
41# endif 54# endif
42#endif 55#endif
56
57#define ECB_CONCAT_(a, b) a ## b
58#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
59#define ECB_STRINGIFY_(a) # a
60#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
43 61
44#define ECB_HEADER_INLINE static ECB_INLINE 62#define ECB_HEADER_INLINE static ECB_INLINE
45 63
46#if ECB_GCC_VERSION(3,1) 64#if ECB_GCC_VERSION(3,1)
47# define ecb_attribute(attrlist) __attribute__(attrlist) 65# define ecb_attribute(attrlist) __attribute__(attrlist)
79#endif 97#endif
80 98
81/* put into ifs if you are very sure that the expression */ 99/* put into ifs if you are very sure that the expression */
82/* is mostly true or mosty false. note that these return */ 100/* is mostly true or mosty false. note that these return */
83/* booleans, not the expression. */ 101/* booleans, not the expression. */
84#define ecb_expect_false(expr) ecb_expect ((expr) ? 1 : 0, 0) 102#define ecb_unlikely(expr) ecb_expect (!!(expr), 0)
85#define ecb_expect_true(expr) ecb_expect ((expr) ? 1 : 0, 1) 103#define ecb_likely(expr) ecb_expect (!!(expr), 1)
86 104
87/* try to tell the compiler that some condition is definitely true */ 105/* try to tell the compiler that some condition is definitely true */
88#define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) 106#define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0)
89 107
90/* count trailing zero bits and count # of one bits */ 108/* count trailing zero bits and count # of one bits */
91#if ECB_GCC_VERSION(3,4) 109#if ECB_GCC_VERSION(3,4)
92ECB_HEADER_INLINE int ecb_ctz32 (unsigned int x) { return __builtin_ctz (x); } 110ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); }
93ECB_HEADER_INLINE int ecb_popcount32 (unsigned int x) { return __builtin_popcount (x); } 111ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); }
94#else 112#else
95ECB_HEADER_INLINE 113ECB_HEADER_INLINE
96ecb_ctz32 (unsigned int x) ecb_const 114ecb_ctz32 (uint32_t x) ecb_const
97{ 115{
98 int r = 0; 116 int r = 0;
99 117
100 x &= -x; /* this isolates the lowest bit */ 118 x &= -x; /* this isolates the lowest bit */
101 119
107 125
108 return r; 126 return r;
109} 127}
110 128
111ECB_HEADER_INLINE 129ECB_HEADER_INLINE
112ecb_popcount32 (unsigned int x) ecb_const 130ecb_popcount32 (uint32_t x) ecb_const
113{ 131{
114 x -= (x >> 1) & 0x55555555; 132 x -= (x >> 1) & 0x55555555;
115 x = ((x >> 2) & 0x33333333) + (x & 0x33333333); 133 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
116 x = ((x >> 4) + x) & 0x0f0f0f0f; 134 x = ((x >> 4) + x) & 0x0f0f0f0f;
117 x *= 0x01010101; 135 x *= 0x01010101;
128} 146}
129 147
130ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; }; 148ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; };
131ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; 149ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; };
132 150
151#if ecb_cplusplus_does_not_suck
152// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm)
153template<typename T, int N>
154static inline int ecb_array_length (const T (&arr)[N])
155{
156 return N;
157}
158#else
159#define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
133#endif 160#endif
134 161
162ECB_INLINE uint32_t
163ecb_rotr32 (uint32_t x, unsigned int count)
164{
165 return (x << (32 - count)) | (x >> count);
166}
167
168ECB_INLINE uint32_t
169ecb_rotl32 (uint32_t x, unsigned int count)
170{
171 return (x >> (32 - count)) | (x << count);
172}
173
174#if ECB_GCC_VERSION(4,3)
175ECB_GCC_VERSION uint32_t ecb_bswap32 (uint32_t x) { return __builtin_bswap32 (x); }
176#else
177ECB_GCC_VERSION uint32_t
178ecb_bswap32 (uint32_t x)
179{
180 return (x >> 24)
181 | ((x >> 8) & 0x0000ff00)
182 | ((x << 8) & 0x00ff0000)
183 | (x << 24);
184}
185#endif
186
187#endif
188

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines