bench-strncpy.c 5.6 KB

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