History

repack: add restrict qualifier to src/dst pointers

Allows compiler to do its job and optimize this code. We don't really want to repack in-place.

Kacper Michajłow committed 2 years ago

Commit 374470d1 parent e0b51791 file · +36 −36Browse files

Unified · Split

video/repack.c+36−36
@@ -63,14 +63,14 @@ struct mp_repack { · 62 unmodified lines
6363 int components[4]; // b[n] = mp_image.planes[components[n]]
6464 // pack: a is dst, b is src
6565 // unpack: a is src, b is dst
66− void (*packed_repack_scanline)(void *a, void *b[], int w);
66+ void (*packed_repack_scanline)(void *restrict a, void *restrict b[], int w);
6767
6868 // Fringe RGB/YUV.
6969 uint8_t comp_size;
7070 uint8_t comp_map[6];
7171 uint8_t comp_shifts[3];
7272 uint8_t *comp_lut;
73− void (*repack_fringe_yuv)(void *dst, void *src[], int w, uint8_t *c);
73+ void (*repack_fringe_yuv)(void *restrict dst, void *restrict src[], int w, uint8_t *restrict c);
7474
7575 // F32 repacking.
7676 int f32_comp_size;
@@ -133,8 +133,8 @@ static void copy_plane(struct mp_image *dst, int dst_x, int dst_y, · 56 unmodified lines
133133 assert(dst->fmt.bpp[p] == src->fmt.bpp[p]);
134134
135135 for (int y = 0; y < h; y++) {
136− void *pd = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);
137− void *ps = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);
136+ void *restrict pd = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);
137+ void *restrict ps = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);
138138 memcpy(pd, ps, size);
139139 }
140140 }
@@ -157,8 +157,8 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 16 unmodified lines
157157 assert(src->fmt.bpp[p] == bpp * 8);
158158
159159 for (int y = 0; y < h; y++) {
160− void *s = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);
161− void *d = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);
160+ void *restrict s = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);
161+ void *restrict d = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);
162162 switch (endian_size) {
163163 case 2:
164164 for (int x = 0; x < num_words; x++)
@@ -191,7 +191,7 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 26 unmodified lines
191191 // packers will use "z" because they write zero.
192192
193193 #define PA_WORD_4(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, sh_c3) \
194− static void name(void *dst, void *src[], int w) { \
194+ static void name(void *restrict dst, void *restrict src[], int w) { \
195195 for (int x = 0; x < w; x++) { \
196196 ((packed_t *)dst)[x] = \
197197 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \
@@ -202,7 +202,7 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 4 unmodified lines
202202 }
203203
204204 #define UN_WORD_4(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, sh_c3, mask)\
205− static void name(void *src, void *dst[], int w) { \
205+ static void name(void *restrict src, void *restrict dst[], int w) { \
206206 for (int x = 0; x < w; x++) { \
207207 packed_t c = ((packed_t *)src)[x]; \
208208 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \
@@ -214,7 +214,7 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 5 unmodified lines
214214
215215
216216 #define PA_WORD_3(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, pad) \
217− static void name(void *dst, void *src[], int w) { \
217+ static void name(void *restrict dst, void *restrict src[], int w) { \
218218 for (int x = 0; x < w; x++) { \
219219 ((packed_t *)dst)[x] = (pad) | \
220220 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \
@@ -230,7 +230,7 @@ UN_WORD_4(un_cccc16, uint64_t, uint16_t, 0, 16, 32, 48, 0xFFFFu) · 9 unmodified lines
230230 PA_WORD_4(pa_cccc16, uint64_t, uint16_t, 0, 16, 32, 48)
231231
232232 #define UN_WORD_3(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, mask) \
233− static void name(void *src, void *dst[], int w) { \
233+ static void name(void *restrict src, void *restrict dst[], int w) { \
234234 for (int x = 0; x < w; x++) { \
235235 packed_t c = ((packed_t *)src)[x]; \
236236 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \
@@ -249,7 +249,7 @@ UN_WORD_3(un_ccc16x16, uint64_t, uint16_t, 0, 16, 32, 0xFFFFu) · 12 unmodified lines
249249 PA_WORD_3(pa_ccc16z16, uint64_t, uint16_t, 0, 16, 32, 0)
250250
251251 #define PA_WORD_2(name, packed_t, plane_t, sh_c0, sh_c1, pad) \
252− static void name(void *dst, void *src[], int w) { \
252+ static void name(void *restrict dst, void *restrict src[], int w) { \
253253 for (int x = 0; x < w; x++) { \
254254 ((packed_t *)dst)[x] = (pad) | \
255255 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \
@@ -258,7 +258,7 @@ PA_WORD_3(pa_ccc16z16, uint64_t, uint16_t, 0, 16, 32, 0) · 2 unmodified lines
258258 }
259259
260260 #define UN_WORD_2(name, packed_t, plane_t, sh_c0, sh_c1, mask) \
261− static void name(void *src, void *dst[], int w) { \
261+ static void name(void *restrict src, void *restrict dst[], int w) { \
262262 for (int x = 0; x < w; x++) { \
263263 packed_t c = ((packed_t *)src)[x]; \
264264 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \
@@ -272,7 +272,7 @@ UN_WORD_2(un_cc16, uint32_t, uint16_t, 0, 16, 0xFFFFu) · 7 unmodified lines
272272 PA_WORD_2(pa_cc16, uint32_t, uint16_t, 0, 16, 0)
273273
274274 #define PA_SEQ_3(name, comp_t) \
275− static void name(void *dst, void *src[], int w) { \
275+ static void name(void *restrict dst, void *restrict src[], int w) { \
276276 comp_t *r = dst; \
277277 for (int x = 0; x < w; x++) { \
278278 *r++ = ((comp_t *)src[0])[x]; \
@@ -282,7 +282,7 @@ PA_WORD_2(pa_cc16, uint32_t, uint16_t, 0, 16, 0) · 3 unmodified lines
282282 }
283283
284284 #define UN_SEQ_3(name, comp_t) \
285− static void name(void *src, void *dst[], int w) { \
285+ static void name(void *restrict src, void *restrict dst[], int w) { \
286286 comp_t *r = src; \
287287 for (int x = 0; x < w; x++) { \
288288 ((comp_t *)dst[0])[x] = *r++; \
@@ -302,8 +302,8 @@ struct regular_repacker { · 13 unmodified lines
302302 int component_width; // number of bits for a single component
303303 int prepadding; // number of bits of LSB padding
304304 int num_components; // number of components that can be accessed
305− void (*pa_scanline)(void *a, void *b[], int w);
306− void (*un_scanline)(void *a, void *b[], int w);
305+ void (*pa_scanline)(void *restrict a, void *restrict b[], int w);
306+ void (*un_scanline)(void *restrict a, void *restrict b[], int w);
307307 };
308308
309309 static const struct regular_repacker regular_repackers[] = {
@@ -384,7 +384,7 @@ static void setup_packed_packer(struct mp_repack *rp) · 74 unmodified lines
384384
385385 int prepad = components[0] ? 0 : 8;
386386 int first_comp = components[0] ? 0 : 1;
387− void (*repack_cb)(void *pa, void *pb[], int w) =
387+ void (*repack_cb)(void *restrict pa, void *restrict pb[], int w) =
388388 rp->pack ? pa->pa_scanline : pa->un_scanline;
389389
390390 if (pa->packed_width != desc.bpp[0] ||
@@ -408,8 +408,8 @@ static void setup_packed_packer(struct mp_repack *rp) · 17 unmodified lines
408408 }
409409
410410 #define PA_SHIFT_LUT8(name, packed_t) \
411− static void name(void *dst, void *src[], int w, uint8_t *lut, \
412− uint8_t s0, uint8_t s1, uint8_t s2) { \
411+ static void name(void *restrict dst, void *restrict src[], int w, \
412+ uint8_t *restrict lut, uint8_t s0, uint8_t s1, uint8_t s2) { \
413413 for (int x = 0; x < w; x++) { \
414414 ((packed_t *)dst)[x] = \
415415 (lut[((uint8_t *)src[0])[x] + 256 * 0] << s0) | \
@@ -420,8 +420,8 @@ static void setup_packed_packer(struct mp_repack *rp) · 4 unmodified lines
420420
421421
422422 #define UN_SHIFT_LUT8(name, packed_t) \
423− static void name(void *src, void *dst[], int w, uint8_t *lut, \
424− uint8_t s0, uint8_t s1, uint8_t s2) { \
423+ static void name(void *restrict src, void *restrict dst[], int w, \
424+ uint8_t *restrict lut, uint8_t s0, uint8_t s1, uint8_t s2) { \
425425 for (int x = 0; x < w; x++) { \
426426 packed_t c = ((packed_t *)src)[x]; \
427427 ((uint8_t *)dst[0])[x] = lut[((c >> s0) & 0xFF) + 256 * 0]; \
@@ -449,7 +449,7 @@ static void fringe_rgb_repack(struct mp_repack *rp, · 21 unmodified lines
449449
450450 assert(rp->comp_size == 1 || rp->comp_size == 2);
451451
452− void (*repack)(void *pa, void *pb[], int w, uint8_t *lut,
452+ void (*repack)(void *restrict pa, void *restrict pb[], int w, uint8_t *restrict lut,
453453 uint8_t s0, uint8_t s1, uint8_t s2) = NULL;
454454 if (rp->pack) {
455455 repack = rp->comp_size == 1 ? pa_shift_lut8_8 : pa_shift_lut8_16;
@@ -525,10 +525,10 @@ static void unpack_pal(struct mp_repack *rp, · 69 unmodified lines
525525 struct mp_image *a, int a_x, int a_y,
526526 struct mp_image *b, int b_x, int b_y, int w)
527527 {
528− uint8_t *src = mp_image_pixel_ptr(a, 0, a_x, a_y);
528+ uint8_t *restrict src = mp_image_pixel_ptr(a, 0, a_x, a_y);
529529 uint32_t *pal = (void *)a->planes[1];
530530
531− uint8_t *dst[4] = {0};
531+ uint8_t *restrict dst[4] = {0};
532532 for (int p = 0; p < b->num_planes; p++)
533533 dst[p] = mp_image_pixel_ptr(b, p, b_x, b_y);
534534
@@ -545,8 +545,8 @@ static void bitmap_repack(struct mp_repack *rp, · 10 unmodified lines
545545 struct mp_image *a, int a_x, int a_y,
546546 struct mp_image *b, int b_x, int b_y, int w)
547547 {
548− uint8_t *pa = mp_image_pixel_ptr(a, 0, a_x, a_y);
549− uint8_t *pb = mp_image_pixel_ptr(b, 0, b_x, b_y);
548+ uint8_t *restrict pa = mp_image_pixel_ptr(a, 0, a_x, a_y);
549+ uint8_t *restrict pb = mp_image_pixel_ptr(b, 0, b_x, b_y);
550550
551551 if (rp->pack) {
552552 for (unsigned x = 0; x < w; x += 8) {
@@ -596,7 +596,7 @@ static void setup_misc_packer(struct mp_repack *rp) · 43 unmodified lines
596596 }
597597
598598 #define PA_P422(name, comp_t) \
599− static void name(void *dst, void *src[], int w, uint8_t *c) { \
599+ static void name(void *restrict dst, void *restrict src[], int w, uint8_t *restrict c) { \
600600 for (int x = 0; x < w; x += 2) { \
601601 ((comp_t *)dst)[x * 2 + c[0]] = ((comp_t *)src[0])[x + 0]; \
602602 ((comp_t *)dst)[x * 2 + c[1]] = ((comp_t *)src[0])[x + 1]; \
@@ -607,7 +607,7 @@ static void setup_misc_packer(struct mp_repack *rp) · 4 unmodified lines
607607
608608
609609 #define UN_P422(name, comp_t) \
610− static void name(void *src, void *dst[], int w, uint8_t *c) { \
610+ static void name(void *restrict src, void *restrict dst[], int w, uint8_t *restrict c) { \
611611 for (int x = 0; x < w; x += 2) { \
612612 ((comp_t *)dst[0])[x + 0] = ((comp_t *)src)[x * 2 + c[0]]; \
613613 ((comp_t *)dst[0])[x + 1] = ((comp_t *)src)[x * 2 + c[1]]; \
@@ -621,7 +621,7 @@ PA_P422(pa_p422_16, uint16_t) · 7 unmodified lines
621621 UN_P422(un_p422_8, uint8_t)
622622 UN_P422(un_p422_16, uint16_t)
623623
624−static void pa_p411_8(void *dst, void *src[], int w, uint8_t *c)
624+static void pa_p411_8(void *restrict dst, void *restrict src[], int w, uint8_t *restrict c)
625625 {
626626 for (int x = 0; x < w; x += 4) {
627627 ((uint8_t *)dst)[x / 4 * 6 + c[0]] = ((uint8_t *)src[0])[x + 0];
@@ -634,7 +634,7 @@ static void pa_p411_8(void *dst, void *src[], int w, uint8_t *c) · 6 unmodified lines
634634 }
635635
636636
637−static void un_p411_8(void *src, void *dst[], int w, uint8_t *c)
637+static void un_p411_8(void *restrict src, void *restrict dst[], int w, uint8_t *restrict c)
638638 {
639639 for (int x = 0; x < w; x += 4) {
640640 ((uint8_t *)dst[0])[x + 0] = ((uint8_t *)src)[x / 4 * 6 + c[0]];
@@ -773,7 +773,7 @@ static void setup_nv_packer(struct mp_repack *rp) · 132 unmodified lines
773773 for (int i = 0; i < MP_ARRAY_SIZE(regular_repackers); i++) {
774774 const struct regular_repacker *pa = &regular_repackers[i];
775775
776− void (*repack_cb)(void *pa, void *pb[], int w) =
776+ void (*repack_cb)(void *restrict pa, void *restrict pb[], int w) =
777777 rp->pack ? pa->pa_scanline : pa->un_scanline;
778778
779779 if (pa->packed_width != desc.component_size * 2 * 8 ||
@@ -794,8 +794,8 @@ static void setup_nv_packer(struct mp_repack *rp) · 14 unmodified lines
794794 }
795795
796796 #define PA_F32(name, packed_t) \
797− static void name(void *dst, float *src, int w, float m, float o, \
798− uint32_t p_max) { \
797+ static void name(void *restrict dst, float *restrict src, int w, float m, \
798+ float o, uint32_t p_max) { \
799799 for (int x = 0; x < w; x++) { \
800800 ((packed_t *)dst)[x] = \
801801 MPCLAMP(lrint((src[x] + o) * m), 0, (packed_t)p_max); \
@@ -803,8 +803,8 @@ static void setup_nv_packer(struct mp_repack *rp) · 1 unmodified line
803803 }
804804
805805 #define UN_F32(name, packed_t) \
806− static void name(void *src, float *dst, int w, float m, float o, \
807− uint32_t unused) { \
806+ static void name(void *restrict src, float *restrict dst, int w, float m, \
807+ float o, uint32_t unused) { \
808808 for (int x = 0; x < w; x++) \
809809 dst[x] = ((packed_t *)src)[x] * m + o; \
810810 }
@@ -821,7 +821,7 @@ static void repack_float(struct mp_repack *rp, · 10 unmodified lines
821821 {
822822 assert(rp->f32_comp_size == 1 || rp->f32_comp_size == 2);
823823
824− void (*packer)(void *a, float *b, int w, float fm, float fb, uint32_t max)
824+ void (*packer)(void *restrict a, float *restrict b, int w, float fm, float fb, uint32_t max)
825825 = rp->pack ? (rp->f32_comp_size == 1 ? pa_f32_8 : pa_f32_16)
826826 : (rp->f32_comp_size == 1 ? un_f32_8 : un_f32_16);
827827

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t