History

lavc/audiodsp: properly unroll vector_clipf

Given that source and destination can alias, the compiler was forced to perform each read-modify-write sequentially. We cannot use the `restrict` qualifier to avoid this here because the AC-3 encoder uses the function in-place. Instead this commit provides an explicit guarantee to the compiler that batches of 8 elements will not overlap, so that it can interleave calculations. In practice contemporary optimising compilers are able to unroll and keep the temporary array in FPU registers (without spilling). On SiFive-U74, this speeds the same signs branch by 4x, and the opposite signs branch 1.5x.

Rémi Denis-Courmont committed 2 years ago

Commit d86b6761 parent d527d231 file · +17 −23Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/audiodsp.c+17−23
@@ -38,41 +38,35 @@ static inline float clipf_c_one(float a, uint32_t mini, · 37 unmodified lines
38static void vector_clipf_c_opposite_sign(float *dst, const float *src,38static void vector_clipf_c_opposite_sign(float *dst, const float *src,
39 float min, float max, int len)39 float min, float max, int len)
40{40{
41 int i;
42 uint32_t mini = av_float2int(min);41 uint32_t mini = av_float2int(min);
43 uint32_t maxi = av_float2int(max);42 uint32_t maxi = av_float2int(max);
44 uint32_t maxisign = maxi ^ (1U << 31);43 uint32_t maxisign = maxi ^ (1U << 31);
45 44
46 for (i = 0; i < len; i += 8) {45 for (int i = 0; i < len; i += 8) {
47 dst[i + 0] = clipf_c_one(src[i + 0], mini, maxi, maxisign);46 float tmp[8];
48 dst[i + 1] = clipf_c_one(src[i + 1], mini, maxi, maxisign);47
49 dst[i + 2] = clipf_c_one(src[i + 2], mini, maxi, maxisign);48 for (int j = 0; j < 8; j++)
50 dst[i + 3] = clipf_c_one(src[i + 3], mini, maxi, maxisign);49 tmp[j]= clipf_c_one(src[i + j], mini, maxi, maxisign);
51 dst[i + 4] = clipf_c_one(src[i + 4], mini, maxi, maxisign);50 for (int j = 0; j < 8; j++)
52 dst[i + 5] = clipf_c_one(src[i + 5], mini, maxi, maxisign);51 dst[i + j] = tmp[j];
53 dst[i + 6] = clipf_c_one(src[i + 6], mini, maxi, maxisign);
54 dst[i + 7] = clipf_c_one(src[i + 7], mini, maxi, maxisign);
55 }52 }
56}53}
57 54
58static void vector_clipf_c(float *dst, const float *src, int len,55static void vector_clipf_c(float *dst, const float *src, int len,
59 float min, float max)56 float min, float max)
60{57{
61 int i;
62
63 if (min < 0 && max > 0) {58 if (min < 0 && max > 0) {
64 vector_clipf_c_opposite_sign(dst, src, min, max, len);59 vector_clipf_c_opposite_sign(dst, src, min, max, len);
65 } else {60 return;
66 for (i = 0; i < len; i += 8) {61 }
67 dst[i] = av_clipf(src[i], min, max);62
68 dst[i + 1] = av_clipf(src[i + 1], min, max);63 for (int i = 0; i < len; i += 8) {
69 dst[i + 2] = av_clipf(src[i + 2], min, max);64 float tmp[8];
70 dst[i + 3] = av_clipf(src[i + 3], min, max);65
71 dst[i + 4] = av_clipf(src[i + 4], min, max);66 for (int j = 0; j < 8; j++)
72 dst[i + 5] = av_clipf(src[i + 5], min, max);67 tmp[j]= av_clipf(src[i + j], min, max);
73 dst[i + 6] = av_clipf(src[i + 6], min, max);68 for (int j = 0; j < 8; j++)
74 dst[i + 7] = av_clipf(src[i + 7], min, max);69 dst[i + j] = tmp[j];
75 }
76 }70 }
77}71}
78 72

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