optimize.hh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Naive optimization pass over expression templates.
  3. // (c) Daniel Llorens - 2015-2023
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #pragma once
  9. #include "small.hh"
  10. namespace ra {
  11. template <class E> constexpr decltype(auto) optimize(E && e) { return std::forward<E>(e); }
  12. // FIXME only reduces iota exprs as op'ed on in ra.hh (operators), not a tree like WithLen does.
  13. #if RA_DO_OPT_IOTA==1
  14. // TODO maybe don't opt iota(int)*real -> iota(real) since a+a+... != n*a
  15. template <class X> constexpr bool iota_op = ra::is_zero_or_scalar<X> && std::is_arithmetic_v<value_t<X>>;
  16. // TODO need something to handle the & variants...
  17. #define ITEM(i) std::get<(i)>(e.t)
  18. // FIXME gets() vs p2781r2
  19. // the qualifier ra::iota is necessary not to pick std::iota through ADL (test/headers.cc).
  20. // --------------
  21. // plus
  22. // --------------
  23. template <class I, class J> requires (is_iota<I> && iota_op<J>)
  24. constexpr auto
  25. optimize(Expr<std::plus<>, std::tuple<I, J>> && e)
  26. {
  27. return ra::iota(ITEM(0).n, ITEM(0).i+ITEM(1), ITEM(0).s);
  28. }
  29. template <class I, class J> requires (iota_op<I> && is_iota<J>)
  30. constexpr auto
  31. optimize(Expr<std::plus<>, std::tuple<I, J>> && e)
  32. {
  33. return ra::iota(ITEM(1).n, ITEM(0)+ITEM(1).i, ITEM(1).s);
  34. }
  35. template <class I, class J> requires (is_iota<I> && is_iota<J>)
  36. constexpr auto
  37. optimize(Expr<std::plus<>, std::tuple<I, J>> && e)
  38. {
  39. return ra::iota(maybe_len(e), ITEM(0).i+ITEM(1).i, ITEM(0).gets()+ITEM(1).gets());
  40. }
  41. // --------------
  42. // minus
  43. // --------------
  44. template <class I, class J> requires (is_iota<I> && iota_op<J>)
  45. constexpr auto
  46. optimize(Expr<std::minus<>, std::tuple<I, J>> && e)
  47. {
  48. return ra::iota(ITEM(0).n, ITEM(0).i-ITEM(1), ITEM(0).s);
  49. }
  50. template <class I, class J> requires (iota_op<I> && is_iota<J>)
  51. constexpr auto
  52. optimize(Expr<std::minus<>, std::tuple<I, J>> && e)
  53. {
  54. return ra::iota(ITEM(1).n, ITEM(0)-ITEM(1).i, -ITEM(1).s);
  55. }
  56. template <class I, class J> requires (is_iota<I> && is_iota<J>)
  57. constexpr auto
  58. optimize(Expr<std::minus<>, std::tuple<I, J>> && e)
  59. {
  60. return ra::iota(maybe_len(e), ITEM(0).i-ITEM(1).i, ITEM(0).gets()-ITEM(1).gets());
  61. }
  62. // --------------
  63. // times
  64. // --------------
  65. template <class I, class J> requires (is_iota<I> && iota_op<J>)
  66. constexpr auto
  67. optimize(Expr<std::multiplies<>, std::tuple<I, J>> && e)
  68. {
  69. return ra::iota(ITEM(0).n, ITEM(0).i*ITEM(1), ITEM(0).gets()*ITEM(1));
  70. }
  71. template <class I, class J> requires (iota_op<I> && is_iota<J>)
  72. constexpr auto
  73. optimize(Expr<std::multiplies<>, std::tuple<I, J>> && e)
  74. {
  75. return ra::iota(ITEM(1).n, ITEM(0)*ITEM(1).i, ITEM(0)*ITEM(1).gets());
  76. }
  77. // --------------
  78. // negate
  79. // --------------
  80. template <class I> requires (is_iota<I>)
  81. constexpr auto
  82. optimize(Expr<std::negate<>, std::tuple<I>> && e)
  83. {
  84. return ra::iota(ITEM(0).n, -ITEM(0).i, -ITEM(0).gets());
  85. }
  86. #endif // RA_DO_OPT_IOTA
  87. #if RA_DO_OPT_SMALLVECTOR==1
  88. // FIXME find a way to peel qualifiers from parameter type of start(), to ignore SmallBase<SmallArray> vs SmallBase<SmallView> or const vs nonconst.
  89. template <class A, class T, dim_t N> constexpr bool match_smallvector =
  90. std::is_same_v<std::decay_t<A>, typename ra::Small<T, N>::template iterator<0>>
  91. || std::is_same_v<std::decay_t<A>, typename ra::Small<T, N>::template const_iterator<0>>;
  92. static_assert(match_smallvector<ra::CellSmall<double, ic_t<std::array { Dim { 4, 1 } }>, 0>, double, 4>);
  93. #define RA_OPT_SMALLVECTOR_OP(OP, NAME, T, N) \
  94. template <class A, class B> \
  95. requires (match_smallvector<A, T, N> && match_smallvector<B, T, N>) \
  96. constexpr auto \
  97. optimize(ra::Expr<NAME, std::tuple<A, B>> && e) \
  98. { \
  99. alignas (alignof(extvector<T, N>)) ra::Small<T, N> val; \
  100. *(extvector<T, N> *)(&val) = *(extvector<T, N> *)((ITEM(0).c.cp)) OP *(extvector<T, N> *)((ITEM(1).c.cp)); \
  101. return val; \
  102. }
  103. #define RA_OPT_SMALLVECTOR_OP_FUNS(T, N) \
  104. static_assert(0==alignof(ra::Small<T, N>) % alignof(extvector<T, N>)); \
  105. RA_OPT_SMALLVECTOR_OP(+, std::plus<>, T, N) \
  106. RA_OPT_SMALLVECTOR_OP(-, std::minus<>, T, N) \
  107. RA_OPT_SMALLVECTOR_OP(/, std::divides<>, T, N) \
  108. RA_OPT_SMALLVECTOR_OP(*, std::multiplies<>, T, N)
  109. #define RA_OPT_SMALLVECTOR_OP_SIZES(T) \
  110. RA_OPT_SMALLVECTOR_OP_FUNS(T, 2) \
  111. RA_OPT_SMALLVECTOR_OP_FUNS(T, 4) \
  112. RA_OPT_SMALLVECTOR_OP_FUNS(T, 8)
  113. RA_OPT_SMALLVECTOR_OP_SIZES(double)
  114. RA_OPT_SMALLVECTOR_OP_SIZES(float)
  115. #undef RA_OPT_SMALLVECTOR_OP_SIZES
  116. #undef RA_OPT_SMALLVECTOR_OP_FUNS
  117. #undef RA_OPT_SMALLVECTOR_OP_OP
  118. #endif // RA_DO_OPT_SMALLVECTOR
  119. #undef ITEM
  120. } // namespace ra