bench-strpbrk.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Measure strpbrk 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 BIG_CHAR MAX_CHAR
  16. #ifndef WIDE
  17. # define SMALL_CHAR 127
  18. #else
  19. # define SMALL_CHAR 1273
  20. #endif /* WIDE */
  21. #ifndef STRPBRK_RESULT
  22. # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
  23. # define RES_TYPE CHAR *
  24. # define TEST_MAIN
  25. # ifndef WIDE
  26. # define TEST_NAME "strpbrk"
  27. # else
  28. # define TEST_NAME "wcspbrk"
  29. # endif /* WIDE */
  30. # include "bench-string.h"
  31. typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
  32. IMPL (STRPBRK, 1)
  33. #endif /* !STRPBRK_RESULT */
  34. #include "json-lib.h"
  35. static void
  36. do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s,
  37. const CHAR *rej, RES_TYPE exp_res)
  38. {
  39. RES_TYPE res = CALL (impl, s, rej);
  40. size_t i, iters = INNER_LOOP_ITERS8 / CHARBYTES;
  41. timing_t start, stop, cur;
  42. if (res != exp_res)
  43. {
  44. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  45. (void *) res, (void *) exp_res);
  46. ret = 1;
  47. return;
  48. }
  49. TIMING_NOW (start);
  50. for (i = 0; i < iters; ++i)
  51. {
  52. CALL (impl, s, rej);
  53. }
  54. TIMING_NOW (stop);
  55. TIMING_DIFF (cur, start, stop);
  56. json_element_double (json_ctx, (double)cur / (double)iters);
  57. }
  58. static void
  59. do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t pos,
  60. size_t len)
  61. {
  62. size_t i;
  63. int c;
  64. RES_TYPE result;
  65. CHAR *rej, *s;
  66. align1 &= 7;
  67. if ((align1 + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
  68. return;
  69. if ((align2 + len) * sizeof (CHAR) >= page_size)
  70. return;
  71. rej = (CHAR *) (buf2) + align2;
  72. s = (CHAR *) (buf1) + align1;
  73. for (i = 0; i < len; ++i)
  74. {
  75. rej[i] = random () & BIG_CHAR;
  76. if (!rej[i])
  77. rej[i] = random () & BIG_CHAR;
  78. if (!rej[i])
  79. rej[i] = 1 + (random () & SMALL_CHAR);
  80. }
  81. rej[len] = '\0';
  82. for (c = 1; c <= BIG_CHAR; ++c)
  83. if (STRCHR (rej, c) == NULL)
  84. break;
  85. for (i = 0; i < pos; ++i)
  86. {
  87. s[i] = random () & BIG_CHAR;
  88. if (STRCHR (rej, s[i]))
  89. {
  90. s[i] = random () & BIG_CHAR;
  91. if (STRCHR (rej, s[i]))
  92. s[i] = c;
  93. }
  94. }
  95. s[pos] = rej[random () % (len + 1)];
  96. if (s[pos])
  97. {
  98. for (i = pos + 1; i < pos + 10; ++i)
  99. s[i] = random () & BIG_CHAR;
  100. s[i] = '\0';
  101. }
  102. result = STRPBRK_RESULT (s, pos);
  103. json_element_object_begin (json_ctx);
  104. json_attr_uint (json_ctx, "len", len);
  105. json_attr_uint (json_ctx, "pos", pos);
  106. json_attr_uint (json_ctx, "align1", align1);
  107. json_attr_uint (json_ctx, "align2", align2);
  108. json_array_begin (json_ctx, "timings");
  109. FOR_EACH_IMPL (impl, 0)
  110. do_one_test (json_ctx, impl, s, rej, 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;
  119. test_init ();
  120. json_init (&json_ctx, 0, stdout);
  121. json_document_begin (&json_ctx);
  122. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  123. json_attr_object_begin (&json_ctx, "functions");
  124. json_attr_object_begin (&json_ctx, TEST_NAME);
  125. json_attr_string (&json_ctx, "bench-variant", "");
  126. json_array_begin (&json_ctx, "ifuncs");
  127. FOR_EACH_IMPL (impl, 0)
  128. json_element_string (&json_ctx, impl->name);
  129. json_array_end (&json_ctx);
  130. json_array_begin (&json_ctx, "results");
  131. for (i = 0; i < 32; ++i)
  132. {
  133. do_test (&json_ctx, 0, 0, 512, i);
  134. do_test (&json_ctx, i, 0, 512, i);
  135. do_test (&json_ctx, 0, i, 512, i);
  136. do_test (&json_ctx, i, i, 512, i);
  137. }
  138. for (i = 1; i < 8; ++i)
  139. {
  140. do_test (&json_ctx, 0, 0, 16 << i, 4);
  141. do_test (&json_ctx, i, 0, 16 << i, 4);
  142. do_test (&json_ctx, 0, i, 16 << i, 4);
  143. do_test (&json_ctx, i, i, 16 << i, 4);
  144. }
  145. for (i = 1; i < 8; ++i)
  146. {
  147. do_test (&json_ctx, i, 0, 64, 10);
  148. do_test (&json_ctx, i, i, 64, 10);
  149. }
  150. for (i = 0; i < 64; ++i)
  151. {
  152. do_test (&json_ctx, 0, 0, i, 6);
  153. do_test (&json_ctx, 0, i, i, 6);
  154. }
  155. json_array_end (&json_ctx);
  156. json_attr_object_end (&json_ctx);
  157. json_attr_object_end (&json_ctx);
  158. json_document_end (&json_ctx);
  159. return ret;
  160. }
  161. #include <support/test-driver.c>