xref: /aosp_15_r20/external/libopus/dnn/x86/dnn_x86.h (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2011-2019 Mozilla
2                  2023 Amazon */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
19    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef DNN_X86_H
29 #define DNN_X86_H
30 
31 #include "cpu_support.h"
32 #include "opus_types.h"
33 
34 #if defined(OPUS_X86_MAY_HAVE_SSE2)
35 void compute_linear_sse2(const LinearLayer *linear, float *out, const float *in);
36 void compute_activation_sse2(float *output, const float *input, int N, int activation);
37 void compute_conv2d_sse2(const Conv2dLayer *conv, float *out, float *mem, const float *in, int height, int hstride, int activation);
38 #endif
39 
40 #if defined(OPUS_X86_MAY_HAVE_SSE4_1)
41 void compute_linear_sse4_1(const LinearLayer *linear, float *out, const float *in);
42 void compute_activation_sse4_1(float *output, const float *input, int N, int activation);
43 void compute_conv2d_sse4_1(const Conv2dLayer *conv, float *out, float *mem, const float *in, int height, int hstride, int activation);
44 #endif
45 
46 #if defined(OPUS_X86_MAY_HAVE_AVX2)
47 void compute_linear_avx2(const LinearLayer *linear, float *out, const float *in);
48 void compute_activation_avx2(float *output, const float *input, int N, int activation);
49 void compute_conv2d_avx2(const Conv2dLayer *conv, float *out, float *mem, const float *in, int height, int hstride, int activation);
50 #endif
51 
52 
53 #if defined(OPUS_X86_PRESUME_AVX2)
54 
55 #define OVERRIDE_COMPUTE_LINEAR
56 #define compute_linear(linear, out, in, arch) ((void)(arch),compute_linear_avx2(linear, out, in))
57 #define OVERRIDE_COMPUTE_ACTIVATION
58 #define compute_activation(output, input, N, activation, arch) ((void)(arch),compute_activation_avx2(output, input, N, activation))
59 #define OVERRIDE_COMPUTE_CONV2D
60 #define compute_conv2d(conv, out, mem, in, height, hstride, activation, arch) ((void)(arch),compute_conv2d_avx2(conv, out, mem, in, height, hstride, activation))
61 
62 #elif defined(OPUS_X86_PRESUME_SSE4_1) && !defined(OPUS_X86_MAY_HAVE_AVX2)
63 
64 #define OVERRIDE_COMPUTE_LINEAR
65 #define compute_linear(linear, out, in, arch) ((void)(arch),compute_linear_sse4_1(linear, out, in))
66 #define OVERRIDE_COMPUTE_ACTIVATION
67 #define compute_activation(output, input, N, activation, arch) ((void)(arch),compute_activation_sse4_1(output, input, N, activation))
68 #define OVERRIDE_COMPUTE_CONV2D
69 #define compute_conv2d(conv, out, mem, in, height, hstride, activation, arch) ((void)(arch),compute_conv2d_sse4_1(conv, out, mem, in, height, hstride, activation))
70 
71 #elif defined(OPUS_X86_PRESUME_SSE2) && !defined(OPUS_X86_MAY_HAVE_AVX2) && !defined(OPUS_X86_MAY_HAVE_SSE4_1)
72 
73 #define OVERRIDE_COMPUTE_LINEAR
74 #define compute_linear(linear, out, in, arch) ((void)(arch),compute_linear_sse2(linear, out, in))
75 #define OVERRIDE_COMPUTE_ACTIVATION
76 #define compute_activation(output, input, N, activation, arch) ((void)(arch),compute_activation_sse2(output, input, N, activation))
77 #define OVERRIDE_COMPUTE_CONV2D
78 #define compute_conv2d(conv, out, mem, in, height, hstride, activation, arch) ((void)(arch),compute_conv2d_sse2(conv, out, mem, in, height, hstride, activation))
79 
80 #elif defined(OPUS_HAVE_RTCD) && (defined(OPUS_X86_MAY_HAVE_AVX2) || defined(OPUS_X86_MAY_HAVE_SSE4_1) || defined(OPUS_X86_MAY_HAVE_SSE2))
81 
82 extern void (*const DNN_COMPUTE_LINEAR_IMPL[OPUS_ARCHMASK + 1])(
83                     const LinearLayer *linear,
84                     float *out,
85                     const float *in
86                     );
87 #define OVERRIDE_COMPUTE_LINEAR
88 #define compute_linear(linear, out, in, arch) \
89     ((*DNN_COMPUTE_LINEAR_IMPL[(arch) & OPUS_ARCHMASK])(linear, out, in))
90 
91 
92 extern void (*const DNN_COMPUTE_ACTIVATION_IMPL[OPUS_ARCHMASK + 1])(
93                     float *output,
94                     const float *input,
95                     int N,
96                     int activation
97                     );
98 #define OVERRIDE_COMPUTE_ACTIVATION
99 #define compute_activation(output, input, N, activation, arch) \
100     ((*DNN_COMPUTE_ACTIVATION_IMPL[(arch) & OPUS_ARCHMASK])(output, input, N, activation))
101 
102 
103 extern void (*const DNN_COMPUTE_CONV2D_IMPL[OPUS_ARCHMASK + 1])(
104                     const Conv2dLayer *conv,
105                     float *out,
106                     float *mem,
107                     const float *in,
108                     int height,
109                     int hstride,
110                     int activation
111                     );
112 #define OVERRIDE_COMPUTE_CONV2D
113 #define compute_conv2d(conv, out, mem, in, height, hstride, activation, arch) \
114     ((*DNN_COMPUTE_CONV2D_IMPL[(arch) & OPUS_ARCHMASK])(conv, out, mem, in, height, hstride, activation))
115 
116 
117 #endif
118 
119 
120 
121 #endif /* DNN_X86_H */
122