bench-strrchr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Measure STRCHR functions.
  2. Copyright (C) 2013-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define TEST_MAIN
  16. #ifdef WIDE
  17. # define TEST_NAME "wcsrchr"
  18. #else
  19. # define TEST_NAME "strrchr"
  20. #endif
  21. #include "bench-string.h"
  22. #include "json-lib.h"
  23. #define BIG_CHAR MAX_CHAR
  24. #ifdef WIDE
  25. # define SMALL_CHAR 1273
  26. #else
  27. # define SMALL_CHAR 127
  28. char *
  29. generic_strrchr (const char *, int);
  30. IMPL (generic_strrchr, 0)
  31. #endif
  32. typedef CHAR *(*proto_t) (const CHAR *, int);
  33. IMPL (STRRCHR, 1)
  34. static void
  35. do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
  36. CHAR *exp_res)
  37. {
  38. CHAR *res = CALL (impl, s, c);
  39. size_t i, iters = INNER_LOOP_ITERS8;
  40. timing_t start, stop, cur;
  41. if (res != exp_res)
  42. {
  43. error (0, 0, "Wrong result in function %s %p %p", impl->name, res,
  44. exp_res);
  45. ret = 1;
  46. return;
  47. }
  48. TIMING_NOW (start);
  49. for (i = 0; i < iters; ++i)
  50. {
  51. CALL (impl, s, c);
  52. }
  53. TIMING_NOW (stop);
  54. TIMING_DIFF (cur, start, stop);
  55. json_element_double (json_ctx, (double) cur / (double) iters);
  56. }
  57. static void
  58. do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
  59. int seek_char, int max_char, size_t freq)
  60. /* For wcsrchr: align here means align not in bytes,
  61. but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
  62. len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
  63. {
  64. size_t i;
  65. size_t pos_chunk_sz = freq ? (pos / freq) : pos;
  66. size_t last_pos = len;
  67. CHAR *result;
  68. CHAR *buf = (CHAR *) buf1;
  69. align &= (getpagesize () - 1);
  70. if ((align + len) * sizeof (CHAR) >= page_size)
  71. return;
  72. for (i = 0; i < len; ++i)
  73. {
  74. buf[align + i] = (random () * random ()) & max_char;
  75. if (!buf[align + i])
  76. buf[align + i] = (random () * random ()) & max_char;
  77. if (!buf[align + i])
  78. buf[align + i] = 1;
  79. if ((i > pos || pos >= len) && buf[align + i] == seek_char)
  80. buf[align + i] = seek_char + 10 + (random () & 15);
  81. }
  82. if (pos_chunk_sz == 0 && pos)
  83. pos_chunk_sz = 1;
  84. for (i = pos_chunk_sz; i < pos && i < len; i += pos_chunk_sz)
  85. {
  86. buf[align + i] = seek_char;
  87. last_pos = i;
  88. }
  89. buf[align + len] = 0;
  90. if (pos < len)
  91. {
  92. buf[align + pos] = seek_char;
  93. result = (CHAR *) (buf + align + pos);
  94. }
  95. else if (last_pos < len)
  96. result = (CHAR *) (buf + align + last_pos);
  97. else if (seek_char == 0)
  98. result = (CHAR *) (buf + align + len);
  99. else
  100. result = NULL;
  101. json_element_object_begin (json_ctx);
  102. json_attr_uint (json_ctx, "len", len);
  103. json_attr_uint (json_ctx, "pos", pos);
  104. json_attr_uint (json_ctx, "align", align);
  105. json_attr_uint (json_ctx, "freq", freq);
  106. json_attr_uint (json_ctx, "seek", seek_char);
  107. json_attr_uint (json_ctx, "max_char", max_char);
  108. json_array_begin (json_ctx, "timings");
  109. FOR_EACH_IMPL (impl, 0)
  110. do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, result);
  111. json_array_end (json_ctx);
  112. json_element_object_end (json_ctx);
  113. }
  114. int
  115. test_main (void)
  116. {
  117. json_ctx_t json_ctx;
  118. size_t i, j, k;
  119. int seek;
  120. test_init ();
  121. json_init (&json_ctx, 0, stdout);
  122. json_document_begin (&json_ctx);
  123. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  124. json_attr_object_begin (&json_ctx, "functions");
  125. json_attr_object_begin (&json_ctx, TEST_NAME);
  126. json_attr_string (&json_ctx, "bench-variant", "");
  127. json_array_begin (&json_ctx, "ifuncs");
  128. FOR_EACH_IMPL (impl, 0)
  129. json_element_string (&json_ctx, impl->name);
  130. json_array_end (&json_ctx);
  131. json_array_begin (&json_ctx, "results");
  132. for (seek = 0; seek <= 23; seek += 23)
  133. {
  134. for (j = 1; j <= 256; j = (j * 4))
  135. {
  136. for (i = 1; i < 9; ++i)
  137. {
  138. do_test (&json_ctx, 0, 16 << i, 2048, seek, SMALL_CHAR, j);
  139. do_test (&json_ctx, i, 16 << i, 2048, seek, SMALL_CHAR, j);
  140. }
  141. for (i = 1; i < 8; ++i)
  142. {
  143. do_test (&json_ctx, i, 64, 256, seek, SMALL_CHAR, j);
  144. do_test (&json_ctx, i, 64, 256, seek, BIG_CHAR, j);
  145. do_test (&json_ctx, i * 15, 64, 256, seek, SMALL_CHAR, j);
  146. do_test (&json_ctx, i * 15, 64, 256, seek, BIG_CHAR, j);
  147. }
  148. for (i = 0; i < 32; ++i)
  149. {
  150. do_test (&json_ctx, 0, i, i + 1, seek, SMALL_CHAR, j);
  151. do_test (&json_ctx, 0, i, i + 1, seek, BIG_CHAR, j);
  152. do_test (&json_ctx, getpagesize () - i / 2 - 1, i, i + 1, seek,
  153. SMALL_CHAR, j);
  154. }
  155. for (i = (16 / sizeof (CHAR)); i <= (288 / sizeof (CHAR)); i += 32)
  156. {
  157. do_test (&json_ctx, 0, i - 16, i, seek, SMALL_CHAR, j);
  158. do_test (&json_ctx, 0, i, i + 16, seek, SMALL_CHAR, j);
  159. }
  160. for (i = (16 / sizeof (CHAR)); i <= (2048 / sizeof (CHAR)); i += i)
  161. {
  162. for (k = 0; k <= (288 / sizeof (CHAR));
  163. k += (48 / sizeof (CHAR)))
  164. {
  165. do_test (&json_ctx, 0, k, i, seek, SMALL_CHAR, j);
  166. do_test (&json_ctx, 0, i, i + k, seek, SMALL_CHAR, j);
  167. if (k < i)
  168. {
  169. do_test (&json_ctx, 0, i - k, i, seek, SMALL_CHAR, j);
  170. do_test (&json_ctx, 0, k, i - k, seek, SMALL_CHAR, j);
  171. do_test (&json_ctx, 0, i, i - k, seek, SMALL_CHAR, j);
  172. }
  173. }
  174. }
  175. if (seek == 0)
  176. {
  177. break;
  178. }
  179. }
  180. }
  181. json_array_end (&json_ctx);
  182. json_attr_object_end (&json_ctx);
  183. json_attr_object_end (&json_ctx);
  184. json_document_end (&json_ctx);
  185. return ret;
  186. }
  187. #include <support/test-driver.c>
  188. #define weak_alias(X,Y)
  189. #define libc_hidden_builtin_def(X)
  190. #ifndef WIDE
  191. # undef STRRCHR
  192. # define STRRCHR generic_strrchr
  193. # define __memrchr memrchr
  194. # include <string/strrchr.c>
  195. #endif