bench-strcat.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Measure strcat 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. #ifndef WIDE
  17. # define TEST_NAME "strcat"
  18. #else
  19. # define TEST_NAME "wcscat"
  20. # define generic_strcat generic_wcscat
  21. #endif /* WIDE */
  22. #include "bench-string.h"
  23. #define BIG_CHAR MAX_CHAR
  24. #ifndef WIDE
  25. # define sfmt "s"
  26. # define SMALL_CHAR 127
  27. #else
  28. # define sfmt "ls"
  29. # define SMALL_CHAR 1273
  30. #endif /* WIDE */
  31. #include "json-lib.h"
  32. typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
  33. CHAR *
  34. generic_strcat (CHAR *dst, const CHAR *src)
  35. {
  36. STRCPY (dst + STRLEN (dst), src);
  37. return dst;
  38. }
  39. IMPL (STRCAT, 1)
  40. IMPL (generic_strcat, 0)
  41. static void
  42. do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *dst, const CHAR *src)
  43. {
  44. size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS_LARGE / CHARBYTES;
  45. timing_t start, stop, cur;
  46. if (CALL (impl, dst, src) != dst)
  47. {
  48. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  49. CALL (impl, dst, src), dst);
  50. ret = 1;
  51. return;
  52. }
  53. if (STRCMP (dst + k, src) != 0)
  54. {
  55. error (0, 0,
  56. "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
  57. impl->name, dst, src);
  58. ret = 1;
  59. return;
  60. }
  61. TIMING_NOW (start);
  62. for (i = 0; i < iters; ++i)
  63. {
  64. dst[k] = '\0';
  65. CALL (impl, dst, src);
  66. }
  67. TIMING_NOW (stop);
  68. TIMING_DIFF (cur, start, stop);
  69. json_element_double (json_ctx, (double) cur / (double) iters);
  70. }
  71. static void
  72. do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len1,
  73. size_t len2, int max_char)
  74. {
  75. size_t i;
  76. CHAR *s1, *s2;
  77. align1 &= 7;
  78. if ((align1 + len1) * sizeof (CHAR) >= page_size)
  79. return;
  80. align2 &= 7;
  81. if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
  82. return;
  83. s1 = (CHAR *) (buf1) + align1;
  84. s2 = (CHAR *) (buf2) + align2;
  85. for (i = 0; i < len1; ++i)
  86. s1[i] = 32 + 23 * i % (max_char - 32);
  87. s1[len1] = '\0';
  88. for (i = 0; i < len2; i++)
  89. s2[i] = 32 + 23 * i % (max_char - 32);
  90. json_element_object_begin (json_ctx);
  91. json_attr_uint (json_ctx, "align1", align1);
  92. json_attr_uint (json_ctx, "align2", align2);
  93. json_attr_uint (json_ctx, "len1", len1);
  94. json_attr_uint (json_ctx, "len2", len2);
  95. json_attr_uint (json_ctx, "max_char", max_char);
  96. json_array_begin (json_ctx, "timings");
  97. FOR_EACH_IMPL (impl, 0)
  98. {
  99. s2[len2] = '\0';
  100. do_one_test (json_ctx, impl, s2, s1);
  101. }
  102. json_array_end (json_ctx);
  103. json_element_object_end (json_ctx);
  104. }
  105. int
  106. test_main (void)
  107. {
  108. json_ctx_t json_ctx;
  109. size_t i;
  110. test_init ();
  111. test_init ();
  112. json_init (&json_ctx, 0, stdout);
  113. json_document_begin (&json_ctx);
  114. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  115. json_attr_object_begin (&json_ctx, "functions");
  116. json_attr_object_begin (&json_ctx, TEST_NAME);
  117. json_attr_string (&json_ctx, "bench-variant", "");
  118. json_array_begin (&json_ctx, "ifuncs");
  119. FOR_EACH_IMPL (impl, 0)
  120. json_element_string (&json_ctx, impl->name);
  121. json_array_end (&json_ctx);
  122. json_array_begin (&json_ctx, "results");
  123. for (i = 0; i < 16; ++i)
  124. {
  125. do_test (&json_ctx, 0, 0, i, i, SMALL_CHAR);
  126. do_test (&json_ctx, 0, 0, i, i, BIG_CHAR);
  127. do_test (&json_ctx, 0, i, i, i, SMALL_CHAR);
  128. do_test (&json_ctx, i, 0, i, i, BIG_CHAR);
  129. }
  130. for (i = 1; i < 8; ++i)
  131. {
  132. do_test (&json_ctx, 0, 0, 8 << i, 8 << i, SMALL_CHAR);
  133. do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 8 << i, SMALL_CHAR);
  134. do_test (&json_ctx, 0, 0, 8 << i, 2 << i, SMALL_CHAR);
  135. do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 2 << i, SMALL_CHAR);
  136. }
  137. for (i = 1; i < 8; ++i)
  138. {
  139. do_test (&json_ctx, i, 2 * i, 8 << i, 1, SMALL_CHAR);
  140. do_test (&json_ctx, 2 * i, i, 8 << i, 1, BIG_CHAR);
  141. do_test (&json_ctx, i, i, 8 << i, 10, SMALL_CHAR);
  142. do_test (&json_ctx, i, i, 8 << i, 10, BIG_CHAR);
  143. }
  144. for (i = 32; i < 256; i += 32)
  145. {
  146. do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
  147. do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
  148. do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
  149. do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
  150. }
  151. for (; i < 512; i += 64)
  152. {
  153. do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
  154. do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
  155. do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
  156. do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
  157. }
  158. for (; i < 1024; i += 128)
  159. {
  160. do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
  161. do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
  162. do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
  163. do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
  164. }
  165. for (; i < 2048; i += 256)
  166. {
  167. do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
  168. do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
  169. do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
  170. do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
  171. }
  172. json_array_end (&json_ctx);
  173. json_attr_object_end (&json_ctx);
  174. json_attr_object_end (&json_ctx);
  175. json_document_end (&json_ctx);
  176. return ret;
  177. }
  178. #include <support/test-driver.c>