1/* Measure memccpy functions.
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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 <https://www.gnu.org/licenses/>. */
18
19#define TEST_MAIN
20#define TEST_NAME "memccpy"
21#include "bench-string.h"
22
23void *
24generic_memccpy (void *dst, const void *src, int c, size_t n)
25{
26 void *p = memchr (src, c, n);
27
28 if (p != NULL)
29 return mempcpy (dst, src, p - src + 1);
30
31 memcpy (dst, src, n);
32 return NULL;
33}
34
35IMPL (memccpy, 1)
36IMPL (generic_memccpy, 0)
37
38typedef void *(*proto_t) (void *, const void *, int c, size_t);
39
40static void
41do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
42 size_t n)
43{
44 size_t i, iters = INNER_LOOP_ITERS_LARGE;
45 timing_t start, stop, cur;
46
47 TIMING_NOW (start);
48 for (i = 0; i < iters; ++i)
49 {
50 CALL (impl, dst, src, c, n);
51 }
52 TIMING_NOW (stop);
53
54 TIMING_DIFF (cur, start, stop);
55
56 TIMING_PRINT_MEAN ((double) cur, (double) iters);
57}
58
59static void
60do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
61 int max_char)
62{
63 size_t i;
64 char *s1, *s2;
65
66 align1 &= 7;
67 if (align1 + len >= page_size)
68 return;
69
70 align2 &= 7;
71 if (align2 + len >= page_size)
72 return;
73
74 s1 = (char *) (buf1 + align1);
75 s2 = (char *) (buf2 + align2);
76
77 for (i = 0; i < len - 1; ++i)
78 {
79 s1[i] = 32 + 23 * i % (max_char - 32);
80 if (s1[i] == (char) c)
81 --s1[i];
82 }
83 s1[len - 1] = c;
84 for (i = len; i + align1 < page_size && i < len + 64; ++i)
85 s1[i] = 32 + 32 * i % (max_char - 32);
86
87 printf (format: "Length %4zd, n %4zd, char %d, alignment %2zd/%2zd:", len, n, c, align1, align2);
88
89 FOR_EACH_IMPL (impl, 0)
90 do_one_test (impl, dst: s2, src: s1, c, len, n);
91
92 putchar (c: '\n');
93}
94
95int
96test_main (void)
97{
98 size_t i;
99
100 test_init ();
101
102 printf (format: "%28s", "");
103 FOR_EACH_IMPL (impl, 0)
104 printf (format: "\t%s", impl->name);
105 putchar (c: '\n');
106
107 for (i = 1; i < 8; ++i)
108 {
109 do_test (align1: i, align2: i, c: 12, len: 16, n: 16, max_char: 127);
110 do_test (align1: i, align2: i, c: 23, len: 16, n: 16, max_char: 255);
111 do_test (align1: i, align2: 2 * i, c: 28, len: 16, n: 16, max_char: 127);
112 do_test (align1: 2 * i, align2: i, c: 31, len: 16, n: 16, max_char: 255);
113 do_test (align1: 8 - i, align2: 2 * i, c: 1, len: 1 << i, n: 2 << i, max_char: 127);
114 do_test (align1: 2 * i, align2: 8 - i, c: 17, len: 2 << i, n: 1 << i, max_char: 127);
115 do_test (align1: 8 - i, align2: 2 * i, c: 0, len: 1 << i, n: 2 << i, max_char: 255);
116 do_test (align1: 2 * i, align2: 8 - i, c: i, len: 2 << i, n: 1 << i, max_char: 255);
117 }
118
119 for (i = 1; i < 8; ++i)
120 {
121 do_test (align1: 0, align2: 0, c: i, len: 4 << i, n: 8 << i, max_char: 127);
122 do_test (align1: 0, align2: 0, c: i, len: 16 << i, n: 8 << i, max_char: 127);
123 do_test (align1: 8 - i, align2: 2 * i, c: i, len: 4 << i, n: 8 << i, max_char: 127);
124 do_test (align1: 8 - i, align2: 2 * i, c: i, len: 16 << i, n: 8 << i, max_char: 127);
125 }
126
127 return ret;
128}
129
130#include <support/test-driver.c>
131

source code of glibc/benchtests/bench-memccpy.c