optimize.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Check that ra::optimize() does what it's supposed to do.
  3. // (c) Daniel Llorens - 2014-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. #define RA_OPT // disable so we can compare with (forced) and without
  9. #ifndef RA_DO_OPT_SMALLVECTOR // test is for 1; forcing 0 skips that part of the test.
  10. #define RA_DO_OPT_SMALLVECTOR 1
  11. #endif
  12. #include "ra/test.hh"
  13. #include "mpdebug.hh"
  14. using std::cout, std::endl, ra::TestRecorder;
  15. using complex = std::complex<double>;
  16. int main()
  17. {
  18. TestRecorder tr(std::cout);
  19. tr.section("optimizing static size Iotas");
  20. {
  21. tr.test_eq(4, optimize(ra::iota(ra::int_c<4>()) + ra::iota(4, 0, 2)).nn);
  22. tr.test_eq(4, optimize(ra::iota(4) + ra::iota(ra::int_c<4>(), 0, 2)).nn);
  23. tr.test_eq(ra::start({0, 3, 6, 9}), optimize(ra::iota(ra::int_c<4>()) + ra::iota(4, 0, 2)));
  24. tr.test_eq(4, optimize(ra::iota(ra::int_c<4>()) - ra::iota(4, 0, 2)).nn);
  25. tr.test_eq(4, optimize(ra::iota(4) - ra::iota(ra::int_c<4>(), 0, 2)).nn);
  26. tr.test_eq(ra::start({0, -1, -2, -3}), optimize(ra::iota(4) - ra::iota(ra::int_c<4>(), 0, 2)));
  27. }
  28. tr.section("Iota ops, expr Iotas WIP");
  29. {
  30. tr.info("naked").test(ra::is_iota<decltype(ra::iota(ra::len))>);
  31. tr.info("nop").test(ra::is_iota<decltype(wlen(10, ra::iota(ra::len)))>);
  32. // works unopt bc Match avoid checking if has_len
  33. tr.test_eq(ra::iota(10, 0, 2), wlen(10, ra::iota(ra::len) + ra::iota(ra::len)));
  34. // works, but opt runs at + site, not after wlen (FIXME?)
  35. tr.test_eq(ra::iota(10, 0, 2), optimize(wlen(10, ra::iota(ra::len) + ra::iota(ra::len))));
  36. // FIXME don't work, because optimize() can't determine the match-length of the result iota
  37. // tr.info("+, naked").test(ra::is_iota<decltype(optimize(ra::iota(ra::len) + ra::iota(ra::len)))>);
  38. // tr.info("+").test(ra::is_iota<decltype(wlen(10, optimize(ra::iota(ra::len) + ra::iota(ra::len))))>);
  39. // tr.test_eq(ra::iota(10, 0, 2), wlen(10, optimize(ra::iota(ra::len) + ra::iota(ra::len))));
  40. }
  41. tr.section("misc/sanity");
  42. {
  43. cout << ra::is_iota<ra::Len> << endl;
  44. tr.test_eq(ra::iota(4, 1, 2), ra::Big<int, 1> {1, 3, 5, 7});
  45. {
  46. auto z = ra::iota(5, 1.5);
  47. tr.info("iota with real org I").test_eq(1.5, z.i);
  48. tr.info("iota with complex org I").test_eq(1.5+ra::start({0, 1, 2, 3, 4}), z);
  49. }
  50. {
  51. auto z = optimize(ra::iota(5, complex(1., 1.)));
  52. tr.info("iota with complex org I").test_eq(complex(1., 1.), z.i);
  53. tr.info("iota with complex org II").test_eq(complex(1., 1.)+ra::start({0., 1., 2., 3., 4.}), z);
  54. }
  55. {
  56. auto i = ra::iota(5);
  57. auto l = optimize(i*i);
  58. tr.info("optimize is nop by default").test_eq(ra::start({0, 1, 4, 9, 16}), l);
  59. }
  60. {
  61. auto i = ra::iota(5);
  62. auto j = i*3.;
  63. tr.info("ops with non-integers don't reduce iota by default").test(!std::is_same_v<decltype(i), decltype(j)>);
  64. }
  65. }
  66. tr.section("Iota ops, plus");
  67. {
  68. static_assert(ra::iota_op<ra::Scalar<int>>);
  69. static_assert(ra::is_iota<decltype(ra::iota(10, long(10)))>);
  70. auto test = [&tr](auto && org)
  71. {
  72. auto i = ra::iota(5, org);
  73. auto j = i+1;
  74. auto k1 = optimize(i+1);
  75. static_assert(ra::is_iota<decltype(k1)>);
  76. auto k2 = optimize(1+i);
  77. auto k3 = optimize(ra::iota(5)+1);
  78. auto k4 = optimize(1+ra::iota(5));
  79. auto k5 = optimize(1.5+ra::iota(5));
  80. auto k6 = optimize(ra::iota(5)-0.5);
  81. tr.info("not optimized w/ blank RA_OPT").test(!std::is_same_v<decltype(i), decltype(j)>);
  82. // it's actually a Iota
  83. tr.test_eq(org+1, k1.i);
  84. tr.test_eq(org+1, k1.i);
  85. tr.test_eq(org+1, k2.i);
  86. tr.test_eq(org+1, k3.i);
  87. tr.test_eq(org+1, k4.i);
  88. tr.test_eq(org+1.5, k5.i);
  89. tr.test_eq(org-0.5, k6.i);
  90. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), j);
  91. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k1);
  92. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k2);
  93. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k3);
  94. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k4);
  95. tr.test_eq(1.5+ra::start({0, 1, 2, 3, 4}), k5);
  96. tr.test_eq(ra::start({0, 1, 2, 3, 4})-0.5, k6);
  97. };
  98. test(int(0));
  99. test(double(0));
  100. test(float(0));
  101. }
  102. tr.section("Iota ops, negate");
  103. {
  104. auto test = [&tr](auto && org)
  105. {
  106. auto i = ra::iota(5, org);
  107. auto j = -i;
  108. auto k1 = optimize(-i);
  109. static_assert(ra::is_iota<decltype(k1)>);
  110. tr.info("not optimized w/ blank RA_OPT").test(!std::is_same_v<decltype(i), decltype(j)>);
  111. // it's actually a Iota
  112. tr.test_eq(-org, k1.i);
  113. tr.test_eq(-ra::start({0, 1, 2, 3, 4}), j);
  114. tr.test_eq(-ra::start({0, 1, 2, 3, 4}), k1);
  115. };
  116. test(int(0));
  117. test(double(0));
  118. test(float(0));
  119. }
  120. tr.section("Iota ops, multiplies");
  121. {
  122. auto test = [&tr](auto && org)
  123. {
  124. auto i = ra::iota(5, org);
  125. auto j = i*2;
  126. auto k1 = optimize(i*2);
  127. auto k2 = optimize(2*i);
  128. auto k3 = optimize(ra::iota(5)*2);
  129. auto k4 = optimize(2*ra::iota(5));
  130. tr.info("not optimized w/ blank RA_OPT").test(!std::is_same_v<decltype(i), decltype(j)>);
  131. // it's actually a Iota
  132. tr.test_eq(0, k1.i);
  133. tr.test_eq(0, k2.i);
  134. tr.test_eq(0, k3.i);
  135. tr.test_eq(0, k4.i);
  136. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), j);
  137. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k1);
  138. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k2);
  139. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k3);
  140. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k4);
  141. };
  142. test(int(0));
  143. test(double(0));
  144. test(float(0));
  145. }
  146. #if RA_DO_OPT_SMALLVECTOR==1
  147. tr.section("small vector ops through vector extensions");
  148. {
  149. using Vec = ra::Small<double, 4>;
  150. Vec const r {6, 8, 10, 12};
  151. // [ra4] Expr holds iterators which hold pointers so auto y = Vec {1, 2, 3, 4} + Vec {5, 6, 7, 8} would hold pointers to lost temps. This is revealed by gcc 6.2. Cf ra::start(iter). So this example only works bc it's optimized.
  152. auto x = optimize(Vec {1, 2, 3, 4} + Vec {5, 6, 7, 8});
  153. tr.info("optimization rvalue terms").test(std::is_same_v<decltype(x), Vec>);
  154. tr.test_eq(r, x);
  155. Vec a {1, 2, 3, 4}, b {5, 6, 7, 8};
  156. auto y = a + b;
  157. auto z = optimize(a + b);
  158. tr.info("optimization of lvalue terms").test(std::is_same_v<decltype(z), Vec>);
  159. tr.info("not optimized by default, yet").test(!std::is_same_v<decltype(y), Vec>);
  160. tr.test_eq(r, y);
  161. tr.test_eq(r, z);
  162. auto q = optimize(a + r);
  163. tr.info("optimization of const lvalue terms").test(std::is_same_v<decltype(q), Vec>);
  164. tr.test_eq(ra::start({7, 10, 13, 16}), q);
  165. ra::Small<double, 4, 4> c = 1 + ra::_1;
  166. auto d = optimize(c(0) + b);
  167. tr.info("optimization of view").test(std::is_same_v<decltype(d), Vec>);
  168. tr.test_eq(r, d);
  169. }
  170. tr.section("small vector ops through vector extensions, other types / sizes");
  171. {
  172. ra::Small<double, 8> a = 1 + ra::_0;
  173. ra::Small<double, 4, 8> b = 33 - ra::_1;
  174. auto c = optimize(a + b(3));
  175. tr.info("optimization of view").test(std::is_same_v<decltype(c), ra::Small<double, 8>>);
  176. tr.test_eq(34, c);
  177. }
  178. #endif // RA_DO_OPT_SMALLVECTOR==1
  179. return tr.summary();
  180. }