| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#ifdef HAS_STDC_PREDEF_H |
| 7 |
#include <stdc-predef.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
|
| 11 |
#if __STDC_VERSION__ <= 201710L |
| 12 |
|
| 13 |
# define auto __auto_type |
| 14 |
# define typeof __typeof__ |
| 15 |
#endif |
| 16 |
#define likely(x) __builtin_expect(!!(x), 1) |
| 17 |
#define unlikely(x) __builtin_expect(!!(x), 0) |
| 18 |
#define likely_if(x) if (likely(x)) |
| 19 |
#define unlikely_if(x) if (unlikely(x)) |
| 20 |
|
| 21 |
#ifndef __has_attribute |
| 22 |
# if __GNUC__ >= 4 |
| 23 |
# define __has_attribute(x) 1 |
| 24 |
# else |
| 25 |
# define __has_attribute(x) 0 |
| 26 |
# endif |
| 27 |
#endif |
| 28 |
|
| 29 |
#if __has_attribute(const) |
| 30 |
# define attr_const __attribute__((const)) |
| 31 |
#else |
| 32 |
# define attr_const |
| 33 |
#endif |
| 34 |
|
| 35 |
#if __has_attribute(format) |
| 36 |
# define attr_printf(a, b) __attribute__((format(printf, a, b))) |
| 37 |
#else |
| 38 |
# define attr_printf(a, b) |
| 39 |
#endif |
| 40 |
|
| 41 |
#if __has_attribute(pure) |
| 42 |
# define attr_pure __attribute__((pure)) |
| 43 |
#else |
| 44 |
# define attr_pure |
| 45 |
#endif |
| 46 |
|
| 47 |
#if __has_attribute(unused) |
| 48 |
# define attr_unused __attribute__((unused)) |
| 49 |
#else |
| 50 |
# define attr_unused |
| 51 |
#endif |
| 52 |
|
| 53 |
#if __has_attribute(warn_unused_result) |
| 54 |
# define attr_warn_unused_result __attribute__((warn_unused_result)) |
| 55 |
#else |
| 56 |
# define attr_warn_unused_result |
| 57 |
#endif |
| 58 |
|
| 59 |
#define must_use attr_warn_unused_result |
| 60 |
|
| 61 |
#if __has_attribute(nonnull) |
| 62 |
# define attr_nonnull(...) __attribute__((nonnull(__VA_ARGS__))) |
| 63 |
# define attr_nonnull_all __attribute__((nonnull)) |
| 64 |
#else |
| 65 |
# define attr_nonnull(...) |
| 66 |
# define attr_nonnull_all |
| 67 |
#endif |
| 68 |
|
| 69 |
#if __has_attribute(returns_nonnull) |
| 70 |
# define attr_ret_nonnull __attribute__((returns_nonnull)) |
| 71 |
#else |
| 72 |
# define attr_ret_nonnull |
| 73 |
#endif |
| 74 |
|
| 75 |
#if __has_attribute(deprecated) |
| 76 |
# define attr_deprecated __attribute__((deprecated)) |
| 77 |
#else |
| 78 |
# define attr_deprecated |
| 79 |
#endif |
| 80 |
|
| 81 |
#if __has_attribute(malloc) |
| 82 |
# define attr_malloc __attribute__((malloc)) |
| 83 |
#else |
| 84 |
# define attr_malloc |
| 85 |
#endif |
| 86 |
|
| 87 |
#if __has_attribute(fallthrough) |
| 88 |
# define fallthrough() __attribute__((fallthrough)) |
| 89 |
#else |
| 90 |
# define fallthrough() |
| 91 |
#endif |
| 92 |
|
| 93 |
#if __has_attribute(cleanup) |
| 94 |
# define cleanup(func) __attribute__((cleanup(func))) |
| 95 |
#else |
| 96 |
# error "Compiler is missing cleanup attribute" |
| 97 |
#endif |
| 98 |
|
| 99 |
#if __STDC_VERSION__ >= 201112L |
| 100 |
# define attr_noret _Noreturn |
| 101 |
#else |
| 102 |
# if __has_attribute(noreturn) |
| 103 |
# define attr_noret __attribute__((noreturn)) |
| 104 |
# else |
| 105 |
# define attr_noret |
| 106 |
# endif |
| 107 |
#endif |
| 108 |
|
| 109 |
#ifndef unreachable |
| 110 |
# if defined(__GNUC__) || defined(__clang__) |
| 111 |
# define unreachable() assert(false); __builtin_unreachable() |
| 112 |
# else |
| 113 |
# define unreachable() assert(false); do {} while(0) |
| 114 |
# endif |
| 115 |
#endif |
| 116 |
|
| 117 |
#ifndef __has_include |
| 118 |
# define __has_include(x) 0 |
| 119 |
#endif |
| 120 |
|
| 121 |
#ifndef __has_builtin |
| 122 |
# define __has_builtin(x) 0 |
| 123 |
#endif |
| 124 |
|
| 125 |
#if !defined(__STDC_NO_THREADS__) && __has_include(<threads.h>) |
| 126 |
# include <threads.h> |
| 127 |
#elif __STDC_VERSION__ >= 201112L |
| 128 |
# define thread_local _Thread_local |
| 129 |
#elif defined(__GNUC__) || defined(__clang__) |
| 130 |
# define thread_local __thread |
| 131 |
#else |
| 132 |
# define thread_local _Pragma("GCC error \"No thread local storage support\"") __error__ |
| 133 |
#endif |
| 134 |
|
| 135 |
#define PICOM_PUBLIC_API __attribute__((visibility("default"))) |
| 136 |
|
| 137 |
|
| 138 |
typedef unsigned long ulong; |
| 139 |
typedef unsigned int uint; |
| 140 |
|
| 141 |
static inline int attr_const popcntul(unsigned long a) { |
| 142 |
return __builtin_popcountl(a); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static inline int attr_const index_of_lowest_one(unsigned a) { |
| 148 |
#if __has_builtin(__builtin_ctz) |
| 149 |
return __builtin_ctz(a); |
| 150 |
#else |
| 151 |
auto lowbit = (a & -a); |
| 152 |
int r = (lowbit & 0xAAAAAAAA) != 0; |
| 153 |
r |= ((lowbit & 0xCCCCCCCC) != 0) << 1; |
| 154 |
r |= ((lowbit & 0xF0F0F0F0) != 0) << 2; |
| 155 |
r |= ((lowbit & 0xFF00FF00) != 0) << 3; |
| 156 |
r |= ((lowbit & 0xFFFF0000) != 0) << 4; |
| 157 |
return r; |
| 158 |
#endif |
| 159 |
} |