2018-12-16 02:47:21 +08:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
|
|
|
|
#pragma once
|
2018-12-16 03:11:46 +08:00
|
|
|
|
2019-04-14 10:45:04 +08:00
|
|
|
#ifdef HAS_STDC_PREDEF_H
|
2018-12-20 07:10:37 +08:00
|
|
|
#include <stdc-predef.h>
|
2019-04-14 10:45:04 +08:00
|
|
|
#endif
|
2018-12-20 07:10:37 +08:00
|
|
|
|
2019-02-09 23:50:40 +08:00
|
|
|
#define auto __auto_type
|
|
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
|
|
#define likely_if(x) if (likely(x))
|
|
|
|
#define unlikely_if(x) if (unlikely(x))
|
2018-12-20 04:36:05 +08:00
|
|
|
|
|
|
|
#ifndef __has_attribute
|
|
|
|
# if __GNUC__ >= 4
|
|
|
|
# define __has_attribute(x) 1
|
|
|
|
# else
|
|
|
|
# define __has_attribute(x) 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_attribute(const)
|
|
|
|
# define attr_const __attribute__((const))
|
|
|
|
#else
|
|
|
|
# define attr_const
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_attribute(format)
|
|
|
|
# define attr_printf(a, b) __attribute__((format(printf, a, b)))
|
|
|
|
#else
|
|
|
|
# define attr_printf(a, b)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_attribute(pure)
|
|
|
|
# define attr_pure __attribute__((pure))
|
|
|
|
#else
|
|
|
|
# define attr_pure
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_attribute(unused)
|
|
|
|
# define attr_unused __attribute__((unused))
|
|
|
|
#else
|
|
|
|
# define attr_unused
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_attribute(warn_unused_result)
|
|
|
|
# define attr_warn_unused_result __attribute__((warn_unused_result))
|
|
|
|
#else
|
|
|
|
# define attr_warn_unused_result
|
|
|
|
#endif
|
2019-02-13 10:04:05 +08:00
|
|
|
// An alias for conveninence
|
|
|
|
#define must_use attr_warn_unused_result
|
2018-12-20 04:36:05 +08:00
|
|
|
|
|
|
|
#if __has_attribute(nonnull)
|
|
|
|
# define attr_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
|
|
|
|
# define attr_nonnull_all __attribute__((nonnull))
|
|
|
|
#else
|
|
|
|
# define attr_nonnull(...)
|
|
|
|
# define attr_nonnull_all
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_attribute(returns_nonnull)
|
|
|
|
# define attr_ret_nonnull __attribute__((returns_nonnull))
|
|
|
|
#else
|
|
|
|
# define attr_ret_nonnull
|
|
|
|
#endif
|
|
|
|
|
2019-05-22 06:23:30 +08:00
|
|
|
#if __has_attribute(deprecated)
|
|
|
|
# define attr_deprecated __attribute__((deprecated))
|
|
|
|
#else
|
|
|
|
# define attr_deprecated
|
|
|
|
#endif
|
|
|
|
|
2018-12-20 04:36:05 +08:00
|
|
|
#if __has_attribute(malloc)
|
|
|
|
# define attr_malloc __attribute__((malloc))
|
|
|
|
#else
|
|
|
|
# define attr_malloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __STDC_VERSION__ >= 201112L
|
|
|
|
# define attr_noret _Noreturn
|
|
|
|
#else
|
|
|
|
# if __has_attribute(noreturn)
|
|
|
|
# define attr_noret __attribute__((noreturn))
|
|
|
|
# else
|
|
|
|
# define attr_noret
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
|
|
# define unreachable __builtin_unreachable()
|
|
|
|
#else
|
|
|
|
# define unreachable do {} while(0)
|
|
|
|
#endif
|
2018-12-20 07:10:37 +08:00
|
|
|
|
|
|
|
#ifndef __STDC_NO_THREADS__
|
|
|
|
# include <threads.h>
|
|
|
|
#elif __STDC_VERSION__ >= 201112L
|
|
|
|
# define thread_local _Thread_local
|
|
|
|
#elif defined(__GNUC__) || defined(__clang__)
|
|
|
|
# define thread_local __thread
|
|
|
|
#else
|
|
|
|
# define thread_local _Pragma("GCC error \"No thread local storage support\"") __error__
|
|
|
|
#endif
|
2019-03-30 17:07:21 +08:00
|
|
|
|
|
|
|
typedef unsigned long ulong;
|
|
|
|
typedef unsigned int uint;
|
2019-08-08 07:47:12 +08:00
|
|
|
|
|
|
|
static inline int popcount(uint x) {
|
|
|
|
return __builtin_popcount(x);
|
|
|
|
}
|