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 #define ATTR_FORMAT_PRINTF(fmt, attr) __attribute__((__format__(__printf__, fmt, attr)))
47 #define COLD __attribute__((cold))
48 #else
49 #define ATTR_ALIAS
50 #define ATTR_FORMAT_PRINTF(fmt, attr)
51 #define COLD
52 #endif
53 
54 #if ARCH_X86_64
55 /* x86-64 needs 32- and 64-byte alignment for AVX2 and AVX-512. */
56 #define ALIGN_64_VAL 64
57 #define ALIGN_32_VAL 32
58 #define ALIGN_16_VAL 16
59 #elif ARCH_X86_32 || ARCH_ARM || ARCH_AARCH64 || ARCH_PPC64LE
60 /* ARM doesn't benefit from anything more than 16-byte alignment. */
61 #define ALIGN_64_VAL 16
62 #define ALIGN_32_VAL 16
63 #define ALIGN_16_VAL 16
64 #else
65 /* No need for extra alignment on platforms without assembly. */
66 #define ALIGN_64_VAL 8
67 #define ALIGN_32_VAL 8
68 #define ALIGN_16_VAL 8
69 #endif
70 
71 /*
72  * API for variables, struct members (ALIGN()) like:
73  * uint8_t var[1][2][3][4]
74  * becomes:
75  * ALIGN(uint8_t var[1][2][3][4], alignment).
76  */
77 #ifdef _MSC_VER
78 #define ALIGN(ll, a) \
79     __declspec(align(a)) ll
80 #else
81 #define ALIGN(line, align) \
82     line __attribute__((aligned(align)))
83 #endif
84 
85 /*
86  * API for stack alignment (ALIGN_STK_$align()) of variables like:
87  * uint8_t var[1][2][3][4]
88  * becomes:
89  * ALIGN_STK_$align(uint8_t, var, 1, [2][3][4])
90  */
91 #define ALIGN_STK_64(type, var, sz1d, sznd) \
92     ALIGN(type var[sz1d]sznd, ALIGN_64_VAL)
93 #define ALIGN_STK_32(type, var, sz1d, sznd) \
94     ALIGN(type var[sz1d]sznd, ALIGN_32_VAL)
95 #define ALIGN_STK_16(type, var, sz1d, sznd) \
96     ALIGN(type var[sz1d]sznd, ALIGN_16_VAL)
97 
98 /*
99  * Forbid inlining of a function:
100  * static NOINLINE void func() {}
101  */
102 #ifdef _MSC_VER
103 #define NOINLINE __declspec(noinline)
104 #elif __has_attribute(noclone)
105 #define NOINLINE __attribute__((noinline, noclone))
106 #else
107 #define NOINLINE __attribute__((noinline))
108 #endif
109 
110 #ifdef __clang__
111 #define NO_SANITIZE(x) __attribute__((no_sanitize(x)))
112 #else
113 #define NO_SANITIZE(x)
114 #endif
115 
116 #if defined(NDEBUG) && (defined(__GNUC__) || defined(__clang__))
117 #undef assert
118 #define assert(x) do { if (!(x)) __builtin_unreachable(); } while (0)
119 #elif defined(NDEBUG) && defined(_MSC_VER)
120 #undef assert
121 #define assert __assume
122 #endif
123 
124 #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
125 #    define dav1d_uninit(x) x=x
126 #else
127 #    define dav1d_uninit(x) x
128 #endif
129 
130 #if defined(_MSC_VER) && !defined(__clang__)
131 #include <intrin.h>
132 
ctz(const unsigned int mask)133 static inline int ctz(const unsigned int mask) {
134     unsigned long idx;
135     _BitScanForward(&idx, mask);
136     return idx;
137 }
138 
clz(const unsigned int mask)139 static inline int clz(const unsigned int mask) {
140     unsigned long leading_zero = 0;
141     _BitScanReverse(&leading_zero, mask);
142     return (31 - leading_zero);
143 }
144 
145 #ifdef _WIN64
clzll(const unsigned long long mask)146 static inline int clzll(const unsigned long long mask) {
147     unsigned long leading_zero = 0;
148     _BitScanReverse64(&leading_zero, mask);
149     return (63 - leading_zero);
150 }
151 #else /* _WIN64 */
clzll(const unsigned long long mask)152 static inline int clzll(const unsigned long long mask) {
153     if (mask >> 32)
154         return clz((unsigned)(mask >> 32));
155     else
156         return clz((unsigned)mask) + 32;
157 }
158 #endif /* _WIN64 */
159 #else /* !_MSC_VER */
ctz(const unsigned int mask)160 static inline int ctz(const unsigned int mask) {
161     return __builtin_ctz(mask);
162 }
163 
clz(const unsigned int mask)164 static inline int clz(const unsigned int mask) {
165     return __builtin_clz(mask);
166 }
167 
clzll(const unsigned long long mask)168 static inline int clzll(const unsigned long long mask) {
169     return __builtin_clzll(mask);
170 }
171 #endif /* !_MSC_VER */
172 
173 #ifndef static_assert
174 #define CHECK_OFFSET(type, field, name) \
175     struct check_##type##_##field { int x[(name == offsetof(type, field)) ? 1 : -1]; }
176 #else
177 #define CHECK_OFFSET(type, field, name) \
178     static_assert(name == offsetof(type, field), #field)
179 #endif
180 
181 #ifdef _MSC_VER
182 #define PACKED(...) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop))
183 #else
184 #define PACKED(...) __VA_ARGS__ __attribute__((__packed__))
185 #endif
186 
187 #endif /* DAV1D_COMMON_ATTRIBUTES_H */
188