Pattern.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: Pattern.h
  4. * Description: Pattern Header
  5. *
  6. * $Date: 20. June 2019
  7. * $Revision: V1.0.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #ifndef _PATTERN_H_
  29. #define _PATTERN_H_
  30. #include "Test.h"
  31. #include "Pattern.h"
  32. #include "arm_math_types.h"
  33. #include "arm_math_types_f16.h"
  34. namespace Client {
  35. template <typename T>
  36. T *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples=MAX_NB_SAMPLES)
  37. {
  38. (void)id;
  39. (void)mgr;
  40. (void)nb;
  41. (void)maxSamples;
  42. return(NULL);
  43. };
  44. template <>
  45. float64_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  46. template <>
  47. float32_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  48. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  49. template <>
  50. float16_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  51. #endif
  52. template <>
  53. q63_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  54. template <>
  55. q31_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  56. template <>
  57. q15_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  58. template <>
  59. q7_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  60. template <>
  61. uint32_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  62. template <>
  63. uint16_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  64. template <>
  65. uint8_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  66. template <typename T>
  67. T *localPattern(Testing::nbSamples_t id, PatternMgr *mgr)
  68. {
  69. (void)id;
  70. (void)mgr;
  71. return(NULL);
  72. };
  73. template <>
  74. float64_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  75. template <>
  76. float32_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  77. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  78. template <>
  79. float16_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  80. #endif
  81. template <>
  82. q63_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  83. template <>
  84. q31_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  85. template <>
  86. q15_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  87. template <>
  88. q7_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  89. template <>
  90. uint32_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  91. template <>
  92. uint16_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  93. template <>
  94. uint8_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  95. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t nb,float64_t* data,PatternMgr *mgr);
  96. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,float32_t*,PatternMgr *);
  97. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  98. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,float16_t*,PatternMgr *);
  99. #endif
  100. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q63_t*,PatternMgr *);
  101. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q31_t*,PatternMgr *);
  102. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q15_t*,PatternMgr *);
  103. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q7_t*,PatternMgr *);
  104. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,uint32_t*,PatternMgr *);
  105. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,uint16_t*,PatternMgr *);
  106. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,uint8_t*,PatternMgr *);
  107. template <class T>
  108. class AnyPattern {
  109. protected:
  110. // Pattern data
  111. T *data;
  112. // To know if the pattern has loaded any data
  113. bool isLoaded;
  114. // Memory generation when the data was loaded.
  115. // If memory generation is different when accessing the data
  116. // the pattern should return NULL.
  117. unsigned long currentGen;
  118. PatternMgr *m_mgr;
  119. // Nb of samples in the loaded pattern
  120. Testing::nbSamples_t m_nbSamples;
  121. public:
  122. AnyPattern()
  123. {
  124. this->data = NULL;
  125. this->isLoaded = false;
  126. this->currentGen = 0;
  127. this->m_mgr=NULL;
  128. this->m_nbSamples = 0;
  129. }
  130. bool isTailEmpty()
  131. {
  132. if (m_mgr)
  133. {
  134. return(m_mgr->IsTailEmpty((char*)this->ptr(),this->nbSamples()*sizeof(T)));
  135. }
  136. else
  137. {
  138. return(true);
  139. }
  140. }
  141. /** Get pointer to the pattern data.
  142. Pointer is NULL in following conditions:
  143. Memory generation of pattern is different from memory manager's one
  144. Pattern not loaded
  145. Number of samples i 0
  146. */
  147. T *ptr()
  148. {
  149. if (this->m_mgr == NULL)
  150. {
  151. return(NULL);
  152. }
  153. if (this->currentGen != this->m_mgr->generation())
  154. {
  155. return(NULL);
  156. }
  157. else
  158. {
  159. if (this->isLoaded)
  160. {
  161. if (this->m_nbSamples > 0)
  162. {
  163. return(this->data);
  164. }
  165. else
  166. {
  167. return(NULL);
  168. }
  169. }
  170. else
  171. {
  172. return(NULL);
  173. }
  174. }
  175. }
  176. Testing::nbSamples_t nbSamples()
  177. {
  178. if (this->m_mgr == NULL)
  179. {
  180. return(0);
  181. }
  182. if (this->currentGen != this->m_mgr->generation())
  183. {
  184. return(0);
  185. }
  186. if (this->isLoaded)
  187. {
  188. return(this->m_nbSamples);
  189. }
  190. else
  191. {
  192. return(0);
  193. }
  194. }
  195. };
  196. /** An input pattern
  197. */
  198. template <class T>
  199. class Pattern : public AnyPattern<T>{
  200. private:
  201. Testing::PatternID_t m_id;
  202. public:
  203. Pattern()
  204. {
  205. }
  206. /** Reload fresh data for the pattern.
  207. If memory manager has not released its memory,
  208. reloading an already loaded pattern will leak some memory
  209. since the previous pattern will still be allocated
  210. in the memory manager.
  211. Generally this reload is used in setUp function of tests.
  212. The memory being released in the tearDown function.
  213. */
  214. void reload(Testing::PatternID_t id,PatternMgr *mgr, Testing::nbSamples_t maxSamples=0)
  215. {
  216. Testing::nbSamples_t nbSamples;
  217. this->m_id = id;
  218. this->m_mgr=mgr;
  219. this->currentGen = this->m_mgr->generation();
  220. this->data = loadPattern<T>(this->m_id,this->m_mgr,nbSamples,maxSamples);
  221. // Initialize the field with the number of samples read
  222. // (which may have been constrained with maxSamples)
  223. this->m_nbSamples = nbSamples;
  224. this->isLoaded = true;
  225. }
  226. };
  227. /** An reference pattern
  228. The difference with input pattern is that reference
  229. patterns are not loaded in dump mode and are not wasting
  230. memory.
  231. */
  232. template <class T>
  233. class RefPattern : public AnyPattern<T>{
  234. private:
  235. Testing::PatternID_t m_id;
  236. public:
  237. RefPattern()
  238. {
  239. }
  240. void reload(Testing::PatternID_t id,PatternMgr *mgr, Testing::nbSamples_t maxSamples=0)
  241. {
  242. Testing::nbSamples_t nbSamples;
  243. this->m_id = id;
  244. this->m_mgr=mgr;
  245. this->currentGen = this->m_mgr->generation();
  246. // Reference patterns are not loaded in dump mode
  247. if (this->m_mgr->runningMode() != Testing::kDumpOnly)
  248. {
  249. this->data = loadPattern<T>(this->m_id,this->m_mgr,nbSamples,maxSamples);
  250. this->m_nbSamples = nbSamples;
  251. this->isLoaded = true;
  252. }
  253. else
  254. {
  255. this->data=NULL;
  256. this->m_nbSamples = 0;
  257. this->isLoaded = false;
  258. }
  259. }
  260. };
  261. /** A local pattern is to be used for an output.
  262. It is the only way for the test to allocate memory in the
  263. memory manager.
  264. Local patterns can be dumped.
  265. A local pattern is not dumped when in test mode.
  266. */
  267. template <class T>
  268. class LocalPattern : public AnyPattern<T>{
  269. private:
  270. Testing::outputID_t m_id;
  271. public:
  272. LocalPattern()
  273. {
  274. }
  275. void create(Testing::nbSamples_t nbSamples,Testing::outputID_t id,PatternMgr *mgr)
  276. {
  277. this->m_nbSamples = nbSamples;
  278. this->m_mgr=mgr;
  279. this->m_id=id;
  280. this->currentGen = this->m_mgr->generation();
  281. this->data = localPattern<T>(nbSamples,this->m_mgr);
  282. this->isLoaded = true;
  283. }
  284. void dump(PatternMgr *mgr)
  285. {
  286. (void)mgr;
  287. /*
  288. If the pattern has never been created then m_mgr is NULL.
  289. */
  290. if (this->m_mgr != NULL)
  291. {
  292. if (this->m_mgr->runningMode() != Testing::kTestOnly)
  293. {
  294. if ((this->ptr() != NULL) && (this->nbSamples() > 0))
  295. {
  296. dumpPattern(this->m_id,this->m_nbSamples,this->data,this->m_mgr);
  297. }
  298. }
  299. }
  300. }
  301. };
  302. }
  303. #endif