bench-bzero.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Measure bzero functions.
  2. Copyright (C) 2022-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 DO_MEMSET
  17. # define TEST_NAME "memset"
  18. #else
  19. # define TEST_NAME "bzero"
  20. #endif
  21. #define MIN_PAGE_SIZE 131072
  22. #include "bench-string.h"
  23. #include "json-lib.h"
  24. #ifdef DO_MEMSET
  25. void *generic_memset (void *, int, size_t);
  26. typedef void *(*proto_t) (void *, int, size_t);
  27. IMPL (memset, 1)
  28. IMPL (generic_memset, 0)
  29. #else
  30. static void
  31. memset_zero (void * s, size_t len)
  32. {
  33. memset (s, '\0', len);
  34. }
  35. typedef void (*proto_t) (void *, size_t);
  36. IMPL (bzero, 1)
  37. IMPL (memset_zero, 0)
  38. #endif
  39. static void
  40. do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s, size_t n)
  41. {
  42. size_t i, iters = INNER_LOOP_ITERS8;
  43. timing_t start, stop, cur;
  44. TIMING_NOW (start);
  45. for (i = 0; i < iters; ++i)
  46. {
  47. #ifdef DO_MEMSET
  48. CALL (impl, s, 0, n);
  49. #else
  50. CALL (impl, s, n);
  51. #endif
  52. }
  53. TIMING_NOW (stop);
  54. TIMING_DIFF (cur, start, stop);
  55. json_element_double (json_ctx, (double) cur / (double) iters);
  56. }
  57. static void
  58. do_test (json_ctx_t *json_ctx, size_t align, size_t len)
  59. {
  60. align &= 4095;
  61. if ((align + len) * sizeof (CHAR) > page_size)
  62. return;
  63. json_element_object_begin (json_ctx);
  64. json_attr_uint (json_ctx, "length", len);
  65. json_attr_uint (json_ctx, "alignment", align);
  66. json_array_begin (json_ctx, "timings");
  67. FOR_EACH_IMPL (impl, 0)
  68. {
  69. do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, len);
  70. }
  71. json_array_end (json_ctx);
  72. json_element_object_end (json_ctx);
  73. }
  74. int
  75. test_main (void)
  76. {
  77. json_ctx_t json_ctx;
  78. size_t i;
  79. test_init ();
  80. alloc_bufs ();
  81. json_init (&json_ctx, 0, stdout);
  82. json_document_begin (&json_ctx);
  83. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  84. json_attr_object_begin (&json_ctx, "functions");
  85. json_attr_object_begin (&json_ctx, TEST_NAME);
  86. json_attr_string (&json_ctx, "bench-variant", "default");
  87. json_array_begin (&json_ctx, "ifuncs");
  88. FOR_EACH_IMPL (impl, 0)
  89. json_element_string (&json_ctx, impl->name);
  90. json_array_end (&json_ctx);
  91. json_array_begin (&json_ctx, "results");
  92. for (i = 0; i < 18; ++i)
  93. do_test (&json_ctx, 0, 1 << i);
  94. for (i = 0; i < 64; ++i)
  95. {
  96. do_test (&json_ctx, i, i);
  97. do_test (&json_ctx, 4096 - i, i);
  98. do_test (&json_ctx, 4095, i);
  99. if (i & (i - 1))
  100. do_test (&json_ctx, 0, i);
  101. }
  102. for (i = 32; i < 1024; i+=32)
  103. {
  104. do_test (&json_ctx, 0, i);
  105. do_test (&json_ctx, i, i);
  106. }
  107. do_test (&json_ctx, 1, 14);
  108. do_test (&json_ctx, 3, 1024);
  109. do_test (&json_ctx, 4, 64);
  110. do_test (&json_ctx, 2, 25);
  111. for (i = 33; i <= 256; i += 4)
  112. {
  113. do_test (&json_ctx, 0, 32 * i);
  114. do_test (&json_ctx, i, 32 * i);
  115. }
  116. json_array_end (&json_ctx);
  117. json_attr_object_end (&json_ctx);
  118. json_attr_object_end (&json_ctx);
  119. json_document_end (&json_ctx);
  120. return ret;
  121. }
  122. #include <support/test-driver.c>
  123. #ifdef DO_MEMSET
  124. # define libc_hidden_builtin_def(X)
  125. # define libc_hidden_def(X)
  126. # define libc_hidden_weak(X)
  127. # define weak_alias(X,Y)
  128. # undef MEMSET
  129. # define MEMSET generic_memset
  130. # include <string/memset.c>
  131. #endif