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

Comparing libecb/ecb.h (file contents):
Revision 1.13 by root, Thu May 26 18:21:04 2011 UTC vs.
Revision 1.21 by root, Thu May 26 19:23:53 2011 UTC

1/* 1/*
2 * libecb 2 * libecb - http://software.schmorp.de/pkg/libecb
3 * 3 *
4 * Copyright (©) 2009-2011 Marc Alexander Lehmann 4 * Copyright (©) 2009-2011 Marc Alexander Lehmann
5 * Copyright (©) 2011 Emanuele Giaquinta 5 * Copyright (©) 2011 Emanuele Giaquinta
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
32 32
33#include <inttypes.h> 33#include <inttypes.h>
34 34
35/* many compilers define _GNUC_ to some versions but then only implement 35/* many compilers define _GNUC_ to some versions but then only implement
36 * what their idiot authors think are the "more important" extensions, 36 * what their idiot authors think are the "more important" extensions,
37 * causing enourmous grief for some better fake benchmark numbers or so. 37 * causing enourmous grief in return for some better fake benchmark numbers.
38 * or so.
38 * we try to detect these and simply assume they are not gcc - if they have 39 * 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 * an issue with that they should have done it right in the first place.
40 */ 41 */
42#ifndef ECB_GCC_VERSION
41#if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__) 43# if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__)
42# define ECB_GCC_VERSION(major,minor) 0 44# define ECB_GCC_VERSION(major,minor) 0
43#else 45# else
44# define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 46# define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
47# endif
45#endif 48#endif
46 49
47#ifndef __cplusplus 50#ifndef __cplusplus
48# if __STDC_VERSION__ >= 199901L 51# if __STDC_VERSION__ >= 199901L
49# define ECB_INLINE inline 52# define ECB_INLINE inline
93/* booleans, not the expression. */ 96/* booleans, not the expression. */
94#define ecb_unlikely(expr) ecb_expect (!!(expr), 0) 97#define ecb_unlikely(expr) ecb_expect (!!(expr), 0)
95#define ecb_likely(expr) ecb_expect (!!(expr), 1) 98#define ecb_likely(expr) ecb_expect (!!(expr), 1)
96 99
97/* try to tell the compiler that some condition is definitely true */ 100/* try to tell the compiler that some condition is definitely true */
98#define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) 101#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
99 102
100/* count trailing zero bits and count # of one bits */ 103/* count trailing zero bits and count # of one bits */
101#if ECB_GCC_VERSION(3,4) 104#if ECB_GCC_VERSION(3,4)
102ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } 105#define ecb_ctz32 (x) __builtin_ctz (x)
103ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } 106#define ecb_popcount32 (x) __builtin_popcount (x)
104#else 107#else
108ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const;
105ECB_HEADER_INLINE 109ECB_HEADER_INLINE
106ecb_ctz32 (uint32_t x) ecb_const 110ecb_ctz32 (uint32_t x)
107{ 111{
108 int r = 0; 112 int r = 0;
109 113
110 x &= -x; /* this isolates the lowest bit */ 114 x &= -x; /* this isolates the lowest bit */
111 115
116 if (x & 0xffff0000) r += 16; 120 if (x & 0xffff0000) r += 16;
117 121
118 return r; 122 return r;
119} 123}
120 124
125ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const;
121ECB_HEADER_INLINE 126ECB_HEADER_INLINE
122ecb_popcount32 (uint32_t x) ecb_const 127ecb_popcount32 (uint32_t x)
123{ 128{
124 x -= (x >> 1) & 0x55555555; 129 x -= (x >> 1) & 0x55555555;
125 x = ((x >> 2) & 0x33333333) + (x & 0x33333333); 130 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
126 x = ((x >> 4) + x) & 0x0f0f0f0f; 131 x = ((x >> 4) + x) & 0x0f0f0f0f;
127 x *= 0x01010101; 132 x *= 0x01010101;
129 return x >> 24; 134 return x >> 24;
130} 135}
131#endif 136#endif
132 137
133#if ECB_GCC_VERSION(4,3) 138#if ECB_GCC_VERSION(4,3)
134ECB_GCC_VERSION uint32_t ecb_bswap32 (uint32_t x) { return __builtin_bswap32 (x); } 139# define ecb_bswap32(x) __builtin_bswap32 (x)
140# define ecb_bswap16(x) (__builtin_bswap32(x) >> 16)
135#else 141#else
136ECB_GCC_VERSION uint32_t 142ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) ecb_const;
143ECB_HEADER_INLINE uint32_t
137ecb_bswap32 (uint32_t x) 144ecb_bswap32 (uint32_t x)
138{ 145{
139 return (x >> 24) 146 return (x >> 24)
140 | ((x >> 8) & 0x0000ff00) 147 | ((x >> 8) & 0x0000ff00)
141 | ((x << 8) & 0x00ff0000) 148 | ((x << 8) & 0x00ff0000)
142 | (x << 24); 149 | (x << 24);
143} 150}
151
152ECB_HEADER_INLINE uint32_t ecb_bswap16 (uint32_t x) ecb_const;
153ECB_HEADER_INLINE uint32_t
154ecb_bswap16 (uint32_t x)
155{
156 return ((x >> 8) & 0xff)
157 | ((x << 8) & 0x00ff0000)
158 | (x << 24);
159}
144#endif 160#endif
145 161
146#if ECB_GCC_VERSION(4,5) 162#if ECB_GCC_VERSION(4,5)
147# define ecb_unreachable() __builtin_unreachable () 163# define ecb_unreachable() __builtin_unreachable ()
148#else 164#else
149/* this seems to work fine, but gcc always emits a warning for it :/ */ 165/* this seems to work fine, but gcc always emits a warning for it :/ */
150ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn)); 166ECB_HEADER_INLINE void ecb_unreachable (void) ecb_noreturn;
151ECB_HEADER_INLINE void ecb_unreachable () { } 167ECB_HEADER_INLINE void ecb_unreachable (void) { }
152#endif 168#endif
153 169
170ECB_HEADER_INLINE unsigned char ecb_byteorder_helper () ecb_const;
154ECB_HEADER_INLINE unsigned char 171ECB_HEADER_INLINE unsigned char
155ecb_byteorder_helper () ecb_const 172ecb_byteorder_helper ()
156{ 173{
157 const uint32_t u = 0x11223344; 174 const uint32_t u = 0x11223344;
158 return *(unsigned char *)&u; 175 return *(unsigned char *)&u;
159} 176}
160 177
178ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const;
161ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; }; 179ECB_HEADER_INLINE ecb_bool ecb_big_endian () { return ecb_byteorder_helper () == 0x11; };
180ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const;
162ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; 181ECB_HEADER_INLINE ecb_bool ecb_little_endian () { return ecb_byteorder_helper () == 0x44; };
182
183#define ecb_mod(m, n) ((m) < 0 ? ((n) - (-(m) % (n))) : ((m) % (n)))
163 184
164#if ecb_cplusplus_does_not_suck 185#if ecb_cplusplus_does_not_suck
165// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) 186// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm)
166template<typename T, int N> 187template<typename T, int N>
167static inline int ecb_array_length (const T (&arr)[N]) 188static inline int ecb_array_length (const T (&arr)[N])
170} 191}
171#else 192#else
172#define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 193#define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
173#endif 194#endif
174 195
196ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
175ECB_INLINE uint32_t 197ECB_INLINE uint32_t
176ecb_rotr32 (uint32_t x, unsigned int count) 198ecb_rotr32 (uint32_t x, unsigned int count)
177{ 199{
178 return (x << (32 - count)) | (x >> count); 200 return (x << (32 - count)) | (x >> count);
179} 201}
180 202
203ECB_INLINE uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
181ECB_INLINE uint32_t 204ECB_INLINE uint32_t
182ecb_rotl32 (uint32_t x, unsigned int count) 205ecb_rotl32 (uint32_t x, unsigned int count)
183{ 206{
184 return (x >> (32 - count)) | (x << count); 207 return (x >> (32 - count)) | (x << count);
185} 208}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines