Coverage Report

Created: 2026-01-13 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/dsp/cpu.h
Line
Count
Source
1
// Copyright 2022 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
//   CPU detection functions and macros.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#ifndef WEBP_DSP_CPU_H_
15
#define WEBP_DSP_CPU_H_
16
17
#include <stddef.h>
18
19
#ifdef HAVE_CONFIG_H
20
#include "src/webp/config.h"
21
#endif
22
23
#include "src/webp/types.h"
24
25
#if defined(__GNUC__)
26
#define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
27
#define LOCAL_GCC_PREREQ(maj, min) (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
28
#else
29
#define LOCAL_GCC_VERSION 0
30
#define LOCAL_GCC_PREREQ(maj, min) 0
31
#endif
32
33
#if defined(__clang__)
34
#define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
35
#define LOCAL_CLANG_PREREQ(maj, min) \
36
  (LOCAL_CLANG_VERSION >= (((maj) << 8) | (min)))
37
#else
38
#define LOCAL_CLANG_VERSION 0
39
#define LOCAL_CLANG_PREREQ(maj, min) 0
40
#endif
41
42
#ifndef __has_builtin
43
#define __has_builtin(x) 0
44
#endif
45
46
//------------------------------------------------------------------------------
47
// x86 defines.
48
49
#if !defined(HAVE_CONFIG_H)
50
#if defined(_MSC_VER) && _MSC_VER > 1310 && \
51
    (defined(_M_X64) || defined(_M_IX86))
52
#define WEBP_MSC_SSE2  // Visual C++ SSE2 targets
53
#endif
54
55
#if defined(_MSC_VER) && _MSC_VER >= 1500 && \
56
    (defined(_M_X64) || defined(_M_IX86))
57
#define WEBP_MSC_SSE41  // Visual C++ SSE4.1 targets
58
#endif
59
60
#if defined(_MSC_VER) && _MSC_VER >= 1700 && \
61
    (defined(_M_X64) || defined(_M_IX86))
62
#define WEBP_MSC_AVX2  // Visual C++ AVX2 targets
63
#endif
64
#endif
65
66
// WEBP_HAVE_* are used to indicate the presence of the instruction set in dsp
67
// files without intrinsics, allowing the corresponding Init() to be called.
68
// Files containing intrinsics will need to be built targeting the instruction
69
// set so should succeed on one of the earlier tests.
70
#if (defined(__SSE2__) || defined(WEBP_MSC_SSE2)) && \
71
    (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_SSE2))
72
#define WEBP_USE_SSE2
73
#endif
74
75
#if defined(WEBP_USE_SSE2) && !defined(WEBP_HAVE_SSE2)
76
#define WEBP_HAVE_SSE2
77
#endif
78
79
#if (defined(__SSE4_1__) || defined(WEBP_MSC_SSE41)) && \
80
    (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_SSE41))
81
#define WEBP_USE_SSE41
82
#endif
83
84
#if defined(WEBP_USE_SSE41) && !defined(WEBP_HAVE_SSE41)
85
#define WEBP_HAVE_SSE41
86
#endif
87
88
#if (defined(__AVX2__) || defined(WEBP_MSC_AVX2)) && \
89
    (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_AVX2))
90
#define WEBP_USE_AVX2
91
#endif
92
93
#if defined(WEBP_USE_AVX2) && !defined(WEBP_HAVE_AVX2)
94
#define WEBP_HAVE_AVX2
95
#endif
96
97
#if defined(WEBP_MSC_AVX2) && _MSC_VER <= 1900
98
#include <immintrin.h>
99
100
static WEBP_INLINE int _mm256_extract_epi32(__m256i a, const int i) {
101
  return a.m256i_i32[i & 7];
102
}
103
static WEBP_INLINE int _mm256_cvtsi256_si32(__m256i a) {
104
  return _mm256_extract_epi32(a, 0);
105
}
106
#endif
107
108
#undef WEBP_MSC_AVX2
109
#undef WEBP_MSC_SSE41
110
#undef WEBP_MSC_SSE2
111
112
//------------------------------------------------------------------------------
113
// Arm defines.
114
115
// The intrinsics currently cause compiler errors with arm-nacl-gcc and the
116
// inline assembly would need to be modified for use with Native Client.
117
#if ((defined(__ARM_NEON__) || defined(__aarch64__)) &&       \
118
     (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_NEON))) && \
119
    !defined(__native_client__)
120
#define WEBP_USE_NEON
121
#endif
122
123
#if !defined(WEBP_USE_NEON) && defined(__ANDROID__) && \
124
    defined(__ARM_ARCH_7A__) && defined(HAVE_CPU_FEATURES_H)
