common_tests.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #pragma once
  2. extern "C" {
  3. #include "bench.h"
  4. #include "test.h"
  5. }
  6. #include <type_traits>
  7. #include "allocator.h"
  8. using namespace arm_cmsis_dsp;
  9. #define REL_ERROR (1.0e-6)
  10. #define ABS_ERROR (1.0e-6)
  11. #define ERROR(A,B,AE,RE) ((fabs((A) - (B)) > (AE + RE * fabs((B)))))
  12. #define ERRVAL(VAL,REF,AE,RE) \
  13. std::cout << "Error = " << fabs(VAL-REF) << "\r\n"; \
  14. std::cout << "compared to " << (AE + RE * abs((REF))) << "\r\n";
  15. /************
  16. *
  17. * Data types
  18. *
  19. */
  20. #if defined(POOL_ALLOCATOR)
  21. template<typename P,int L=arm_cmsis_dsp::DYNAMIC>
  22. using PVector = Vector<P,L,pool_allocator>;
  23. template<typename P,int R=arm_cmsis_dsp::DYNAMIC,int C=arm_cmsis_dsp::DYNAMIC>
  24. using PMat = Matrix<P,R,C,pool_allocator>;
  25. #else
  26. template<typename P,int L=arm_cmsis_dsp::DYNAMIC>
  27. using PVector = Vector<P,L,stat_allocator>;
  28. template<typename P,int R=arm_cmsis_dsp::DYNAMIC,int C=arm_cmsis_dsp::DYNAMIC>
  29. using PMat = Matrix<P,R,C,stat_allocator>;
  30. #endif
  31. template<typename P,int stride=1>
  32. using PView = VectorView<P,stride>;
  33. template<typename T,
  34. int L,
  35. template<int> typename A>
  36. void init_array(Vector<T,L,A> &pDst,std::size_t nb)
  37. {
  38. for(std::size_t i=0;i<nb;i++)
  39. {
  40. pDst[i] = T(i);
  41. }
  42. }
  43. #if !defined(DISABLEFLOAT16)
  44. template<int L,
  45. template<int> typename A>
  46. void init_array(Vector<float16_t,L,A> &pDst,std::size_t nb)
  47. {
  48. for(std::size_t i=0;i<nb;i++)
  49. {
  50. // Scaled down to prevent saturations in the tests
  51. pDst[i] = (float16_t)i*0.001;
  52. }
  53. }
  54. #endif
  55. template<typename T>
  56. void init_array(Vector_Base<T> &pDst,std::size_t nb)
  57. {
  58. for(std::size_t i=0;i<nb;i++)
  59. {
  60. pDst[i] = T(i);
  61. }
  62. }
  63. //template<int L,
  64. // template<int> typename A>
  65. //void init_array(Vector<float16_t,L,A> &pDst,std::size_t nb);
  66. //extern template void init_array<>(Vector_Base<float16_t> &pDst,std::size_t nb);
  67. template<typename T,
  68. typename std::enable_if<std::is_pointer<T>::value,bool>::type = true>
  69. bool validate(const T a, const T b, std::size_t nb,float abser = ABS_ERROR, float reler = REL_ERROR)
  70. {
  71. for(std::size_t i=0;i<nb;i++)
  72. {
  73. if constexpr (number_traits<std::remove_cv_t<std::remove_reference_t<decltype(a[0])>>>::is_float)
  74. {
  75. if (ERROR(a[i],b[i],abser,reler) )
  76. {
  77. std::cout << "Error at:" << i << " ; res=" << a[i] << " ; ref=" << b[i] << "\r\n";
  78. ERRVAL(a[i],b[i],abser,reler);
  79. return(false);
  80. }
  81. }
  82. else
  83. {
  84. if (a[i]!=b[i])
  85. {
  86. std::cout << "Error at:" << i << " ; res=" << a[i] << " ; ref=" << b[i] << "\r\n";
  87. return(false);
  88. }
  89. }
  90. }
  91. return(true);
  92. }
  93. template<typename TA,typename TB,
  94. typename std::enable_if<IsVector<TA>::value &&
  95. !HasMatrixIndexing<TA>::value &&
  96. IsVector<TB>::value &&
  97. !HasMatrixIndexing<TB>::value,bool>::type = true>
  98. bool validate(const TA &a, const TB &b,float abser = ABS_ERROR, float reler = REL_ERROR)
  99. {
  100. for(index_t i=0;i<a.length();i++)
  101. {
  102. if constexpr (number_traits<typename ElementType<TA>::type>::is_float)
  103. {
  104. if (ERROR(a[i],b[i],abser,reler) )
  105. {
  106. std::cout << "Error at:" << i << " ; res=" << a[i] << " ; ref=" << b[i] << "\r\n";
  107. ERRVAL(a[i],b[i],abser,reler);
  108. return(false);
  109. }
  110. }
  111. else
  112. {
  113. if (a[i]!=b[i])
  114. {
  115. std::cout << "Error at:" << i << " ; res=" << a[i] << " ; ref=" << b[i] << "\r\n";
  116. return(false);
  117. }
  118. }
  119. }
  120. return(true);
  121. }
  122. template<typename T,
  123. typename std::enable_if<!std::is_pointer<T>::value
  124. && !IsVector<T>::value && !HasMatrixIndexing<T>::value,bool>::type = true>
  125. bool validate(const T a, const T b,float abser = ABS_ERROR, float reler = REL_ERROR)
  126. {
  127. if constexpr (number_traits<std::remove_cv_t<T>>::is_float)
  128. {
  129. if (ERROR(a,b,abser,reler))
  130. {
  131. std::cout << "Error: res=" << a << " ; ref=" << b << "\r\n";
  132. ERRVAL(a,b,abser,reler);
  133. return(false);
  134. }
  135. }
  136. else
  137. {
  138. if (a != b )
  139. {
  140. std::cout << "Error : res=" << a << " ; ref=" << b << "\r\n";
  141. return(false);
  142. }
  143. }
  144. return(true);
  145. }
  146. template<typename MA,typename MB,
  147. typename std::enable_if<
  148. HasMatrixIndexing<MA>::value &&
  149. HasMatrixIndexing<MB>::value &&
  150. number_traits<typename ElementType<MA>::type>::is_float,bool>::type = true>
  151. bool validate(const MA& a, const MB& b,float abser = ABS_ERROR, float reler = REL_ERROR)
  152. {
  153. for(index_t row=0;row < a.rows() ; row++)
  154. {
  155. for(index_t col=0;col < a.columns() ; col++)
  156. {
  157. if (ERROR(a(row,col),b(row,col),abser,reler) )
  158. {
  159. //std::cout << fabs(a(row,col)-b(row,col)) << "\r\n";
  160. //std::cout << REL_ERROR*fabsf(a(row,col)) << "\r\n";
  161. //std::cout << a(row,col) << "\r\n";
  162. //std::cout << b(row,col) << "\r\n";
  163. std::cout << "Error at : (" << row << "," << col << ") ; res=" << a(row,col) << " ; ref=" << b(row,col) << "\r\n";
  164. ERRVAL(a(row,col),b(row,col),abser,reler);
  165. return(false);
  166. }
  167. }
  168. }
  169. return(true);
  170. }
  171. template<typename MA,typename MB,
  172. typename std::enable_if<
  173. HasMatrixIndexing<MA>::value &&
  174. HasMatrixIndexing<MB>::value &&
  175. number_traits<typename ElementType<MA>::type>::is_float,bool>::type = true>
  176. bool validateLT(const MA& a, const MB& b,float abser = ABS_ERROR, float reler = REL_ERROR)
  177. {
  178. for(index_t row=0;row < a.rows() ; row++)
  179. {
  180. for(index_t col=0;col <= row ; col++)
  181. {
  182. if (ERROR(a(row,col),b(row,col),abser,reler) )
  183. {
  184. //std::cout << fabs(a(row,col)-b(row,col)) << "\r\n";
  185. //std::cout << REL_ERROR*fabsf(a(row,col)) << "\r\n";
  186. //std::cout << a(row,col) << "\r\n";
  187. //std::cout << b(row,col) << "\r\n";
  188. std::cout << "Error at : (" << row << "," << col << ") ; res=" << a(row,col) << " ; ref=" << b(row,col) << "\r\n";
  189. ERRVAL(a(row,col),b(row,col),abser,reler);
  190. return(false);
  191. }
  192. }
  193. }
  194. return(true);
  195. }
  196. template<typename MA,typename MB,
  197. typename std::enable_if<
  198. HasMatrixIndexing<MA>::value &&
  199. HasMatrixIndexing<MB>::value &&
  200. number_traits<typename ElementType<MA>::type>::is_fixed,bool>::type = true>
  201. bool validate(const MA& a, const MB& b,float abser = ABS_ERROR, float reler = REL_ERROR)
  202. {
  203. (void)abser;
  204. (void)reler;
  205. for(index_t row=0;row < a.rows() ; row++)
  206. {
  207. for(index_t col=0;col < a.columns() ; col++)
  208. {
  209. if (a(row,col).v != b(row,col).v)
  210. {
  211. std::cout << "Error at : (" << row << "," << col << ") ; res=" << a(row,col) << " ; ref=" << b(row,col) << "\r\n";
  212. std::cout << "Error = " << abs(a(row,col).v - b(row,col).v) << "\r\n";
  213. return(false);
  214. }
  215. }
  216. }
  217. return(true);
  218. }
  219. template<>
  220. bool validate(const float32_t* a, const float32_t* b, std::size_t nb,float abser , float reler );
  221. extern template
  222. bool validate<>(const float32_t* a, const float32_t* b, std::size_t nb,float abser , float reler );
  223. template<typename T>
  224. void title(const std::string &s)
  225. {
  226. #if !defined(SERIAL_DUMP)
  227. #if defined(STATIC_TEST)
  228. std::cout<<"\r\n\033[31;1;4m" << s << " " << NameOfType<T>::xls << "\033[0m\r\n";
  229. #else
  230. std::cout<<"\r\n\033[31;1;4m" << s << " dynamic " << NameOfType<T>::xls << "\033[0m\r\n";
  231. #endif
  232. #else
  233. #if defined(STATIC_TEST)
  234. std::cout << "\r\n" << s << " " << NameOfType<T>::xls << "\r\n";
  235. #else
  236. std::cout << "\r\n" << s << " dynamic " << NameOfType<T>::xls << "\r\n";
  237. #endif
  238. #endif
  239. };