xref: /aosp_15_r20/external/libopus/celt/arm/armcpu.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2010 Xiph.Org Foundation
2  * Copyright (c) 2013 Parrot */
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 COPYRIGHT OWNER
19    OR 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 /* Original code from libtheora modified to suit to Opus */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #ifdef OPUS_HAVE_RTCD
35 
36 #include "armcpu.h"
37 #include "cpu_support.h"
38 #include "os_support.h"
39 #include "opus_types.h"
40 #include "arch.h"
41 
42 #define OPUS_CPU_ARM_V4_FLAG    (1<<OPUS_ARCH_ARM_V4)
43 #define OPUS_CPU_ARM_EDSP_FLAG  (1<<OPUS_ARCH_ARM_EDSP)
44 #define OPUS_CPU_ARM_MEDIA_FLAG (1<<OPUS_ARCH_ARM_MEDIA)
45 #define OPUS_CPU_ARM_NEON_FLAG  (1<<OPUS_ARCH_ARM_NEON)
46 #define OPUS_CPU_ARM_DOTPROD_FLAG  (1<<OPUS_ARCH_ARM_DOTPROD)
47 
48 #if defined(_MSC_VER)
49 /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
50 # define WIN32_LEAN_AND_MEAN
51 # define WIN32_EXTRA_LEAN
52 # include <windows.h>
53 
opus_cpu_capabilities(void)54 static OPUS_INLINE opus_uint32 opus_cpu_capabilities(void){
55   opus_uint32 flags;
56   flags=0;
57   /* MSVC has no OPUS_INLINE __asm support for ARM, but it does let you __emit
58    * instructions via their assembled hex code.
59    * All of these instructions should be essentially nops. */
60 # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \
61  || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
62   __try{
63     /*PLD [r13]*/
64     __emit(0xF5DDF000);
65     flags|=OPUS_CPU_ARM_EDSP_FLAG;
66   }
67   __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){
68     /*Ignore exception.*/
69   }
70 #  if defined(OPUS_ARM_MAY_HAVE_MEDIA) \
71  || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
72   __try{
73     /*SHADD8 r3,r3,r3*/
74     __emit(0xE6333F93);
75     flags|=OPUS_CPU_ARM_MEDIA_FLAG;
76   }
77   __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){
78     /*Ignore exception.*/
79   }
80 #   if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
81   __try{
82     /*VORR q0,q0,q0*/
83     __emit(0xF2200150);
84     flags|=OPUS_CPU_ARM_NEON_FLAG;
85   }
86   __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){
87     /*Ignore exception.*/
88   }
89 #   endif
90 #  endif
91 # endif
92   return flags;
93 }
94 
95 #elif defined(__linux__)
96 /* Linux based */
97 #include <stdio.h>
98 
opus_cpu_capabilities(void)99 opus_uint32 opus_cpu_capabilities(void)
100 {
101   opus_uint32 flags = 0;
102   FILE *cpuinfo;
103 
104   /* Reading /proc/self/auxv would be easier, but that doesn't work reliably on
105    * Android */
106   cpuinfo = fopen("/proc/cpuinfo", "r");
107 
108   if(cpuinfo != NULL)
109   {
110     /* 512 should be enough for anybody (it's even enough for all the flags that
111      * x86 has accumulated... so far). */
112     char buf[512];
113 
114     while(fgets(buf, 512, cpuinfo) != NULL)
115     {
116 # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \
117  || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
118       /* Search for edsp and neon flag */
119       if(memcmp(buf, "Features", 8) == 0)
120       {
121         char *p;
122         p = strstr(buf, " edsp");
123         if(p != NULL && (p[5] == ' ' || p[5] == '\n'))
124           flags |= OPUS_CPU_ARM_EDSP_FLAG;
125 
126 #  if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
127         p = strstr(buf, " neon");
128         if(p != NULL && (p[5] == ' ' || p[5] == '\n'))
129           flags |= OPUS_CPU_ARM_NEON_FLAG;
130         p = strstr(buf, " asimd");
131         if(p != NULL && (p[6] == ' ' || p[6] == '\n'))
132           flags |= OPUS_CPU_ARM_NEON_FLAG | OPUS_CPU_ARM_MEDIA_FLAG | OPUS_CPU_ARM_EDSP_FLAG;
133 #  endif
134 #  if defined(OPUS_ARM_MAY_HAVE_DOTPROD)
135         p = strstr(buf, " asimddp");
136         if(p != NULL && (p[8] == ' ' || p[8] == '\n'))
137           flags |= OPUS_CPU_ARM_DOTPROD_FLAG;
138 #  endif
139       }
140 # endif
141 
142 # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \
143  || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
144       /* Search for media capabilities (>= ARMv6) */
145       if(memcmp(buf, "CPU architecture:", 17) == 0)
146       {
147         int version;
148         version = atoi(buf+17);
149 
150         if(version >= 6)
151           flags |= OPUS_CPU_ARM_MEDIA_FLAG;
152       }
153 # endif
154     }
155 
156 #if defined(OPUS_ARM_PRESUME_AARCH64_NEON_INTR)
157     flags |= OPUS_CPU_ARM_EDSP_FLAG | OPUS_CPU_ARM_MEDIA_FLAG | OPUS_CPU_ARM_NEON_FLAG;
158 # if defined(OPUS_ARM_PRESUME_DOTPROD)
159     flags |= OPUS_CPU_ARM_DOTPROD_FLAG;
160 # endif
161 #endif
162 
163     fclose(cpuinfo);
164   }
165   return flags;
166 }
167 
168 #elif defined(__APPLE__)
169 #include <sys/types.h>
170 #include <sys/sysctl.h>
171 
opus_cpu_capabilities(void)172 opus_uint32 opus_cpu_capabilities(void)
173 {
174   opus_uint32 flags = 0;
175 
176 #if defined(OPUS_ARM_MAY_HAVE_DOTPROD)
177   size_t size = sizeof(uint32_t);
178   uint32_t value = 0;
179   if (!sysctlbyname("hw.optional.arm.FEAT_DotProd", &value, &size, NULL, 0) && value)
180   {
181     flags |= OPUS_CPU_ARM_DOTPROD_FLAG;
182   }
183 #endif
184 
185 #if defined(OPUS_ARM_PRESUME_AARCH64_NEON_INTR)
186   flags |= OPUS_CPU_ARM_EDSP_FLAG | OPUS_CPU_ARM_MEDIA_FLAG | OPUS_CPU_ARM_NEON_FLAG;
187 # if defined(OPUS_ARM_PRESUME_DOTPROD)
188   flags |= OPUS_CPU_ARM_DOTPROD_FLAG;
189 # endif
190 #endif
191   return flags;
192 }
193 
194 #else
195 /* The feature registers which can tell us what the processor supports are
196  * accessible in priveleged modes only, so we can't have a general user-space
197  * detection method like on x86.*/
198 # error "Configured to use ARM asm but no CPU detection method available for " \
199    "your platform.  Reconfigure with --disable-rtcd (or send patches)."
200 #endif
201 
opus_select_arch_impl(void)202 static int opus_select_arch_impl(void)
203 {
204   opus_uint32 flags = opus_cpu_capabilities();
205   int arch = 0;
206 
207   if(!(flags & OPUS_CPU_ARM_EDSP_FLAG)) {
208     /* Asserts ensure arch values are sequential */
209     celt_assert(arch == OPUS_ARCH_ARM_V4);
210     return arch;
211   }
212   arch++;
213 
214   if(!(flags & OPUS_CPU_ARM_MEDIA_FLAG)) {
215     celt_assert(arch == OPUS_ARCH_ARM_EDSP);
216     return arch;
217   }
218   arch++;
219 
220   if(!(flags & OPUS_CPU_ARM_NEON_FLAG)) {
221     celt_assert(arch == OPUS_ARCH_ARM_MEDIA);
222     return arch;
223   }
224   arch++;
225 
226   if(!(flags & OPUS_CPU_ARM_DOTPROD_FLAG)) {
227     celt_assert(arch == OPUS_ARCH_ARM_NEON);
228     return arch;
229   }
230   arch++;
231 
232   celt_assert(arch == OPUS_ARCH_ARM_DOTPROD);
233   return arch;
234 }
235 
opus_select_arch(void)236 int opus_select_arch(void) {
237   int arch = opus_select_arch_impl();
238 #ifdef FUZZING
239   arch = rand()%(arch+1);
240 #endif
241   return arch;
242 }
243 #endif
244