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 do_one_test (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
,
57 hp_timing_t start
__attribute ((unused
));
58 hp_timing_t stop
__attribute ((unused
));
59 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
62 for (i
= 0; i
< 32; ++i
)
64 HP_TIMING_NOW (start
);
65 CALL (impl
, s1
, s2
, len
);
67 HP_TIMING_BEST (best_time
, start
, stop
);
70 printf ("\t%zd", (size_t) best_time
);
75 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
84 if (align1
+ len
>= page_size
)
88 if (align2
+ len
>= page_size
)
91 s1
= (char *) (buf1
+ align1
);
92 s2
= (char *) (buf2
+ align2
);
94 for (i
= 0; i
< len
; i
++)
95 s1
[i
] = s2
[i
] = 1 + 23 * i
% 255;
99 s2
[len
- 1] -= exp_result
;
102 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
104 FOR_EACH_IMPL (impl
, 0)
105 do_one_test (impl
, s1
, s2
, len
, exp_result
);
112 do_random_tests (void)
114 size_t i
, j
, n
, align1
, align2
, pos
, len
;
117 unsigned char *p1
= buf1
+ page_size
- 512;
118 unsigned char *p2
= buf2
+ page_size
- 512;
120 for (n
= 0; n
< ITERATIONS
; n
++)
122 align1
= random () & 31;
124 align2
= random () & 31;
126 align2
= align1
+ (random () & 24);
127 pos
= random () & 511;
132 pos
= 511 - j
- (random () & 7);
133 len
= random () & 511;
135 len
= 511 - j
- (random () & 7);
136 j
= len
+ align1
+ 64;
137 if (j
> 512) j
= 512;
138 for (i
= 0; i
< j
; ++i
)
139 p1
[i
] = random () & 255;
140 for (i
= 0; i
< j
; ++i
)
141 p2
[i
] = random () & 255;
145 memcpy (p2
+ align2
, p1
+ align1
, len
);
148 memcpy (p2
+ align2
, p1
+ align1
, pos
);
149 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
151 p2
[align2
+ pos
] = random () & 255;
152 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
153 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
156 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
162 FOR_EACH_IMPL (impl
, 1)
164 r
= CALL (impl
, (char *) (p1
+ align1
), (char *) (p2
+ align2
), len
);
165 /* Test whether on 64-bit architectures where ABI requires
166 callee to promote has the promotion been done. */
167 asm ("" : "=g" (r
) : "0" (r
));
168 if ((r
== 0 && result
)
169 || (r
< 0 && result
>= 0)
170 || (r
> 0 && result
<= 0))
172 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
173 n
, impl
->name
, align1
, align2
, len
, pos
, r
, result
, p1
, p2
);
188 FOR_EACH_IMPL (impl
, 0)
189 printf ("\t%s", impl
->name
);
192 for (i
= 1; i
< 16; ++i
)
194 do_test (i
, i
, i
, 0);
195 do_test (i
, i
, i
, 1);
196 do_test (i
, i
, i
, -1);
199 for (i
= 0; i
< 16; ++i
)
201 do_test (0, 0, i
, 0);
202 do_test (0, 0, i
, 1);
203 do_test (0, 0, i
, -1);
206 for (i
= 1; i
< 10; ++i
)
208 do_test (0, 0, 2 << i
, 0);
209 do_test (0, 0, 2 << i
, 1);
210 do_test (0, 0, 2 << i
, -1);
211 do_test (0, 0, 16 << i
, 0);
212 do_test (8 - i
, 2 * i
, 16 << i
, 0);
213 do_test (0, 0, 16 << i
, 1);
214 do_test (0, 0, 16 << i
, -1);
217 for (i
= 1; i
< 8; ++i
)
219 do_test (i
, 2 * i
, 8 << i
, 0);
220 do_test (i
, 2 * i
, 8 << i
, 1);
221 do_test (i
, 2 * i
, 8 << i
, -1);
228 #include "../test-skeleton.c"