Coverage Report

Created: 2026-07-25 07:06

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
#if (defined(__VSX__) && defined(__powerpc64__)) && \
169
    (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_VSX))
170
#define WEBP_USE_VSX
171
#endif
172
173
#if defined(WEBP_USE_VSX) && !defined(WEBP_HAVE_VSX)
174
#define WEBP_HAVE_VSX
175
#endif
176
177
//------------------------------------------------------------------------------
178
179
#ifndef WEBP_DSP_OMIT_C_CODE
180
#define WEBP_DSP_OMIT_C_CODE 1
181
#endif
182
183
#if defined(WEBP_USE_NEON) && WEBP_DSP_OMIT_C_CODE
184
#define WEBP_NEON_OMIT_C_CODE 1
185
#else
186
#define WEBP_NEON_OMIT_C_CODE 0
187
#endif
188
189
#if !(LOCAL_CLANG_PREREQ(3, 8) || LOCAL_GCC_PREREQ(4, 8) || WEBP_AARCH64)
190
#define WEBP_NEON_WORK_AROUND_GCC 1
191
#else
192
#define WEBP_NEON_WORK_AROUND_GCC 0
193
#endif
194
195
//------------------------------------------------------------------------------
196
197
// This macro prevents thread_sanitizer from reporting known concurrent writes.
198
#define WEBP_TSAN_IGNORE_FUNCTION
199
#if defined(__has_feature)
200
#if __has_feature(thread_sanitizer)
201
#undef WEBP_TSAN_IGNORE_FUNCTION
202
#define WEBP_TSAN_IGNORE_FUNCTION __attribute__((no_sanitize_thread))
203
#endif
204
#endif
205
206
#if defined(__has_feature)
207
// Clang 21 should have all the MSAN fixes needed for WebP.
208
#if __has_feature(memory_sanitizer) && !LOCAL_CLANG_PREREQ(21, 0)
209
#define WEBP_MSAN
210
#endif
211
#endif
212
213
#if defined(WEBP_USE_THREAD)
214
#if defined(_WIN32)
215
#include <windows.h>
216
217
#if _WIN32_WINNT < 0x0600
218
#error _WIN32_WINNT must target Windows Vista / Server 2008 or newer.
219
#endif
220
// clang-format off
221
#define WEBP_DSP_INIT_VARS(func)               \
222
  static VP8CPUInfo func##_last_cpuinfo_used = \
223
      (VP8CPUInfo)&func##_last_cpuinfo_used;   \
224
  static SRWLOCK func##_lock = SRWLOCK_INIT
