1/* Copyright (c) 2015 Xiph.Org Foundation
2   Written by Viswanath Puttagunta */
3/**
4   @file celt_ne10_fft.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_init.h>
40#include <NE10_dsp.h>
41#include "os_support.h"
42#include "kiss_fft.h"
43#include "stack_alloc.h"
44
45#if !defined(FIXED_POINT)
46# define NE10_FFT_ALLOC_C2C_TYPE_NEON ne10_fft_alloc_c2c_float32_neon
47# define NE10_FFT_CFG_TYPE_T ne10_fft_cfg_float32_t
48# define NE10_FFT_STATE_TYPE_T ne10_fft_state_float32_t
49# define NE10_FFT_DESTROY_C2C_TYPE ne10_fft_destroy_c2c_float32
50# define NE10_FFT_CPX_TYPE_T ne10_fft_cpx_float32_t
51# define NE10_FFT_C2C_1D_TYPE_NEON ne10_fft_c2c_1d_float32_neon
52#else
53# define NE10_FFT_ALLOC_C2C_TYPE_NEON(nfft) ne10_fft_alloc_c2c_int32_neon(nfft)
54# define NE10_FFT_CFG_TYPE_T ne10_fft_cfg_int32_t
55# define NE10_FFT_STATE_TYPE_T ne10_fft_state_int32_t
56# define NE10_FFT_DESTROY_C2C_TYPE ne10_fft_destroy_c2c_int32
57# define NE10_FFT_DESTROY_C2C_TYPE ne10_fft_destroy_c2c_int32
58# define NE10_FFT_CPX_TYPE_T ne10_fft_cpx_int32_t
59# define NE10_FFT_C2C_1D_TYPE_NEON ne10_fft_c2c_1d_int32_neon
60#endif
61
62#if defined(CUSTOM_MODES)
63
64/* nfft lengths in NE10 that support scaled fft */
65# define NE10_FFTSCALED_SUPPORT_MAX 4
66static const int ne10_fft_scaled_support[NE10_FFTSCALED_SUPPORT_MAX] = {
67   480, 240, 120, 60
68};
69
70int opus_fft_alloc_arm_neon(kiss_fft_state *st)
71{
72   int i;
73   size_t memneeded = sizeof(struct arch_fft_state);
74
75   st->arch_fft = (arch_fft_state *)opus_alloc(memneeded);
76   if (!st->arch_fft)
77      return -1;
78
79   for (i = 0; i < NE10_FFTSCALED_SUPPORT_MAX; i++) {
80      if(st->nfft == ne10_fft_scaled_support[i])
81         break;
82   }
83   if (i == NE10_FFTSCALED_SUPPORT_MAX) {
84      /* This nfft length (scaled fft) is not supported in NE10 */
85      st->arch_fft->is_supported = 0;
86      st->arch_fft->priv = NULL;
87   }
88   else {
89      st->arch_fft->is_supported = 1;
90      st->arch_fft->priv = (void *)NE10_FFT_ALLOC_C2C_TYPE_NEON(st->nfft);
91      if (st->arch_fft->priv == NULL) {
92         return -1;
93      }
94   }
95   return 0;
96}
97
98void opus_fft_free_arm_neon(kiss_fft_state *st)
99{
100   NE10_FFT_CFG_TYPE_T cfg;
101
102   if (!st->arch_fft)
103      return;
104
105   cfg = (NE10_FFT_CFG_TYPE_T)st->arch_fft->priv;
106   if (cfg)
107      NE10_FFT_DESTROY_C2C_TYPE(cfg);
108   opus_free(st->arch_fft);
109}
110#endif
111
112void opus_fft_neon(const kiss_fft_state *st,
113                   const kiss_fft_cpx *fin,
114                   kiss_fft_cpx *fout)
115{
116   NE10_FFT_STATE_TYPE_T state;
117   NE10_FFT_CFG_TYPE_T cfg = &state;
118   VARDECL(NE10_FFT_CPX_TYPE_T, buffer);
119   SAVE_STACK;
120   ALLOC(buffer, st->nfft, NE10_FFT_CPX_TYPE_T);
121
122   if (!st->arch_fft->is_supported) {
123      /* This nfft length (scaled fft) not supported in NE10 */
124      opus_fft_c(st, fin, fout);
125   }
126   else {
127      memcpy((void *)cfg, st->arch_fft->priv, sizeof(NE10_FFT_STATE_TYPE_T));
128      state.buffer = (NE10_FFT_CPX_TYPE_T *)&buffer[0];
129#if !defined(FIXED_POINT)
130      state.is_forward_scaled = 1;
131
132      NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
133                                (NE10_FFT_CPX_TYPE_T *)fin,
134                                cfg, 0);
135#else
136      NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
137                                (NE10_FFT_CPX_TYPE_T *)fin,
138                                cfg, 0, 1);
139#endif
140   }
141   RESTORE_STACK;
142}
143
144void opus_ifft_neon(const kiss_fft_state *st,
145                    const kiss_fft_cpx *fin,
146                    kiss_fft_cpx *fout)
147{
148   NE10_FFT_STATE_TYPE_T state;
149   NE10_FFT_CFG_TYPE_T cfg = &state;
150   VARDECL(NE10_FFT_CPX_TYPE_T, buffer);
151   SAVE_STACK;
152   ALLOC(buffer, st->nfft, NE10_FFT_CPX_TYPE_T);
153
154   if (!st->arch_fft->is_supported) {
155      /* This nfft length (scaled fft) not supported in NE10 */
156      opus_ifft_c(st, fin, fout);
157   }
158   else {
159      memcpy((void *)cfg, st->arch_fft->priv, sizeof(NE10_FFT_STATE_TYPE_T));
160      state.buffer = (NE10_FFT_CPX_TYPE_T *)&buffer[0];
161#if !defined(FIXED_POINT)
162      state.is_backward_scaled = 0;
163
164      NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
165                                (NE10_FFT_CPX_TYPE_T *)fin,
166                                cfg, 1);
167#else
168      NE10_FFT_C2C_1D_TYPE_NEON((NE10_FFT_CPX_TYPE_T *)fout,
169                                (NE10_FFT_CPX_TYPE_T *)fin,
170                                cfg, 1, 0);
171#endif
172   }
173   RESTORE_STACK;
174}
175