1 /* Test and measure memcmp functions.
2 Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include "test-string.h"
24 typedef int (*proto_t) (const char *, const char *, size_t);
25 int simple_memcmp (const char *, const char *, size_t);
27 IMPL (simple_memcmp, 0)
31 simple_memcmp (const char *s1, const char *s2, size_t n)
36 && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
41 check_result (impl_t *impl, const char *s1, const char *s2, size_t len,
44 int result = CALL (impl, s1, s2, len);
45 if ((exp_result == 0 && result != 0)
46 || (exp_result < 0 && result >= 0)
47 || (exp_result > 0 && result <= 0))
49 error (0, 0, "Wrong result in function %s %d %d", impl->name,
59 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t len,
62 if (check_result (impl, s1, s2, len, exp_result) < 0)
67 hp_timing_t start __attribute ((unused));
68 hp_timing_t stop __attribute ((unused));
69 hp_timing_t best_time = ~ (hp_timing_t) 0;
72 for (i = 0; i < 32; ++i)
74 HP_TIMING_NOW (start);
75 CALL (impl, s1, s2, len);
77 HP_TIMING_BEST (best_time, start, stop);
80 printf ("\t%zd", (size_t) best_time);
85 do_test (size_t align1, size_t align2, size_t len, int exp_result)
94 if (align1 + len >= page_size)
98 if (align2 + len >= page_size)
101 s1 = (char *) (buf1 + align1);
102 s2 = (char *) (buf2 + align2);
104 for (i = 0; i < len; i++)
105 s1[i] = s2[i] = 1 + 23 * i % 255;
109 s2[len - 1] -= exp_result;
112 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
114 FOR_EACH_IMPL (impl, 0)
115 do_one_test (impl, s1, s2, len, exp_result);
122 do_random_tests (void)
124 size_t i, j, n, align1, align2, pos, len;
127 unsigned char *p1 = buf1 + page_size - 512;
128 unsigned char *p2 = buf2 + page_size - 512;
130 for (n = 0; n < ITERATIONS; n++)
132 align1 = random () & 31;
134 align2 = random () & 31;
136 align2 = align1 + (random () & 24);
137 pos = random () & 511;
142 pos = 511 - j - (random () & 7);
143 len = random () & 511;
145 len = 511 - j - (random () & 7);
146 j = len + align1 + 64;
147 if (j > 512) j = 512;
148 for (i = 0; i < j; ++i)
149 p1[i] = random () & 255;
150 for (i = 0; i < j; ++i)
151 p2[i] = random () & 255;
155 memcpy (p2 + align2, p1 + align1, len);
158 memcpy (p2 + align2, p1 + align1, pos);
159 if (p2[align2 + pos] == p1[align1 + pos])
161 p2[align2 + pos] = random () & 255;
162 if (p2[align2 + pos] == p1[align1 + pos])
163 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
166 if (p1[align1 + pos] < p2[align2 + pos])
172 FOR_EACH_IMPL (impl, 1)
174 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2), len);
175 /* Test whether on 64-bit architectures where ABI requires
176 callee to promote has the promotion been done. */
177 asm ("" : "=g" (r) : "0" (r));
178 if ((r == 0 && result)
179 || (r < 0 && result >= 0)
180 || (r > 0 && result <= 0))
182 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
183 n, impl->name, align1, align2, len, pos, r, result, p1, p2);
193 char s1[116], s2[116];
430 exp_result = simple_memcmp (s1, s2, n);
431 FOR_EACH_IMPL (impl, 0)
432 check_result (impl, s1, s2, n, exp_result);
445 FOR_EACH_IMPL (impl, 0)
446 printf ("\t%s", impl->name);
449 for (i = 1; i < 16; ++i)
451 do_test (i, i, i, 0);
452 do_test (i, i, i, 1);
453 do_test (i, i, i, -1);
456 for (i = 0; i < 16; ++i)
458 do_test (0, 0, i, 0);
459 do_test (0, 0, i, 1);
460 do_test (0, 0, i, -1);
463 for (i = 1; i < 10; ++i)
465 do_test (0, 0, 2 << i, 0);
466 do_test (0, 0, 2 << i, 1);
467 do_test (0, 0, 2 << i, -1);
468 do_test (0, 0, 16 << i, 0);
469 do_test (8 - i, 2 * i, 16 << i, 0);
470 do_test (0, 0, 16 << i, 1);
471 do_test (0, 0, 16 << i, -1);
474 for (i = 1; i < 8; ++i)
476 do_test (i, 2 * i, 8 << i, 0);
477 do_test (i, 2 * i, 8 << i, 1);
478 do_test (i, 2 * i, 8 << i, -1);
485 #include "../test-skeleton.c"