Tools.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. import os.path
  2. import struct
  3. import numpy as np
  4. def normalize(a):
  5. return(a/np.max(np.abs(a)))
  6. TAILONLY = 1
  7. BODYONLY = 2
  8. BODYANDTAIL = 3
  9. # Datatype formats
  10. F64 = 64
  11. F32 = 0
  12. F16 = 16
  13. Q31 = 31
  14. Q15 = 15
  15. Q7 = 7
  16. def loopnb(format,loopkind):
  17. nb = 0
  18. if loopkind == TAILONLY:
  19. if format == 64:
  20. nb = 2
  21. if format == 0 or format == 31:
  22. nb = 3
  23. if format == 15 or format == 16:
  24. nb = 7
  25. if format == 7:
  26. nb = 15
  27. if loopkind == BODYONLY:
  28. if format == 64:
  29. nb = 4
  30. if format == 0 or format == 31:
  31. nb = 8
  32. if format == 15 or format == 16:
  33. nb = 16
  34. if format == 7:
  35. nb = 32
  36. if loopkind == BODYANDTAIL:
  37. if format == 64:
  38. nb = 5
  39. if format == 0 or format == 31:
  40. nb = 11 # 9
  41. if format == 15 or format == 16:
  42. nb = 23 # 17
  43. if format == 7:
  44. nb = 47 # 33
  45. return(nb)
  46. # Tools to generate pattern files
  47. def createMissingDir(destPath):
  48. theDir=os.path.normpath(destPath)
  49. if not os.path.exists(theDir):
  50. os.makedirs(theDir)
  51. # Pack an array of boolean into uint32
  52. def packset(a):
  53. b = np.packbits(a)
  54. newSize = int(np.ceil(b.shape[0] / 4.0)) * 4
  55. c = np.copy(b)
  56. c.resize(newSize)
  57. #print(c)
  58. vecSize = round(newSize/4)
  59. c=c.reshape(vecSize,4)
  60. #print(c)
  61. r = np.zeros(vecSize)
  62. result = []
  63. for i in range(0,vecSize):
  64. #print(c[i,:])
  65. #print("%X %X %X %X" % (c[i,0],c[i,1],c[i,2],c[i,3]))
  66. d = (c[i,0] << 24) | (c[i,1] << 16) | (c[i,2] << 8) | c[i,3]
  67. result.append(np.uint32(d))
  68. return(result)
  69. def float_to_hex(f):
  70. """ Convert and x86 float to an ARM unsigned long int.
  71. Args:
  72. f (float): value to be converted
  73. Raises:
  74. Nothing
  75. Returns:
  76. str : representation of the hex value
  77. """
  78. return hex(struct.unpack('<I', struct.pack('<f', f))[0])
  79. def float16_to_hex(f):
  80. """ Convert and x86 float to an ARM unsigned long int.
  81. Args:
  82. f (float): value to be converted
  83. Raises:
  84. Nothing
  85. Returns:
  86. str : representation of the hex value
  87. """
  88. return hex(struct.unpack('<H', struct.pack('<e', f))[0])
  89. def float64_to_hex(f):
  90. """ Convert and x86 float to an ARM unsigned long int.
  91. Args:
  92. f (float): value to be converted
  93. Raises:
  94. Nothing
  95. Returns:
  96. str : representation of the hex value
  97. """
  98. return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
  99. def to_q63(v):
  100. r = int(round(v * 2**63))
  101. if (r > 0x07FFFFFFFFFFFFFFF):
  102. r = 0x07FFFFFFFFFFFFFFF
  103. if (r < -0x08000000000000000):
  104. r = -0x08000000000000000
  105. return ("0x%s" % format(struct.unpack('<Q', struct.pack('<q', r))[0],'016X'))
  106. def to_q31(v):
  107. r = int(round(v * 2**31))
  108. if (r > 0x07FFFFFFF):
  109. r = 0x07FFFFFFF
  110. if (r < -0x080000000):
  111. r = -0x080000000
  112. return ("0x%s" % format(struct.unpack('<I', struct.pack('<i', r))[0],'08X'))
  113. def to_q15(v):
  114. r = int(round(v * 2**15))
  115. if (r > 0x07FFF):
  116. r = 0x07FFF
  117. if (r < -0x08000):
  118. r = -0x08000
  119. return ("0x%s" % format(struct.unpack('<H', struct.pack('<h', r))[0],'04X'))
  120. def to_q7(v):
  121. r = int(round(v * 2**7))
  122. if (r > 0x07F):
  123. r = 0x07F
  124. if (r < -0x080):
  125. r = -0x080
  126. return ("0x%s" % format(struct.unpack('<B', struct.pack('<b', r))[0],'02X'))
  127. def s8(r):
  128. return ("0x%s" % format(struct.unpack('<B', struct.pack('<b', r))[0],'02X'))
  129. def s16(r):
  130. return ("0x%s" % format(struct.unpack('<H', struct.pack('<h', r))[0],'04X'))
  131. def s32(r):
  132. return ("0x%s" % format(struct.unpack('<I', struct.pack('<i', r))[0],'08X'))
  133. def u32(r):
  134. return ("0x%s" % format(struct.unpack('<I', struct.pack('<I', r))[0],'08X'))
  135. class Config:
  136. def __init__(self,patternDir,paramDir,ext):
  137. self._patternDir = "%s%s" % (patternDir,ext.upper())
  138. self._paramDir = "%s%s" % (paramDir,ext.upper())
  139. self._ext = ext
  140. self._overwrite=True
  141. createMissingDir(self._patternDir)
  142. createMissingDir(self._paramDir)
  143. def setOverwrite(self,v):
  144. self._overwrite=v
  145. def canOverwrite(self,path):
  146. return(self._overwrite or not os.path.exists(path))
  147. def inputP(self,i,name=None):
  148. """ Path to a reference pattern from the ID
  149. Args:
  150. i (int): ID to the reference pattern
  151. Raises:
  152. Nothing
  153. Returns:
  154. str : path to the file where to generate the pattern data
  155. """
  156. if name:
  157. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,self._ext)))
  158. else:
  159. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,self._ext)))
  160. def inputS32P(self,i,name=None):
  161. """ Path to a reference pattern from the ID
  162. Args:
  163. i (int): ID to the reference pattern
  164. Raises:
  165. Nothing
  166. Returns:
  167. str : path to the file where to generate the pattern data
  168. """
  169. if name:
  170. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s32")))
  171. else:
  172. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s32")))
  173. def inputS16P(self,i,name=None):
  174. """ Path to a reference pattern from the ID
  175. Args:
  176. i (int): ID to the reference pattern
  177. Raises:
  178. Nothing
  179. Returns:
  180. str : path to the file where to generate the pattern data
  181. """
  182. if name:
  183. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s16")))
  184. else:
  185. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s16")))
  186. def inputS8P(self,i,name=None):
  187. """ Path to a reference pattern from the ID
  188. Args:
  189. i (int): ID to the reference pattern
  190. Raises:
  191. Nothing
  192. Returns:
  193. str : path to the file where to generate the pattern data
  194. """
  195. if name:
  196. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s8")))
  197. else:
  198. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"s8")))
  199. def inputF32P(self,i,name=None):
  200. """ Path to a reference pattern from the ID
  201. Args:
  202. i (int): ID to the reference pattern
  203. Raises:
  204. Nothing
  205. Returns:
  206. str : path to the file where to generate the pattern data
  207. """
  208. if name:
  209. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f32")))
  210. else:
  211. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f32")))
  212. def inputF16P(self,i,name=None):
  213. """ Path to a reference pattern from the ID
  214. Args:
  215. i (int): ID to the reference pattern
  216. Raises:
  217. Nothing
  218. Returns:
  219. str : path to the file where to generate the pattern data
  220. """
  221. if name:
  222. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f16")))
  223. else:
  224. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"f16")))
  225. def inputQ31P(self,i,name=None):
  226. """ Path to a reference pattern from the ID
  227. Args:
  228. i (int): ID to the reference pattern
  229. Raises:
  230. Nothing
  231. Returns:
  232. str : path to the file where to generate the pattern data
  233. """
  234. if name:
  235. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q31")))
  236. else:
  237. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q31")))
  238. def inputQ15P(self,i,name=None):
  239. """ Path to a reference pattern from the ID
  240. Args:
  241. i (int): ID to the reference pattern
  242. Raises:
  243. Nothing
  244. Returns:
  245. str : path to the file where to generate the pattern data
  246. """
  247. if name:
  248. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q15")))
  249. else:
  250. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q15")))
  251. def inputQ7P(self,i,name=None):
  252. """ Path to a reference pattern from the ID
  253. Args:
  254. i (int): ID to the reference pattern
  255. Raises:
  256. Nothing
  257. Returns:
  258. str : path to the file where to generate the pattern data
  259. """
  260. if name:
  261. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q7")))
  262. else:
  263. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"q7")))
  264. def inputU32P(self,i,name=None):
  265. """ Path to a reference pattern from the ID
  266. Args:
  267. i (int): ID to the reference pattern
  268. Raises:
  269. Nothing
  270. Returns:
  271. str : path to the file where to generate the pattern data
  272. """
  273. if name:
  274. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"u32")))
  275. else:
  276. return(os.path.join(self._patternDir,"Input%d_%s.txt" % (i,"u32")))
  277. def refP(self,i,name=None):
  278. """ Path to a reference pattern from the ID
  279. Args:
  280. i (int): ID to the reference pattern
  281. Raises:
  282. Nothing
  283. Returns:
  284. str : path to the file where to generate the pattern data
  285. """
  286. if name:
  287. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,self._ext)))
  288. else:
  289. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,self._ext)))
  290. def refS8P(self,i,name=None):
  291. """ Path to a reference pattern from the ID
  292. Args:
  293. i (int): ID to the reference pattern
  294. Raises:
  295. Nothing
  296. Returns:
  297. str : path to the file where to generate the pattern data
  298. """
  299. if name:
  300. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s8")))
  301. else:
  302. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s8")))
  303. def refS16P(self,i,name=None):
  304. """ Path to a reference pattern from the ID
  305. Args:
  306. i (int): ID to the reference pattern
  307. Raises:
  308. Nothing
  309. Returns:
  310. str : path to the file where to generate the pattern data
  311. """
  312. if name:
  313. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s16")))
  314. else:
  315. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s16")))
  316. def refS32P(self,i,name=None):
  317. """ Path to a reference pattern from the ID
  318. Args:
  319. i (int): ID to the reference pattern
  320. Raises:
  321. Nothing
  322. Returns:
  323. str : path to the file where to generate the pattern data
  324. """
  325. if name:
  326. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"s32")))
  327. else:
  328. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"s32")))
  329. def refQ63P(self,i,name=None):
  330. """ Path to a reference pattern from the ID
  331. Args:
  332. i (int): ID to the reference pattern
  333. Raises:
  334. Nothing
  335. Returns:
  336. str : path to the file where to generate the pattern data
  337. """
  338. if name:
  339. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q63")))
  340. else:
  341. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q63")))
  342. def refQ31P(self,i,name=None):
  343. """ Path to a reference pattern from the ID
  344. Args:
  345. i (int): ID to the reference pattern
  346. Raises:
  347. Nothing
  348. Returns:
  349. str : path to the file where to generate the pattern data
  350. """
  351. if name:
  352. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"q31")))
  353. else:
  354. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"q31")))
  355. def refF32P(self,i,name=None):
  356. """ Path to a reference pattern from the ID
  357. Args:
  358. i (int): ID to the reference pattern
  359. Raises:
  360. Nothing
  361. Returns:
  362. str : path to the file where to generate the pattern data
  363. """
  364. if name:
  365. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f32")))
  366. else:
  367. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f32")))
  368. def refF16P(self,i,name=None):
  369. """ Path to a reference pattern from the ID
  370. Args:
  371. i (int): ID to the reference pattern
  372. Raises:
  373. Nothing
  374. Returns:
  375. str : path to the file where to generate the pattern data
  376. """
  377. if name:
  378. return(os.path.join(self._patternDir,"%s%d_%s.txt" % (name,i,"f16")))
  379. else:
  380. return(os.path.join(self._patternDir,"Reference%d_%s.txt" % (i,"f16")))
  381. def paramP(self,i,name=None):
  382. """ Path to a parameters from the ID
  383. Args:
  384. i (int): ID to the params
  385. Raises:
  386. Nothing
  387. Returns:
  388. str : path to the file where to generate the pattern data
  389. """
  390. if name:
  391. return(os.path.join(self._paramDir,"%s%d.txt" % (name,i)))
  392. else:
  393. return(os.path.join(self._paramDir,"Params%d.txt" % i))
  394. def _writeVectorF64(self,i,data):
  395. """ Write pattern data
  396. The format is recognized by the text framework script.
  397. First line is the sample width (B,H or W,D for 8,16,32 or 64 bits)
  398. Second line is number of samples
  399. Other lines are hexadecimal representation of the samples in format
  400. which can be read on big endian ARM.
  401. Args:
  402. j (int): ID of pattern file
  403. data (array): Vector containing the data
  404. Raises:
  405. Nothing
  406. Returns:
  407. Nothing
  408. """
  409. if self.canOverwrite(i):
  410. with open(i,"w") as f:
  411. # Write sample dimension nb sample header
  412. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  413. f.write("D\n%d\n" % len(data))
  414. for v in data:
  415. f.write("// %f\n" % v)
  416. f.write("%s\n" % float64_to_hex(v))
  417. def _writeVectorF32(self,i,data):
  418. """ Write pattern data
  419. The format is recognized by the text framework script.
  420. First line is the sample width (B,H or W for 8,16 or 32 bits)
  421. Second line is number of samples
  422. Other lines are hexadecimal representation of the samples in format
  423. which can be read on big endian ARM.
  424. Args:
  425. j (int): ID of pattern file
  426. data (array): Vector containing the data
  427. Raises:
  428. Nothing
  429. Returns:
  430. Nothing
  431. """
  432. if self.canOverwrite(i):
  433. with open(i,"w") as f:
  434. # Write sample dimension nb sample header
  435. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  436. f.write("W\n%d\n" % len(data))
  437. for v in data:
  438. f.write("// %f\n" % v)
  439. f.write("%s\n" % float_to_hex(v))
  440. def _writeVectorF16(self,i,data):
  441. """ Write pattern data
  442. The format is recognized by the text framework script.
  443. First line is the sample width (B,H or W for 8,16 or 32 bits)
  444. Second line is number of samples
  445. Other lines are hexadecimal representation of the samples in format
  446. which can be read on big endian ARM.
  447. Args:
  448. j (int): ID of pattern file
  449. data (array): Vector containing the data
  450. Raises:
  451. Nothing
  452. Returns:
  453. Nothing
  454. """
  455. if self.canOverwrite(i):
  456. with open(i,"w") as f:
  457. # Write sample dimension nb sample header
  458. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  459. f.write("H\n%d\n" % len(data))
  460. for v in data:
  461. f.write("// %f\n" % v)
  462. f.write("%s\n" % float16_to_hex(v))
  463. def _writeVectorQ63(self,i,data):
  464. """ Write pattern data
  465. The format is recognized by the text framework script.
  466. First line is the sample width (B,H or W for 8,16 or 32 bits)
  467. Second line is number of samples
  468. Other lines are hexadecimal representation of the samples in format
  469. which can be read on big endian ARM.
  470. Args:
  471. j (int): ID of pattern file
  472. data (array): Vector containing the data
  473. Raises:
  474. Nothing
  475. Returns:
  476. Nothing
  477. """
  478. if self.canOverwrite(i):
  479. with open(i,"w") as f:
  480. # Write sample dimension nb sample header
  481. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  482. f.write("D\n%d\n" % len(data))
  483. for v in data:
  484. f.write("// %f\n" % v)
  485. f.write("%s\n" % to_q63(v))
  486. def _writeVectorQ31(self,i,data):
  487. """ Write pattern data
  488. The format is recognized by the text framework script.
  489. First line is the sample width (B,H or W for 8,16 or 32 bits)
  490. Second line is number of samples
  491. Other lines are hexadecimal representation of the samples in format
  492. which can be read on big endian ARM.
  493. Args:
  494. j (int): ID of pattern file
  495. data (array): Vector containing the data
  496. Raises:
  497. Nothing
  498. Returns:
  499. Nothing
  500. """
  501. if self.canOverwrite(i):
  502. with open(i,"w") as f:
  503. # Write sample dimension nb sample header
  504. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  505. f.write("W\n%d\n" % len(data))
  506. for v in data:
  507. f.write("// %f\n" % v)
  508. f.write("%s\n" % to_q31(v))
  509. def _writeVectorQ15(self,i,data):
  510. """ Write pattern data
  511. The format is recognized by the text framework script.
  512. First line is the sample width (B,H or W for 8,16 or 32 bits)
  513. Second line is number of samples
  514. Other lines are hexadecimal representation of the samples in format
  515. which can be read on big endian ARM.
  516. Args:
  517. j (int): ID of pattern file
  518. data (array): Vector containing the data
  519. Raises:
  520. Nothing
  521. Returns:
  522. Nothing
  523. """
  524. if self.canOverwrite(i):
  525. with open(i,"w") as f:
  526. # Write sample dimension nb sample header
  527. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  528. f.write("H\n%d\n" % len(data))
  529. for v in data:
  530. f.write("// %f\n" % v)
  531. f.write("%s\n" % to_q15(v))
  532. def _writeVectorS16(self,i,data):
  533. """ Write pattern data
  534. The format is recognized by the text framework script.
  535. First line is the sample width (B,H or W for 8,16 or 32 bits)
  536. Second line is number of samples
  537. Other lines are hexadecimal representation of the samples in format
  538. which can be read on big endian ARM.
  539. Args:
  540. j (int): ID of pattern file
  541. data (array): Vector containing the data
  542. Raises:
  543. Nothing
  544. Returns:
  545. Nothing
  546. """
  547. if self.canOverwrite(i):
  548. with open(i,"w") as f:
  549. # Write sample dimension nb sample header
  550. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  551. f.write("H\n%d\n" % len(data))
  552. for v in data:
  553. f.write("// %d\n" % v)
  554. f.write("%s\n" % s16(v))
  555. def _writeVectorS32(self,i,data):
  556. """ Write pattern data
  557. The format is recognized by the text framework script.
  558. First line is the sample width (B,H or W for 8,16 or 32 bits)
  559. Second line is number of samples
  560. Other lines are hexadecimal representation of the samples in format
  561. which can be read on big endian ARM.
  562. Args:
  563. j (int): ID of pattern file
  564. data (array): Vector containing the data
  565. Raises:
  566. Nothing
  567. Returns:
  568. Nothing
  569. """
  570. if self.canOverwrite(i):
  571. with open(i,"w") as f:
  572. # Write sample dimension nb sample header
  573. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  574. f.write("W\n%d\n" % len(data))
  575. for v in data:
  576. f.write("// %d\n" % v)
  577. f.write("%s\n" % s32(v))
  578. def _writeVectorU32(self,i,data):
  579. """ Write pattern data
  580. The format is recognized by the text framework script.
  581. First line is the sample width (B,H or W for 8,16 or 32 bits)
  582. Second line is number of samples
  583. Other lines are hexadecimal representation of the samples in format
  584. which can be read on big endian ARM.
  585. Args:
  586. j (int): ID of pattern file
  587. data (array): Vector containing the data
  588. Raises:
  589. Nothing
  590. Returns:
  591. Nothing
  592. """
  593. if self.canOverwrite(i):
  594. with open(i,"w") as f:
  595. # Write sample dimension nb sample header
  596. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  597. f.write("W\n%d\n" % len(data))
  598. for v in data:
  599. f.write("// %s\n" % v)
  600. f.write("%s\n" % u32(v))
  601. def _writeVectorQ7(self,i,data):
  602. """ Write pattern data
  603. The format is recognized by the text framework script.
  604. First line is the sample width (B,H or W for 8,16 or 32 bits)
  605. Second line is number of samples
  606. Other lines are hexadecimal representation of the samples in format
  607. which can be read on big endian ARM.
  608. Args:
  609. j (int): ID of pattern file
  610. data (array): Vector containing the data
  611. Raises:
  612. Nothing
  613. Returns:
  614. Nothing
  615. """
  616. if self.canOverwrite(i):
  617. with open(i,"w") as f:
  618. # Write sample dimension nb sample header
  619. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  620. f.write("B\n%d\n" % len(data))
  621. for v in data:
  622. f.write("// %f\n" % v)
  623. f.write("%s\n" % to_q7(v))
  624. def _writeVectorS8(self,i,data):
  625. """ Write pattern data
  626. The format is recognized by the text framework script.
  627. First line is the sample width (B,H or W for 8,16 or 32 bits)
  628. Second line is number of samples
  629. Other lines are hexadecimal representation of the samples in format
  630. which can be read on big endian ARM.
  631. Args:
  632. j (int): ID of pattern file
  633. data (array): Vector containing the data
  634. Raises:
  635. Nothing
  636. Returns:
  637. Nothing
  638. """
  639. if self.canOverwrite(i):
  640. with open(i,"w") as f:
  641. # Write sample dimension nb sample header
  642. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  643. f.write("B\n%d\n" % len(data))
  644. for v in data:
  645. f.write("// %d\n" % v)
  646. f.write("%s\n" % s8(v))
  647. def writeReference(self,j,data,name=None):
  648. if (self._ext == "f64"):
  649. self._writeVectorF64(self.refP(j,name),data)
  650. if (self._ext == "f32"):
  651. self._writeVectorF32(self.refP(j,name),data)
  652. if (self._ext == "f16"):
  653. self._writeVectorF16(self.refP(j,name),data)
  654. if (self._ext == "q63"):
  655. self._writeVectorQ63(self.refP(j,name),data)
  656. if (self._ext == "q31"):
  657. self._writeVectorQ31(self.refP(j,name),data)
  658. if (self._ext == "q15"):
  659. self._writeVectorQ15(self.refP(j,name),data)
  660. if (self._ext == "q7"):
  661. self._writeVectorQ7(self.refP(j,name),data)
  662. if (self._ext == "u32"):
  663. self._writeVectorU32(self.refP(j,name),data)
  664. if (self._ext == "s8"):
  665. self._writeVectorS8(self.refP(j,name),data)
  666. def writeReferenceQ63(self,j,data,name=None):
  667. self._writeVectorQ63(self.refQ63P(j,name),data)
  668. def writeReferenceQ31(self,j,data,name=None):
  669. self._writeVectorQ31(self.refQ31P(j,name),data)
  670. def writeReferenceS8(self,j,data,name=None):
  671. self._writeVectorS8(self.refS8P(j,name),data)
  672. def writeReferenceS16(self,j,data,name=None):
  673. self._writeVectorS16(self.refS16P(j,name),data)
  674. def writeReferenceS32(self,j,data,name=None):
  675. self._writeVectorS32(self.refS32P(j,name),data)
  676. def writeReferenceF32(self,j,data,name=None):
  677. self._writeVectorF32(self.refF32P(j,name),data)
  678. def writeReferenceF16(self,j,data,name=None):
  679. self._writeVectorF16(self.refF16P(j,name),data)
  680. def writeInput(self,j,data,name=None):
  681. if (self._ext == "f64"):
  682. self._writeVectorF64(self.inputP(j,name),data)
  683. if (self._ext == "f32"):
  684. self._writeVectorF32(self.inputP(j,name),data)
  685. if (self._ext == "f16"):
  686. self._writeVectorF16(self.inputP(j,name),data)
  687. if (self._ext == "q31"):
  688. self._writeVectorQ31(self.inputP(j,name),data)
  689. if (self._ext == "q15"):
  690. self._writeVectorQ15(self.inputP(j,name),data)
  691. if (self._ext == "q7"):
  692. self._writeVectorQ7(self.inputP(j,name),data)
  693. if (self._ext == "u32"):
  694. self._writeVectorU32(self.inputP(j,name),data)
  695. if (self._ext == "s8"):
  696. self._writeVectorS8(self.inputP(j,name),data)
  697. def writeInputF32(self,j,data,name=None):
  698. self._writeVectorF32(self.inputF32P(j,name),data)
  699. def writeInputF16(self,j,data,name=None):
  700. self._writeVectorF16(self.inputF16P(j,name),data)
  701. def writeInputQ31(self,j,data,name=None):
  702. self._writeVectorQ31(self.inputQ31P(j,name),data)
  703. def writeInputQ15(self,j,data,name=None):
  704. self._writeVectorQ15(self.inputQ15P(j,name),data)
  705. def writeInputQ7(self,j,data,name=None):
  706. self._writeVectorQ7(self.inputQ7P(j,name),data)
  707. def writeInputS32(self,j,data,name=None):
  708. self._writeVectorS32(self.inputS32P(j,name),data)
  709. def writeInputS16(self,j,data,name=None):
  710. self._writeVectorS16(self.inputS16P(j,name),data)
  711. def writeInputS8(self,j,data,name=None):
  712. self._writeVectorS8(self.inputS8P(j,name),data)
  713. def writeInputU32(self,j,data,name=None):
  714. self._writeVectorU32(self.inputU32P(j,name),data)
  715. def writeParam(self,j,data,name=None):
  716. """ Write pattern data
  717. The format is recognized by the text framework script.
  718. First line is the sample width (B,H or W for 8,16 or 32 bits)
  719. Second line is number of samples
  720. Other lines are hexadecimal representation of the samples in format
  721. which can be read on big endian ARM.
  722. Args:
  723. j (int): ID of parameter file
  724. data (array): Vector containing the data
  725. Raises:
  726. Nothing
  727. Returns:
  728. Nothing
  729. """
  730. i=self.paramP(j,name)
  731. if self.canOverwrite(i):
  732. with open(i,"w") as f:
  733. # Write sample dimension nb sample header
  734. #np.savetxt(i, data, newline="\n", header="W\n%d" % len(data),comments ="" )
  735. f.write("%d\n" % len(data))
  736. for v in data:
  737. f.write("%d\n" % v)