1 /* Measure memcmp functions.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
21 # define TEST_NAME "wmemcmp"
23 # define TEST_NAME "memcmp"
25 #include "bench-string.h"
27 # include <inttypes.h>
30 # define MEMCMP wmemcmp
31 # define MEMCPY wmemcpy
32 # define SIMPLE_MEMCMP simple_wmemcmp
34 # define UCHAR wchar_t
36 # define CHAR__MIN WCHAR_MIN
37 # define CHAR__MAX WCHAR_MAX
39 simple_wmemcmp (const wchar_t *s1
, const wchar_t *s2
, size_t n
)
43 wmemcmp has to use SIGNED comparison for elements.
44 memcmp has to use UNSIGNED comparison for elemnts.
46 while (n
-- && (ret
= *s1
< *s2
? -1 : *s1
== *s2
? 0 : 1) == 0) {s1
++; s2
++;}
52 # define MEMCMP memcmp
53 # define MEMCPY memcpy
54 # define SIMPLE_MEMCMP simple_memcmp
57 # define UCHAR unsigned char
59 # define CHAR__MIN CHAR_MIN
60 # define CHAR__MAX CHAR_MAX
63 simple_memcmp (const char *s1
, const char *s2
, size_t n
)
67 while (n
-- && (ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) == 0);
72 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
74 IMPL (SIMPLE_MEMCMP
, 0)
78 do_one_test (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
81 size_t i
, iters
= INNER_LOOP_ITERS
;
82 timing_t start
, stop
, cur
;
85 for (i
= 0; i
< iters
; ++i
)
87 CALL (impl
, s1
, s2
, len
);
91 TIMING_DIFF (cur
, start
, stop
);
93 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
97 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
106 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
110 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
113 s1
= (CHAR
*) (buf1
+ align1
);
114 s2
= (CHAR
*) (buf2
+ align2
);
116 for (i
= 0; i
< len
; i
++)
117 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% CHAR__MAX
;
121 s2
[len
- 1] -= exp_result
;
123 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
125 FOR_EACH_IMPL (impl
, 0)
126 do_one_test (impl
, s1
, s2
, len
, exp_result
);
139 FOR_EACH_IMPL (impl
, 0)
140 printf ("\t%s", impl
->name
);
143 for (i
= 1; i
< 16; ++i
)
145 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 0);
146 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 1);
147 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, -1);
150 for (i
= 0; i
< 16; ++i
)
152 do_test (0, 0, i
, 0);
153 do_test (0, 0, i
, 1);
154 do_test (0, 0, i
, -1);
157 for (i
= 1; i
< 10; ++i
)
159 do_test (0, 0, 2 << i
, 0);
160 do_test (0, 0, 2 << i
, 1);
161 do_test (0, 0, 2 << i
, -1);
162 do_test (0, 0, 16 << i
, 0);
163 do_test ((8 - i
) * CHARBYTES
, (2 * i
) * CHARBYTES
, 16 << i
, 0);
164 do_test (0, 0, 16 << i
, 1);
165 do_test (0, 0, 16 << i
, -1);
168 for (i
= 1; i
< 8; ++i
)
170 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 0);
171 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 1);
172 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, -1);
177 #include "../test-skeleton.c"