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

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
3838 static void vector_clipf_c_opposite_sign(float *dst, const float *src,
3939 float min, float max, int len)
4040 {
41− int i;
4241 uint32_t mini = av_float2int(min);
4342 uint32_t maxi = av_float2int(max);
4443 uint32_t maxisign = maxi ^ (1U << 31);
4544
46− for (i = 0; i < len; i += 8) {
47− dst[i + 0] = clipf_c_one(src[i + 0], mini, maxi, maxisign);
48− dst[i + 1] = clipf_c_one(src[i + 1], mini, maxi, maxisign);
49− dst[i + 2] = clipf_c_one(src[i + 2], mini, maxi, maxisign);
50− dst[i + 3] = clipf_c_one(src[i + 3], mini, maxi, maxisign);
51− dst[i + 4] = clipf_c_one(src[i + 4], mini, maxi, maxisign);
52− dst[i + 5] = clipf_c_one(src[i + 5], mini, maxi, maxisign);
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);
45+ for (int i = 0; i < len; i += 8) {
46+ float tmp[8];
47+
48+ for (int j = 0; j < 8; j++)
49+ tmp[j]= clipf_c_one(src[i + j], mini, maxi, maxisign);
50+ for (int j = 0; j < 8; j++)
51+ dst[i + j] = tmp[j];
5552 }
5653 }
5754
5855 static void vector_clipf_c(float *dst, const float *src, int len,
5956 float min, float max)
6057 {
61− int i;
62−
6358 if (min < 0 && max > 0) {
6459 vector_clipf_c_opposite_sign(dst, src, min, max, len);
65− } else {
66− for (i = 0; i < len; i += 8) {
67− dst[i] = av_clipf(src[i], min, max);
68− dst[i + 1] = av_clipf(src[i + 1], min, max);
69− dst[i + 2] = av_clipf(src[i + 2], min, max);
70− dst[i + 3] = av_clipf(src[i + 3], min, max);
71− dst[i + 4] = av_clipf(src[i + 4], min, max);
72− dst[i + 5] = av_clipf(src[i + 5], min, max);
73− dst[i + 6] = av_clipf(src[i + 6], min, max);
74− dst[i + 7] = av_clipf(src[i + 7], min, max);
75− }
60+ return;
61+ }
62+
63+ for (int i = 0; i < len; i += 8) {
64+ float tmp[8];
65+
66+ for (int j = 0; j < 8; j++)
67+ tmp[j]= av_clipf(src[i + j], min, max);
68+ for (int j = 0; j < 8; j++)
69+ dst[i + j] = tmp[j];
7670 }
7771 }
7872

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