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.
Showing the changes to libavcodec (1 of 1 file). Show all
Unified · Split
| @@ -38,41 +38,35 @@ static inline float clipf_c_one(float a, uint32_t mini, · 37 unmodified lines | |||
| 38 | static void vector_clipf_c_opposite_sign(float *dst, const float *src, | 38 | static 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 | ||
| 58 | static void vector_clipf_c(float *dst, const float *src, int len, | 55 | static 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 | ||