Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 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" |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 40 | #include "arch.h" |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 41 | |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 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) |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 46 | |
| 47 | #if defined(_MSC_VER) |
| 48 | /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/ |
| 49 | # define WIN32_LEAN_AND_MEAN |
| 50 | # define WIN32_EXTRA_LEAN |
| 51 | # include <windows.h> |
| 52 | |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame] | 53 | static OPUS_INLINE opus_uint32 opus_cpu_capabilities(void){ |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 54 | opus_uint32 flags; |
| 55 | flags=0; |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame] | 56 | /* MSVC has no OPUS_INLINE __asm support for ARM, but it does let you __emit |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 57 | * instructions via their assembled hex code. |
| 58 | * All of these instructions should be essentially nops. */ |
Jonathan Lennox | 1a94b2b | 2016-07-08 18:30:19 -0400 | [diff] [blame] | 59 | # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \ |
| 60 | || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 61 | __try{ |
| 62 | /*PLD [r13]*/ |
| 63 | __emit(0xF5DDF000); |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 64 | flags|=OPUS_CPU_ARM_EDSP_FLAG; |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 65 | } |
| 66 | __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){ |
| 67 | /*Ignore exception.*/ |
| 68 | } |
Jonathan Lennox | 1a94b2b | 2016-07-08 18:30:19 -0400 | [diff] [blame] | 69 | # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \ |
| 70 | || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 71 | __try{ |
| 72 | /*SHADD8 r3,r3,r3*/ |
| 73 | __emit(0xE6333F93); |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 74 | flags|=OPUS_CPU_ARM_MEDIA_FLAG; |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 75 | } |
| 76 | __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){ |
| 77 | /*Ignore exception.*/ |
| 78 | } |
Jonathan Lennox | b4aa5dc | 2015-08-03 17:04:20 -0400 | [diff] [blame] | 79 | # if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 80 | __try{ |
| 81 | /*VORR q0,q0,q0*/ |
| 82 | __emit(0xF2200150); |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 83 | flags|=OPUS_CPU_ARM_NEON_FLAG; |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 84 | } |
| 85 | __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){ |
| 86 | /*Ignore exception.*/ |
| 87 | } |
| 88 | # endif |
| 89 | # endif |
| 90 | # endif |
| 91 | return flags; |
| 92 | } |
| 93 | |
| 94 | #elif defined(__linux__) |
| 95 | /* Linux based */ |
Ralph Giles | b69bfb2 | 2020-06-14 00:53:30 -0700 | [diff] [blame] | 96 | #include <stdio.h> |
| 97 | |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 98 | opus_uint32 opus_cpu_capabilities(void) |
| 99 | { |
| 100 | opus_uint32 flags = 0; |
| 101 | FILE *cpuinfo; |
| 102 | |
| 103 | /* Reading /proc/self/auxv would be easier, but that doesn't work reliably on |
| 104 | * Android */ |
| 105 | cpuinfo = fopen("/proc/cpuinfo", "r"); |
| 106 | |
| 107 | if(cpuinfo != NULL) |
| 108 | { |
| 109 | /* 512 should be enough for anybody (it's even enough for all the flags that |
| 110 | * x86 has accumulated... so far). */ |
| 111 | char buf[512]; |
| 112 | |
| 113 | while(fgets(buf, 512, cpuinfo) != NULL) |
| 114 | { |
Jonathan Lennox | 1a94b2b | 2016-07-08 18:30:19 -0400 | [diff] [blame] | 115 | # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \ |
| 116 | || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 117 | /* Search for edsp and neon flag */ |
| 118 | if(memcmp(buf, "Features", 8) == 0) |
| 119 | { |
| 120 | char *p; |
| 121 | p = strstr(buf, " edsp"); |
| 122 | if(p != NULL && (p[5] == ' ' || p[5] == '\n')) |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 123 | flags |= OPUS_CPU_ARM_EDSP_FLAG; |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 124 | |
Jonathan Lennox | b4aa5dc | 2015-08-03 17:04:20 -0400 | [diff] [blame] | 125 | # if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 126 | p = strstr(buf, " neon"); |
| 127 | if(p != NULL && (p[5] == ' ' || p[5] == '\n')) |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 128 | flags |= OPUS_CPU_ARM_NEON_FLAG; |
Timothy B. Terriberry | 39386e0 | 2013-11-18 13:30:13 -0500 | [diff] [blame] | 129 | # endif |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 130 | } |
Timothy B. Terriberry | 39386e0 | 2013-11-18 13:30:13 -0500 | [diff] [blame] | 131 | # endif |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 132 | |
Jonathan Lennox | 1a94b2b | 2016-07-08 18:30:19 -0400 | [diff] [blame] | 133 | # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \ |
| 134 | || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 135 | /* Search for media capabilities (>= ARMv6) */ |
| 136 | if(memcmp(buf, "CPU architecture:", 17) == 0) |
| 137 | { |
| 138 | int version; |
| 139 | version = atoi(buf+17); |
| 140 | |
| 141 | if(version >= 6) |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 142 | flags |= OPUS_CPU_ARM_MEDIA_FLAG; |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 143 | } |
Timothy B. Terriberry | 39386e0 | 2013-11-18 13:30:13 -0500 | [diff] [blame] | 144 | # endif |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | fclose(cpuinfo); |
| 148 | } |
| 149 | return flags; |
| 150 | } |
| 151 | #else |
| 152 | /* The feature registers which can tell us what the processor supports are |
| 153 | * accessible in priveleged modes only, so we can't have a general user-space |
| 154 | * detection method like on x86.*/ |
| 155 | # error "Configured to use ARM asm but no CPU detection method available for " \ |
| 156 | "your platform. Reconfigure with --disable-rtcd (or send patches)." |
| 157 | #endif |
| 158 | |
| 159 | int opus_select_arch(void) |
| 160 | { |
| 161 | opus_uint32 flags = opus_cpu_capabilities(); |
| 162 | int arch = 0; |
| 163 | |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 164 | if(!(flags & OPUS_CPU_ARM_EDSP_FLAG)) { |
| 165 | /* Asserts ensure arch values are sequential */ |
| 166 | celt_assert(arch == OPUS_ARCH_ARM_V4); |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 167 | return arch; |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 168 | } |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 169 | arch++; |
| 170 | |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 171 | if(!(flags & OPUS_CPU_ARM_MEDIA_FLAG)) { |
| 172 | celt_assert(arch == OPUS_ARCH_ARM_EDSP); |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 173 | return arch; |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 174 | } |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 175 | arch++; |
| 176 | |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 177 | if(!(flags & OPUS_CPU_ARM_NEON_FLAG)) { |
| 178 | celt_assert(arch == OPUS_ARCH_ARM_MEDIA); |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 179 | return arch; |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 180 | } |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 181 | arch++; |
| 182 | |
Jonathan Lennox | fdb3b3a | 2015-12-22 19:21:43 -0500 | [diff] [blame] | 183 | celt_assert(arch == OPUS_ARCH_ARM_NEON); |
Aurélien Zanelli | cd4c824 | 2013-05-31 15:07:00 +0200 | [diff] [blame] | 184 | return arch; |
| 185 | } |
| 186 | |
| 187 | #endif |