xref: /aosp_15_r20/external/libdav1d/include/common/attributes.h (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
1 /*
2  * Copyright © 2018, VideoLAN and dav1d authors
3  * Copyright © 2018, Two Orioles, LLC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef DAV1D_COMMON_ATTRIBUTES_H
29 #define DAV1D_COMMON_ATTRIBUTES_H
30 
31 #include "config.h"
32 
33 #include <stddef.h>
34 #include <assert.h>
35 
36 #ifndef __has_attribute
37 #define __has_attribute(x) 0
38 #endif
39 
40 #ifndef __has_feature
41 #define __has_feature(x) 0
42 #endif
43 
44 #ifdef __GNUC__
45 #define ATTR_ALIAS __attribute__((may_alias))
46 #if defined(__MINGW32__) && !defined(__clang__)
47 #define ATTR_FORMAT_PRINTF(fmt, attr) __attribute__((__format__(__gnu_printf__, fmt, attr)))
48 #else
49 #define ATTR_FORMAT_PRINTF(fmt, attr) __attribute__((__format__(__printf__, fmt, attr)))
50 #endif
51 #define COLD __attribute__((cold))
52 #else
53 #define ATTR_ALIAS
54 #define ATTR_FORMAT_PRINTF(fmt, attr)
55 #define COLD
56 #endif
57 
58 #if ARCH_X86_64
59 /* x86-64 needs 32- and 64-byte alignment for AVX2 and AVX-512. */
60 #define ALIGN_64_VAL 64
61 #define ALIGN_32_VAL 32
62 #define ALIGN_16_VAL 16
63 #elif ARCH_AARCH64 || ARCH_ARM || ARCH_LOONGARCH || ARCH_PPC64LE || ARCH_X86_32
64 /* ARM doesn't benefit from anything more than 16-byte alignment. */
65 #define ALIGN_64_VAL 16
66 #define ALIGN_32_VAL 16
67 #define ALIGN_16_VAL 16
68 #else
69 /* No need for extra alignment on platforms without assembly. */
70 #define ALIGN_64_VAL 8
71 #define ALIGN_32_VAL 8
72 #define ALIGN_16_VAL 8
73 #endif
74 
75 /*
76  * API for variables, struct members (ALIGN()) like:
77  * uint8_t var[1][2][3][4]
78  * becomes:
79  * ALIGN(uint8_t var[1][2][3][4], alignment).
80  */
81 #ifdef _MSC_VER
82 #define ALIGN(ll, a) \
83     __declspec(align(a)) ll
84 #else
85 #define ALIGN(line, align) \
86     line __attribute__((aligned(align)))
87 #endif
88 
89 /*
90  * API for stack alignment (ALIGN_STK_$align()) of variables like:
91  * uint8_t var[1][2][3][4]
92  * becomes:
93  * ALIGN_STK_$align(uint8_t, var, 1, [2][3][4])
94  */
95 #define ALIGN_STK_64(type, var, sz1d, sznd) \
96     ALIGN(type var[sz1d]sznd, ALIGN_64_VAL)
97 #define ALIGN_STK_32(type, var, sz1d, sznd) \
98     ALIGN(type var[sz1d]sznd, ALIGN_32_VAL)
99 #define ALIGN_STK_16(type, var, sz1d, sznd) \
100     ALIGN(type var[sz1d]sznd, ALIGN_16_VAL)
101 
102 /*
103  * Forbid inlining of a function:
104  * static NOINLINE void func() {}
105  */
106 #ifdef _MSC_VER
107 #define NOINLINE __declspec(noinline)
108 #elif __has_attribute(noclone)
109 #define NOINLINE __attribute__((noinline, noclone))
110 #else
111 #define NOINLINE __attribute__((noinline))
112 #endif
113 
114 #ifdef _MSC_VER
115 #define ALWAYS_INLINE __forceinline
116 #else
117 #define ALWAYS_INLINE __attribute__((always_inline)) inline
118 #endif
119 
120 #if (defined(__ELF__) || defined(__MACH__) || (defined(_WIN32) && defined(__clang__))) && __has_attribute(visibility)
121 #define EXTERN extern __attribute__((visibility("hidden")))
122 #else
123 #define EXTERN extern
124 #endif
125 
126 #ifdef __clang__
127 #define NO_SANITIZE(x) __attribute__((no_sanitize(x)))
128 #else
129 #define NO_SANITIZE(x)
130 #endif
131 
132 #if defined(NDEBUG) && (defined(__GNUC__) || defined(__clang__))
133 #undef assert
134 #define assert(x) do { if (!(x)) __builtin_unreachable(); } while (0)
135 #elif defined(NDEBUG) && defined(_MSC_VER)
136 #undef assert
137 #define assert __assume
138 #endif
139 
140 #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
141 #    define dav1d_uninit(x) x=x
142 #else
143 #    define dav1d_uninit(x) x
144 #endif
145 
146 #if defined(_MSC_VER) && !defined(__clang__)
147 #include <intrin.h>
148 
ctz(const unsigned int mask)149 static inline int ctz(const unsigned int mask) {
150     unsigned long idx;
151     _BitScanForward(&idx, mask);
152     return idx;
153 }
154 
clz(const unsigned int mask)155 static inline int clz(const unsigned int mask) {
156     unsigned long leading_zero = 0;
157     _BitScanReverse(&leading_zero, mask);
158     return (31 - leading_zero);
159 }
160 
161 #ifdef _WIN64
clzll(const unsigned long long mask)162 static inline int clzll(const unsigned long long mask) {
163     unsigned long leading_zero = 0;
164     _BitScanReverse64(&leading_zero, mask);
165     return (63 - leading_zero);
166 }
167 #else /* _WIN64 */
clzll(const unsigned long long mask)168 static inline int clzll(const unsigned long long mask) {
169     if (mask >> 32)
170         return clz((unsigned)(mask >> 32));
171     else
172         return clz((unsigned)mask) + 32;
173 }
174 #endif /* _WIN64 */
175 #else /* !_MSC_VER */
ctz(const unsigned int mask)176 static inline int ctz(const unsigned int mask) {
177     return __builtin_ctz(mask);
178 }
179 
clz(const unsigned int mask)180 static inline int clz(const unsigned int mask) {
181     return __builtin_clz(mask);
182 }
183 
clzll(const unsigned long long mask)184 static inline int clzll(const unsigned long long mask) {
185     return __builtin_clzll(mask);
186 }
187 #endif /* !_MSC_VER */
188 
189 #ifndef static_assert
190 #define CHECK_OFFSET(type, field, name) \
191     struct check_##type##_##field { int x[(name == offsetof(type, field)) ? 1 : -1]; }
192 #define CHECK_SIZE(type, size) \
193     struct check_##type##_size { int x[(size == sizeof(type)) ? 1 : -1]; }
194 #else
195 #define CHECK_OFFSET(type, field, name) \
196     static_assert(name == offsetof(type, field), #field)
197 #define CHECK_SIZE(type, size) \
198     static_assert(size == sizeof(type), #type)
199 #endif
200 
201 #ifdef _MSC_VER
202 #define PACKED(...) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop))
203 #else
204 #define PACKED(...) __VA_ARGS__ __attribute__((__packed__))
205 #endif
206 
207 #endif /* DAV1D_COMMON_ATTRIBUTES_H */
208