Pattern.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. namespace Client {
  33. template <typename T>
  34. T *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples=MAX_NB_SAMPLES)
  35. {
  36. return(NULL);
  37. };
  38. template <>
  39. float64_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  40. template <>
  41. float32_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  42. template <>
  43. q63_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  44. template <>
  45. q31_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  46. template <>
  47. q15_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  48. template <>
  49. q7_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  50. template <>
  51. uint32_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  52. template <>
  53. uint16_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  54. template <>
  55. uint8_t *loadPattern(Testing::PatternID_t id, PatternMgr *mgr,Testing::nbSamples_t &nb, Testing::nbSamples_t maxSamples);
  56. template <typename T>
  57. T *localPattern(Testing::nbSamples_t id, PatternMgr *mgr)
  58. {
  59. return(NULL);
  60. };
  61. template <>
  62. float64_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  63. template <>
  64. float32_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  65. template <>
  66. q63_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  67. template <>
  68. q31_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  69. template <>
  70. q15_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  71. template <>
  72. q7_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  73. template <>
  74. uint32_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  75. template <>
  76. uint16_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  77. template <>
  78. uint8_t *localPattern(Testing::nbSamples_t nb, PatternMgr *mgr);
  79. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t nb,float64_t* data,PatternMgr *mgr);
  80. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,float32_t*,PatternMgr *);
  81. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q63_t*,PatternMgr *);
  82. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q31_t*,PatternMgr *);
  83. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q15_t*,PatternMgr *);
  84. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,q7_t*,PatternMgr *);
  85. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,uint32_t*,PatternMgr *);
  86. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,uint16_t*,PatternMgr *);
  87. extern void dumpPattern(Testing::outputID_t id,Testing::nbSamples_t,uint8_t*,PatternMgr *);
  88. template <class T>
  89. class AnyPattern {
  90. protected:
  91. // Pattern data
  92. T *data;
  93. // To know if the pattern has loaded any data
  94. bool isLoaded;
  95. // Memory generation when the data was loaded.
  96. // If memory generation is different when accessing the data
  97. // the pattern should return NULL.
  98. unsigned long currentGen;
  99. PatternMgr *m_mgr;
  100. // Nb of samples in the loaded pattern
  101. Testing::nbSamples_t m_nbSamples;
  102. public:
  103. AnyPattern()
  104. {
  105. this->data = NULL;
  106. this->isLoaded = false;
  107. this->currentGen = 0;
  108. this->m_mgr=NULL;
  109. this->m_nbSamples = 0;
  110. }
  111. bool isTailEmpty()
  112. {
  113. if (m_mgr)
  114. {
  115. return(m_mgr->IsTailEmpty((char*)this->ptr(),this->nbSamples()*sizeof(T)));
  116. }
  117. else
  118. {
  119. return(true);
  120. }
  121. }
  122. /** Get pointer to the pattern data.
  123. Pointer is NULL in following conditions:
  124. Memory generation of pattern is different from memory manager's one
  125. Pattern not loaded
  126. Number of samples i 0
  127. */
  128. T *ptr()
  129. {
  130. if (this->m_mgr == NULL)
  131. {
  132. return(NULL);
  133. }
  134. if (this->currentGen != this->m_mgr->generation())
  135. {
  136. return(NULL);
  137. }
  138. else
  139. {
  140. if (this->isLoaded)
  141. {
  142. if (this->m_nbSamples > 0)
  143. {
  144. return(this->data);
  145. }
  146. else
  147. {
  148. return(NULL);
  149. }
  150. }
  151. else
  152. {
  153. return(NULL);
  154. }
  155. }
  156. }
  157. Testing::nbSamples_t nbSamples()
  158. {
  159. if (this->m_mgr == NULL)
  160. {
  161. return(0);
  162. }
  163. if (this->currentGen != this->m_mgr->generation())
  164. {
  165. return(0);
  166. }
  167. if (this->isLoaded)
  168. {
  169. return(this->m_nbSamples);
  170. }
  171. else
  172. {
  173. return(0);
  174. }
  175. }
  176. };
  177. /** An input pattern
  178. */
  179. template <class T>
  180. class Pattern : public AnyPattern<T>{
  181. private:
  182. Testing::PatternID_t m_id;
  183. public:
  184. Pattern()
  185. {
  186. }
  187. /** Reload fresh data for the pattern.
  188. If memory manager has not released its memory,
  189. reloading an already loaded pattern will leak some memory
  190. since the previous pattern will still be allocated
  191. in the memory manager.
  192. Generally this reload is used in setUp function of tests.
  193. The memory being released in the tearDown function.
  194. */
  195. void reload(Testing::PatternID_t id,PatternMgr *mgr, Testing::nbSamples_t maxSamples=0)
  196. {
  197. Testing::nbSamples_t nbSamples;
  198. this->m_id = id;
  199. this->m_mgr=mgr;
  200. this->currentGen = this->m_mgr->generation();
  201. this->data = loadPattern<T>(this->m_id,this->m_mgr,nbSamples,maxSamples);
  202. // Initialize the field with the number of samples read
  203. // (which may have been constrained with maxSamples)
  204. this->m_nbSamples = nbSamples;
  205. this->isLoaded = true;
  206. }
  207. };
  208. /** An reference pattern
  209. The difference with input pattern is that reference
  210. patterns are not loaded in dump mode and are not wasting
  211. memory.
  212. */
  213. template <class T>
  214. class RefPattern : public AnyPattern<T>{
  215. private:
  216. Testing::PatternID_t m_id;
  217. public:
  218. RefPattern()
  219. {
  220. }
  221. void reload(Testing::PatternID_t id,PatternMgr *mgr, Testing::nbSamples_t maxSamples=0)
  222. {
  223. Testing::nbSamples_t nbSamples;
  224. this->m_id = id;
  225. this->m_mgr=mgr;
  226. this->currentGen = this->m_mgr->generation();
  227. // Reference patterns are not loaded in dump mode
  228. if (this->m_mgr->runningMode() != Testing::kDumpOnly)
  229. {
  230. this->data = loadPattern<T>(this->m_id,this->m_mgr,nbSamples,maxSamples);
  231. this->m_nbSamples = nbSamples;
  232. this->isLoaded = true;
  233. }
  234. else
  235. {
  236. this->data=NULL;
  237. this->m_nbSamples = 0;
  238. this->isLoaded = false;
  239. }
  240. }
  241. };
  242. /** A local pattern is to be used for an output.
  243. It is the only way for the test to allocate memory in the
  244. memory manager.
  245. Local patterns can be dumped.
  246. A local pattern is not dumped when in test mode.
  247. */
  248. template <class T>
  249. class LocalPattern : public AnyPattern<T>{
  250. private:
  251. Testing::outputID_t m_id;
  252. public:
  253. LocalPattern()
  254. {
  255. }
  256. void create(Testing::nbSamples_t nbSamples,Testing::outputID_t id,PatternMgr *mgr)
  257. {
  258. this->m_nbSamples = nbSamples;
  259. this->m_mgr=mgr;
  260. this->m_id=id;
  261. this->currentGen = this->m_mgr->generation();
  262. this->data = localPattern<T>(nbSamples,this->m_mgr);
  263. this->isLoaded = true;
  264. }
  265. void dump(PatternMgr *mgr)
  266. {
  267. /*
  268. If the pattern has never been created then m_mgr is NULL.
  269. */
  270. if (this->m_mgr != NULL)
  271. {
  272. if (this->m_mgr->runningMode() != Testing::kTestOnly)
  273. {
  274. if ((this->ptr() != NULL) && (this->nbSamples() > 0))
  275. {
  276. dumpPattern(this->m_id,this->m_nbSamples,this->data,this->m_mgr);
  277. }
  278. }
  279. }
  280. }
  281. };
  282. }
  283. #endif