1 /* Copyright (c) 2015 Xiph.Org Foundation
2    Written by Viswanath Puttagunta */
3 /**
4    @file celt_fft_ne10.c
5    @brief ARM Neon optimizations for fft using NE10 library
6  */
7 
8 /*
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12 
13    - Redistributions of source code must retain the above copyright
14    notice, this list of conditions and the following disclaimer.
15 
16    - Redistributions in binary form must reproduce the above copyright
17    notice, this list of conditions and the following disclaimer in the
18    documentation and/or other materials provided with the distribution.
19 
20    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #ifndef SKIP_CONFIG_H
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 #endif
38 
39 #include <NE10_dsp.h>
40 #include "os_support.h"
41 #include "kiss_fft.h"
42 #include "stack_alloc.h"
43 
44 #if !defined(FIXED_POINT)
45 # define NE10_FFT_ALLOC_C2C_TYPE_NEON ne10_fft_alloc_c2c_float32_neon
46 # define NE10_FFT_CFG_TYPE_T ne10_fft_cfg_float32_t
47 # define NE10_FFT_STATE_TYPE_T ne10_fft_state_float32_t
48 # define NE10_FFT_DESTROY_C2C_TYPE ne10_fft_destroy_c2c_float32
49 # define NE10_FFT_CPX_TYPE_T ne10_fft_cpx_float32_t
50 # define NE10_FFT_C2C_1D_TYPE_NEON ne10_fft_c2c_1d_float32_neon
51 #else
52 # define NE10_FFT_ALLOC_C2C_TYPE_NEON(nfft) ne10_fft_alloc_c2c_int32_neon(nfft)
53 # define NE10_FFT_CFG_TYPE_T ne10_fft_cfg_int32_t
54 # define NE10_FFT_STATE_TYPE_T ne10_fft_state_int32_t
55 # define NE10_FFT_DESTROY_C2C_TYPE ne10_fft_destroy_c2c_int32
56 # define NE10_FFT_DESTROY_C2C_TYPE ne10_fft_destroy_c2c_int32
57 # define NE10_FFT_CPX_TYPE_T ne10_fft_cpx_int32_t
58 # define NE10_FFT_C2C_1D_TYPE_NEON ne10_fft_c2c_1d_int32_neon
59 #endif
60 
61 #if defined(CUSTOM_MODES)
62 
63 /* nfft lengths in NE10 that support scaled fft */
64 # define NE10_FFTSCALED_SUPPORT_MAX 4
65 static const int ne10_fft_scaled_support[NE10_FFTSCALED_SUPPORT_MAX] = {
66    480, 240, 120, 60
67 };
68 
opus_fft_alloc_arm_neon(kiss_fft_state * st)69 int opus_fft_alloc_arm_neon(kiss_fft_state *st)
70 {
71    int i;
72    size_t memneeded = sizeof(struct arch_fft_state);
73 
74    st->arch_fft = (arch_fft_state *)opus_alloc(memneeded);
75    if (!st->arch_fft)
76       return -1;
77 
78    for (i = 0; i < NE10_FFTSCALED_SUPPORT_MAX; i++) {
79       if(st->nfft == ne10_fft_scaled_support[i])
80          break;
81    }
82    if (i == NE10_FFTSCALED_SUPPORT_MAX) {
83       /* This nfft length (scaled fft) is not supported in NE10 */
84       st->arch_fft->is_supported = 0;
85       st->arch_fft->priv = NULL;
86    }
87    else {
88       st->arch_fft->is_supported = 1;
89       st->arch_fft->priv = (void *)NE10_FFT_ALLOC_C2C_TYPE_NEON(st->nfft);
90       if (st->arch_fft->priv == NULL) {
91          return -1;
92       }
93    }
94    return 0;
95 }
96 
opus_fft_free_arm_neon(kiss_fft_state * st)97 void opus_fft_free_arm_neon(kiss_fft_state *st)
98 {
99    NE10_FFT_CFG_TYPE_T cfg;
100 
101    if (!st->arch_fft)
102       return;
103 
104    cfg = (NE10_FFT_CFG_TYPE_T)st->arch_fft->priv;
105    if (cfg)
106       NE10_FFT_DESTROY_C2C_TYPE(cfg);
107    opus_free(st->arch_fft);
108 }
109 #endif
110 
opus_fft_neon(const kiss_fft_state * st,const kiss_fft_cpx * fin,kiss_fft_cpx * fout)111 void opus_fft_neon(const kiss_fft_state *st,
112                    const kiss_fft_cpx *fin,
113                    kiss_fft_cpx *fout)
114 {
115    NE10_FFT_STATE_TYPE_T state;
116    NE10_FFT_CFG_TYPE_T cfg = &state;
117    VARDECL(NE10_FFT_CPX_TYPE_T, buffer);
118    SAVE_STACK;
119    ALLOC(buffer, st->nfft, NE10_FFT_CPX_TYPE_T);
120 
121    if (!st->arch_fft->is_supported) {
122       /* This nfft length (scaled fft) not supported in NE10 */
123       opus_fft_c(st, fin, fout);
124    }
125    else {
126       memcpy((void *)cfg, st->arch_fft->priv, sizeof(NE10_FFT_STATE_TYPE_T));
127       state.buffer = (NE10_FFT_CPX_TYPE_T *)&buffer[0];
128 #if !defined(FIXED_POINT)
129       state.is_forward_scaled = 1;
130 
131       NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
132                                 (NE10_FFT_CPX_TYPE_T *)fin,
133                                 cfg, 0);
134 #else
135       NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
136                                 (NE10_FFT_CPX_TYPE_T *)fin,
137                                 cfg, 0, 1);
138 #endif
139    }
140    RESTORE_STACK;
141 }
142 
opus_ifft_neon(const kiss_fft_state * st,const kiss_fft_cpx * fin,kiss_fft_cpx * fout)143 void opus_ifft_neon(const kiss_fft_state *st,
144                     const kiss_fft_cpx *fin,
145                     kiss_fft_cpx *fout)
146 {
147    NE10_FFT_STATE_TYPE_T state;
148    NE10_FFT_CFG_TYPE_T cfg = &state;
149    VARDECL(NE10_FFT_CPX_TYPE_T, buffer);
150    SAVE_STACK;
151    ALLOC(buffer, st->nfft, NE10_FFT_CPX_TYPE_T);
152 
153    if (!st->arch_fft->is_supported) {
154       /* This nfft length (scaled fft) not supported in NE10 */
155       opus_ifft_c(st, fin, fout);
156    }
157    else {
158       memcpy((void *)cfg, st->arch_fft->priv, sizeof(NE10_FFT_STATE_TYPE_T));
159       state.buffer = (NE10_FFT_CPX_TYPE_T *)&buffer[0];
160 #if !defined(FIXED_POINT)
161       state.is_backward_scaled = 0;
162 
163       NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
164                                 (NE10_FFT_CPX_TYPE_T *)fin,
165                                 cfg, 1);
166 #else
167       NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
168                                 (NE10_FFT_CPX_TYPE_T *)fin,
169                                 cfg, 1, 0);
170 #endif
171    }
172    RESTORE_STACK;
173 }
174