Christophe Favergeon | aefd277 | 2020-01-08 09:01:17 +0100 | [diff] [blame^] | 1 | import numpy as np |
| 2 | import math |
| 3 | import argparse |
| 4 | |
| 5 | parser = argparse.ArgumentParser(description='Generate C arrays') |
| 6 | parser.add_argument('-f', nargs='?',type = str, default="../Source/CommonTables/arm_mve_tables.c", help="C File path") |
| 7 | parser.add_argument('-he', nargs='?',type = str, default="../Include/arm_mve_tables.h", help="H File path") |
| 8 | |
| 9 | args = parser.parse_args() |
| 10 | |
| 11 | COLLIM = 80 |
| 12 | |
| 13 | condition="""#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_TWIDDLECOEF_F32_%d) || defined(ARM_TABLE_TWIDDLECOEF_F32_%d) |
| 14 | """ |
| 15 | |
| 16 | def printCUInt32Array(f,name,arr): |
| 17 | nb = 0 |
| 18 | print("uint32_t %s[%d]={" % (name,len(arr)),file=f) |
| 19 | |
| 20 | for d in arr: |
| 21 | val = "%d," % d |
| 22 | nb = nb + len(val) |
| 23 | if nb > COLLIM: |
| 24 | print("",file=f) |
| 25 | nb = len(val) |
| 26 | print(val,end="",file=f) |
| 27 | |
| 28 | print("};\n",file=f) |
| 29 | |
| 30 | def printCFloat32Array(f,name,arr): |
| 31 | nb = 0 |
| 32 | print("float32_t %s[%d]={" % (name,len(arr)),file=f) |
| 33 | |
| 34 | for d in arr: |
| 35 | val = "%.20ff," % d |
| 36 | nb = nb + len(val) |
| 37 | if nb > COLLIM: |
| 38 | print("",file=f) |
| 39 | nb = len(val) |
| 40 | print(val,end="",file=f) |
| 41 | |
| 42 | print("};\n",file=f) |
| 43 | |
| 44 | def printHUInt32Array(f,name,arr): |
| 45 | print("extern uint32_t %s[%d];" % (name,len(arr)),file=f) |
| 46 | |
| 47 | def printHFloat32Array(f,name,arr): |
| 48 | print("extern float32_t %s[%d];" % (name,len(arr)),file=f) |
| 49 | |
| 50 | def twiddle(n): |
| 51 | a=2.0*math.pi*np.linspace(0,n,num=n,endpoint=False)/n |
| 52 | c=np.cos(a) |
| 53 | s=np.sin(a) |
| 54 | |
| 55 | r = np.empty((c.size + s.size,), dtype=c.dtype) |
| 56 | r[0::2] = c |
| 57 | r[1::2] = s |
| 58 | return(r) |
| 59 | |
| 60 | def reorderTwiddle(f,h,n): |
| 61 | numStages = 6 |
| 62 | coefs= twiddle(n) |
| 63 | |
| 64 | |
| 65 | if n == 4096: |
| 66 | numStages = 6 |
| 67 | arraySize = 1364 |
| 68 | |
| 69 | if n == 1024: |
| 70 | numStages = 5 |
| 71 | arraySize = 340 |
| 72 | |
| 73 | if n == 256: |
| 74 | numStages = 4 |
| 75 | arraySize = 84 |
| 76 | |
| 77 | if n == 64: |
| 78 | numStages = 3 |
| 79 | arraySize = 20 |
| 80 | |
| 81 | if n == 16: |
| 82 | numStages = 2 |
| 83 | arraySize = 4 |
| 84 | |
| 85 | incr = 1 |
| 86 | nbOfElt = n |
| 87 | |
| 88 | maxNb = 0 |
| 89 | |
| 90 | tab1 = np.zeros(2*arraySize) |
| 91 | tab2 = np.zeros(2*arraySize) |
| 92 | tab3 = np.zeros(2*arraySize) |
| 93 | |
| 94 | tab1Index=0 |
| 95 | tab2Index=0 |
| 96 | tab3Index=0 |
| 97 | |
| 98 | tab1Offset = np.zeros(numStages) |
| 99 | tab2Offset = np.zeros(numStages) |
| 100 | tab3Offset = np.zeros(numStages) |
| 101 | |
| 102 | |
| 103 | |
| 104 | for stage in range(0,numStages-1): |
| 105 | nbOfElt = nbOfElt >> 2 |
| 106 | pVectCoef1 = 0 |
| 107 | pVectCoef2 = 0 |
| 108 | pVectCoef3 = 0 |
| 109 | |
| 110 | tab1Offset[stage] = tab1Index |
| 111 | tab2Offset[stage] = tab2Index |
| 112 | tab3Offset[stage] = tab3Index |
| 113 | |
| 114 | for i in range(0,nbOfElt): |
| 115 | tab1[tab1Index] = coefs[pVectCoef1] |
| 116 | tab1[tab1Index + 1] = coefs[pVectCoef1 + 1]; |
| 117 | tab1Index = tab1Index + 2 |
| 118 | pVectCoef1 = pVectCoef1 + (incr * 1 * 2) |
| 119 | |
| 120 | tab2[tab2Index] = coefs[pVectCoef2] |
| 121 | tab2[tab2Index + 1] = coefs[pVectCoef2 + 1]; |
| 122 | tab2Index = tab2Index + 2 |
| 123 | pVectCoef2 = pVectCoef2 + (incr * 2 * 2) |
| 124 | |
| 125 | tab3[tab3Index] = coefs[pVectCoef3] |
| 126 | tab3[tab3Index + 1] = coefs[pVectCoef3 + 1]; |
| 127 | tab3Index = tab3Index + 2 |
| 128 | pVectCoef3 = pVectCoef3 + (incr * 3 * 2) |
| 129 | |
| 130 | maxNb = maxNb + 1 |
| 131 | |
| 132 | incr = 4 * incr |
| 133 | |
| 134 | print(condition % (n, n << 1),file=f) |
| 135 | print(condition % (n, n << 1),file=h) |
| 136 | printCUInt32Array(f,"rearranged_twiddle_tab_stride1_arr_%d" % n,list(tab1Offset)) |
| 137 | printHUInt32Array(h,"rearranged_twiddle_tab_stride1_arr_%d" % n,list(tab1Offset)) |
| 138 | |
| 139 | printCUInt32Array(f,"rearranged_twiddle_tab_stride2_arr_%d" % n,list(tab2Offset)) |
| 140 | printHUInt32Array(h,"rearranged_twiddle_tab_stride2_arr_%d" % n,list(tab2Offset)) |
| 141 | |
| 142 | printCUInt32Array(f,"rearranged_twiddle_tab_stride3_arr_%d" % n,list(tab3Offset)) |
| 143 | printHUInt32Array(h,"rearranged_twiddle_tab_stride3_arr_%d" % n,list(tab3Offset)) |
| 144 | |
| 145 | printCFloat32Array(f,"rearranged_twiddle_stride1_%d" % n,list(tab1)) |
| 146 | printHFloat32Array(h,"rearranged_twiddle_stride1_%d" % n,list(tab1)) |
| 147 | |
| 148 | printCFloat32Array(f,"rearranged_twiddle_stride2_%d" % n,list(tab2)) |
| 149 | printHFloat32Array(h,"rearranged_twiddle_stride2_%d" % n,list(tab2)) |
| 150 | |
| 151 | printCFloat32Array(f,"rearranged_twiddle_stride3_%d" % n,list(tab3)) |
| 152 | printHFloat32Array(h,"rearranged_twiddle_stride3_%d" % n,list(tab3)) |
| 153 | print("#endif\n",file=f) |
| 154 | print("#endif\n",file=h) |
| 155 | |
| 156 | |
| 157 | |
| 158 | #test = twiddle(16) |
| 159 | #printCFloat32Array("Test",list(test)) |
| 160 | |
| 161 | cheader="""/* ---------------------------------------------------------------------- |
| 162 | * Project: CMSIS DSP Library |
| 163 | * Title: arm_mve_tables.c |
| 164 | * Description: common tables like fft twiddle factors, Bitreverse, reciprocal etc |
| 165 | * used for MVE implementation only |
| 166 | * |
| 167 | * $Date: 08. January 2020 |
| 168 | * $Revision: V1.7.0 |
| 169 | * |
| 170 | * Target Processor: Cortex-M cores |
| 171 | * -------------------------------------------------------------------- */ |
| 172 | /* |
| 173 | * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved. |
| 174 | * |
| 175 | * SPDX-License-Identifier: Apache-2.0 |
| 176 | * |
| 177 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 178 | * not use this file except in compliance with the License. |
| 179 | * You may obtain a copy of the License at |
| 180 | * |
| 181 | * www.apache.org/licenses/LICENSE-2.0 |
| 182 | * |
| 183 | * Unless required by applicable law or agreed to in writing, software |
| 184 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 185 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 186 | * See the License for the specific language governing permissions and |
| 187 | * limitations under the License. |
| 188 | */ |
| 189 | |
| 190 | #include "arm_math.h" |
| 191 | |
| 192 | #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) |
| 193 | |
| 194 | #if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) |
| 195 | |
| 196 | |
| 197 | """ |
| 198 | |
| 199 | cfooter=""" |
| 200 | |
| 201 | #endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) */ |
| 202 | #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */ |
| 203 | """ |
| 204 | |
| 205 | hheader="""/* ---------------------------------------------------------------------- |
| 206 | * Project: CMSIS DSP Library |
| 207 | * Title: arm_mve_tables.h |
| 208 | * Description: common tables like fft twiddle factors, Bitreverse, reciprocal etc |
| 209 | * used for MVE implementation only |
| 210 | * |
| 211 | * $Date: 08. January 2020 |
| 212 | * $Revision: V1.7.0 |
| 213 | * |
| 214 | * Target Processor: Cortex-M cores |
| 215 | * -------------------------------------------------------------------- */ |
| 216 | /* |
| 217 | * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved. |
| 218 | * |
| 219 | * SPDX-License-Identifier: Apache-2.0 |
| 220 | * |
| 221 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 222 | * not use this file except in compliance with the License. |
| 223 | * You may obtain a copy of the License at |
| 224 | * |
| 225 | * www.apache.org/licenses/LICENSE-2.0 |
| 226 | * |
| 227 | * Unless required by applicable law or agreed to in writing, software |
| 228 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 229 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 230 | * See the License for the specific language governing permissions and |
| 231 | * limitations under the License. |
| 232 | */ |
| 233 | |
| 234 | #ifndef _ARM_MVE_TABLES_H |
| 235 | #define _ARM_MVE_TABLES_H |
| 236 | |
| 237 | #include "arm_math.h" |
| 238 | |
| 239 | #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) |
| 240 | |
| 241 | #if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) |
| 242 | |
| 243 | |
| 244 | """ |
| 245 | |
| 246 | hfooter=""" |
| 247 | #endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES) */ |
| 248 | |
| 249 | #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */ |
| 250 | |
| 251 | #endif /*_ARM_MVE_TABLES_H*/ |
| 252 | """ |
| 253 | |
| 254 | with open(args.f,'w') as f: |
| 255 | with open(args.he,'w') as h: |
| 256 | print(cheader,file=f) |
| 257 | print(hheader,file=h) |
| 258 | |
| 259 | reorderTwiddle(f,h,16) |
| 260 | reorderTwiddle(f,h,64) |
| 261 | reorderTwiddle(f,h,256) |
| 262 | reorderTwiddle(f,h,1024) |
| 263 | reorderTwiddle(f,h,4096) |
| 264 | |
| 265 | print(cfooter,file=f) |
| 266 | print(hfooter,file=h) |