Rewrite some loops to avoid -Wunsafe-loop-optimizations warnings.

For example, loops of the form:
"for (i = 0; i < 1 + max_value; ++i) ..." or
"for (i = 0; i <= max_value; ++i) ..." are possibly infinite loops,
theoretically speaking (even if practically, they aren't).
So, compiler cannot optimize those loops.

When possible, I rewrote such loops to be finite even theoretically.

Cherry-picked from aomedia/master: 4e69284

Change-Id: Ied47a24833b689c0ec011f8645cf1c01856f7c59
This commit is contained in:
Urvang Joshi 2016-07-15 15:58:25 -07:00
Родитель 77853e56ea
Коммит b42827f650
2 изменённых файлов: 114 добавлений и 141 удалений

Просмотреть файл

@ -68,24 +68,25 @@ static void scale1d_2t1_i(const unsigned char *source, int source_step,
unsigned int source_scale, unsigned int source_length, unsigned int source_scale, unsigned int source_length,
unsigned char *dest, int dest_step, unsigned char *dest, int dest_step,
unsigned int dest_scale, unsigned int dest_length) { unsigned int dest_scale, unsigned int dest_length) {
unsigned int i, j; const unsigned int source_pitch = source_step;
unsigned int temp; const unsigned char *const dest_end = dest + dest_length * dest_step;
int source_pitch = source_step;
(void)source_length; (void)source_length;
(void)source_scale; (void)source_scale;
(void)dest_scale; (void)dest_scale;
source_step *= 2; source_step *= 2; // Every other row.
dest[0] = source[0];
for (i = dest_step, j = source_step; i < dest_length * dest_step; dest[0] = source[0]; // Special case: 1st pixel.
i += dest_step, j += source_step) { source += source_step;
temp = 8; dest += dest_step;
temp += 3 * source[j - source_pitch];
temp += 10 * source[j]; while (dest < dest_end) {
temp += 3 * source[j + source_pitch]; const unsigned int a = 3 * source[-source_pitch];
temp >>= 4; const unsigned int b = 10 * source[0];
dest[i] = (char)(temp); const unsigned int c = 3 * source[source_pitch];
*dest = (unsigned char)((8 + a + b + c) >> 4);
source += source_step;
dest += dest_step;
} }
} }
@ -119,17 +120,18 @@ static void scale1d_2t1_ps(const unsigned char *source, int source_step,
unsigned int source_length, unsigned char *dest, unsigned int source_length, unsigned char *dest,
int dest_step, unsigned int dest_scale, int dest_step, unsigned int dest_scale,
unsigned int dest_length) { unsigned int dest_length) {
unsigned int i, j; const unsigned char *const dest_end = dest + dest_length * dest_step;
(void)source_length; (void)source_length;
(void)source_scale; (void)source_scale;
(void)dest_scale; (void)dest_scale;
source_step *= 2; source_step *= 2; // Every other row.
j = 0;
for (i = 0; i < dest_length * dest_step; i += dest_step, j += source_step) while (dest < dest_end) {
dest[i] = source[j]; *dest = *source;
source += source_step;
dest += dest_step;
}
} }
/**************************************************************************** /****************************************************************************
* *
@ -159,12 +161,12 @@ static void scale1d_c(const unsigned char *source, int source_step,
unsigned int source_scale, unsigned int source_length, unsigned int source_scale, unsigned int source_length,
unsigned char *dest, int dest_step, unsigned char *dest, int dest_step,
unsigned int dest_scale, unsigned int dest_length) { unsigned int dest_scale, unsigned int dest_length) {
unsigned int i; const unsigned char *const dest_end = dest + dest_length * dest_step;
unsigned int round_value = dest_scale / 2; const unsigned int round_value = dest_scale / 2;
unsigned int left_modifier = dest_scale; unsigned int left_modifier = dest_scale;
unsigned int right_modifier = 0; unsigned int right_modifier = 0;
unsigned char left_pixel = *source; unsigned char left_pixel = source[0];
unsigned char right_pixel = *(source + source_step); unsigned char right_pixel = source[source_step];
(void)source_length; (void)source_length;
@ -173,18 +175,18 @@ static void scale1d_c(const unsigned char *source, int source_step,
/* assert ( (source_length - 1) * dest_scale >= (dest_length - 1) * /* assert ( (source_length - 1) * dest_scale >= (dest_length - 1) *
* source_scale);*/ * source_scale);*/
for (i = 0; i < dest_length * dest_step; i += dest_step) { while (dest < dest_end) {
dest[i] = (char)((left_modifier * left_pixel + *dest = (unsigned char)((left_modifier * left_pixel +
right_modifier * right_pixel + round_value) / right_modifier * right_pixel + round_value) /
dest_scale); dest_scale);
right_modifier += source_scale; right_modifier += source_scale;
while (right_modifier > dest_scale) { while (right_modifier > dest_scale) {
right_modifier -= dest_scale; right_modifier -= dest_scale;
source += source_step; source += source_step;
left_pixel = *source; left_pixel = source[0];
right_pixel = *(source + source_step); right_pixel = source[source_step];
} }
left_modifier = dest_scale - right_modifier; left_modifier = dest_scale - right_modifier;
@ -236,11 +238,10 @@ static void Scale2D(
unsigned int dest_width, unsigned int dest_height, unsigned char *temp_area, unsigned int dest_width, unsigned int dest_height, unsigned char *temp_area,
unsigned char temp_area_height, unsigned int hscale, unsigned int hratio, unsigned char temp_area_height, unsigned int hscale, unsigned int hratio,
unsigned int vscale, unsigned int vratio, unsigned int interlaced) { unsigned int vscale, unsigned int vratio, unsigned int interlaced) {
/*unsigned*/ unsigned int i, j, k;
int i, j, k; unsigned int bands;
int bands; unsigned int dest_band_height;
int dest_band_height; unsigned int source_band_height;
int source_band_height;
typedef void (*Scale1D)(const unsigned char *source, int source_step, typedef void (*Scale1D)(const unsigned char *source, int source_step,
unsigned int source_scale, unsigned int source_length, unsigned int source_scale, unsigned int source_length,
@ -331,7 +332,7 @@ static void Scale2D(
if (ratio_scalable) { if (ratio_scalable) {
if (source_height == dest_height) { if (source_height == dest_height) {
/* for each band of the image */ /* for each band of the image */
for (k = 0; k < (int)dest_height; k++) { for (k = 0; k < dest_height; ++k) {
horiz_line_scale(source, source_width, dest, dest_width); horiz_line_scale(source, source_width, dest, dest_width);
source += source_pitch; source += source_pitch;
dest += dest_pitch; dest += dest_pitch;
@ -346,14 +347,13 @@ static void Scale2D(
horiz_line_scale(source, source_width, temp_area, dest_width); horiz_line_scale(source, source_width, temp_area, dest_width);
} }
for (k = 0; for (k = 0; k < (dest_height + dest_band_height - 1) / dest_band_height;
k < (int)(dest_height + dest_band_height - 1) / dest_band_height; ++k) {
k++) {
/* scale one band horizontally */ /* scale one band horizontally */
for (i = 0; i < source_band_height; i++) { for (i = 0; i < source_band_height; ++i) {
/* Trap case where we could read off the base of the source buffer */ /* Trap case where we could read off the base of the source buffer */
line_src = (unsigned char *)source + i * source_pitch; line_src = source + i * source_pitch;
if (line_src < source_base) line_src = source_base; if (line_src < source_base) line_src = source_base;
@ -388,7 +388,7 @@ static void Scale2D(
if (source_height == dest_height) { if (source_height == dest_height) {
/* for each band of the image */ /* for each band of the image */
for (k = 0; k < (int)dest_height; k++) { for (k = 0; k < dest_height; ++k) {
Scale1Dh(source, 1, hscale, source_width + 1, dest, 1, hratio, Scale1Dh(source, 1, hscale, source_width + 1, dest, 1, hratio,
dest_width); dest_width);
source += source_pitch; source += source_pitch;
@ -414,10 +414,10 @@ static void Scale2D(
/* for each band of the image */ /* for each band of the image */
bands = (dest_height + dest_band_height - 1) / dest_band_height; bands = (dest_height + dest_band_height - 1) / dest_band_height;
for (k = 0; k < bands; k++) { for (k = 0; k < bands; ++k) {
/* scale one band horizontally */ /* scale one band horizontally */
for (i = 1; i < source_band_height + 1; i++) { for (i = 1; i < source_band_height + 1; ++i) {
if (k * source_band_height + i < (int)source_height) { if (k * source_band_height + i < source_height) {
Scale1Dh(source + i * source_pitch, 1, hscale, source_width + 1, Scale1Dh(source + i * source_pitch, 1, hscale, source_width + 1,
temp_area + i * dest_pitch, 1, hratio, dest_width); temp_area + i * dest_pitch, 1, hratio, dest_width);
} else { /* Duplicate the last row */ } else { /* Duplicate the last row */
@ -428,7 +428,7 @@ static void Scale2D(
} }
/* scale one band vertically */ /* scale one band vertically */
for (j = 0; j < (int)dest_width; j++) { for (j = 0; j < dest_width; ++j) {
Scale1Dv(&temp_area[j], dest_pitch, vscale, source_band_height + 1, Scale1Dv(&temp_area[j], dest_pitch, vscale, source_band_height + 1,
&dest[j], dest_pitch, vratio, dest_band_height); &dest[j], dest_pitch, vratio, dest_band_height);
} }
@ -487,12 +487,12 @@ void aom_scale_frame(YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
temp_area, temp_height, hscale, hratio, vscale, vratio, interlaced); temp_area, temp_height, hscale, hratio, vscale, vratio, interlaced);
if (dw < (int)dst->y_width) if (dw < (int)dst->y_width)
for (i = 0; i < dh; i++) for (i = 0; i < dh; ++i)
memset(dst->y_buffer + i * dst->y_stride + dw - 1, memset(dst->y_buffer + i * dst->y_stride + dw - 1,
dst->y_buffer[i * dst->y_stride + dw - 2], dst->y_width - dw + 1); dst->y_buffer[i * dst->y_stride + dw - 2], dst->y_width - dw + 1);
if (dh < (int)dst->y_height) if (dh < (int)dst->y_height)
for (i = dh - 1; i < (int)dst->y_height; i++) for (i = dh - 1; i < (int)dst->y_height; ++i)
memcpy(dst->y_buffer + i * dst->y_stride, memcpy(dst->y_buffer + i * dst->y_stride,
dst->y_buffer + (dh - 2) * dst->y_stride, dst->y_width + 1); dst->y_buffer + (dh - 2) * dst->y_stride, dst->y_width + 1);
@ -502,13 +502,13 @@ void aom_scale_frame(YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
vratio, interlaced); vratio, interlaced);
if (dw / 2 < (int)dst->uv_width) if (dw / 2 < (int)dst->uv_width)
for (i = 0; i < dst->uv_height; i++) for (i = 0; i < dst->uv_height; ++i)
memset(dst->u_buffer + i * dst->uv_stride + dw / 2 - 1, memset(dst->u_buffer + i * dst->uv_stride + dw / 2 - 1,
dst->u_buffer[i * dst->uv_stride + dw / 2 - 2], dst->u_buffer[i * dst->uv_stride + dw / 2 - 2],
dst->uv_width - dw / 2 + 1); dst->uv_width - dw / 2 + 1);
if (dh / 2 < (int)dst->uv_height) if (dh / 2 < (int)dst->uv_height)
for (i = dh / 2 - 1; i < (int)dst->y_height / 2; i++) for (i = dh / 2 - 1; i < (int)dst->y_height / 2; ++i)
memcpy(dst->u_buffer + i * dst->uv_stride, memcpy(dst->u_buffer + i * dst->uv_stride,
dst->u_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width); dst->u_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width);
@ -518,13 +518,13 @@ void aom_scale_frame(YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
vratio, interlaced); vratio, interlaced);
if (dw / 2 < (int)dst->uv_width) if (dw / 2 < (int)dst->uv_width)
for (i = 0; i < dst->uv_height; i++) for (i = 0; i < dst->uv_height; ++i)
memset(dst->v_buffer + i * dst->uv_stride + dw / 2 - 1, memset(dst->v_buffer + i * dst->uv_stride + dw / 2 - 1,
dst->v_buffer[i * dst->uv_stride + dw / 2 - 2], dst->v_buffer[i * dst->uv_stride + dw / 2 - 2],
dst->uv_width - dw / 2 + 1); dst->uv_width - dw / 2 + 1);
if (dh / 2 < (int)dst->uv_height) if (dh / 2 < (int)dst->uv_height)
for (i = dh / 2 - 1; i < (int)dst->y_height / 2; i++) for (i = dh / 2 - 1; i < (int)dst->y_height / 2; ++i)
memcpy(dst->v_buffer + i * dst->uv_stride, memcpy(dst->v_buffer + i * dst->uv_stride,
dst->v_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width); dst->v_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width);
} }

Просмотреть файл

@ -39,27 +39,23 @@ void aom_horizontal_line_5_4_scale_c(const unsigned char *source,
unsigned int source_width, unsigned int source_width,
unsigned char *dest, unsigned char *dest,
unsigned int dest_width) { unsigned int dest_width) {
unsigned i; const unsigned char *const source_end = source + source_width;
unsigned int a, b, c, d, e;
unsigned char *des = dest;
const unsigned char *src = source;
(void)dest_width; (void)dest_width;
for (i = 0; i < source_width; i += 5) { while (source < source_end) {
a = src[0]; const unsigned int a = source[0];
b = src[1]; const unsigned int b = source[1];
c = src[2]; const unsigned int c = source[2];
d = src[3]; const unsigned int d = source[3];
e = src[4]; const unsigned int e = source[4];
des[0] = (unsigned char)a; dest[0] = (unsigned char)a;
des[1] = (unsigned char)((b * 192 + c * 64 + 128) >> 8); dest[1] = (unsigned char)((b * 192 + c * 64 + 128) >> 8);
des[2] = (unsigned char)((c * 128 + d * 128 + 128) >> 8); dest[2] = (unsigned char)((c * 128 + d * 128 + 128) >> 8);
des[3] = (unsigned char)((d * 64 + e * 192 + 128) >> 8); dest[3] = (unsigned char)((d * 64 + e * 192 + 128) >> 8);
src += 5; source += 5;
des += 4; dest += 4;
} }
} }
@ -67,25 +63,21 @@ void aom_vertical_band_5_4_scale_c(unsigned char *source,
unsigned int src_pitch, unsigned char *dest, unsigned int src_pitch, unsigned char *dest,
unsigned int dest_pitch, unsigned int dest_pitch,
unsigned int dest_width) { unsigned int dest_width) {
unsigned int i; const unsigned char *const dest_end = dest + dest_width;
unsigned int a, b, c, d, e; while (dest < dest_end) {
unsigned char *des = dest; const unsigned int a = source[0 * src_pitch];
unsigned char *src = source; const unsigned int b = source[1 * src_pitch];
const unsigned int c = source[2 * src_pitch];
const unsigned int d = source[3 * src_pitch];
const unsigned int e = source[4 * src_pitch];
for (i = 0; i < dest_width; i++) { dest[0 * dest_pitch] = (unsigned char)a;
a = src[0 * src_pitch]; dest[1 * dest_pitch] = (unsigned char)((b * 192 + c * 64 + 128) >> 8);
b = src[1 * src_pitch]; dest[2 * dest_pitch] = (unsigned char)((c * 128 + d * 128 + 128) >> 8);
c = src[2 * src_pitch]; dest[3 * dest_pitch] = (unsigned char)((d * 64 + e * 192 + 128) >> 8);
d = src[3 * src_pitch];
e = src[4 * src_pitch];
des[0 * dest_pitch] = (unsigned char)a; ++source;
des[1 * dest_pitch] = (unsigned char)((b * 192 + c * 64 + 128) >> 8); ++dest;
des[2 * dest_pitch] = (unsigned char)((c * 128 + d * 128 + 128) >> 8);
des[3 * dest_pitch] = (unsigned char)((d * 64 + e * 192 + 128) >> 8);
src++;
des++;
} }
} }
@ -114,26 +106,21 @@ void aom_horizontal_line_5_3_scale_c(const unsigned char *source,
unsigned int source_width, unsigned int source_width,
unsigned char *dest, unsigned char *dest,
unsigned int dest_width) { unsigned int dest_width) {
unsigned int i; const unsigned char *const source_end = source + source_width;
unsigned int a, b, c, d, e;
unsigned char *des = dest;
const unsigned char *src = source;
(void)dest_width; (void)dest_width;
while (source < source_end) {
const unsigned int a = source[0];
const unsigned int b = source[1];
const unsigned int c = source[2];
const unsigned int d = source[3];
const unsigned int e = source[4];
for (i = 0; i < source_width; i += 5) { dest[0] = (unsigned char)a;
a = src[0]; dest[1] = (unsigned char)((b * 85 + c * 171 + 128) >> 8);
b = src[1]; dest[2] = (unsigned char)((d * 171 + e * 85 + 128) >> 8);
c = src[2];
d = src[3];
e = src[4];
des[0] = (unsigned char)a; source += 5;
des[1] = (unsigned char)((b * 85 + c * 171 + 128) >> 8); dest += 3;
des[2] = (unsigned char)((d * 171 + e * 85 + 128) >> 8);
src += 5;
des += 3;
} }
} }
@ -141,24 +128,20 @@ void aom_vertical_band_5_3_scale_c(unsigned char *source,
unsigned int src_pitch, unsigned char *dest, unsigned int src_pitch, unsigned char *dest,
unsigned int dest_pitch, unsigned int dest_pitch,
unsigned int dest_width) { unsigned int dest_width) {
unsigned int i; const unsigned char *const dest_end = dest + dest_width;
unsigned int a, b, c, d, e; while (dest < dest_end) {
unsigned char *des = dest; const unsigned int a = source[0 * src_pitch];
unsigned char *src = source; const unsigned int b = source[1 * src_pitch];
const unsigned int c = source[2 * src_pitch];
const unsigned int d = source[3 * src_pitch];
const unsigned int e = source[4 * src_pitch];
for (i = 0; i < dest_width; i++) { dest[0 * dest_pitch] = (unsigned char)a;
a = src[0 * src_pitch]; dest[1 * dest_pitch] = (unsigned char)((b * 85 + c * 171 + 128) >> 8);
b = src[1 * src_pitch]; dest[2 * dest_pitch] = (unsigned char)((d * 171 + e * 85 + 128) >> 8);
c = src[2 * src_pitch];
d = src[3 * src_pitch];
e = src[4 * src_pitch];
des[0 * dest_pitch] = (unsigned char)a; ++source;
des[1 * dest_pitch] = (unsigned char)((b * 85 + c * 171 + 128) >> 8); ++dest;
des[2 * dest_pitch] = (unsigned char)((d * 171 + e * 85 + 128) >> 8);
src++;
des++;
} }
} }
@ -186,18 +169,12 @@ void aom_horizontal_line_2_1_scale_c(const unsigned char *source,
unsigned int source_width, unsigned int source_width,
unsigned char *dest, unsigned char *dest,
unsigned int dest_width) { unsigned int dest_width) {
unsigned int i; const unsigned char *const source_end = source + source_width;
unsigned int a;
unsigned char *des = dest;
const unsigned char *src = source;
(void)dest_width; (void)dest_width;
while (source < source_end) {
for (i = 0; i < source_width; i += 2) { dest[0] = source[0];
a = src[0]; source += 2;
des[0] = (unsigned char)(a); ++dest;
src += 2;
des += 1;
} }
} }
@ -215,18 +192,14 @@ void aom_vertical_band_2_1_scale_i_c(unsigned char *source,
unsigned char *dest, unsigned char *dest,
unsigned int dest_pitch, unsigned int dest_pitch,
unsigned int dest_width) { unsigned int dest_width) {
int i; const unsigned char *const dest_end = dest + dest_width;
int temp;
int width = dest_width;
(void)dest_pitch; (void)dest_pitch;
while (dest < dest_end) {
for (i = 0; i < width; i++) { const unsigned int a = source[-src_pitch] * 3;
temp = 8; const unsigned int b = source[0] * 10;
temp += source[i - (int)src_pitch] * 3; const unsigned int c = source[src_pitch] * 3;
temp += source[i] * 10; dest[0] = (unsigned char)((8 + a + b + c) >> 4);
temp += source[i + src_pitch] * 3; ++source;
temp >>= 4; ++dest;
dest[i] = (unsigned char)(temp);
} }
} }