tile: switch to using <fenv.h> fallback functions
[glibc.git] / benchtests / bench-strncat.c
blob904daa7b7ee20914aca2a1a8e46b93192ad3a12c
1 /* Measure strncat functions.
2 Copyright (C) 2013 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/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "strncat"
21 #include "bench-string.h"
23 typedef char *(*proto_t) (char *, const char *, size_t);
24 char *stupid_strncat (char *, const char *, size_t);
25 char *simple_strncat (char *, const char *, size_t);
27 IMPL (stupid_strncat, 0)
28 IMPL (strncat, 2)
30 char *
31 stupid_strncat (char *dst, const char *src, size_t n)
33 char *ret = dst;
34 while (*dst++ != '\0');
35 --dst;
36 while (n--)
37 if ( (*dst++ = *src++) == '\0')
38 return ret;
39 *dst = '\0';
40 return ret;
43 static void
44 do_one_test (impl_t *impl, char *dst, const char *src, size_t n)
46 size_t k = strlen (dst);
47 if (CALL (impl, dst, src, n) != dst)
49 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
50 CALL (impl, dst, src, n), dst);
51 ret = 1;
52 return;
55 size_t len = strlen (src);
56 if (memcmp (dst + k, src, len + 1 > n ? n : len + 1) != 0)
58 error (0, 0, "Incorrect cancatination in function %s",
59 impl->name);
60 ret = 1;
61 return;
63 if (n < len && dst[k + n] != '\0')
65 error (0, 0, "There is no zero in the end of output string in %s",
66 impl->name);
67 ret = 1;
68 return;
70 if (HP_TIMING_AVAIL)
72 hp_timing_t start __attribute ((unused));
73 hp_timing_t stop __attribute ((unused));
74 hp_timing_t best_time = ~ (hp_timing_t) 0;
75 size_t i;
77 for (i = 0; i < 32; ++i)
79 dst[k] = '\0';
80 HP_TIMING_NOW (start);
81 CALL (impl, dst, src, n);
82 HP_TIMING_NOW (stop);
83 HP_TIMING_BEST (best_time, start, stop);
86 printf ("\t%zd", (size_t) best_time);
90 static void
91 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
92 size_t n, int max_char)
94 size_t i;
95 char *s1, *s2;
97 align1 &= 7;
98 if (align1 + len1 >= page_size)
99 return;
100 if (align1 + n > page_size)
101 return;
102 align2 &= 7;
103 if (align2 + len1 + len2 >= page_size)
104 return;
105 if (align2 + len1 + n > page_size)
106 return;
107 s1 = (char *) (buf1 + align1);
108 s2 = (char *) (buf2 + align2);
110 for (i = 0; i < len1; ++i)
111 s1[i] = 32 + 23 * i % (max_char - 32);
112 s1[len1] = '\0';
114 for (i = 0; i < len2; i++)
115 s2[i] = 32 + 23 * i % (max_char - 32);
117 if (HP_TIMING_AVAIL)
118 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
119 len1, len2, align1, align2, n);
121 FOR_EACH_IMPL (impl, 0)
123 s2[len2] = '\0';
124 do_one_test (impl, s2, s1, n);
127 if (HP_TIMING_AVAIL)
128 putchar ('\n');
132 main (void)
134 size_t i, n;
136 test_init ();
138 printf ("%28s", "");
139 FOR_EACH_IMPL (impl, 0)
140 printf ("\t%s", impl->name);
141 putchar ('\n');
143 for (n = 2; n <= 2048; n*=4)
145 do_test (0, 2, 2, 2, n, 127);
146 do_test (0, 0, 4, 4, n, 127);
147 do_test (4, 0, 4, 4, n, 255);
148 do_test (0, 0, 8, 8, n, 127);
149 do_test (0, 8, 8, 8, n, 127);
151 for (i = 1; i < 8; ++i)
153 do_test (0, 0, 8 << i, 8 << i, n, 127);
154 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, 127);
155 do_test (0, 0, 8 << i, 2 << i, n, 127);
156 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, 127);
159 for (i = 1; i < 8; ++i)
161 do_test (i, 2 * i, 8 << i, 1, n, 127);
162 do_test (2 * i, i, 8 << i, 1, n, 255);
163 do_test (i, i, 8 << i, 10, n, 127);
167 return ret;