1import numpy as np
2import math
3import argparse
4import sys
5import struct
6
7sys.path.append("PatternGeneration")
8
9import Tools
10
11# Force to f16 value
12def f16(x):
13    return struct.unpack('<e', struct.pack('<e', x))[0]
14
15# Force to f32 value
16def f32(x):
17    return struct.unpack('<f', struct.pack('<f', x))[0]
18
19parser = argparse.ArgumentParser(description='Generate C arrays')
20parser.add_argument('-f', nargs='?',type = str, default="../Source/CommonTables/arm_mve_tables.c", help="C File path")
21parser.add_argument('-f16', nargs='?',type = str, default="../Source/CommonTables/arm_mve_tables_f16.c", help="C File path")
22parser.add_argument('-he', nargs='?',type = str, default="../Include/arm_mve_tables.h", help="H File path")
23parser.add_argument('-he16', nargs='?',type = str, default="../Include/arm_mve_tables_f16.h", help="H File path")
24
25args = parser.parse_args()
26
27COLLIM = 80
28
29condition="""#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_TWIDDLECOEF_%s_%d) || defined(ARM_TABLE_TWIDDLECOEF_%s_%d)
30"""
31
32F32 = 1
33F16 = 2
34Q31 = 3
35Q15 = 4
36Q7  = 5
37
38def printCUInt32Array(f,name,arr):
39    nb = 0
40    print("const uint32_t %s[%d]={" % (name,len(arr)),file=f)
41
42    for d in arr:
43        val = "%d," % d
44        nb = nb + len(val)
45        if nb > COLLIM:
46            print("",file=f)
47            nb = len(val)
48        print(val,end="",file=f)
49
50    print("};\n",file=f)
51
52def printCFloat32Array(f,name,arr):
53    nb = 0
54    print("const float32_t %s[%d]={" % (name,len(arr)),file=f)
55
56    for d in arr:
57        val = "%.20ff," % f32(d)
58        nb = nb + len(val)
59        if nb > COLLIM:
60            print("",file=f)
61            nb = len(val)
62        print(val,end="",file=f)
63
64    print("};\n",file=f)
65
66def printCFloat16Array(f,name,arr):
67    nb = 0
68    print("const float16_t %s[%d]={" % (name,len(arr)),file=f)
69
70    for d in arr:
71        val = "(float16_t)%.13ff," % f16(d)
72        nb = nb + len(val)
73        if nb > COLLIM:
74            print("",file=f)
75            nb = len(val)
76        print(val,end="",file=f)
77
78    print("};\n",file=f)
79
80def printCQ31Array(f,name,arr):
81    nb = 0
82    print("const q31_t %s[%d]={" % (name,len(arr)),file=f)
83
84    for d in arr:
85        val = "%s," % Tools.to_q31(d)
86        nb = nb + len(val)
87        if nb > COLLIM:
88            print("",file=f)
89            nb = len(val)
90        print(val,end="",file=f)
91
92    print("};\n",file=f)
93
94def printCQ15Array(f,name,arr):
95    nb = 0
96    print("const q15_t %s[%d]={" % (name,len(arr)),file=f)
97
98    for d in arr:
99        val = "%s," % Tools.to_q15(d)
100        nb = nb + len(val)
101        if nb > COLLIM:
102            print("",file=f)
103            nb = len(val)
104        print(val,end="",file=f)
105
106    print("};\n",file=f)
107
108def printCQ7Array(f,name,arr):
109    nb = 0
110    print("const q7_t %s[%d]={" % (name,len(arr)),file=f)
111
112    for d in arr:
113        val = "%s," % Tools.to_q7(d)
114        nb = nb + len(val)
115        if nb > COLLIM:
116            print("",file=f)
117            nb = len(val)
118        print(val,end="",file=f)
119
120    print("};\n",file=f)
121
122def printHUInt32Array(f,name,arr):
123 print("extern const uint32_t %s[%d];" % (name,len(arr)),file=f)
124
125def printHFloat32Array(f,name,arr):
126 print("extern const float32_t %s[%d];" % (name,len(arr)),file=f)
127
128def printHFloat16Array(f,name,arr):
129 print("extern const float16_t %s[%d];" % (name,len(arr)),file=f)
130
131def printHQ31Array(f,name,arr):
132 print("extern const q31_t %s[%d];" % (name,len(arr)),file=f)
133
134def printHQ15Array(f,name,arr):
135 print("extern const q15_t %s[%d];" % (name,len(arr)),file=f)
136
137def printHQ7Array(f,name,arr):
138 print("extern const q7_t %s[%d];" % (name,len(arr)),file=f)
139
140def twiddle(n):
141    a=2.0*math.pi*np.linspace(0,n,num=n,endpoint=False)/n
142    c=np.cos(a)
143    s=np.sin(a)
144
145    r = np.empty((c.size + s.size,), dtype=c.dtype)
146    r[0::2] = c
147    r[1::2] = s
148    return(r)
149
150def reorderTwiddle(theType,conjugate,f,h,n):
151    numStages = 6
152    coefs= twiddle(n)
153
154
155    if n == 4096:
156       numStages = 6
157       arraySize = 1364
158
159    if n == 1024:
160       numStages = 5
161       arraySize = 340
162
163    if n == 256:
164       numStages = 4
165       arraySize = 84
166
167    if n == 64:
168       numStages = 3
169       arraySize = 20
170
171    if n == 16:
172       numStages = 2
173       arraySize = 4
174
175    incr = 1
176    nbOfElt = n
177
178    maxNb = 0
179
180    tab1 = np.zeros(2*arraySize)
181    tab2 = np.zeros(2*arraySize)
182    tab3 = np.zeros(2*arraySize)
183
184    tab1Index=0
185    tab2Index=0
186    tab3Index=0
187
188    tab1Offset = np.zeros(numStages)
189    tab2Offset = np.zeros(numStages)
190    tab3Offset = np.zeros(numStages)
191
192
193
194    for stage in range(0,numStages-1):
195        nbOfElt = nbOfElt >> 2
196        pVectCoef1 = 0
197        pVectCoef2 = 0
198        pVectCoef3 = 0
199
200        tab1Offset[stage] = tab1Index
201        tab2Offset[stage] = tab2Index
202        tab3Offset[stage] = tab3Index
203
204        for i in range(0,nbOfElt):
205            tab1[tab1Index] = coefs[pVectCoef1]
206            if not conjugate:
207               tab1[tab1Index + 1] = coefs[pVectCoef1 + 1]
208            else:
209               tab1[tab1Index + 1] = -coefs[pVectCoef1 + 1]
210            tab1Index = tab1Index + 2
211            pVectCoef1 = pVectCoef1 + (incr * 1 * 2)
212
213            tab2[tab2Index] = coefs[pVectCoef2]
214            if not conjugate:
215               tab2[tab2Index + 1] = coefs[pVectCoef2 + 1]
216            else:
217               tab2[tab2Index + 1] = -coefs[pVectCoef2 + 1]
218            tab2Index = tab2Index + 2
219            pVectCoef2 = pVectCoef2 + (incr * 2 * 2)
220
221            tab3[tab3Index] = coefs[pVectCoef3]
222            if not conjugate:
223               tab3[tab3Index + 1] = coefs[pVectCoef3 + 1]
224            else:
225              tab3[tab3Index + 1] = -coefs[pVectCoef3 + 1]
226            tab3Index = tab3Index + 2
227            pVectCoef3 = pVectCoef3 + (incr * 3 * 2)
228
229            maxNb = maxNb + 1
230
231        incr = 4 * incr
232
233    # F32 SECTION FOR THIS FFT LENGTH
234    if theType == F32:
235       print(condition % ("F32",n, "F32",n << 1),file=f)
236       print(condition % ("F32",n, "F32",n << 1),file=h)
237       printCUInt32Array(f,"rearranged_twiddle_tab_stride1_arr_%d_f32" % n,list(tab1Offset))
238       printHUInt32Array(h,"rearranged_twiddle_tab_stride1_arr_%d_f32" % n,list(tab1Offset))
239
240       printCUInt32Array(f,"rearranged_twiddle_tab_stride2_arr_%d_f32" % n,list(tab2Offset))
241       printHUInt32Array(h,"rearranged_twiddle_tab_stride2_arr_%d_f32" % n,list(tab2Offset))
242
243       printCUInt32Array(f,"rearranged_twiddle_tab_stride3_arr_%d_f32" % n,list(tab3Offset))
244       printHUInt32Array(h,"rearranged_twiddle_tab_stride3_arr_%d_f32" % n,list(tab3Offset))
245
246       printCFloat32Array(f,"rearranged_twiddle_stride1_%d_f32" % n,list(tab1))
247       printHFloat32Array(h,"rearranged_twiddle_stride1_%d_f32" % n,list(tab1))
248
249       printCFloat32Array(f,"rearranged_twiddle_stride2_%d_f32" % n,list(tab2))
250       printHFloat32Array(h,"rearranged_twiddle_stride2_%d_f32" % n,list(tab2))
251
252       printCFloat32Array(f,"rearranged_twiddle_stride3_%d_f32" % n,list(tab3))
253       printHFloat32Array(h,"rearranged_twiddle_stride3_%d_f32" % n,list(tab3))
254       print("#endif\n",file=f)
255       print("#endif\n",file=h)
256
257    # F16 SECTION FOR THIS FFT LENGTH
258    if theType == F16:
259       print(condition % ("F16",n, "F16",n << 1),file=f)
260       print(condition % ("F16",n, "F16",n << 1),file=h)
261       printCUInt32Array(f,"rearranged_twiddle_tab_stride1_arr_%d_f16" % n,list(tab1Offset))
262       printHUInt32Array(h,"rearranged_twiddle_tab_stride1_arr_%d_f16" % n,list(tab1Offset))
263
264       printCUInt32Array(f,"rearranged_twiddle_tab_stride2_arr_%d_f16" % n,list(tab2Offset))
265       printHUInt32Array(h,"rearranged_twiddle_tab_stride2_arr_%d_f16" % n,list(tab2Offset))
266
267       printCUInt32Array(f,"rearranged_twiddle_tab_stride3_arr_%d_f16" % n,list(tab3Offset))
268       printHUInt32Array(h,"rearranged_twiddle_tab_stride3_arr_%d_f16" % n,list(tab3Offset))
269
270       printCFloat16Array(f,"rearranged_twiddle_stride1_%d_f16" % n,list(tab1))
271       printHFloat16Array(h,"rearranged_twiddle_stride1_%d_f16" % n,list(tab1))
272
273       printCFloat16Array(f,"rearranged_twiddle_stride2_%d_f16" % n,list(tab2))
274       printHFloat16Array(h,"rearranged_twiddle_stride2_%d_f16" % n,list(tab2))
275
276       printCFloat16Array(f,"rearranged_twiddle_stride3_%d_f16" % n,list(tab3))
277       printHFloat16Array(h,"rearranged_twiddle_stride3_%d_f16" % n,list(tab3))
278       print("#endif\n",file=f)
279       print("#endif\n",file=h)
280
281    # Q31 SECTION FOR THIS FFT LENGTH
282    if theType == Q31:
283       print(condition % ("Q31",n, "Q31",n << 1),file=f)
284       print(condition % ("Q31",n, "Q31",n << 1),file=h)
285       printCUInt32Array(f,"rearranged_twiddle_tab_stride1_arr_%d_q31" % n,list(tab1Offset))
286       printHUInt32Array(h,"rearranged_twiddle_tab_stride1_arr_%d_q31" % n,list(tab1Offset))
287
288       printCUInt32Array(f,"rearranged_twiddle_tab_stride2_arr_%d_q31" % n,list(tab2Offset))
289       printHUInt32Array(h,"rearranged_twiddle_tab_stride2_arr_%d_q31" % n,list(tab2Offset))
290
291       printCUInt32Array(f,"rearranged_twiddle_tab_stride3_arr_%d_q31" % n,list(tab3Offset))
292       printHUInt32Array(h,"rearranged_twiddle_tab_stride3_arr_%d_q31" % n,list(tab3Offset))
293
294       printCQ31Array(f,"rearranged_twiddle_stride1_%d_q31" % n,list(tab1))
295       printHQ31Array(h,"rearranged_twiddle_stride1_%d_q31" % n,list(tab1))
296
297       printCQ31Array(f,"rearranged_twiddle_stride2_%d_q31" % n,list(tab2))
298       printHQ31Array(h,"rearranged_twiddle_stride2_%d_q31" % n,list(tab2))
299
300       printCQ31Array(f,"rearranged_twiddle_stride3_%d_q31" % n,list(tab3))
301       printHQ31Array(h,"rearranged_twiddle_stride3_%d_q31" % n,list(tab3))
302       print("#endif\n",file=f)
303       print("#endif\n",file=h)
304
305    # Q15 SECTION FOR THIS FFT LENGTH
306    if theType == Q15:
307       print(condition % ("Q15",n, "Q15",n << 1),file=f)
308       print(condition % ("Q15",n, "Q15",n << 1),file=h)
309       printCUInt32Array(f,"rearranged_twiddle_tab_stride1_arr_%d_q15" % n,list(tab1Offset))
310       printHUInt32Array(h,"rearranged_twiddle_tab_stride1_arr_%d_q15" % n,list(tab1Offset))
311
312       printCUInt32Array(f,"rearranged_twiddle_tab_stride2_arr_%d_q15" % n,list(tab2Offset))
313       printHUInt32Array(h,"rearranged_twiddle_tab_stride2_arr_%d_q15" % n,list(tab2Offset))
314
315       printCUInt32Array(f,"rearranged_twiddle_tab_stride3_arr_%d_q15" % n,list(tab3Offset))
316       printHUInt32Array(h,"rearranged_twiddle_tab_stride3_arr_%d_q15" % n,list(tab3Offset))
317
318       printCQ15Array(f,"rearranged_twiddle_stride1_%d_q15" % n,list(tab1))
319       printHQ15Array(h,"rearranged_twiddle_stride1_%d_q15" % n,list(tab1))
320
321       printCQ15Array(f,"rearranged_twiddle_stride2_%d_q15" % n,list(tab2))
322       printHQ15Array(h,"rearranged_twiddle_stride2_%d_q15" % n,list(tab2))
323
324       printCQ15Array(f,"rearranged_twiddle_stride3_%d_q15" % n,list(tab3))
325       printHQ15Array(h,"rearranged_twiddle_stride3_%d_q15" % n,list(tab3))
326       print("#endif\n",file=f)
327       print("#endif\n",file=h)
328
329
330
331
332#test = twiddle(16)
333#printCFloat32Array("Test",list(test))
334
335cheader="""/* ----------------------------------------------------------------------
336 * Project:      CMSIS DSP Library
337 * Title:        arm_mve_tables%s.c
338 * Description:  common tables like fft twiddle factors, Bitreverse, reciprocal etc
339 *               used for MVE implementation only
340 *
341 * @version  V1.10.0
342 * @date     04 October 2021
343 *
344 * Target Processor: Cortex-M and Cortex-A cores
345 * -------------------------------------------------------------------- */
346/*
347 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
348 *
349 * SPDX-License-Identifier: Apache-2.0
350 *
351 * Licensed under the Apache License, Version 2.0 (the License); you may
352 * not use this file except in compliance with the License.
353 * You may obtain a copy of the License at
354 *
355 * www.apache.org/licenses/LICENSE-2.0
356 *
357 * Unless required by applicable law or agreed to in writing, software
358 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
359 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
360 * See the License for the specific language governing permissions and
361 * limitations under the License.
362 */
363
364 #include "arm_math_types%s.h"
365
366 """
367
368cifdeMVEF="""
369
370#if defined(%s) && !defined(ARM_MATH_AUTOVECTORIZE)
371
372#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES)
373"""
374
375cfooterMVEF="""
376
377#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) */
378#endif /* defined(%s) && !defined(ARM_MATH_AUTOVECTORIZE) */
379"""
380
381cifdeMVEI="""
382
383#if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
384
385#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES)
386"""
387
388cfooterMVEI="""
389
390#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) */
391#endif /* defined(ARM_MATH_MVEI)  */
392"""
393
394hheader="""/* ----------------------------------------------------------------------
395 * Project:      CMSIS DSP Library
396 * Title:        arm_mve_tables%s.h
397 * Description:  common tables like fft twiddle factors, Bitreverse, reciprocal etc
398 *               used for MVE implementation only
399 *
400 * @version  V1.10.0
401 * @date     04 October 2021
402 *
403 * Target Processor: Cortex-M and Cortex-A cores
404 * -------------------------------------------------------------------- */
405/*
406 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
407 *
408 * SPDX-License-Identifier: Apache-2.0
409 *
410 * Licensed under the Apache License, Version 2.0 (the License); you may
411 * not use this file except in compliance with the License.
412 * You may obtain a copy of the License at
413 *
414 * www.apache.org/licenses/LICENSE-2.0
415 *
416 * Unless required by applicable law or agreed to in writing, software
417 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
418 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
419 * See the License for the specific language governing permissions and
420 * limitations under the License.
421 */
422
423 #ifndef _ARM_MVE_TABLES_%sH
424 #define _ARM_MVE_TABLES_%sH
425
426#include "arm_math_types%s.h"
427
428#ifdef   __cplusplus
429extern "C"
430{
431#endif
432
433
434 """
435
436hifdefMVEF="""
437#if defined(%s) && !defined(ARM_MATH_AUTOVECTORIZE)
438
439#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES)
440"""
441
442hfooterMVEF="""
443#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) */
444
445#endif /* defined(%s) && !defined(ARM_MATH_AUTOVECTORIZE) */
446
447"""
448
449hifdefMVEI="""
450#if defined(ARM_MATH_MVEI)  && !defined(ARM_MATH_AUTOVECTORIZE)
451
452#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES)
453"""
454
455hfooterMVEI="""
456#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) */
457
458#endif /* defined(ARM_MATH_MVEI) */
459
460"""
461
462hfooter="""
463#ifdef   __cplusplus
464}
465#endif
466
467#endif /*_ARM_MVE_TABLES_%sH*/
468"""
469
470with open(args.f16,'w') as f:
471  with open(args.he16,'w') as h:
472     print(cheader % ("_f16","_f16"),file=f)
473     print(hheader % ("_f16","F16_","F16_","_f16"),file=h)
474
475     print("#if defined(ARM_FLOAT16_SUPPORTED)",file=f)
476
477     print(cifdeMVEF % ("ARM_MATH_MVE_FLOAT16",),file=f)
478     print(hifdefMVEF % "ARM_MATH_MVE_FLOAT16",file=h)
479     reorderTwiddle(F16,False,f,h,16)
480     reorderTwiddle(F16,False,f,h,64)
481     reorderTwiddle(F16,False,f,h,256)
482     reorderTwiddle(F16,False,f,h,1024)
483     reorderTwiddle(F16,False,f,h,4096)
484     print(cfooterMVEF % ("ARM_MATH_MVE_FLOAT16"),file=f)
485     print(hfooterMVEF % "ARM_MATH_MVE_FLOAT16",file=h)
486
487     print("#endif /* if defined(ARM_FLOAT16_SUPPORTED) */",file=f)
488
489     print(hfooter % "F16_",file=h)
490
491with open(args.f,'w') as f:
492  with open(args.he,'w') as h:
493     print(cheader % ("",""),file=f)
494     print(hheader % ("","","",""),file=h)
495
496
497     print(cifdeMVEF % ("ARM_MATH_MVEF",),file=f)
498     print(hifdefMVEF % "ARM_MATH_MVEF",file=h)
499     reorderTwiddle(F32,False,f,h,16)
500     reorderTwiddle(F32,False,f,h,64)
501     reorderTwiddle(F32,False,f,h,256)
502     reorderTwiddle(F32,False,f,h,1024)
503     reorderTwiddle(F32,False,f,h,4096)
504     print(cfooterMVEF % ("ARM_MATH_MVEF"),file=f)
505     print(hfooterMVEF % "ARM_MATH_MVEF",file=h)
506
507     print(cifdeMVEI,file=f)
508     print(hifdefMVEI,file=h)
509     reorderTwiddle(Q31,True,f,h,16)
510     reorderTwiddle(Q31,True,f,h,64)
511     reorderTwiddle(Q31,True,f,h,256)
512     reorderTwiddle(Q31,True,f,h,1024)
513     reorderTwiddle(Q31,True,f,h,4096)
514     print(cfooterMVEI,file=f)
515     print(hfooterMVEI,file=h)
516
517     print(cifdeMVEI,file=f)
518     print(hifdefMVEI,file=h)
519     reorderTwiddle(Q15,True,f,h,16)
520     reorderTwiddle(Q15,True,f,h,64)
521     reorderTwiddle(Q15,True,f,h,256)
522     reorderTwiddle(Q15,True,f,h,1024)
523     reorderTwiddle(Q15,True,f,h,4096)
524     print(cfooterMVEI,file=f)
525     print(hfooterMVEI,file=h)
526
527     #print(cifdeMVEI,file=f)
528     #print(hifdefMVEI,file=h)
529     #reorderTwiddle(Q7,True,f,h,16)
530     #reorderTwiddle(Q7,True,f,h,64)
531     #reorderTwiddle(Q7,True,f,h,256)
532     #reorderTwiddle(Q7,True,f,h,1024)
533     #reorderTwiddle(Q7,True,f,h,4096)
534     #print(cfooterMVEI,file=f)
535     #print(hfooterMVEI,file=h)
536
537     print(hfooter % "",file=h)
538