From 3f3735e3f39c68d33104add994bfe5d055b32e17 Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Wed, 9 Sep 2026 09:50:43 -0700 Subject: [PATCH] Make loops iterate over arrays using size_t index A follow-up to commit 24b6126: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/8373923 Use the new expressions (of the size_t type) for buffer sizes in for loops for consistency with the buffer allocation and memset calls. Change-Id: I753db9c2d0b92d3de3517faae1201dbdd325d129 Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/8383016 Reviewed-by: richard winterton Commit-Queue: Wan-Teh Chang --- unit_test/convert_argb_test.cc | 109 +++++++++++++++++---------------- unit_test/planar_test.cc | 32 +++++----- 2 files changed, 71 insertions(+), 70 deletions(-) diff --git a/unit_test/convert_argb_test.cc b/unit_test/convert_argb_test.cc index f62917a74..44b5bda18 100644 --- a/unit_test/convert_argb_test.cc +++ b/unit_test/convert_argb_test.cc @@ -516,12 +516,12 @@ TESTBPTOB(NV12, 2, 2, RGB565, RGB565, 2) (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ align_buffer_page_end(dst_argb_opt, \ (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ - for (int i = 0; i < kStrideA * kHeightA * (int)sizeof(TYPE_A); ++i) { \ + for (size_t i = 0; i < (size_t)kStrideA * kHeightA * sizeof(TYPE_A); \ + ++i) { \ src_argb[i + OFF] = (fastrand() & 0xff); \ } \ memset(dst_argb_c, 1, (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ - memset(dst_argb_opt, 101, \ - (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ + memset(dst_argb_opt, 101, (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ MaskCpuFlags(disable_cpu_flags_); \ FMT_A##To##FMT_B((TYPE_A*)(src_argb + OFF), kStrideA, (TYPE_B*)dst_argb_c, \ kStrideB, kWidth, NEG kHeight); \ @@ -530,7 +530,8 @@ TESTBPTOB(NV12, 2, 2, RGB565, RGB565, 2) FMT_A##To##FMT_B((TYPE_A*)(src_argb + OFF), kStrideA, \ (TYPE_B*)dst_argb_opt, kStrideB, kWidth, NEG kHeight); \ } \ - for (int i = 0; i < kStrideB * kHeightB * (int)sizeof(TYPE_B); ++i) { \ + for (size_t i = 0; i < (size_t)kStrideB * kHeightB * sizeof(TYPE_B); \ + ++i) { \ ASSERT_EQ(dst_argb_c[i], dst_argb_opt[i]); \ } \ free_aligned_buffer_page_end(src_argb); \ @@ -538,44 +539,44 @@ TESTBPTOB(NV12, 2, 2, RGB565, RGB565, 2) free_aligned_buffer_page_end(dst_argb_opt); \ } -#define TESTATOBRANDOM(FMT_A, TYPE_A, EPP_A, STRIDE_A, HEIGHT_A, FMT_B, \ - TYPE_B, EPP_B, STRIDE_B, HEIGHT_B) \ - TEST_F(LibYUVConvertTest, FMT_A##To##FMT_B##_Random) { \ - for (int times = 0; times < benchmark_iterations_; ++times) { \ - const int kWidth = (fastrand() & 63) + 1; \ - const int kHeight = (fastrand() & 31) + 1; \ - const int kHeightA = (kHeight + HEIGHT_A - 1) / HEIGHT_A * HEIGHT_A; \ - const int kHeightB = (kHeight + HEIGHT_B - 1) / HEIGHT_B * HEIGHT_B; \ - const int kStrideA = \ - (kWidth * EPP_A + STRIDE_A - 1) / STRIDE_A * STRIDE_A; \ - const int kStrideB = \ - (kWidth * EPP_B + STRIDE_B - 1) / STRIDE_B * STRIDE_B; \ - align_buffer_page_end(src_argb, \ - (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ - align_buffer_page_end(dst_argb_c, \ - (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ - align_buffer_page_end(dst_argb_opt, \ - (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ - for (int i = 0; i < kStrideA * kHeightA * (int)sizeof(TYPE_A); ++i) { \ - src_argb[i] = 0xfe; \ - } \ - memset(dst_argb_c, 123, \ - (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ - memset(dst_argb_opt, 123, \ - (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ - MaskCpuFlags(disable_cpu_flags_); \ - FMT_A##To##FMT_B((TYPE_A*)src_argb, kStrideA, (TYPE_B*)dst_argb_c, \ - kStrideB, kWidth, kHeight); \ - MaskCpuFlags(benchmark_cpu_info_); \ - FMT_A##To##FMT_B((TYPE_A*)src_argb, kStrideA, (TYPE_B*)dst_argb_opt, \ - kStrideB, kWidth, kHeight); \ - for (int i = 0; i < kStrideB * kHeightB * (int)sizeof(TYPE_B); ++i) { \ - ASSERT_EQ(dst_argb_c[i], dst_argb_opt[i]); \ - } \ - free_aligned_buffer_page_end(src_argb); \ - free_aligned_buffer_page_end(dst_argb_c); \ - free_aligned_buffer_page_end(dst_argb_opt); \ - } \ +#define TESTATOBRANDOM(FMT_A, TYPE_A, EPP_A, STRIDE_A, HEIGHT_A, FMT_B, \ + TYPE_B, EPP_B, STRIDE_B, HEIGHT_B) \ + TEST_F(LibYUVConvertTest, FMT_A##To##FMT_B##_Random) { \ + for (int times = 0; times < benchmark_iterations_; ++times) { \ + const int kWidth = (fastrand() & 63) + 1; \ + const int kHeight = (fastrand() & 31) + 1; \ + const int kHeightA = (kHeight + HEIGHT_A - 1) / HEIGHT_A * HEIGHT_A; \ + const int kHeightB = (kHeight + HEIGHT_B - 1) / HEIGHT_B * HEIGHT_B; \ + const int kStrideA = \ + (kWidth * EPP_A + STRIDE_A - 1) / STRIDE_A * STRIDE_A; \ + const int kStrideB = \ + (kWidth * EPP_B + STRIDE_B - 1) / STRIDE_B * STRIDE_B; \ + align_buffer_page_end(src_argb, \ + (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ + align_buffer_page_end(dst_argb_c, \ + (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ + align_buffer_page_end(dst_argb_opt, \ + (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ + for (size_t i = 0; i < (size_t)kStrideA * kHeightA * sizeof(TYPE_A); \ + ++i) { \ + src_argb[i] = 0xfe; \ + } \ + memset(dst_argb_c, 123, (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ + memset(dst_argb_opt, 123, (size_t)kStrideB * kHeightB * sizeof(TYPE_B)); \ + MaskCpuFlags(disable_cpu_flags_); \ + FMT_A##To##FMT_B((TYPE_A*)src_argb, kStrideA, (TYPE_B*)dst_argb_c, \ + kStrideB, kWidth, kHeight); \ + MaskCpuFlags(benchmark_cpu_info_); \ + FMT_A##To##FMT_B((TYPE_A*)src_argb, kStrideA, (TYPE_B*)dst_argb_opt, \ + kStrideB, kWidth, kHeight); \ + for (size_t i = 0; i < (size_t)kStrideB * kHeightB * sizeof(TYPE_B); \ + ++i) { \ + ASSERT_EQ(dst_argb_c[i], dst_argb_opt[i]); \ + } \ + free_aligned_buffer_page_end(src_argb); \ + free_aligned_buffer_page_end(dst_argb_c); \ + free_aligned_buffer_page_end(dst_argb_opt); \ + } \ } #if defined(ENABLE_FULL_TESTS) @@ -802,9 +803,9 @@ TESTATOA(AB64, uint16_t, 4, 4, 1, AR64, uint16_t, 4, 4, 1) const int kStrideB = \ (kWidth * BPP_B + STRIDE_B - 1) / STRIDE_B * STRIDE_B; \ align_buffer_page_end(src_argb, (size_t)kStrideA * kHeightA + OFF); \ - align_buffer_page_end(dst_argb_c, (size_t)kStrideB * kHeightB); \ - align_buffer_page_end(dst_argb_opt, (size_t)kStrideB * kHeightB); \ - for (int i = 0; i < kStrideA * kHeightA; ++i) { \ + align_buffer_page_end(dst_argb_c, (size_t)kStrideB * kHeightB); \ + align_buffer_page_end(dst_argb_opt, (size_t)kStrideB * kHeightB); \ + for (size_t i = 0; i < (size_t)kStrideA * kHeightA; ++i) { \ src_argb[i + OFF] = (fastrand() & 0xff); \ } \ memset(dst_argb_c, 1, (size_t)kStrideB * kHeightB); \ @@ -817,7 +818,7 @@ TESTATOA(AB64, uint16_t, 4, 4, 1, AR64, uint16_t, 4, 4, 1) FMT_A##To##FMT_B##Dither(src_argb + OFF, kStrideA, dst_argb_opt, \ kStrideB, NULL, kWidth, NEG kHeight); \ } \ - for (int i = 0; i < kStrideB * kHeightB; ++i) { \ + for (size_t i = 0; i < (size_t)kStrideB * kHeightB; ++i) { \ ASSERT_EQ(dst_argb_c[i], dst_argb_opt[i]); \ } \ free_aligned_buffer_page_end(src_argb); \ @@ -840,7 +841,7 @@ TESTATOA(AB64, uint16_t, 4, 4, 1, AR64, uint16_t, 4, 4, 1) align_buffer_page_end(src_argb, (size_t)kStrideA * kHeightA); \ align_buffer_page_end(dst_argb_c, (size_t)kStrideB * kHeightB); \ align_buffer_page_end(dst_argb_opt, (size_t)kStrideB * kHeightB); \ - for (int i = 0; i < kStrideA * kHeightA; ++i) { \ + for (size_t i = 0; i < (size_t)kStrideA * kHeightA; ++i) { \ src_argb[i] = (fastrand() & 0xff); \ } \ memset(dst_argb_c, 123, (size_t)kStrideB * kHeightB); \ @@ -851,7 +852,7 @@ TESTATOA(AB64, uint16_t, 4, 4, 1, AR64, uint16_t, 4, 4, 1) MaskCpuFlags(benchmark_cpu_info_); \ FMT_A##To##FMT_B##Dither(src_argb, kStrideA, dst_argb_opt, kStrideB, \ NULL, kWidth, kHeight); \ - for (int i = 0; i < kStrideB * kHeightB; ++i) { \ + for (size_t i = 0; i < (size_t)kStrideB * kHeightB; ++i) { \ ASSERT_EQ(dst_argb_c[i], dst_argb_opt[i]); \ } \ free_aligned_buffer_page_end(src_argb); \ @@ -900,13 +901,12 @@ TESTATOBD(ARGB, 4, 4, 1, RGB565, 2, 2, 1) (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ align_buffer_page_end(dst_argb_opt, \ (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ - for (int i = 0; i < kStrideA * kHeightA * (int)sizeof(TYPE_A); ++i) { \ + for (size_t i = 0; i < (size_t)kStrideA * kHeightA * sizeof(TYPE_A); \ + ++i) { \ src_argb[i + OFF] = (fastrand() & 0xff); \ } \ - memset(dst_argb_c, 1, \ - (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ - memset(dst_argb_opt, 101, \ - (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ + memset(dst_argb_c, 1, (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ + memset(dst_argb_opt, 101, (size_t)kStrideA * kHeightA * sizeof(TYPE_A)); \ MaskCpuFlags(disable_cpu_flags_); \ FMT_ATOB((TYPE_A*)(src_argb + OFF), kStrideA, (TYPE_A*)dst_argb_c, \ kStrideA, kWidth, NEG kHeight); \ @@ -921,7 +921,8 @@ TESTATOBD(ARGB, 4, 4, 1, RGB565, 2, 2, 1) MaskCpuFlags(benchmark_cpu_info_); \ FMT_ATOB((TYPE_A*)dst_argb_opt, kStrideA, (TYPE_A*)dst_argb_opt, kStrideA, \ kWidth, NEG kHeight); \ - for (int i = 0; i < kStrideA * kHeightA * (int)sizeof(TYPE_A); ++i) { \ + for (size_t i = 0; i < (size_t)kStrideA * kHeightA * sizeof(TYPE_A); \ + ++i) { \ ASSERT_EQ(src_argb[i + OFF], dst_argb_opt[i]); \ ASSERT_EQ(dst_argb_c[i], dst_argb_opt[i]); \ } \ diff --git a/unit_test/planar_test.cc b/unit_test/planar_test.cc index 3eccb8e19..92463fc42 100644 --- a/unit_test/planar_test.cc +++ b/unit_test/planar_test.cc @@ -174,7 +174,7 @@ static int TestAttenuateI(int width, align_buffer_page_end(src_argb, (size_t)kStride * height + off); align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb[i + off] = (fastrand() & 0xff); } memset(dst_argb_c, 0, (size_t)kStride * height); @@ -189,7 +189,7 @@ static int TestAttenuateI(int width, invert * height); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) { @@ -246,7 +246,7 @@ static int TestUnattenuateI(int width, align_buffer_page_end(src_argb, (size_t)kStride * height + off); align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb[i + off] = (fastrand() & 0xff); } ARGBAttenuate(src_argb + off, kStride, src_argb + off, kStride, width, @@ -263,7 +263,7 @@ static int TestUnattenuateI(int width, invert * height); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) { @@ -1852,7 +1852,7 @@ static int TestMultiply(int width, align_buffer_page_end(src_argb_b, (size_t)kStride * height + off); align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb_a[i + off] = (fastrand() & 0xff); src_argb_b[i + off] = (fastrand() & 0xff); } @@ -1868,7 +1868,7 @@ static int TestMultiply(int width, dst_argb_opt, kStride, width, invert * height); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) { @@ -1926,7 +1926,7 @@ static int TestAdd(int width, align_buffer_page_end(src_argb_b, (size_t)kStride * height + off); align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb_a[i + off] = (fastrand() & 0xff); src_argb_b[i + off] = (fastrand() & 0xff); } @@ -1942,7 +1942,7 @@ static int TestAdd(int width, kStride, width, invert * height); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) { @@ -2000,7 +2000,7 @@ static int TestSubtract(int width, align_buffer_page_end(src_argb_b, (size_t)kStride * height + off); align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb_a[i + off] = (fastrand() & 0xff); src_argb_b[i + off] = (fastrand() & 0xff); } @@ -2016,7 +2016,7 @@ static int TestSubtract(int width, dst_argb_opt, kStride, width, invert * height); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) { @@ -2074,7 +2074,7 @@ static int TestSobel(int width, align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); memset(src_argb_a, 0, (size_t)kStride * height + off); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb_a[i + off] = (fastrand() & 0xff); } memset(dst_argb_c, 0, (size_t)kStride * height); @@ -2089,7 +2089,7 @@ static int TestSobel(int width, invert * height); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) { @@ -2220,7 +2220,7 @@ static int TestSobelXY(int width, align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); memset(src_argb_a, 0, (size_t)kStride * height + off); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb_a[i + off] = (fastrand() & 0xff); } memset(dst_argb_c, 0, (size_t)kStride * height); @@ -2235,7 +2235,7 @@ static int TestSobelXY(int width, invert * height); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) { @@ -2293,7 +2293,7 @@ static int TestBlur(int width, align_buffer_page_end(dst_cumsum, (size_t)width * height * 16); align_buffer_page_end(dst_argb_c, (size_t)kStride * height); align_buffer_page_end(dst_argb_opt, (size_t)kStride * height); - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { src_argb_a[i + off] = (fastrand() & 0xff); } memset(dst_cumsum, 0, (size_t)width * height * 16); @@ -2311,7 +2311,7 @@ static int TestBlur(int width, invert * height, radius); } int max_diff = 0; - for (int i = 0; i < kStride * height; ++i) { + for (size_t i = 0; i < (size_t)kStride * height; ++i) { int abs_diff = abs(static_cast(dst_argb_c[i]) - static_cast(dst_argb_opt[i])); if (abs_diff > max_diff) {