Pattern.h 10 KB

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