vector_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. extern "C" {
  2. extern void vector_test();
  3. }
  4. #include "allocator.h"
  5. #include <dsppp/arch.hpp>
  6. #include <dsppp/fixed_point.hpp>
  7. #include <dsppp/matrix.hpp>
  8. #include <iostream>
  9. #include <cmsis_tests.h>
  10. template<typename T,int NB>
  11. static void test()
  12. {
  13. std::cout << "----\r\n" << "N = " << NB << "\r\n";
  14. #if defined(STATIC_TEST)
  15. PVector<T,NB> a;
  16. PVector<T,NB> b;
  17. #else
  18. PVector<T> a(NB);
  19. PVector<T> b(NB);
  20. #endif
  21. init_array(a,NB);
  22. init_array(b,NB);
  23. INIT_SYSTICK;
  24. START_CYCLE_MEASUREMENT;
  25. startSectionNB(1);
  26. #if defined(STATIC_TEST)
  27. PVector<T,NB> res = a + b;
  28. #else
  29. PVector<T> res = copy(a + b);
  30. #endif
  31. stopSectionNB(1);
  32. STOP_CYCLE_MEASUREMENT;
  33. INIT_SYSTICK
  34. START_CYCLE_MEASUREMENT;
  35. #if defined(STATIC_TEST)
  36. PVector<T,NB> ref;
  37. #else
  38. PVector<T> ref(NB);
  39. #endif
  40. cmsisdsp_add(a.const_ptr(),b.const_ptr(),ref.ptr(),NB);
  41. STOP_CYCLE_MEASUREMENT;
  42. if (!validate(res.const_ptr(),ref.const_ptr(),NB))
  43. {
  44. printf("add failed \r\n");
  45. }
  46. std::cout << "=====\r\n";
  47. }
  48. template<typename T,int NB>
  49. void test_view()
  50. {
  51. std::cout << "----\r\n" << "N = " << NB << "\r\n";
  52. #if defined(STATIC_TEST)
  53. PVector<T,NB> a;
  54. PVector<T,NB> b;
  55. #else
  56. PVector<T> a(NB);
  57. PVector<T> b(NB);
  58. #endif
  59. init_array(a,NB);
  60. init_array(b,NB);
  61. //std::cout << a;
  62. //std::cout << "\r\n";
  63. //std::cout << PVector<T,NB/2>(a.template sub<2>());
  64. INIT_SYSTICK;
  65. START_CYCLE_MEASUREMENT;
  66. startSectionNB(1);
  67. #if defined(STATIC_TEST)
  68. PVector<T,NB/2> res = a.template sub<2>() + b.template sub<2>();
  69. #else
  70. PVector<T> res = a.template sub<2>() + b.template sub<2>();
  71. #endif
  72. stopSectionNB(1);
  73. STOP_CYCLE_MEASUREMENT;
  74. PVector<T,NB/2> ref;
  75. INIT_SYSTICK;
  76. START_CYCLE_MEASUREMENT;
  77. PVector<T,NB/2> da(a.template sub<2>());
  78. PVector<T,NB/2> db(b.template sub<2>());
  79. cmsisdsp_add(da.const_ptr(),db.const_ptr(),ref.ptr(),NB/2);
  80. STOP_CYCLE_MEASUREMENT;
  81. if (!validate(res.const_ptr(),ref.const_ptr(),NB/2))
  82. {
  83. printf("add failed \r\n");
  84. }
  85. std::cout << "=====\r\n";
  86. }
  87. template<typename T,int NB>
  88. void test_fill()
  89. {
  90. std::cout << "----\r\n" << "N = " << NB << "\r\n";
  91. INIT_SYSTICK;
  92. START_CYCLE_MEASUREMENT;
  93. startSectionNB(1);
  94. #if defined(STATIC_TEST)
  95. PVector<T,NB> res(T(1));
  96. #else
  97. PVector<T> res(NB,T(1));
  98. #endif
  99. stopSectionNB(1);
  100. STOP_CYCLE_MEASUREMENT;
  101. }
  102. template<typename T>
  103. void all_vector_test()
  104. {
  105. const int nb_tails = TailForTests<T>::tail;
  106. const int nb_loops = TailForTests<T>::loop;
  107. title<T>("Vector");
  108. // For benchmarks
  109. test<T,NBVEC_4>();
  110. test<T,NBVEC_8>();
  111. test<T,NBVEC_9>();
  112. test<T,NBVEC_16>();
  113. test<T,NBVEC_32>();
  114. test<T,NBVEC_64>();
  115. test<T,NBVEC_128>();
  116. test<T,NBVEC_256>();
  117. test<T,NBVEC_258>();
  118. test<T,NBVEC_512>();
  119. test<T,NBVEC_1024>();
  120. test<T,NBVEC_2048>();
  121. // For tests
  122. test<T,1>();
  123. test<T,nb_tails>();
  124. test<T,nb_loops>();
  125. test<T,nb_loops+1>();
  126. test<T,nb_loops+nb_tails>();
  127. title<T>("Vector View");
  128. test_view<T,NBVEC_4>();
  129. test_view<T,NBVEC_8>();
  130. test_view<T,NBVEC_16>();
  131. test_view<T,NBVEC_32>();
  132. test_view<T,NBVEC_64>();
  133. if constexpr (nb_tails>1)
  134. {
  135. test_view<T,nb_tails>();
  136. }
  137. test_view<T,nb_loops>();
  138. test_view<T,nb_loops+1>();
  139. test_view<T,nb_loops+nb_tails>();
  140. title<T>("Vector fill");
  141. test_fill<T,NBVEC_4>();
  142. test_fill<T,NBVEC_8>();
  143. test_fill<T,NBVEC_16>();
  144. test_fill<T,NBVEC_32>();
  145. test_fill<T,NBVEC_64>();
  146. test_fill<T,1>();
  147. test_fill<T,nb_tails>();
  148. test_fill<T,nb_loops>();
  149. test_fill<T,nb_loops+1>();
  150. test_fill<T,nb_loops+nb_tails>();
  151. }
  152. void vector_test()
  153. {
  154. #if defined(VECTOR_TEST)
  155. #if defined(F64_DT)
  156. all_vector_test<double>();
  157. #endif
  158. #if defined(F32_DT)
  159. all_vector_test<float>();
  160. #endif
  161. #if defined(F16_DT) && !defined(DISABLEFLOAT16)
  162. all_vector_test<float16_t>();
  163. #endif
  164. #if defined(Q31_DT)
  165. all_vector_test<Q31>();
  166. #endif
  167. #if defined(Q15_DT)
  168. all_vector_test<Q15>();
  169. #endif
  170. #if defined(Q7_DT)
  171. all_vector_test<Q7>();
  172. #endif
  173. #endif
  174. }