/*1* Copyright 2009-2015 Samy Al Bahra.2* Copyright 2014 Paul Khuong.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#ifndef CK_CC_H28#define CK_CC_H2930#if defined(__GNUC__) || defined(__SUNPRO_C)31#include "gcc/ck_cc.h"32#endif3334#ifndef CK_CC_RESTRICT35#define CK_CC_RESTRICT36#endif3738#ifndef CK_CC_INLINE39#define CK_CC_INLINE inline40#endif4142#ifndef CK_CC_FORCE_INLINE43#define CK_CC_FORCE_INLINE inline44#endif4546#define CK_CC_DECONST_PTR(X) ((void *)(uintptr_t)(X))4748/*49* Container function.50* This relies on (compiler) implementation-defined behavior.51*/52#ifndef CK_CC_CONTAINER53#define CK_CC_CONTAINER(F, T, M, N) \54CK_CC_INLINE static T * \55N(F *p) \56{ \57F *n = p; \58return (T *)(void *)(((char *)n) - ((size_t)&((T *)0)->M)); \59}60#endif6162#define CK_CC_PAD(x) union { char pad[x]; }6364#ifndef CK_CC_ALIASED65#define CK_CC_ALIASED66#endif6768#ifndef CK_CC_UNUSED69#define CK_CC_UNUSED70#endif7172#ifndef CK_CC_USED73#define CK_CC_USED74#endif7576#ifndef CK_CC_IMM77#define CK_CC_IMM78#endif7980#ifndef CK_CC_PACKED81#define CK_CC_PACKED82#endif8384#ifndef CK_CC_WEAKREF85#define CK_CC_WEAKREF86#endif8788#ifndef CK_CC_ALIGN89#define CK_CC_ALIGN(X)90#endif9192#ifndef CK_CC_CACHELINE93#define CK_CC_CACHELINE94#endif9596#ifndef CK_CC_LIKELY97#define CK_CC_LIKELY(x) x98#endif99100#ifndef CK_CC_UNLIKELY101#define CK_CC_UNLIKELY(x) x102#endif103104#ifndef CK_CC_TYPEOF105#define CK_CC_TYPEOF(X, DEFAULT) (DEFAULT)106#endif107108#define CK_F_CC_FFS_G(L, T) \109CK_CC_INLINE static int \110ck_cc_##L(T v) \111{ \112unsigned int i; \113\114if (v == 0) \115return 0; \116\117for (i = 1; (v & 1) == 0; i++, v >>= 1); \118return i; \119}120121#ifndef CK_F_CC_FFS122#define CK_F_CC_FFS123CK_F_CC_FFS_G(ffs, unsigned int)124#endif /* CK_F_CC_FFS */125126#ifndef CK_F_CC_FFSL127#define CK_F_CC_FFSL128CK_F_CC_FFS_G(ffsl, unsigned long)129#endif /* CK_F_CC_FFSL */130131#ifndef CK_F_CC_FFSLL132#define CK_F_CC_FFSLL133CK_F_CC_FFS_G(ffsll, unsigned long long)134#endif /* CK_F_CC_FFSLL */135136#undef CK_F_CC_FFS_G137138#ifndef CK_F_CC_CTZ139#define CK_F_CC_CTZ140CK_CC_INLINE static int141ck_cc_ctz(unsigned int x)142{143unsigned int i;144145if (x == 0)146return 0;147148for (i = 0; (x & 1) == 0; i++, x >>= 1);149return i;150}151#endif152153#ifndef CK_F_CC_POPCOUNT154#define CK_F_CC_POPCOUNT155CK_CC_INLINE static int156ck_cc_popcount(unsigned int x)157{158unsigned int acc;159160for (acc = 0; x != 0; x >>= 1)161acc += x & 1;162163return acc;164}165#endif166167168#ifdef __cplusplus169#define CK_CPP_CAST(type, arg) static_cast<type>(arg)170#else171#define CK_CPP_CAST(type, arg) arg172#endif173174#endif /* CK_CC_H */175176177