1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Python Wrapper
3  * Title:        cmsismodule.h
4  * Description:  C code for the CMSIS-DSP Python wrapper
5  *
6  * $Date:        27 April 2021
7  * $Revision:    V1.0
8  *
9  * Target Processor: Cortex-M cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 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 
29 #define MODNAME "cmsisdsp_fastmath"
30 #define MODINITNAME cmsisdsp_fastmath
31 
32 #include "cmsisdsp_module.h"
33 
34 
35 NUMPYVECTORFROMBUFFER(f32,float32_t,NPY_FLOAT);
36 
37 
38 
39 
typeRegistration(PyObject * module)40 void typeRegistration(PyObject *module) {
41 
42 
43 }
44 
45 
46 
47 static PyObject *
cmsis_arm_vlog_q15(PyObject * obj,PyObject * args)48 cmsis_arm_vlog_q15(PyObject *obj, PyObject *args)
49 {
50 
51   PyObject *pSrc=NULL; // input
52   q15_t *pSrc_converted=NULL; // input
53   q15_t *pDst=NULL; // output
54   uint32_t blockSize; // input
55 
56   if (PyArg_ParseTuple(args,"O",&pSrc))
57   {
58 
59     GETARGUMENT(pSrc,NPY_INT16,int16_t,int16_t);
60     blockSize = arraySizepSrc ;
61 
62     pDst=PyMem_Malloc(sizeof(q15_t)*blockSize);
63 
64 
65     arm_vlog_q15(pSrc_converted,pDst,blockSize);
66  INT16ARRAY1(pDstOBJ,blockSize,pDst);
67 
68     PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
69 
70     FREEARGUMENT(pSrc_converted);
71     Py_DECREF(pDstOBJ);
72     return(pythonResult);
73 
74   }
75   return(NULL);
76 }
77 
78 
79 
80 
81 static PyObject *
cmsis_arm_vlog_q31(PyObject * obj,PyObject * args)82 cmsis_arm_vlog_q31(PyObject *obj, PyObject *args)
83 {
84 
85   PyObject *pSrc=NULL; // input
86   q31_t *pSrc_converted=NULL; // input
87   q31_t *pDst=NULL; // output
88   uint32_t blockSize; // input
89 
90   if (PyArg_ParseTuple(args,"O",&pSrc))
91   {
92 
93     GETARGUMENT(pSrc,NPY_INT32,int32_t,int32_t);
94     blockSize = arraySizepSrc ;
95 
96     pDst=PyMem_Malloc(sizeof(q31_t)*blockSize);
97 
98 
99     arm_vlog_q31(pSrc_converted,pDst,blockSize);
100  INT32ARRAY1(pDstOBJ,blockSize,pDst);
101 
102     PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
103 
104     FREEARGUMENT(pSrc_converted);
105     Py_DECREF(pDstOBJ);
106     return(pythonResult);
107 
108   }
109   return(NULL);
110 }
111 
112 
113 
114 static PyObject *
cmsis_arm_sin_f32(PyObject * obj,PyObject * args)115 cmsis_arm_sin_f32(PyObject *obj, PyObject *args)
116 {
117 
118   float32_t x; // input
119 
120   if (PyArg_ParseTuple(args,"f",&x))
121   {
122 
123 
124     float32_t returnValue = arm_sin_f32(x);
125     PyObject* theReturnOBJ=Py_BuildValue("f",returnValue);
126 
127     PyObject *pythonResult = Py_BuildValue("O",theReturnOBJ);
128 
129     Py_DECREF(theReturnOBJ);
130     return(pythonResult);
131 
132   }
133   return(NULL);
134 }
135 
136 
137 static PyObject *
cmsis_arm_sin_q31(PyObject * obj,PyObject * args)138 cmsis_arm_sin_q31(PyObject *obj, PyObject *args)
139 {
140 
141   q31_t x; // input
142 
143   if (PyArg_ParseTuple(args,"i",&x))
144   {
145 
146 
147     q31_t returnValue = arm_sin_q31(x);
148     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
149 
150     PyObject *pythonResult = Py_BuildValue("O",theReturnOBJ);
151 
152     Py_DECREF(theReturnOBJ);
153     return(pythonResult);
154 
155   }
156   return(NULL);
157 }
158 
159 
160 static PyObject *
cmsis_arm_sin_q15(PyObject * obj,PyObject * args)161 cmsis_arm_sin_q15(PyObject *obj, PyObject *args)
162 {
163 
164   q15_t x; // input
165 
166   if (PyArg_ParseTuple(args,"h",&x))
167   {
168 
169 
170     q15_t returnValue = arm_sin_q15(x);
171     PyObject* theReturnOBJ=Py_BuildValue("h",returnValue);
172 
173     PyObject *pythonResult = Py_BuildValue("O",theReturnOBJ);
174 
175     Py_DECREF(theReturnOBJ);
176     return(pythonResult);
177 
178   }
179   return(NULL);
180 }
181 
182 
183 static PyObject *
cmsis_arm_cos_f32(PyObject * obj,PyObject * args)184 cmsis_arm_cos_f32(PyObject *obj, PyObject *args)
185 {
186 
187   float32_t x; // input
188 
189   if (PyArg_ParseTuple(args,"f",&x))
190   {
191 
192 
193     float32_t returnValue = arm_cos_f32(x);
194     PyObject* theReturnOBJ=Py_BuildValue("f",returnValue);
195 
196     PyObject *pythonResult = Py_BuildValue("O",theReturnOBJ);
197 
198     Py_DECREF(theReturnOBJ);
199     return(pythonResult);
200 
201   }
202   return(NULL);
203 }
204 
205 
206 static PyObject *
cmsis_arm_cos_q31(PyObject * obj,PyObject * args)207 cmsis_arm_cos_q31(PyObject *obj, PyObject *args)
208 {
209 
210   q31_t x; // input
211 
212   if (PyArg_ParseTuple(args,"i",&x))
213   {
214 
215 
216     q31_t returnValue = arm_cos_q31(x);
217     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
218 
219     PyObject *pythonResult = Py_BuildValue("O",theReturnOBJ);
220 
221     Py_DECREF(theReturnOBJ);
222     return(pythonResult);
223 
224   }
225   return(NULL);
226 }
227 
228 
229 static PyObject *
cmsis_arm_cos_q15(PyObject * obj,PyObject * args)230 cmsis_arm_cos_q15(PyObject *obj, PyObject *args)
231 {
232 
233   q15_t x; // input
234 
235   if (PyArg_ParseTuple(args,"h",&x))
236   {
237 
238 
239     q15_t returnValue = arm_cos_q15(x);
240     PyObject* theReturnOBJ=Py_BuildValue("h",returnValue);
241 
242     PyObject *pythonResult = Py_BuildValue("O",theReturnOBJ);
243 
244     Py_DECREF(theReturnOBJ);
245     return(pythonResult);
246 
247   }
248   return(NULL);
249 }
250 
251 
252 static PyObject *
cmsis_arm_sqrt_f32(PyObject * obj,PyObject * args)253 cmsis_arm_sqrt_f32(PyObject *obj, PyObject *args)
254 {
255 
256   float32_t in; // input
257   float32_t pOut; // output
258 
259   if (PyArg_ParseTuple(args,"f",&in))
260   {
261 
262 
263     arm_status returnValue = arm_sqrt_f32(in,&pOut);
264     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
265     PyObject* pOutOBJ=Py_BuildValue("f",pOut);
266 
267     PyObject *pythonResult = Py_BuildValue("OO",theReturnOBJ,pOutOBJ);
268 
269     Py_DECREF(theReturnOBJ);
270     Py_DECREF(pOutOBJ);
271 
272     return(pythonResult);
273 
274   }
275   return(NULL);
276 }
277 
278 
279 static PyObject *
cmsis_arm_sqrt_q31(PyObject * obj,PyObject * args)280 cmsis_arm_sqrt_q31(PyObject *obj, PyObject *args)
281 {
282 
283   q31_t in; // input
284   q31_t pOut; // output
285 
286   if (PyArg_ParseTuple(args,"i",&in))
287   {
288 
289 
290 
291 
292     arm_status returnValue = arm_sqrt_q31(in,&pOut);
293     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
294     PyObject* pOutOBJ=Py_BuildValue("i",pOut);
295 
296     PyObject *pythonResult = Py_BuildValue("OO",theReturnOBJ,pOutOBJ);
297 
298     Py_DECREF(theReturnOBJ);
299     Py_DECREF(pOutOBJ);
300     return(pythonResult);
301 
302   }
303   return(NULL);
304 }
305 
306 static PyObject *
cmsis_arm_divide_q31(PyObject * obj,PyObject * args)307 cmsis_arm_divide_q31(PyObject *obj, PyObject *args)
308 {
309 
310   q31_t num,den; // input
311   q31_t pOut;
312   int16_t shift; // output
313 
314   if (PyArg_ParseTuple(args,"ii",&num,&den))
315   {
316 
317 
318 
319     arm_status returnValue = arm_divide_q31(num,den,&pOut,&shift);
320     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
321     PyObject* pOutOBJ=Py_BuildValue("i",pOut);
322     PyObject* pShiftOBJ=Py_BuildValue("h",shift);
323 
324     PyObject *pythonResult = Py_BuildValue("OOO",theReturnOBJ,pOutOBJ,pShiftOBJ);
325 
326     Py_DECREF(theReturnOBJ);
327     Py_DECREF(pOutOBJ);
328     Py_DECREF(pShiftOBJ);
329     return(pythonResult);
330 
331   }
332   return(NULL);
333 }
334 
335 static PyObject *
cmsis_arm_divide_q15(PyObject * obj,PyObject * args)336 cmsis_arm_divide_q15(PyObject *obj, PyObject *args)
337 {
338 
339   q15_t num,den; // input
340   q15_t pOut;
341   int16_t shift; // output
342 
343   if (PyArg_ParseTuple(args,"hh",&num,&den))
344   {
345 
346 
347 
348     arm_status returnValue = arm_divide_q15(num,den,&pOut,&shift);
349     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
350     PyObject* pOutOBJ=Py_BuildValue("h",pOut);
351     PyObject* pShiftOBJ=Py_BuildValue("h",shift);
352 
353     PyObject *pythonResult = Py_BuildValue("OOO",theReturnOBJ,pOutOBJ,pShiftOBJ);
354 
355     Py_DECREF(theReturnOBJ);
356     Py_DECREF(pOutOBJ);
357     Py_DECREF(pShiftOBJ);
358     return(pythonResult);
359 
360   }
361   return(NULL);
362 }
363 
364 static PyObject *
cmsis_arm_sqrt_q15(PyObject * obj,PyObject * args)365 cmsis_arm_sqrt_q15(PyObject *obj, PyObject *args)
366 {
367 
368   q15_t in; // input
369   q15_t pOut; // output
370 
371   if (PyArg_ParseTuple(args,"h",&in))
372   {
373 
374 
375 
376     arm_status returnValue = arm_sqrt_q15(in,&pOut);
377     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
378     PyObject* pOutOBJ=Py_BuildValue("h",pOut);
379 
380     PyObject *pythonResult = Py_BuildValue("OO",theReturnOBJ,pOutOBJ);
381 
382     Py_DECREF(theReturnOBJ);
383     Py_DECREF(pOutOBJ);
384     return(pythonResult);
385 
386   }
387   return(NULL);
388 }
389 
390 
391 static PyObject *
cmsis_arm_vexp_f32(PyObject * obj,PyObject * args)392 cmsis_arm_vexp_f32(PyObject *obj, PyObject *args)
393 {
394 
395   PyObject *pSrc=NULL; // input
396   float32_t *pSrc_converted=NULL; // input
397   float32_t *pDst=NULL; // output
398   uint32_t blockSize; // input
399 
400   if (PyArg_ParseTuple(args,"O",&pSrc))
401   {
402 
403     GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
404     blockSize = arraySizepSrc ;
405 
406     pDst=PyMem_Malloc(sizeof(float32_t)*blockSize);
407 
408 
409     arm_vexp_f32(pSrc_converted,pDst,blockSize);
410  FLOATARRAY1(pDstOBJ,blockSize,pDst);
411 
412     PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
413 
414     FREEARGUMENT(pSrc_converted);
415     Py_DECREF(pDstOBJ);
416     return(pythonResult);
417 
418   }
419   return(NULL);
420 }
421 
422 static PyObject *
cmsis_arm_vexp_f64(PyObject * obj,PyObject * args)423 cmsis_arm_vexp_f64(PyObject *obj, PyObject *args)
424 {
425 
426   PyObject *pSrc=NULL; // input
427   float64_t *pSrc_converted=NULL; // input
428   float64_t *pDst=NULL; // output
429   uint32_t blockSize; // input
430 
431   if (PyArg_ParseTuple(args,"O",&pSrc))
432   {
433 
434     GETARGUMENT(pSrc,NPY_DOUBLE,double,float64_t);
435     blockSize = arraySizepSrc ;
436 
437     pDst=PyMem_Malloc(sizeof(float64_t)*blockSize);
438 
439 
440     arm_vexp_f64(pSrc_converted,pDst,blockSize);
441  FLOAT64ARRAY1(pDstOBJ,blockSize,pDst);
442 
443     PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
444 
445     FREEARGUMENT(pSrc_converted);
446     Py_DECREF(pDstOBJ);
447     return(pythonResult);
448 
449   }
450   return(NULL);
451 }
452 
453 static PyObject *
cmsis_arm_vlog_f32(PyObject * obj,PyObject * args)454 cmsis_arm_vlog_f32(PyObject *obj, PyObject *args)
455 {
456 
457   PyObject *pSrc=NULL; // input
458   float32_t *pSrc_converted=NULL; // input
459   float32_t *pDst=NULL; // output
460   uint32_t blockSize; // input
461 
462   if (PyArg_ParseTuple(args,"O",&pSrc))
463   {
464 
465     GETARGUMENT(pSrc,NPY_DOUBLE,double,float32_t);
466     blockSize = arraySizepSrc ;
467 
468     pDst=PyMem_Malloc(sizeof(float32_t)*blockSize);
469 
470 
471     arm_vlog_f32(pSrc_converted,pDst,blockSize);
472  FLOATARRAY1(pDstOBJ,blockSize,pDst);
473 
474     PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
475 
476     FREEARGUMENT(pSrc_converted);
477     Py_DECREF(pDstOBJ);
478     return(pythonResult);
479 
480   }
481   return(NULL);
482 }
483 
484 static PyObject *
cmsis_arm_vlog_f64(PyObject * obj,PyObject * args)485 cmsis_arm_vlog_f64(PyObject *obj, PyObject *args)
486 {
487 
488   PyObject *pSrc=NULL; // input
489   float64_t *pSrc_converted=NULL; // input
490   float64_t *pDst=NULL; // output
491   uint32_t blockSize; // input
492 
493   if (PyArg_ParseTuple(args,"O",&pSrc))
494   {
495 
496     GETARGUMENT(pSrc,NPY_DOUBLE,double,float64_t);
497     blockSize = arraySizepSrc ;
498 
499     pDst=PyMem_Malloc(sizeof(float64_t)*blockSize);
500 
501 
502     arm_vlog_f64(pSrc_converted,pDst,blockSize);
503  FLOAT64ARRAY1(pDstOBJ,blockSize,pDst);
504 
505     PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);
506 
507     FREEARGUMENT(pSrc_converted);
508     Py_DECREF(pDstOBJ);
509     return(pythonResult);
510 
511   }
512   return(NULL);
513 }
514 
515 static PyObject *
cmsis_arm_atan2_q31(PyObject * obj,PyObject * args)516 cmsis_arm_atan2_q31(PyObject *obj, PyObject *args)
517 {
518 
519   q31_t x,y; // input
520   q31_t pOut;
521 
522   if (PyArg_ParseTuple(args,"ii",&y,&x))
523   {
524 
525 
526 
527     arm_status returnValue = arm_atan2_q31(y,x,&pOut);
528     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
529     PyObject* pOutOBJ=Py_BuildValue("i",pOut);
530 
531     PyObject *pythonResult = Py_BuildValue("OO",theReturnOBJ,pOutOBJ);
532 
533     Py_DECREF(theReturnOBJ);
534     Py_DECREF(pOutOBJ);
535     return(pythonResult);
536 
537   }
538   return(NULL);
539 }
540 
541 static PyObject *
cmsis_arm_atan2_q15(PyObject * obj,PyObject * args)542 cmsis_arm_atan2_q15(PyObject *obj, PyObject *args)
543 {
544 
545   q15_t x,y; // input
546   q15_t pOut;
547 
548   if (PyArg_ParseTuple(args,"hh",&y,&x))
549   {
550 
551 
552 
553     arm_status returnValue = arm_atan2_q15(y,x,&pOut);
554     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
555     PyObject* pOutOBJ=Py_BuildValue("h",pOut);
556 
557     PyObject *pythonResult = Py_BuildValue("OO",theReturnOBJ,pOutOBJ);
558 
559     Py_DECREF(theReturnOBJ);
560     Py_DECREF(pOutOBJ);
561     return(pythonResult);
562 
563   }
564   return(NULL);
565 }
566 
567 static PyObject *
cmsis_arm_atan2_f32(PyObject * obj,PyObject * args)568 cmsis_arm_atan2_f32(PyObject *obj, PyObject *args)
569 {
570 
571   float32_t x,y; // input
572   float32_t pOut;
573 
574   if (PyArg_ParseTuple(args,"ff",&y,&x))
575   {
576 
577 
578 
579     arm_status returnValue = arm_atan2_f32(y,x,&pOut);
580     PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
581     PyObject* pOutOBJ=Py_BuildValue("f",pOut);
582 
583     PyObject *pythonResult = Py_BuildValue("OO",theReturnOBJ,pOutOBJ);
584 
585     Py_DECREF(theReturnOBJ);
586     Py_DECREF(pOutOBJ);
587     return(pythonResult);
588 
589   }
590   return(NULL);
591 }
592 
593 
594 static PyMethodDef CMSISDSPMethods[] = {
595 
596 
597 
598 
599 {"arm_vlog_q15",  cmsis_arm_vlog_q15, METH_VARARGS,""},
600 {"arm_vlog_q31",  cmsis_arm_vlog_q31, METH_VARARGS,""},
601 
602 
603 {"arm_sin_f32",  cmsis_arm_sin_f32, METH_VARARGS,""},
604 {"arm_sin_q31",  cmsis_arm_sin_q31, METH_VARARGS,""},
605 {"arm_sin_q15",  cmsis_arm_sin_q15, METH_VARARGS,""},
606 {"arm_cos_f32",  cmsis_arm_cos_f32, METH_VARARGS,""},
607 {"arm_cos_q31",  cmsis_arm_cos_q31, METH_VARARGS,""},
608 {"arm_cos_q15",  cmsis_arm_cos_q15, METH_VARARGS,""},
609 {"arm_sqrt_f32",  cmsis_arm_sqrt_f32, METH_VARARGS,""},
610 {"arm_sqrt_q31",  cmsis_arm_sqrt_q31, METH_VARARGS,""},
611 {"arm_sqrt_q15",  cmsis_arm_sqrt_q15, METH_VARARGS,""},
612 {"arm_divide_q31",  cmsis_arm_divide_q31, METH_VARARGS,""},
613 {"arm_divide_q15",  cmsis_arm_divide_q15, METH_VARARGS,""},
614 {"arm_vexp_f32",  cmsis_arm_vexp_f32, METH_VARARGS,""},
615 {"arm_vlog_f32",  cmsis_arm_vlog_f32, METH_VARARGS,""},
616 {"arm_vexp_f64",  cmsis_arm_vexp_f64, METH_VARARGS,""},
617 {"arm_vlog_f64",  cmsis_arm_vlog_f64, METH_VARARGS,""},
618 {"arm_atan2_f32",  cmsis_arm_atan2_f32, METH_VARARGS,""},
619 {"arm_atan2_q31",  cmsis_arm_atan2_q31, METH_VARARGS,""},
620 {"arm_atan2_q15",  cmsis_arm_atan2_q15, METH_VARARGS,""},
621     {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
622     {NULL, NULL, 0, NULL}        /* Sentinel */
623 };
624 
625 #ifdef IS_PY3K
cmsisdsp_traverse(PyObject * m,visitproc visit,void * arg)626 static int cmsisdsp_traverse(PyObject *m, visitproc visit, void *arg) {
627     Py_VISIT(GETSTATE(m)->error);
628     return 0;
629 }
630 
cmsisdsp_clear(PyObject * m)631 static int cmsisdsp_clear(PyObject *m) {
632     Py_CLEAR(GETSTATE(m)->error);
633     return 0;
634 }
635 
636 
637 static struct PyModuleDef moduledef = {
638         PyModuleDef_HEAD_INIT,
639         MODNAME,
640         NULL,
641         sizeof(struct module_state),
642         CMSISDSPMethods,
643         NULL,
644         cmsisdsp_traverse,
645         cmsisdsp_clear,
646         NULL
647 };
648 
649 #define INITERROR return NULL
650 
651 PyMODINIT_FUNC
CAT(PyInit_,MODINITNAME)652 CAT(PyInit_,MODINITNAME)(void)
653 
654 
655 #else
656 #define INITERROR return
657 
658 void CAT(init,MODINITNAME)(void)
659 #endif
660 {
661     import_array();
662 
663   #ifdef IS_PY3K
664     PyObject *module = PyModule_Create(&moduledef);
665   #else
666     PyObject *module = Py_InitModule(MODNAME, CMSISDSPMethods);
667   #endif
668 
669   if (module == NULL)
670       INITERROR;
671   struct module_state *st = GETSTATE(module);
672 
673   st->error = PyErr_NewException(MODNAME".Error", NULL, NULL);
674   if (st->error == NULL) {
675       Py_DECREF(module);
676       INITERROR;
677   }
678 
679 
680   typeRegistration(module);
681 
682   #ifdef IS_PY3K
683     return module;
684   #endif
685 }