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