newlib: testsuite: fix memcpy-1's source check, and make strcmp-1 fast
Commit Message
Two fixes to the string tests.
memcpy-1 did not check what it meant to: its "src is not modified" loop
over j compared src[i] with backup_src[i], the last value of the index
of the previous loop, so one byte was checked 1024 times and the rest
never.
strcmp-1 was slow for no gain in coverage: every one of its million-odd
iterations rewrote both 1 KB buffers byte by byte, with a modulo per
byte, before a strcmp of at most MAX_BLOCK_SIZE bytes. Two billion
stores for a million strcmp calls, the function under test under one
percent of the run time. An iteration only ever writes the first
MAX_OFFSET + MAX_BLOCK_SIZE + MAX_ZEROS bytes of src and MAX_OFFSET +
MAX_BLOCK_SIZE + MAX_DIFF + MAX_LEN + 1 of dest; fill the buffers once
and refill just those windows, which leaves them byte-identical to the
full refill. Same comparisons, same results, six times faster: under
an m68k simulator from 25 s to 4 s, with its memory sanitizer from 50 s
to 8.6 s.
Signed-off-by: Bernie Innocenti <bernie@codewiz.org>
Assisted-By: Claude Opus 5 <noreply@anthropic.com>
---
newlib/testsuite/newlib.string/memcpy-1.c | 2 +-
newlib/testsuite/newlib.string/strcmp-1.c | 23 +++++++++++++++++------
2 files changed, 18 insertions(+), 7 deletions(-)
@@ -145,7 +145,7 @@ main (void)
/* Check src is not modified. */
for (j = 0; j < BUFF_SIZE; j++)
- if (src[i] != backup_src[i])
+ if (src[j] != backup_src[j])
print_error ("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u of src is modified.\n",
@@ -145,6 +145,18 @@ main (void)
char *p;
int ret;
+ /* Fill both buffers with the pattern once. An iteration writes at most
+ the first SRC_DIRTY bytes of src and DEST_DIRTY bytes of dest, so only
+ those are refilled before each strcmp: filling the whole buffers every
+ time was most of the test's run time. */
+#define SRC_DIRTY (MAX_OFFSET + MAX_BLOCK_SIZE + MAX_ZEROS)
+#define DEST_DIRTY (MAX_OFFSET + MAX_BLOCK_SIZE + MAX_DIFF + MAX_LEN + 1)
+ for (i = 0; i < BUFF_SIZE; i++)
+ {
+ src[i] = 'A' + (i % 26);
+ dest[i] = src[i];
+ }
+
/* Make calls to strcmp with block sizes ranging between 1 and
MAX_BLOCK_SIZE bytes, aligned and misaligned source and destination. */
for (sa = 0; sa <= MAX_OFFSET; sa++)
@@ -157,12 +169,11 @@ main (void)
{
if (n - m > MAX_DIFF)
continue;
- /* Make a copy of the source. */
- for (i = 0; i < BUFF_SIZE; i++)
- {
- src[i] = 'A' + (i % 26);
- dest[i] = src[i];
- }
+ /* Restore what the previous iteration changed. */
+ for (i = 0; i < SRC_DIRTY; i++)
+ src[i] = 'A' + (i % 26);
+ for (i = 0; i < DEST_DIRTY; i++)
+ dest[i] = 'A' + (i % 26);
memcpy (dest + da, src + sa, n);
/* Make src 0-terminated. */