loop_unroll.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Macro for explicit loop unrolling.
  2. Copyright (C) 2019-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. #ifndef _LOOP_UNROLL_H
  16. #define _LOOP_UNROLL_H
  17. /* Loop unroll macro to be used for explicit force loop unrolling with a
  18. configurable number of iterations. The idea is to make the loop unrolling
  19. independent of whether the compiler is able to unroll through specific
  20. optimizations options (-funroll-loops or -funroll-all-loops).
  21. For instance, to implement strcpy with SRC being the source input and
  22. DEST the destination buffer, it is expected the macro to be used in this
  23. way:
  24. #define ITERATION(index) \
  25. ({ char c = *str++; *dest++ = c; c != '\0' })
  26. while (1)
  27. UNROLL_REPEAT (4, ITERATION)
  28. The loop will be manually unrolled 4 times. Another option is to do
  29. the index update after the tests:
  30. #define ITERATION(index) \
  31. ({ char c = *(str + index); *(dest + index) = c; c != '\0' })
  32. #define UPDATE(n) \
  33. str += n; dst += n
  34. while (1)
  35. UNROLL_REPEAT_UPDATE (4, ITERATION, UPDATE)
  36. The loop will be manually unrolled 4 times and the SRC and DEST pointers
  37. will be updated only after the last iteration.
  38. Currently, both macros unroll the loop 8 times at maximum. */
  39. #define UNROLL_REPEAT_1(X) if (!X(0)) break;
  40. #define UNROLL_REPEAT_2(X) UNROLL_REPEAT_1 (X) if (!X (1)) break;
  41. #define UNROLL_REPEAT_3(X) UNROLL_REPEAT_2 (X) if (!X (2)) break;
  42. #define UNROLL_REPEAT_4(X) UNROLL_REPEAT_3 (X) if (!X (3)) break;
  43. #define UNROLL_REPEAT_5(X) UNROLL_REPEAT_4 (X) if (!X (4)) break;
  44. #define UNROLL_REPEAT_6(X) UNROLL_REPEAT_5 (X) if (!X (5)) break;
  45. #define UNROLL_REPEAT_7(X) UNROLL_REPEAT_6 (X) if (!X (6)) break;
  46. #define UNROLL_REPEAT_8(X) UNROLL_REPEAT_7 (X) if (!X (7)) break;
  47. #define UNROLL_EXPAND(...) __VA_ARGS__
  48. #define UNROLL_REPEAT__(N, X) UNROLL_EXPAND(UNROLL_REPEAT_ ## N) (X)
  49. #define UNROLL_REPEAT_(N, X) UNROLL_REPEAT__ (N, X)
  50. #define UNROLL_REPEAT(N, X) \
  51. (void) ({ \
  52. UNROLL_REPEAT_ (UNROLL_EXPAND(N), X); \
  53. })
  54. #define UNROLL_REPEAT_UPDATE(N, X, U) \
  55. (void) ({ \
  56. UNROLL_REPEAT_ (UNROLL_EXPAND(N), X); \
  57. UPDATE (N); \
  58. })
  59. #endif