#ifndef __UTIL_H
#define __UTIL_H
#define BIT(n) (1ul << (n))
#define MASK(n) (BIT(n)-1ul)
#define IS_ALIGNED(n, b) (!((n) & MASK(b)))
#define ROUND_DOWN(n, b) (((n) >> (b)) << (b))
#define ROUND_UP(n, b) (((((n) - 1ul) >> (b)) + 1ul) << (b))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#ifndef __ASSEMBLER__
#define NULL ((void *)0)
#define PACKED __attribute__((packed))
#define NORETURN __attribute__((__noreturn__))
#define CONST __attribute__((__const__))
#define PURE __attribute__((__pure__))
#define ALIGN(n) __attribute__((__aligned__(n)))
#define FASTCALL __attribute__((fastcall))
#ifdef __clang__
#define VISIBLE
#else
#define VISIBLE __attribute__((externally_visible))
#endif
#define NO_INLINE __attribute__((noinline))
#define FORCE_INLINE __attribute__((always_inline))
#define SECTION(sec) __attribute__((__section__(sec)))
#define UNUSED __attribute__((unused))
#define USED __attribute__((used))
#define FASTCALL __attribute__((fastcall))
void __builtin_unreachable(void);
#define UNREACHABLE() __builtin_unreachable()
#define MAY_ALIAS __attribute__((may_alias))
#define OFFSETOF(type, member) \
__builtin_offsetof(type, member)
#ifdef __GNUC__
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (!!(x))
#define unlikely(x) (!!(x))
#endif
#define asm __asm__
#define config_set(macro) _is_set_(macro)
#define _macrotest_1 ,
#define _is_set_(value) _is_set__(_macrotest_##value)
#define _is_set__(comma) _is_set___(comma 1, 0)
#define _is_set___(_, v, ...) v
#define config_ternary(macro, true, false) _config_ternary(macro, true, false)
#define _config_ternary(value, true, false) _config_ternary_(_macrotest_##value, true, false)
#define _config_ternary_(comma, true, false) _config_ternary__(comma true, false)
#define _config_ternary__(_, v, ...) v
void halt(void) NORETURN;
void memzero(void *s, unsigned long n);
void *memset(void *s, unsigned long c, unsigned long n);
void *memcpy(void* ptr_dst, const void* ptr_src, unsigned long n);
int PURE strncmp(const char *s1, const char *s2, int n);
long CONST char_to_long(char c);
long PURE str_to_long(const char* str);
#endif
int __builtin_clzl (unsigned long x);
int __builtin_ctzl (unsigned long x);
static inline long
CONST clzl(unsigned long x)
{
return __builtin_clzl(x);
}
static inline long
CONST ctzl(unsigned long x)
{
return __builtin_ctzl(x);
}
#define CTZL(x) __builtin_ctzl(x)
int __builtin_popcountl (unsigned long x);
static inline long
CONST popcountl(unsigned long x)
{
return __builtin_popcountl(x);
}
#define POPCOUNTL(x) __builtin_popcountl(x)
#endif