225
#define WEBP_DSP_INIT(func)                                \
226
  do {                                                     \
227
    AcquireSRWLockExclusive(&func##_lock);                 \
228
    if (func##_last_cpuinfo_used != VP8GetCPUInfo) func(); \
229
    func##_last_cpuinfo_used = VP8GetCPUInfo;              \
230
    ReleaseSRWLockExclusive(&func##_lock);                 \
231
  } while (0)
232
// clang-format on
233
#else  // !defined(_WIN32)
234
// NOLINTNEXTLINE
235
#include <pthread.h>
236
237
// clang-format off
238
#define WEBP_DSP_INIT_VARS(func)               \
239
  static VP8CPUInfo func##_last_cpuinfo_used = \
240
      (VP8CPUInfo)&func##_last_cpuinfo_used;   \
241
  static pthread_mutex_t func##_lock = PTHREAD_MUTEX_INITIALIZER
242
#define WEBP_DSP_INIT(func)                                \
243
758k
  do {                                                     \
244
758k
    if (pthread_mutex_lock(&func##_lock)) break;           \
245
758k
    if (func##_last_cpuinfo_used != VP8GetCPUInfo) func(); \
246
758k
    func##_last_cpuinfo_used = VP8GetCPUInfo;              \
247
758k
    (void)pthread_mutex_unlock(&func##_lock);              \
248
758k
  } while (0)
249
// clang-format on
250
#endif  // defined(_WIN32)
251
#else   // !defined(WEBP_USE_THREAD)
252
// clang-format off
253
#define WEBP_DSP_INIT_VARS(func)                        \
254
  static volatile VP8CPUInfo func##_last_cpuinfo_used = \
255
      (VP8CPUInfo)&func##_last_cpuinfo_used
256
#define WEBP_DSP_INIT(func)                               \
257
  do {                                                    \
258
    if (func##_last_cpuinfo_used == VP8GetCPUInfo) break; \
259
    func();                                               \
260
    func##_last_cpuinfo_used = VP8GetCPUInfo;             \
261
  } while (0)
262
// clang-format on
263
#endif  // defined(WEBP_USE_THREAD)
264
265
// Defines an Init + helper function that control multiple initialization of
266
// function pointers / tables.
267
/* Usage:
268
   WEBP_DSP_INIT_FUNC(InitFunc) {
269
     ...function body
270
   }
271
*/
272
#define WEBP_DSP_INIT_FUNC(name)                                            \
273
  WEBP_DSP_INIT_VARS(name##_body);                                          \
274
  static WEBP_TSAN_IGNORE_FUNCTION void name##_body(void);                  \
275
758k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
vp8_dec.c:InitGetCoeffs
Line
Count
Source
275
163k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
WebPInitAlphaProcessing
Line
Count
Source
275
19.8k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
VP8DspInit
Line
Count
Source
275
139k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
VP8FiltersInit
Line
Count
Source
275
4.07k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
VP8LDspInit
Line
Count
Source
275
120k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
Unexecuted instantiation: WebPRescalerDspInit
Unexecuted instantiation: WebPInitYUV444Converters
WebPInitUpsamplers
Line
Count
Source
275
139k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
WebPInitSamplers
Line
Count
Source
275
139k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
WebPInitGammaTables
Line
Count
Source
275
10.9k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
WebPInitConvertARGBToYUV
Line
Count
Source
275
5.48k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
VP8EncDspCostInit
Line
Count
Source
275
5.48k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
VP8EncDspInit
Line
Count
Source
275
5.48k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
VP8LEncDspInit
Line
Count
Source
275
5.18k
  WEBP_TSAN_IGNORE_FUNCTION void name(void) { WEBP_DSP_INIT(name##_body); } \
Unexecuted instantiation: VP8SSIMDspInit
276
  static WEBP_TSAN_IGNORE_FUNCTION void name##_body(void)
277
278
#define WEBP_UBSAN_IGNORE_UNDEF
279
#define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
280
#if defined(__clang__) && defined(__has_attribute)
281
#if __has_attribute(no_sanitize)
282
// This macro prevents the undefined behavior sanitizer from reporting
283
// failures. This is only meant to silence unaligned loads on platforms that
284
// are known to support them.
285
#undef WEBP_UBSAN_IGNORE_UNDEF
286
#define WEBP_UBSAN_IGNORE_UNDEF __attribute__((no_sanitize("undefined")))
287
288
// This macro prevents the undefined behavior sanitizer from reporting
289
// failures related to unsigned integer overflows. This is only meant to
290
// silence cases where this well defined behavior is expected.
291
#undef WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
292
#define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW \
293
  __attribute__((no_sanitize("unsigned-integer-overflow")))
294
#endif
295
#endif
296
297
// If 'ptr' is NULL, returns NULL. Otherwise returns 'ptr + off'.
298
// Prevents undefined behavior sanitizer nullptr-with-nonzero-offset warning.
299
#if !defined(WEBP_OFFSET_PTR)
300
39.6M
#define WEBP_OFFSET_PTR(ptr, off) (((ptr) == NULL) ? NULL : ((ptr) + (off)))
301
#endif
302
303
// Regularize the definition of WEBP_SWAP_16BIT_CSP (backward compatibility)
304
#if !defined(WEBP_SWAP_16BIT_CSP)
305
#define WEBP_SWAP_16BIT_CSP 0
306
#endif
307
308
// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
309
#if !defined(WORDS_BIGENDIAN) &&                   \
310
    (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
311
     (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
312
#define WORDS_BIGENDIAN
313
#endif
314
315
typedef enum {
316
  kSSE2,
317
  kSSE3,
318
  kSlowSSSE3,  // special feature for slow SSSE3 architectures
319
  kSSE4_1,
320
  kAVX,
321
  kAVX2,
322
  kNEON,
323
  kMIPS32,
324
  kMIPSdspR2,
325
  kMSA,
326
  kVSX
327
} CPUFeature;
328
329
// returns true if the CPU supports the feature.
330
typedef int (*VP8CPUInfo)(CPUFeature feature);
331
332
#endif  // WEBP_DSP_CPU_H_