125
#define WEBP_ANDROID_NEON  // Android targets that may have NEON
126
#define WEBP_USE_NEON
127
#endif
128
129
// Note: ARM64 is supported in Visual Studio 2017, but requires the direct
130
// inclusion of arm64_neon.h; Visual Studio 2019 includes this file in
131
// arm_neon.h. Compile errors were seen with Visual Studio 2019 16.4 with
132
// vtbl4_u8(); a fix was made in 16.6.
133
#if defined(_MSC_VER) &&                      \
134
    ((_MSC_VER >= 1700 && defined(_M_ARM)) || \
135
     (_MSC_VER >= 1926 && (defined(_M_ARM64) || defined(_M_ARM64EC))))
136
#define WEBP_USE_NEON
137
#define WEBP_USE_INTRINSICS
138
#endif
139
140
#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
141
#define WEBP_AARCH64 1
142
#else
143
#define WEBP_AARCH64 0
144
#endif
145
146
#if defined(WEBP_USE_NEON) && !defined(WEBP_HAVE_NEON)
147
#define WEBP_HAVE_NEON
148
#endif
149
150
//------------------------------------------------------------------------------
151
// MIPS defines.
152
153
#if defined(__mips__) && !defined(__mips64) && defined(__mips_isa_rev) && \
154
    (__mips_isa_rev >= 1) && (__mips_isa_rev < 6)
155
#define WEBP_USE_MIPS32
156
#if (__mips_isa_rev >= 2)
157
#define WEBP_USE_MIPS32_R2
158
#if defined(__mips_dspr2) || (defined(__mips_dsp_rev) && __mips_dsp_rev >= 2)
159
#define WEBP_USE_MIPS_DSP_R2
160
#endif
161
#endif
162
#endif
163
164
#if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
165
#define WEBP_USE_MSA
166
#endif
167
168
//------------------------------------------------------------------------------
169
170
#ifndef WEBP_DSP_OMIT_C_CODE
171
#define WEBP_DSP_OMIT_C_CODE 1
172
#endif
173
174
#if defined(WEBP_USE_NEON) && WEBP_DSP_OMIT_C_CODE
175
#define WEBP_NEON_OMIT_C_CODE 1
176
#else
177
#define WEBP_NEON_OMIT_C_CODE 0
178
#endif
179
180
#if !(LOCAL_CLANG_PREREQ(3, 8) || LOCAL_GCC_PREREQ(4, 8) || WEBP_AARCH64)
181
#define WEBP_NEON_WORK_AROUND_GCC 1
182
#else
183
#define WEBP_NEON_WORK_AROUND_GCC 0
184
#endif
185
186
//------------------------------------------------------------------------------
187
188
// This macro prevents thread_sanitizer from reporting known concurrent writes.
189
#define WEBP_TSAN_IGNORE_FUNCTION
190
#if defined(__has_feature)
191
#if __has_feature(thread_sanitizer)
192
#undef WEBP_TSAN_IGNORE_FUNCTION
193
#define WEBP_TSAN_IGNORE_FUNCTION __attribute__((no_sanitize_thread))
194
#endif
195
#endif
196
197
#if defined(__has_feature)
198
// Clang 21 should have all the MSAN fixes needed for WebP.
199
#if __has_feature(memory_sanitizer) && !LOCAL_CLANG_PREREQ(21, 0)
200
#define WEBP_MSAN
201
#endif
202
#endif
203
204
#if defined(WEBP_USE_THREAD)
205
#if defined(_WIN32)
206
#include <windows.h>
207
208
#if _WIN32_WINNT < 0x0600
209
#error _WIN32_WINNT must target Windows Vista / Server 2008 or newer.
210
#endif
211
// clang-format off
212
#define WEBP_DSP_INIT_VARS(func)               \
213
  static VP8CPUInfo func##_last_cpuinfo_used = \
214
      (VP8CPUInfo)&func##_last_cpuinfo_used;   \
215
  static SRWLOCK func##_lock = SRWLOCK_INIT
