mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-23 17:53:37 +08:00
486 lines
9.4 KiB
C
486 lines
9.4 KiB
C
/* Test and measure memcmp functions.
|
|
Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
Written by Jakub Jelinek <jakub@redhat.com>, 1999.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, write to the Free
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
02111-1307 USA. */
|
|
|
|
#define TEST_MAIN
|
|
#include "test-string.h"
|
|
|
|
typedef int (*proto_t) (const char *, const char *, size_t);
|
|
int simple_memcmp (const char *, const char *, size_t);
|
|
|
|
IMPL (simple_memcmp, 0)
|
|
IMPL (memcmp, 1)
|
|
|
|
int
|
|
simple_memcmp (const char *s1, const char *s2, size_t n)
|
|
{
|
|
int ret = 0;
|
|
|
|
while (n--
|
|
&& (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
|
|
return ret;
|
|
}
|
|
|
|
static int
|
|
check_result (impl_t *impl, const char *s1, const char *s2, size_t len,
|
|
int exp_result)
|
|
{
|
|
int result = CALL (impl, s1, s2, len);
|
|
if ((exp_result == 0 && result != 0)
|
|
|| (exp_result < 0 && result >= 0)
|
|
|| (exp_result > 0 && result <= 0))
|
|
{
|
|
error (0, 0, "Wrong result in function %s %d %d", impl->name,
|
|
result, exp_result);
|
|
ret = 1;
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
do_one_test (impl_t *impl, const char *s1, const char *s2, size_t len,
|
|
int exp_result)
|
|
{
|
|
if (check_result (impl, s1, s2, len, exp_result) < 0)
|
|
return;
|
|
|
|
if (HP_TIMING_AVAIL)
|
|
{
|
|
hp_timing_t start __attribute ((unused));
|
|
hp_timing_t stop __attribute ((unused));
|
|
hp_timing_t best_time = ~ (hp_timing_t) 0;
|
|
size_t i;
|
|
|
|
for (i = 0; i < 32; ++i)
|
|
{
|
|
HP_TIMING_NOW (start);
|
|
CALL (impl, s1, s2, len);
|
|
HP_TIMING_NOW (stop);
|
|
HP_TIMING_BEST (best_time, start, stop);
|
|
}
|
|
|
|
printf ("\t%zd", (size_t) best_time);
|
|
}
|
|
}
|
|
|
|
static void
|
|
do_test (size_t align1, size_t align2, size_t len, int exp_result)
|
|
{
|
|
size_t i;
|
|
char *s1, *s2;
|
|
|
|
if (len == 0)
|
|
return;
|
|
|
|
align1 &= 7;
|
|
if (align1 + len >= page_size)
|
|
return;
|
|
|
|
align2 &= 7;
|
|
if (align2 + len >= page_size)
|
|
return;
|
|
|
|
s1 = (char *) (buf1 + align1);
|
|
s2 = (char *) (buf2 + align2);
|
|
|
|
for (i = 0; i < len; i++)
|
|
s1[i] = s2[i] = 1 + 23 * i % 255;
|
|
|
|
s1[len] = align1;
|
|
s2[len] = align2;
|
|
s2[len - 1] -= exp_result;
|
|
|
|
if (HP_TIMING_AVAIL)
|
|
printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
|
|
|
|
FOR_EACH_IMPL (impl, 0)
|
|
do_one_test (impl, s1, s2, len, exp_result);
|
|
|
|
if (HP_TIMING_AVAIL)
|
|
putchar ('\n');
|
|
}
|
|
|
|
static void
|
|
do_random_tests (void)
|
|
{
|
|
size_t i, j, n, align1, align2, pos, len;
|
|
int result;
|
|
long r;
|
|
unsigned char *p1 = buf1 + page_size - 512;
|
|
unsigned char *p2 = buf2 + page_size - 512;
|
|
|
|
for (n = 0; n < ITERATIONS; n++)
|
|
{
|
|
align1 = random () & 31;
|
|
if (random () & 1)
|
|
align2 = random () & 31;
|
|
else
|
|
align2 = align1 + (random () & 24);
|
|
pos = random () & 511;
|
|
j = align1;
|
|
if (align2 > j)
|
|
j = align2;
|
|
if (pos + j >= 512)
|
|
pos = 511 - j - (random () & 7);
|
|
len = random () & 511;
|
|
if (len + j >= 512)
|
|
len = 511 - j - (random () & 7);
|
|
j = len + align1 + 64;
|
|
if (j > 512) j = 512;
|
|
for (i = 0; i < j; ++i)
|
|
p1[i] = random () & 255;
|
|
for (i = 0; i < j; ++i)
|
|
p2[i] = random () & 255;
|
|
|
|
result = 0;
|
|
if (pos >= len)
|
|
memcpy (p2 + align2, p1 + align1, len);
|
|
else
|
|
{
|
|
memcpy (p2 + align2, p1 + align1, pos);
|
|
if (p2[align2 + pos] == p1[align1 + pos])
|
|
{
|
|
p2[align2 + pos] = random () & 255;
|
|
if (p2[align2 + pos] == p1[align1 + pos])
|
|
p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
|
|
}
|
|
|
|
if (p1[align1 + pos] < p2[align2 + pos])
|
|
result = -1;
|
|
else
|
|
result = 1;
|
|
}
|
|
|
|
FOR_EACH_IMPL (impl, 1)
|
|
{
|
|
r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2), len);
|
|
/* Test whether on 64-bit architectures where ABI requires
|
|
callee to promote has the promotion been done. */
|
|
asm ("" : "=g" (r) : "0" (r));
|
|
if ((r == 0 && result)
|
|
|| (r < 0 && result >= 0)
|
|
|| (r > 0 && result <= 0))
|
|
{
|
|
error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
|
|
n, impl->name, align1, align2, len, pos, r, result, p1, p2);
|
|
ret = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
check1 (void)
|
|
{
|
|
char s1[116], s2[116];
|
|
int n, exp_result;
|
|
|
|
s1[0] = -108;
|
|
s2[0] = -108;
|
|
s1[1] = 99;
|
|
s2[1] = 99;
|
|
s1[2] = -113;
|
|
s2[2] = -113;
|
|
s1[3] = 1;
|
|
s2[3] = 1;
|
|
s1[4] = 116;
|
|
s2[4] = 116;
|
|
s1[5] = 99;
|
|
s2[5] = 99;
|
|
s1[6] = -113;
|
|
s2[6] = -113;
|
|
s1[7] = 1;
|
|
s2[7] = 1;
|
|
s1[8] = 84;
|
|
s2[8] = 84;
|
|
s1[9] = 99;
|
|
s2[9] = 99;
|
|
s1[10] = -113;
|
|
s2[10] = -113;
|
|
s1[11] = 1;
|
|
s2[11] = 1;
|
|
s1[12] = 52;
|
|
s2[12] = 52;
|
|
s1[13] = 99;
|
|
s2[13] = 99;
|
|
s1[14] = -113;
|
|
s2[14] = -113;
|
|
s1[15] = 1;
|
|
s2[15] = 1;
|
|
s1[16] = -76;
|
|
s2[16] = -76;
|
|
s1[17] = -14;
|
|
s2[17] = -14;
|
|
s1[18] = -109;
|
|
s2[18] = -109;
|
|
s1[19] = 1;
|
|
s2[19] = 1;
|
|
s1[20] = -108;
|
|
s2[20] = -108;
|
|
s1[21] = -14;
|
|
s2[21] = -14;
|
|
s1[22] = -109;
|
|
s2[22] = -109;
|
|
s1[23] = 1;
|
|
s2[23] = 1;
|
|
s1[24] = 84;
|
|
s2[24] = 84;
|
|
s1[25] = -15;
|
|
s2[25] = -15;
|
|
s1[26] = -109;
|
|
s2[26] = -109;
|
|
s1[27] = 1;
|
|
s2[27] = 1;
|
|
s1[28] = 52;
|
|
s2[28] = 52;
|
|
s1[29] = -15;
|
|
s2[29] = -15;
|
|
s1[30] = -109;
|
|
s2[30] = -109;
|
|
s1[31] = 1;
|
|
s2[31] = 1;
|
|
s1[32] = 20;
|
|
s2[32] = 20;
|
|
s1[33] = -15;
|
|
s2[33] = -15;
|
|
s1[34] = -109;
|
|
s2[34] = -109;
|
|
s1[35] = 1;
|
|
s2[35] = 1;
|
|
s1[36] = 20;
|
|
s2[36] = 20;
|
|
s1[37] = -14;
|
|
s2[37] = -14;
|
|
s1[38] = -109;
|
|
s2[38] = -109;
|
|
s1[39] = 1;
|
|
s2[39] = 1;
|
|
s1[40] = 52;
|
|
s2[40] = 52;
|
|
s1[41] = -14;
|
|
s2[41] = -14;
|
|
s1[42] = -109;
|
|
s2[42] = -109;
|
|
s1[43] = 1;
|
|
s2[43] = 1;
|
|
s1[44] = 84;
|
|
s2[44] = 84;
|
|
s1[45] = -14;
|
|
s2[45] = -14;
|
|
s1[46] = -109;
|
|
s2[46] = -109;
|
|
s1[47] = 1;
|
|
s2[47] = 1;
|
|
s1[48] = 116;
|
|
s2[48] = 116;
|
|
s1[49] = -14;
|
|
s2[49] = -14;
|
|
s1[50] = -109;
|
|
s2[50] = -109;
|
|
s1[51] = 1;
|
|
s2[51] = 1;
|
|
s1[52] = 116;
|
|
s2[52] = 116;
|
|
s1[53] = -15;
|
|
s2[53] = -15;
|
|
s1[54] = -109;
|
|
s2[54] = -109;
|
|
s1[55] = 1;
|
|
s2[55] = 1;
|
|
s1[56] = -44;
|
|
s2[56] = -44;
|
|
s1[57] = -14;
|
|
s2[57] = -14;
|
|
s1[58] = -109;
|
|
s2[58] = -109;
|
|
s1[59] = 1;
|
|
s2[59] = 1;
|
|
s1[60] = -108;
|
|
s2[60] = -108;
|
|
s1[61] = -15;
|
|
s2[61] = -15;
|
|
s1[62] = -109;
|
|
s2[62] = -109;
|
|
s1[63] = 1;
|
|
s2[63] = 1;
|
|
s1[64] = -76;
|
|
s2[64] = -76;
|
|
s1[65] = -15;
|
|
s2[65] = -15;
|
|
s1[66] = -109;
|
|
s2[66] = -109;
|
|
s1[67] = 1;
|
|
s2[67] = 1;
|
|
s1[68] = -44;
|
|
s2[68] = -44;
|
|
s1[69] = -15;
|
|
s2[69] = -15;
|
|
s1[70] = -109;
|
|
s2[70] = -109;
|
|
s1[71] = 1;
|
|
s2[71] = 1;
|
|
s1[72] = -12;
|
|
s2[72] = -12;
|
|
s1[73] = -15;
|
|
s2[73] = -15;
|
|
s1[74] = -109;
|
|
s2[74] = -109;
|
|
s1[75] = 1;
|
|
s2[75] = 1;
|
|
s1[76] = -12;
|
|
s2[76] = -12;
|
|
s1[77] = -14;
|
|
s2[77] = -14;
|
|
s1[78] = -109;
|
|
s2[78] = -109;
|
|
s1[79] = 1;
|
|
s2[79] = 1;
|
|
s1[80] = 20;
|
|
s2[80] = -68;
|
|
s1[81] = -12;
|
|
s2[81] = 64;
|
|
s1[82] = -109;
|
|
s2[82] = -106;
|
|
s1[83] = 1;
|
|
s2[83] = 1;
|
|
s1[84] = -12;
|
|
s2[84] = -12;
|
|
s1[85] = -13;
|
|
s2[85] = -13;
|
|
s1[86] = -109;
|
|
s2[86] = -109;
|
|
s1[87] = 1;
|
|
s2[87] = 1;
|
|
s1[88] = -44;
|
|
s2[88] = -44;
|
|
s1[89] = -13;
|
|
s2[89] = -13;
|
|
s1[90] = -109;
|
|
s2[90] = -109;
|
|
s1[91] = 1;
|
|
s2[91] = 1;
|
|
s1[92] = -76;
|
|
s2[92] = -76;
|
|
s1[93] = -13;
|
|
s2[93] = -13;
|
|
s1[94] = -109;
|
|
s2[94] = -109;
|
|
s1[95] = 1;
|
|
s2[95] = 1;
|
|
s1[96] = -108;
|
|
s2[96] = -108;
|
|
s1[97] = -13;
|
|
s2[97] = -13;
|
|
s1[98] = -109;
|
|
s2[98] = -109;
|
|
s1[99] = 1;
|
|
s2[99] = 1;
|
|
s1[100] = 116;
|
|
s2[100] = 116;
|
|
s1[101] = -13;
|
|
s2[101] = -13;
|
|
s1[102] = -109;
|
|
s2[102] = -109;
|
|
s1[103] = 1;
|
|
s2[103] = 1;
|
|
s1[104] = 84;
|
|
s2[104] = 84;
|
|
s1[105] = -13;
|
|
s2[105] = -13;
|
|
s1[106] = -109;
|
|
s2[106] = -109;
|
|
s1[107] = 1;
|
|
s2[107] = 1;
|
|
s1[108] = 52;
|
|
s2[108] = 52;
|
|
s1[109] = -13;
|
|
s2[109] = -13;
|
|
s1[110] = -109;
|
|
s2[110] = -109;
|
|
s1[111] = 1;
|
|
s2[111] = 1;
|
|
s1[112] = 20;
|
|
s2[112] = 20;
|
|
s1[113] = -13;
|
|
s2[113] = -13;
|
|
s1[114] = -109;
|
|
s2[114] = -109;
|
|
s1[115] = 1;
|
|
s2[115] = 1;
|
|
|
|
n = 116;
|
|
exp_result = simple_memcmp (s1, s2, n);
|
|
FOR_EACH_IMPL (impl, 0)
|
|
check_result (impl, s1, s2, n, exp_result);
|
|
}
|
|
|
|
int
|
|
test_main (void)
|
|
{
|
|
size_t i;
|
|
|
|
test_init ();
|
|
|
|
check1 ();
|
|
|
|
printf ("%23s", "");
|
|
FOR_EACH_IMPL (impl, 0)
|
|
printf ("\t%s", impl->name);
|
|
putchar ('\n');
|
|
|
|
for (i = 1; i < 16; ++i)
|
|
{
|
|
do_test (i, i, i, 0);
|
|
do_test (i, i, i, 1);
|
|
do_test (i, i, i, -1);
|
|
}
|
|
|
|
for (i = 0; i < 16; ++i)
|
|
{
|
|
do_test (0, 0, i, 0);
|
|
do_test (0, 0, i, 1);
|
|
do_test (0, 0, i, -1);
|
|
}
|
|
|
|
for (i = 1; i < 10; ++i)
|
|
{
|
|
do_test (0, 0, 2 << i, 0);
|
|
do_test (0, 0, 2 << i, 1);
|
|
do_test (0, 0, 2 << i, -1);
|
|
do_test (0, 0, 16 << i, 0);
|
|
do_test (8 - i, 2 * i, 16 << i, 0);
|
|
do_test (0, 0, 16 << i, 1);
|
|
do_test (0, 0, 16 << i, -1);
|
|
}
|
|
|
|
for (i = 1; i < 8; ++i)
|
|
{
|
|
do_test (i, 2 * i, 8 << i, 0);
|
|
do_test (i, 2 * i, 8 << i, 1);
|
|
do_test (i, 2 * i, 8 << i, -1);
|
|
}
|
|
|
|
do_random_tests ();
|
|
return ret;
|
|
}
|
|
|
|
#include "../test-skeleton.c"
|