aboutsummaryrefslogtreecommitdiff
path: root/src/compiler.h
blob: 00da4cb5b8cc6514d8d057b4aed6a63f423974d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2018 Yuxuan Shui <[email protected]>
#pragma once

#ifdef HAS_STDC_PREDEF_H
#include <stdc-predef.h>
#endif

// clang-format off
#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))

#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
// An alias for conveninence
#define must_use attr_warn_unused_result

#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

#if __has_attribute(deprecated)
# define attr_deprecated __attribute__((deprecated))
#else
# define attr_deprecated
#endif

#if __has_attribute(malloc)
# define attr_malloc __attribute__((malloc))
#else
# define attr_malloc
#endif

#if __has_attribute(fallthrough)
# define fallthrough() __attribute__((fallthrough))
#else
# define fallthrough()
#endif

#if __has_attribute(cleanup)
# define cleanup(func) __attribute__((cleanup(func)))
#else
# error "Compiler is missing cleanup attribute"
#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

#ifndef __has_include
#define __has_include(x) 0
#endif

#if !defined(__STDC_NO_THREADS__) && __has_include(<threads.h>)
# 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
// clang-format on

typedef unsigned long ulong;
typedef unsigned int uint;

static inline int attr_const popcntul(unsigned long a) {
	return __builtin_popcountl(a);
}