1 // SPDX-License-Identifier: BSD-3-Clause
2 //
3 // Copyright(c) 2020 Intel Corporation. All rights reserved.
4 //
5 // Author: Amery Song <chao.song@intel.com>
6 //	   Keyon Jie <yang.jie@linux.intel.com>
7 
8 #include <sof/audio/buffer.h>
9 #include <sof/audio/format.h>
10 #include <sof/common.h>
11 #include <rtos/alloc.h>
12 #include <sof/math/fft.h>
13 
fft_plan_new(void * inb,void * outb,uint32_t size,int bits)14 struct fft_plan *fft_plan_new(void *inb, void *outb, uint32_t size, int bits)
15 {
16 	struct fft_plan *plan;
17 	int lim = 1;
18 	int len = 0;
19 	int i;
20 
21 	if (!inb || !outb)
22 		return NULL;
23 
24 	plan = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(struct fft_plan));
25 	if (!plan)
26 		return NULL;
27 
28 	switch (bits) {
29 	case 16:
30 		plan->inb16 = inb;
31 		plan->outb16 = outb;
32 		break;
33 	case 32:
34 		plan->inb32 = inb;
35 		plan->outb32 = outb;
36 		break;
37 	default:
38 		rfree(plan);
39 		return NULL;
40 	}
41 
42 	/* calculate the exponent of 2 */
43 	while (lim < size) {
44 		lim <<= 1;
45 		len++;
46 	}
47 
48 	plan->size = lim;
49 	plan->len = len;
50 
51 	plan->bit_reverse_idx = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
52 					plan->size * sizeof(uint16_t));
53 	if (!plan->bit_reverse_idx) {
54 		rfree(plan);
55 		return NULL;
56 	}
57 
58 	/* set up the bit reverse index */
59 	for (i = 1; i < plan->size; ++i)
60 		plan->bit_reverse_idx[i] = (plan->bit_reverse_idx[i >> 1] >> 1) |
61 					   ((i & 1) << (len - 1));
62 
63 	return plan;
64 }
65 
fft_plan_free(struct fft_plan * plan)66 void fft_plan_free(struct fft_plan *plan)
67 {
68 	if (!plan)
69 		return;
70 
71 	rfree(plan->bit_reverse_idx);
72 	rfree(plan);
73 }
74