bench-memccpy.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Measure memccpy 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. #define TEST_NAME "memccpy"
  17. #include "bench-string.h"
  18. void *
  19. generic_memccpy (void *dst, const void *src, int c, size_t n)
  20. {
  21. const void *p = memchr (src, c, n);
  22. if (p != NULL)
  23. return mempcpy (dst, src, p - src + 1);
  24. memcpy (dst, src, n);
  25. return NULL;
  26. }
  27. IMPL (memccpy, 1)
  28. IMPL (generic_memccpy, 0)
  29. typedef void *(*proto_t) (void *, const void *, int c, size_t);
  30. static void
  31. do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
  32. size_t n)
  33. {
  34. size_t i, iters = INNER_LOOP_ITERS_LARGE * 4;
  35. timing_t start, stop, cur;
  36. TIMING_NOW (start);
  37. for (i = 0; i < iters; ++i)
  38. {
  39. CALL (impl, dst, src, c, n);
  40. }
  41. TIMING_NOW (stop);
  42. TIMING_DIFF (cur, start, stop);
  43. TIMING_PRINT_MEAN ((double) cur, (double) iters);
  44. }
  45. static void
  46. do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
  47. int max_char)
  48. {
  49. size_t i;
  50. char *s1, *s2;
  51. align1 &= 7;
  52. if (align1 + len >= page_size)
  53. return;
  54. align2 &= 7;
  55. if (align2 + len >= page_size)
  56. return;
  57. s1 = (char *) (buf1 + align1);
  58. s2 = (char *) (buf2 + align2);
  59. for (i = 0; i < len - 1; ++i)
  60. {
  61. s1[i] = 32 + 23 * i % (max_char - 32);
  62. if (s1[i] == (char) c)
  63. --s1[i];
  64. }
  65. s1[len - 1] = c;
  66. for (i = len; i + align1 < page_size && i < len + 64; ++i)
  67. s1[i] = 32 + 32 * i % (max_char - 32);
  68. printf ("Length %4zd, n %4zd, char %d, alignment %2zd/%2zd:", len, n, c, align1, align2);
  69. FOR_EACH_IMPL (impl, 0)
  70. do_one_test (impl, s2, s1, c, len, n);
  71. putchar ('\n');
  72. }
  73. int
  74. test_main (void)
  75. {
  76. size_t i;
  77. test_init ();
  78. printf ("%28s", "");
  79. FOR_EACH_IMPL (impl, 0)
  80. printf ("\t%s", impl->name);
  81. putchar ('\n');
  82. for (i = 1; i < 8; ++i)
  83. {
  84. do_test (i, i, 12, 16, 16, 127);
  85. do_test (i, i, 23, 16, 16, 255);
  86. do_test (i, 2 * i, 28, 16, 16, 127);
  87. do_test (2 * i, i, 31, 16, 16, 255);
  88. do_test (8 - i, 2 * i, 1, 1 << i, 2 << i, 127);
  89. do_test (2 * i, 8 - i, 17, 2 << i, 1 << i, 127);
  90. do_test (8 - i, 2 * i, 0, 1 << i, 2 << i, 255);
  91. do_test (2 * i, 8 - i, i, 2 << i, 1 << i, 255);
  92. }
  93. for (i = 1; i < 8; ++i)
  94. {
  95. do_test (0, 0, i, 4 << i, 8 << i, 127);
  96. do_test (0, 0, i, 16 << i, 8 << i, 127);
  97. do_test (8 - i, 2 * i, i, 4 << i, 8 << i, 127);
  98. do_test (8 - i, 2 * i, i, 16 << i, 8 << i, 127);
  99. }
  100. return ret;
  101. }
  102. #include <support/test-driver.c>