optimize.hh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 RA_FWD(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 something to handle the & variants...
  17. #define ITEM(i) std::get<(i)>(e.t)
  18. // FIXME gets() vs p2781r2
  19. // qualified ra::iota is necessary not to pick std::iota through ADL (test/headers.cc).
  20. // plus
  21. template <class I, class J> requires (is_iota<I> && iota_op<J>)
  22. constexpr auto
  23. optimize(Expr<std::plus<>, std::tuple<I, J>> && e)
  24. {
  25. return ra::iota(ITEM(0).n, ITEM(0).i+ITEM(1), ITEM(0).s);
  26. }
  27. template <class I, class J> requires (iota_op<I> && is_iota<J>)
  28. constexpr auto
  29. optimize(Expr<std::plus<>, std::tuple<I, J>> && e)
  30. {
  31. return ra::iota(ITEM(1).n, ITEM(0)+ITEM(1).i, ITEM(1).s);
  32. }
  33. template <class I, class J> requires (is_iota<I> && is_iota<J>)
  34. constexpr auto
  35. optimize(Expr<std::plus<>, std::tuple<I, J>> && e)
  36. {
  37. return ra::iota(maybe_len(e), ITEM(0).i+ITEM(1).i, ITEM(0).gets()+ITEM(1).gets());
  38. }
  39. // minus
  40. template <class I, class J> requires (is_iota<I> && iota_op<J>)
  41. constexpr auto
  42. optimize(Expr<std::minus<>, std::tuple<I, J>> && e)
  43. {
  44. return ra::iota(ITEM(0).n, ITEM(0).i-ITEM(1), ITEM(0).s);
  45. }
  46. template <class I, class J> requires (iota_op<I> && is_iota<J>)
  47. constexpr auto
  48. optimize(Expr<std::minus<>, std::tuple<I, J>> && e)
  49. {
  50. return ra::iota(ITEM(1).n, ITEM(0)-ITEM(1).i, -ITEM(1).s);
  51. }
  52. template <class I, class J> requires (is_iota<I> && is_iota<J>)
  53. constexpr auto
  54. optimize(Expr<std::minus<>, std::tuple<I, J>> && e)
  55. {
  56. return ra::iota(maybe_len(e), ITEM(0).i-ITEM(1).i, ITEM(0).gets()-ITEM(1).gets());
  57. }
  58. // times
  59. template <class I, class J> requires (is_iota<I> && iota_op<J>)
  60. constexpr auto
  61. optimize(Expr<std::multiplies<>, std::tuple<I, J>> && e)
  62. {
  63. return ra::iota(ITEM(0).n, ITEM(0).i*ITEM(1), ITEM(0).gets()*ITEM(1));
  64. }
  65. template <class I, class J> requires (iota_op<I> && is_iota<J>)
  66. constexpr auto
  67. optimize(Expr<std::multiplies<>, std::tuple<I, J>> && e)
  68. {
  69. return ra::iota(ITEM(1).n, ITEM(0)*ITEM(1).i, ITEM(0)*ITEM(1).gets());
  70. }
  71. // negate
  72. template <class I> requires (is_iota<I>)
  73. constexpr auto
  74. optimize(Expr<std::negate<>, std::tuple<I>> && e)
  75. {
  76. return ra::iota(ITEM(0).n, -ITEM(0).i, -ITEM(0).gets());
  77. }
  78. #endif // RA_DO_OPT_IOTA
  79. #if RA_DO_OPT_SMALLVECTOR==1
  80. // FIXME peel qualifiers from start() parameter, to ignore SmallBase<SmallArray> vs SmallBase<SmallView> or const vs nonconst.
  81. template <class A, class T, dim_t N> constexpr bool match_smallvector =
  82. std::is_same_v<std::decay_t<A>, typename ra::Small<T, N>::template iterator<0>>
  83. || std::is_same_v<std::decay_t<A>, typename ra::Small<T, N>::template const_iterator<0>>;
  84. static_assert(match_smallvector<ra::CellSmall<double, ic_t<std::array { Dim { 4, 1 } }>, 0>, double, 4>);
  85. #define RA_OPT_SMALLVECTOR_OP(OP, NAME, T, N) \
  86. template <class A, class B> \
  87. requires (match_smallvector<A, T, N> && match_smallvector<B, T, N>) \
  88. constexpr auto \
  89. optimize(ra::Expr<NAME, std::tuple<A, B>> && e) \
  90. { \
  91. alignas (alignof(extvector<T, N>)) ra::Small<T, N> val; \
  92. *(extvector<T, N> *)(&val) = *(extvector<T, N> *)((ITEM(0).c.cp)) OP *(extvector<T, N> *)((ITEM(1).c.cp)); \
  93. return val; \
  94. }
  95. #define RA_OPT_SMALLVECTOR_OP_FUNS(T, N) \
  96. static_assert(0==alignof(ra::Small<T, N>) % alignof(extvector<T, N>)); \
  97. RA_OPT_SMALLVECTOR_OP(+, std::plus<>, T, N) \
  98. RA_OPT_SMALLVECTOR_OP(-, std::minus<>, T, N) \
  99. RA_OPT_SMALLVECTOR_OP(/, std::divides<>, T, N) \
  100. RA_OPT_SMALLVECTOR_OP(*, std::multiplies<>, T, N)
  101. #define RA_OPT_SMALLVECTOR_OP_SIZES(T) \
  102. RA_OPT_SMALLVECTOR_OP_FUNS(T, 2) \
  103. RA_OPT_SMALLVECTOR_OP_FUNS(T, 4) \
  104. RA_OPT_SMALLVECTOR_OP_FUNS(T, 8)
  105. FOR_EACH(RA_OPT_SMALLVECTOR_OP_SIZES, float, double)
  106. #undef RA_OPT_SMALLVECTOR_OP_SIZES
  107. #undef RA_OPT_SMALLVECTOR_OP_FUNS
  108. #undef RA_OPT_SMALLVECTOR_OP_OP
  109. #endif // RA_DO_OPT_SMALLVECTOR
  110. #undef ITEM
  111. } // namespace ra