Pattern.h 8.5 KB

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