bench-hash-funcs-kernel.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Actual benchmark kernels used by bench-hash-funcs.h
  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. #include "bench-util.h"
  16. /* We go through the trouble of using macros here because many of the
  17. hash functions are meant to be inlined so its not fair to benchmark
  18. them with a function pointer where they won't be inlinable. */
  19. #undef RUN_FUNC
  20. #undef POSTFIX
  21. #ifdef SIMPLE
  22. # define RUN_FUNC SIMPLE_TEST_FUNC
  23. # define POSTFIX _simple
  24. #else
  25. # define RUN_FUNC TEST_FUNC
  26. # define POSTFIX _optimized
  27. #endif
  28. #define PRIMITIVE_CAT(x, y) x ## y
  29. #define CAT(x, y) PRIMITIVE_CAT (x, y)
  30. static double __attribute_optimization_barrier__
  31. CAT (do_one_test_kernel, POSTFIX) (const char *s, size_t len)
  32. {
  33. unsigned int iters;
  34. timing_t start, stop, cur;
  35. /* Warmup. */
  36. for (iters = NFIXED_ITERS / 32; iters; --iters)
  37. DO_NOT_OPTIMIZE_OUT (RUN_FUNC (s, len));
  38. TIMING_NOW (start);
  39. for (iters = NFIXED_ITERS; iters; --iters)
  40. DO_NOT_OPTIMIZE_OUT (RUN_FUNC (s, len));
  41. TIMING_NOW (stop);
  42. TIMING_DIFF (cur, start, stop);
  43. (void) (len);
  44. return (double) cur / (double) NFIXED_ITERS;
  45. }
  46. static double __attribute_optimization_barrier__
  47. CAT (do_rand_test_kernel, POSTFIX) (char const *bufs,
  48. unsigned int const *sizes)
  49. {
  50. unsigned int i, iters;
  51. size_t offset;
  52. timing_t start, stop, cur;
  53. /* Warmup. */
  54. for (i = 0, offset = 0; i < NRAND_BUFS; ++i, offset += RAND_BENCH_MAX_LEN)
  55. DO_NOT_OPTIMIZE_OUT (RUN_FUNC (bufs + offset, sizes[i]));
  56. TIMING_NOW (start);
  57. for (iters = NRAND_ITERS; iters; --iters)
  58. {
  59. for (i = 0, offset = 0; i < NRAND_BUFS;
  60. ++i, offset += RAND_BENCH_MAX_LEN)
  61. DO_NOT_OPTIMIZE_OUT (RUN_FUNC (bufs + offset, sizes[i]));
  62. }
  63. TIMING_NOW (stop);
  64. TIMING_DIFF (cur, start, stop);
  65. (void) (sizes);
  66. return (double) cur / (double) (NRAND_ITERS * NRAND_BUFS);
  67. }