216
#define WEBP_DSP_INIT(func)                                \
217
  do {                                                     \
218
    AcquireSRWLockExclusive(&func##_lock);                 \
219
    if (func##_last_cpuinfo_used != VP8GetCPUInfo) func(); \
220
    func##_last_cpuinfo_used = VP8GetCPUInfo;              \
221
    ReleaseSRWLockExclusive(&func##_lock);                 \
222
  } while (0)
223
// clang-format on
224
#else  // !defined(_WIN32)
225
// NOLINTNEXTLINE
226
#include <pthread.h>
227
228
// clang-format off
229
#define WEBP_DSP_INIT_VARS(func)               \
230
  static VP8CPUInfo func##_last_cpuinfo_used = \
231
      (VP8CPUInfo)&func##_last_cpuinfo_used;   \
232
  static pthread_mutex_t func##_lock = PTHREAD_MUTEX_INITIALIZER
233
#define WEBP_DSP_INIT(func)                                \
234
10.5k
  do {                                                     \
235
10.5k
    if (pthread_mutex_lock(&func##_lock)) break;           \
236
10.5k
    if (func##_last_cpuinfo_used != VP8GetCPUInfo) func(); \
237
10.5k
    func##_last_cpuinfo_used = VP8GetCPUInfo;              \
238
10.5k
    (void)pthread_mutex_unlock(&func##_lock);              \
239
10.5k
  } while (0)
240
// clang-format on
241
#endif  // defined(_WIN32)
242
#else   // !defined(WEBP_USE_THREAD)
243
// clang-format off
244
#define WEBP_DSP_INIT_VARS(func)                        \
245
  static volatile VP8CPUInfo func##_last_cpuinfo_used = \
246
      (VP8CPUInfo)&func##_last_cpuinfo_used
247
#define WEBP_DSP_INIT(func)                               \
248
  do {                                                    \
249
    if (func##_last_cpuinfo_used == VP8GetCPUInfo) break; \
250
    func();                                               \
251
    func##_last_cpuinfo_used = VP8GetCPUInfo;             \
252
  } while (0)
253
// clang-format on
254
#endif  // defined(WEBP_USE_THREAD)
255
256
// Defines an Init + helper function that control multiple initialization of
257
// function pointers / tables.
258
/* Usage:
259
   WEBP_DSP_INIT_FUNC(InitFunc) {
260
     ...function body
261
   }
262
*/
263
#define WEBP_DSP_INIT_FUNC(name)                                            \
264
  WEBP_DSP_INIT_VARS(name##_body);                                          \
265
  static WEBP_TSAN_IGNORE_FUNCTION void name##_body(void);                  \
266
10.5k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
vp8_dec.c:InitGetCoeffs
Line
Count
Source
266
1.88k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
WebPInitAlphaProcessing
Line
Count
Source
266
1.78k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
Unexecuted instantiation: VP8EncDspCostInit
VP8DspInit
Line
Count
Source
266
1.78k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
Unexecuted instantiation: VP8EncDspInit
Unexecuted instantiation: VP8FiltersInit
VP8LDspInit
Line
Count
Source
266
1.51k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
Unexecuted instantiation: WebPRescalerDspInit
Unexecuted instantiation: WebPInitYUV444Converters
WebPInitUpsamplers
Line
Count
Source
266
1.78k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
WebPInitSamplers
Line
Count
Source
266
1.78k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
Unexecuted instantiation: WebPInitGammaTables
Unexecuted instantiation: WebPInitConvertARGBToYUV
Unexecuted instantiation: VP8LEncDspInit
Unexecuted instantiation: VP8SSIMDspInit
267
  static WEBP_TSAN_IGNORE_FUNCTION void name##_body(void)
268
269
#define WEBP_UBSAN_IGNORE_UNDEF
270
#define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
271
#if defined(__clang__) && defined(__has_attribute)
272
#if __has_attribute(no_sanitize)
273
// This macro prevents the undefined behavior sanitizer from reporting
274
// failures. This is only meant to silence unaligned loads on platforms that
275
// are known to support them.
276
#undef WEBP_UBSAN_IGNORE_UNDEF
277
#define WEBP_UBSAN_IGNORE_UNDEF __attribute__((no_sanitize("undefined")))
278
279
// This macro prevents the undefined behavior sanitizer from reporting
280
// failures related to unsigned integer overflows. This is only meant to
281
// silence cases where this well defined behavior is expected.
282
#undef WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
283
#define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW \
284
  __attribute__((no_sanitize("unsigned-integer-overflow")))
285
#endif
286
#endif
287
288
// If 'ptr' is NULL, returns NULL. Otherwise returns 'ptr + off'.
289
// Prevents undefined behavior sanitizer nullptr-with-nonzero-offset warning.
290
#if !defined(WEBP_OFFSET_PTR)
291
0
#define WEBP_OFFSET_PTR(ptr, off) (((ptr) == NULL) ? NULL : ((ptr) + (off)))
292
#endif
293
294
// Regularize the definition of WEBP_SWAP_16BIT_CSP (backward compatibility)
295
#if !defined(WEBP_SWAP_16BIT_CSP)
296
#define WEBP_SWAP_16BIT_CSP 0
297
#endif
298
299
// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
300
#if !defined(WORDS_BIGENDIAN) &&                   \
301
    (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
302
     (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
303
#define WORDS_BIGENDIAN
304
#endif
305
306
typedef enum {
307
  kSSE2,
308
  kSSE3,
309
  kSlowSSSE3,  // special feature for slow SSSE3 architectures
310
  kSSE4_1,
311
  kAVX,
312
  kAVX2,
313
  kNEON,
314
  kMIPS32,
315
  kMIPSdspR2,
316
  kMSA
317
} CPUFeature;
318
319
// returns true if the CPU supports the feature.
320
typedef int (*VP8CPUInfo)(CPUFeature feature);
321
322
#endif  // WEBP_DSP_CPU_H_