bench-rawmemchr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Measure memchr 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. #include <assert.h>
  16. #include <stdint.h>
  17. #define TEST_MAIN
  18. #define TEST_NAME "rawmemchr"
  19. #include "bench-string.h"
  20. #include "json-lib.h"
  21. typedef char *(*proto_t) (const char *, int);
  22. char *
  23. generic_rawmemchr (const char *s, int c)
  24. {
  25. if ((unsigned char) c != 0)
  26. return (char*) memchr (s, c, PTRDIFF_MAX);
  27. return (char *)s + strlen (s);
  28. }
  29. IMPL (rawmemchr, 1)
  30. IMPL (generic_rawmemchr, 0)
  31. static void
  32. do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s, int c, char *exp_res)
  33. {
  34. size_t i, iters = INNER_LOOP_ITERS_LARGE * 4;
  35. timing_t start, stop, cur;
  36. char *res = CALL (impl, s, c);
  37. if (res != exp_res)
  38. {
  39. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  40. res, exp_res);
  41. ret = 1;
  42. return;
  43. }
  44. TIMING_NOW (start);
  45. for (i = 0; i < iters; ++i)
  46. {
  47. CALL (impl, s, c);
  48. }
  49. TIMING_NOW (stop);
  50. TIMING_DIFF (cur, start, stop);
  51. json_element_double (json_ctx, (double) cur / (double) iters);
  52. }
  53. static void
  54. do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, int seek_char)
  55. {
  56. size_t i;
  57. char *result;
  58. align &= getpagesize () - 1;
  59. if (align + len >= page_size)
  60. return;
  61. for (i = 0; i < len; ++i)
  62. {
  63. buf1[align + i] = 1 + 23 * i % 127;
  64. if (buf1[align + i] == seek_char)
  65. buf1[align + i] = seek_char + 1;
  66. }
  67. buf1[align + len] = 0;
  68. assert (pos < len);
  69. buf1[align + pos] = seek_char;
  70. buf1[align + len] = -seek_char;
  71. result = (char *) (buf1 + align + pos);
  72. json_element_object_begin (json_ctx);
  73. json_attr_uint (json_ctx, "length", pos);
  74. json_attr_uint (json_ctx, "alignment", align);
  75. json_attr_uint (json_ctx, "char", seek_char);
  76. json_array_begin (json_ctx, "timings");
  77. FOR_EACH_IMPL (impl, 0)
  78. do_one_test (json_ctx, impl, (char *) (buf1 + align), seek_char, result);
  79. json_array_end (json_ctx);
  80. json_element_object_end (json_ctx);
  81. }
  82. int
  83. test_main (void)
  84. {
  85. json_ctx_t json_ctx;
  86. size_t i;
  87. test_init ();
  88. json_init (&json_ctx, 0, stdout);
  89. json_document_begin (&json_ctx);
  90. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  91. json_attr_object_begin (&json_ctx, "functions");
  92. json_attr_object_begin (&json_ctx, TEST_NAME);
  93. json_attr_string (&json_ctx, "bench-variant", "");
  94. json_array_begin (&json_ctx, "ifuncs");
  95. FOR_EACH_IMPL (impl, 0)
  96. json_element_string (&json_ctx, impl->name);
  97. json_array_end (&json_ctx);
  98. json_array_begin (&json_ctx, "results");
  99. for (i = 1; i < 7; ++i)
  100. {
  101. do_test (&json_ctx, 0, 16 << i, 2048, 23);
  102. do_test (&json_ctx, i, 64, 256, 23);
  103. do_test (&json_ctx, 0, 16 << i, 2048, 0);
  104. do_test (&json_ctx, i, 64, 256, 0);
  105. }
  106. for (i = 1; i < 32; ++i)
  107. {
  108. do_test (&json_ctx, 0, i, i + 1, 23);
  109. do_test (&json_ctx, 0, i, i + 1, 0);
  110. }
  111. for (; i < 256; i += 32)
  112. {
  113. do_test (&json_ctx, 0, i, i + 1, 23);
  114. do_test (&json_ctx, 0, i - 1, i, 23);
  115. }
  116. for (; i < 512; i += 64)
  117. {
  118. do_test (&json_ctx, 0, i, i + 1, 23);
  119. do_test (&json_ctx, 0, i - 1, i, 23);
  120. }
  121. for (; i < 1024; i += 128)
  122. {
  123. do_test (&json_ctx, 0, i, i + 1, 23);
  124. do_test (&json_ctx, 0, i - 1, i, 23);
  125. }
  126. for (; i < 2048; i += 256)
  127. {
  128. do_test (&json_ctx, 0, i, i + 1, 23);
  129. do_test (&json_ctx, 0, i - 1, i, 23);
  130. }
  131. for (; i < 4096; i += 512)
  132. {
  133. do_test (&json_ctx, 0, i, i + 1, 23);
  134. do_test (&json_ctx, 0, i - 1, i, 23);
  135. }
  136. json_array_end (&json_ctx);
  137. json_attr_object_end (&json_ctx);
  138. json_attr_object_end (&json_ctx);
  139. json_document_end (&json_ctx);
  140. return ret;
  141. }
  142. #include <support/test-driver.c>