From: Neale Ranns Date: Wed, 3 Oct 2018 18:13:27 +0000 (-0400) Subject: clib_count_equal_*: don't read of the end of a small array and init data only if... X-Git-Tag: v18.10-rc1~31 X-Git-Url: https://gerrit.fd.io/r/gitweb?p=vpp.git;a=commitdiff_plain;h=2329e090fc4c158988d4f14b1edb6a8623149679 clib_count_equal_*: don't read of the end of a small array and init data only if used (VPP-1429) Change-Id: I8afa57ecca590698d3430746968aa0a5b0070469 Signed-off-by: Neale Ranns --- diff --git a/src/vppinfra/string.h b/src/vppinfra/string.h index 5a47725f5c3..8f165dfa18e 100644 --- a/src/vppinfra/string.h +++ b/src/vppinfra/string.h @@ -324,12 +324,17 @@ clib_memset_u8 (void *p, u8 val, uword count) static_always_inline uword clib_count_equal_u64 (u64 * data, uword max_count) { - uword count = 0; - u64 first = data[0]; + uword count; + u64 first; + if (max_count == 1) + return 1; if (data[0] != data[1]) return 1; + count = 0; + first = data[0]; + #if defined(CLIB_HAVE_VEC256) u64x4 splat = u64x4_splat (first); while (1) @@ -369,12 +374,17 @@ clib_count_equal_u64 (u64 * data, uword max_count) static_always_inline uword clib_count_equal_u32 (u32 * data, uword max_count) { - uword count = 0; - u32 first = data[0]; + uword count; + u32 first; + if (max_count == 1) + return 1; if (data[0] != data[1]) return 1; + count = 0; + first = data[0]; + #if defined(CLIB_HAVE_VEC256) u32x8 splat = u32x8_splat (first); while (1) @@ -432,12 +442,17 @@ clib_count_equal_u32 (u32 * data, uword max_count) static_always_inline uword clib_count_equal_u16 (u16 * data, uword max_count) { - uword count = 0; - u16 first = data[0]; + uword count; + u16 first; + if (max_count == 1) + return 1; if (data[0] != data[1]) return 1; + count = 0; + first = data[0]; + #if defined(CLIB_HAVE_VEC256) u16x16 splat = u16x16_splat (first); while (1) @@ -495,12 +510,17 @@ clib_count_equal_u16 (u16 * data, uword max_count) static_always_inline uword clib_count_equal_u8 (u8 * data, uword max_count) { - uword count = 0; - u8 first = data[0]; + uword count; + u8 first; + if (max_count == 1) + return 1; if (data[0] != data[1]) return 1; + count = 0; + first = data[0]; + #if defined(CLIB_HAVE_VEC256) u8x32 splat = u8x32_splat (first); while (1)