FFmpeg
Loading...
Searching...
No Matches
uops_tmpl.h
Go to the documentation of this file.
1/**
2 * Copyright (C) 2026 Niklas Haas
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef SWSCALE_UOPS_TMPL_H
22#define SWSCALE_UOPS_TMPL_H
23
24/**
25 * Helper macros for the C-based backend.
26 *
27 * To use these macros, `pixel_t` should be defined as the type of pixels.
28 */
29
30#include <assert.h>
31#include <float.h>
32#include <stdint.h>
33
35#include "libavutil/sanitizer.h"
36
37#include "ops_chain.h"
38#include "uops_macros.h"
39
40#ifndef SWS_BLOCK_SIZE
41# define SWS_BLOCK_SIZE 32
42#endif
43
44typedef union block_t {
49} block_t;
50
51#define SIZEOF_BLOCK (sizeof(pixel_t) * SWS_BLOCK_SIZE)
52
53/**
54 * Internal context holding per-iter execution data. The data pointers will be
55 * directly incremented by the corresponding read/write functions.
56 */
57typedef struct SwsOpIter {
58 uintptr_t in[4];
59 uintptr_t out[4];
60 int x, y;
61
62 /* Link back to per-slice execution context */
64} SwsOpIter;
65
66/* Private data for generic kernels. */
67typedef union SwsOpPriv {
68 float f32[4];
69 uint32_t u32[4];
70 uint16_t u16[4];
71 uint8_t u8[4];
72 void *ptr;
73} SwsOpPriv;
74
75#define SWS_OP_PRIV(impl) ((const void *) (impl)->priv)
76
77/* Private data for filtered reads */
78typedef struct SwsFilterPriv {
79 void *weights;
82
83static_assert(alignof(SwsFilterPriv) <= alignof(SwsOpPriv),
84 "SwsFilterPriv alignment exceeds SwsOpPriv");
85
86/* UBSan checks inside the loop body defeat the vectorizer, so do not ask
87 * for it in those builds. Clang warns for every loop otherwise. */
88#if HAVE_UBSAN
89# define SWS_LOOP
90#elif defined(__clang__)
91# define SWS_LOOP AV_PRAGMA(clang loop vectorize(assume_safety))
92#elif defined(__GNUC__)
93# define SWS_LOOP AV_PRAGMA(GCC ivdep)
94#else
95# define SWS_LOOP
96#endif
97
98/* Miscellaneous helpers */
99#define bitfn2(name, ext) name ## _ ## ext
100#define bitfn(name, ext) bitfn2(name, ext)
101#define fn(name) bitfn(name, PX)
102
103#define bump_ptr(ptr, bump) ((pixel_t *) ((uintptr_t) (ptr) + (bump)))
104
105/* Helpers for dealing with component masks */
106#define X SWS_COMP_TEST(mask, 0)
107#define Y SWS_COMP_TEST(mask, 1)
108#define Z SWS_COMP_TEST(mask, 2)
109#define W SWS_COMP_TEST(mask, 3)
110
111/* Helper macros to make writing common function signatures less painful */
112#define DECL_FUNC(NAME, ...) \
113 av_always_inline static void \
114 fn(NAME)(SwsOpIter *restrict iter, const SwsOpImpl *restrict impl, \
115 pixel_t *restrict x, pixel_t *restrict y, \
116 pixel_t *restrict z, pixel_t *restrict w, \
117 __VA_ARGS__)
118
119#define DECL_READ(NAME, ...) \
120 DECL_FUNC(NAME, __VA_ARGS__, \
121 const pixel_t *restrict in0, const pixel_t *restrict in1, \
122 const pixel_t *restrict in2, const pixel_t *restrict in3) \
123
124#define DECL_WRITE(NAME, ...) \
125 DECL_FUNC(NAME, __VA_ARGS__, \
126 pixel_t *restrict out0, pixel_t *restrict out1, \
127 pixel_t *restrict out2, pixel_t *restrict out3) \
128
129#define CALL(NAME, ...) fn(NAME)(iter, impl, x, y, z, w, __VA_ARGS__)
130
131#define NEXT_IMPL(N) \
132 ((const SwsOpImpl *) &impl->priv[FFALIGN(N, alignof(SwsOpImpl))])
133
134/* Helper macro to call into the next continuation with a given type; N gives
135 * the size of private data consumed by this op */
136#define CONTINUE(N, ...) \
137 ((void (*)(SwsOpIter *, const SwsOpImpl *, \
138 void *restrict x, void *restrict y, \
139 void *restrict z, void *restrict w)) NEXT_IMPL(N)->func) \
140 (iter, NEXT_IMPL(N), __VA_ARGS__)
141
142/* Helper macros for common op setup code */
143#define DECL_SETUP(NAME, PARAMS, OUT) \
144 av_unused static int fn(NAME)(const SwsImplParams *PARAMS, \
145 SwsImplResult *OUT)
146
147/* Helper macro for declaring kernel entry points */
148#define DECL_IMPL(FUNC, NAME, TYPE, UOP, ...) \
149 av_flatten static void NAME##_c(SwsOpIter *restrict iter, \
150 const SwsOpImpl *restrict impl, \
151 void *restrict x, void *restrict y, \
152 void *restrict z, void *restrict w) \
153 { \
154 CALL(FUNC, __VA_ARGS__); \
155 }
156
157#define DECL_IMPL_READ(...) \
158 DECL_IMPL(__VA_ARGS__, \
159 (const pixel_t *) iter->in[0], (const pixel_t *) iter->in[1], \
160 (const pixel_t *) iter->in[2], (const pixel_t *) iter->in[3])
161
162#define DECL_IMPL_WRITE(...) \
163 DECL_IMPL(__VA_ARGS__, \
164 (pixel_t *) iter->out[0], (pixel_t *) iter->out[1], \
165 (pixel_t *) iter->out[2], (pixel_t *) iter->out[3])
166
167#define REF_ENTRY(DUMMY, NAME, ...) &uop_##NAME,
168#define DECL_ENTRY(SETUP, NAME, ...) \
169 static const SwsUOpEntry uop_##NAME = { \
170 .func = (SwsFuncPtr) NAME##_c, \
171 __VA_ARGS__, \
172 SETUP \
173 };
174
175#endif
Macro definitions for various function/variable attributes.
void * weights
Definition uops_tmpl.h:79
Copyright (C) 2026 Niklas Haas.
Internal context holding per-iter execution data.
Definition uops_tmpl.h:57
uintptr_t in[4]
Definition uops_tmpl.h:58
const SwsOpExec * exec
Definition uops_tmpl.h:63
uintptr_t out[4]
Definition uops_tmpl.h:59
uint32_t u32[4]
Definition uops_tmpl.h:69
uint8_t u8[4]
Definition uops_tmpl.h:71
void * ptr
Definition uops_tmpl.h:72
uint16_t u16[4]
Definition uops_tmpl.h:70
float f32[4]
Definition uops_tmpl.h:68
uint32_t u32[SWS_BLOCK_SIZE]
Definition uops_tmpl.h:46
float f32[SWS_BLOCK_SIZE]
Definition uops_tmpl.h:45
uint16_t u16[SWS_BLOCK_SIZE]
Definition uops_tmpl.h:47
uint8_t u8[SWS_BLOCK_SIZE]
Definition uops_tmpl.h:48
#define SWS_BLOCK_SIZE
Copyright (C) 2026 Niklas Haas.
Definition uops_tmpl.h:41