bench-memcmp.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Measure memcmp 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 TEST_MEMCMPEQ
  17. # define TEST_NAME "__memcmpeq"
  18. #elif defined WIDE
  19. # define TEST_NAME "wmemcmp"
  20. #else
  21. # define TEST_NAME "memcmp"
  22. #endif
  23. #include "bench-string.h"
  24. #include "json-lib.h"
  25. typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
  26. IMPL (MEMCMP, 1)
  27. static void
  28. do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1,
  29. const CHAR *s2, size_t len)
  30. {
  31. size_t i, iters = INNER_LOOP_ITERS_LARGE;
  32. timing_t start, stop, cur;
  33. TIMING_NOW (start);
  34. for (i = 0; i < iters; ++i)
  35. {
  36. CALL (impl, s1, s2, len);
  37. }
  38. TIMING_NOW (stop);
  39. TIMING_DIFF (cur, start, stop);
  40. json_element_double (json_ctx, (double) cur / (double) iters);
  41. }
  42. static void
  43. do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
  44. int exp_result)
  45. {
  46. size_t i;
  47. CHAR *s1, *s2;
  48. align1 &= (4096 - CHARBYTES);
  49. if (align1 + (len + 1) * CHARBYTES >= page_size)
  50. return;
  51. align2 &= (4096 - CHARBYTES);
  52. if (align2 + (len + 1) * CHARBYTES >= page_size)
  53. return;
  54. json_element_object_begin (json_ctx);
  55. json_attr_uint (json_ctx, "length", (double) len);
  56. json_attr_uint (json_ctx, "align1", (double) align1);
  57. json_attr_uint (json_ctx, "align2", (double) align2);
  58. json_attr_uint (json_ctx, "result", (double) exp_result);
  59. json_array_begin (json_ctx, "timings");
  60. s1 = (CHAR *)(buf1 + align1);
  61. s2 = (CHAR *)(buf2 + align2);
  62. for (i = 0; i < len; i++)
  63. s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % MAX_CHAR;
  64. if (len)
  65. {
  66. s1[len] = align1;
  67. s2[len] = align2;
  68. s2[len - 1] -= exp_result;
  69. }
  70. FOR_EACH_IMPL (impl, 0)
  71. {
  72. do_one_test (json_ctx, impl, s1, s2, len);
  73. }
  74. json_array_end (json_ctx);
  75. json_element_object_end (json_ctx);
  76. }
  77. int
  78. test_main (void)
  79. {
  80. json_ctx_t json_ctx;
  81. size_t i;
  82. test_init ();
  83. json_init (&json_ctx, 0, stdout);
  84. json_document_begin (&json_ctx);
  85. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  86. json_attr_object_begin (&json_ctx, "functions");
  87. json_attr_object_begin (&json_ctx, TEST_NAME);
  88. json_attr_string (&json_ctx, "bench-variant", "default");
  89. json_array_begin (&json_ctx, "ifuncs");
  90. FOR_EACH_IMPL (impl, 0)
  91. json_element_string (&json_ctx, impl->name);
  92. json_array_end (&json_ctx);
  93. json_array_begin (&json_ctx, "results");
  94. for (i = 0; i < 32; ++i)
  95. {
  96. do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 0);
  97. do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 1);
  98. do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, -1);
  99. }
  100. for (i = 0; i < 32; ++i)
  101. {
  102. do_test (&json_ctx, 0, 0, i, 0);
  103. do_test (&json_ctx, 0, 0, i, 1);
  104. do_test (&json_ctx, 0, 0, i, -1);
  105. do_test (&json_ctx, 4096 - i, 0, i, 0);
  106. do_test (&json_ctx, 4096 - i, 0, i, 1);
  107. do_test (&json_ctx, 4096 - i, 0, i, -1);
  108. }
  109. for (i = 33; i < 385; i += 32)
  110. {
  111. do_test (&json_ctx, 0, 0, i, 0);
  112. do_test (&json_ctx, 0, 0, i, 1);
  113. do_test (&json_ctx, 0, 0, i, -1);
  114. do_test (&json_ctx, i, 0, i, 0);
  115. do_test (&json_ctx, 0, i, i, 1);
  116. do_test (&json_ctx, i, i, i, -1);
  117. }
  118. for (i = 1; i < 10; ++i)
  119. {
  120. do_test (&json_ctx, 0, 0, 2 << i, 0);
  121. do_test (&json_ctx, 0, 0, 2 << i, 1);
  122. do_test (&json_ctx, 0, 0, 2 << i, -1);
  123. do_test (&json_ctx, (8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
  124. do_test (&json_ctx, 0, 0, 16 << i, 0);
  125. do_test (&json_ctx, 0, 0, 16 << i, 1);
  126. do_test (&json_ctx, 0, 0, 16 << i, -1);
  127. do_test (&json_ctx, i, 0, 2 << i, 0);
  128. do_test (&json_ctx, 0, i, 2 << i, 1);
  129. do_test (&json_ctx, i, i, 2 << i, -1);
  130. do_test (&json_ctx, i, 0, 16 << i, 0);
  131. do_test (&json_ctx, 0, i, 16 << i, 1);
  132. do_test (&json_ctx, i, i, 16 << i, -1);
  133. }
  134. for (i = 1; i < 10; ++i)
  135. {
  136. do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
  137. do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
  138. do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
  139. }
  140. json_array_end (&json_ctx);
  141. json_attr_object_end (&json_ctx);
  142. json_attr_object_end (&json_ctx);
  143. json_document_end (&json_ctx);
  144. return ret;
  145. }
  146. #include <support/test-driver.c>