Chris@69: /* Copyright (c) 2010 Xiph.Org Foundation Chris@69: * Copyright (c) 2013 Parrot */ Chris@69: /* Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: Chris@69: - Redistributions of source code must retain the above copyright Chris@69: notice, this list of conditions and the following disclaimer. Chris@69: Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@69: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@69: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@69: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER Chris@69: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@69: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@69: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Chris@69: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF Chris@69: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING Chris@69: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@69: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@69: */ Chris@69: Chris@69: /* Original code from libtheora modified to suit to Opus */ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #ifdef OPUS_HAVE_RTCD Chris@69: Chris@69: #include "armcpu.h" Chris@69: #include "cpu_support.h" Chris@69: #include "os_support.h" Chris@69: #include "opus_types.h" Chris@69: #include "arch.h" Chris@69: Chris@69: #define OPUS_CPU_ARM_V4_FLAG (1< Chris@69: Chris@69: static OPUS_INLINE opus_uint32 opus_cpu_capabilities(void){ Chris@69: opus_uint32 flags; Chris@69: flags=0; Chris@69: /* MSVC has no OPUS_INLINE __asm support for ARM, but it does let you __emit Chris@69: * instructions via their assembled hex code. Chris@69: * All of these instructions should be essentially nops. */ Chris@69: # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \ Chris@69: || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) Chris@69: __try{ Chris@69: /*PLD [r13]*/ Chris@69: __emit(0xF5DDF000); Chris@69: flags|=OPUS_CPU_ARM_EDSP_FLAG; Chris@69: } Chris@69: __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){ Chris@69: /*Ignore exception.*/ Chris@69: } Chris@69: # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \ Chris@69: || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) Chris@69: __try{ Chris@69: /*SHADD8 r3,r3,r3*/ Chris@69: __emit(0xE6333F93); Chris@69: flags|=OPUS_CPU_ARM_MEDIA_FLAG; Chris@69: } Chris@69: __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){ Chris@69: /*Ignore exception.*/ Chris@69: } Chris@69: # if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) Chris@69: __try{ Chris@69: /*VORR q0,q0,q0*/ Chris@69: __emit(0xF2200150); Chris@69: flags|=OPUS_CPU_ARM_NEON_FLAG; Chris@69: } Chris@69: __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){ Chris@69: /*Ignore exception.*/ Chris@69: } Chris@69: # endif Chris@69: # endif Chris@69: # endif Chris@69: return flags; Chris@69: } Chris@69: Chris@69: #elif defined(__linux__) Chris@69: /* Linux based */ Chris@69: opus_uint32 opus_cpu_capabilities(void) Chris@69: { Chris@69: opus_uint32 flags = 0; Chris@69: FILE *cpuinfo; Chris@69: Chris@69: /* Reading /proc/self/auxv would be easier, but that doesn't work reliably on Chris@69: * Android */ Chris@69: cpuinfo = fopen("/proc/cpuinfo", "r"); Chris@69: Chris@69: if(cpuinfo != NULL) Chris@69: { Chris@69: /* 512 should be enough for anybody (it's even enough for all the flags that Chris@69: * x86 has accumulated... so far). */ Chris@69: char buf[512]; Chris@69: Chris@69: while(fgets(buf, 512, cpuinfo) != NULL) Chris@69: { Chris@69: # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \ Chris@69: || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) Chris@69: /* Search for edsp and neon flag */ Chris@69: if(memcmp(buf, "Features", 8) == 0) Chris@69: { Chris@69: char *p; Chris@69: p = strstr(buf, " edsp"); Chris@69: if(p != NULL && (p[5] == ' ' || p[5] == '\n')) Chris@69: flags |= OPUS_CPU_ARM_EDSP_FLAG; Chris@69: Chris@69: # if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) Chris@69: p = strstr(buf, " neon"); Chris@69: if(p != NULL && (p[5] == ' ' || p[5] == '\n')) Chris@69: flags |= OPUS_CPU_ARM_NEON_FLAG; Chris@69: # endif Chris@69: } Chris@69: # endif Chris@69: Chris@69: # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \ Chris@69: || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) Chris@69: /* Search for media capabilities (>= ARMv6) */ Chris@69: if(memcmp(buf, "CPU architecture:", 17) == 0) Chris@69: { Chris@69: int version; Chris@69: version = atoi(buf+17); Chris@69: Chris@69: if(version >= 6) Chris@69: flags |= OPUS_CPU_ARM_MEDIA_FLAG; Chris@69: } Chris@69: # endif Chris@69: } Chris@69: Chris@69: fclose(cpuinfo); Chris@69: } Chris@69: return flags; Chris@69: } Chris@69: #else Chris@69: /* The feature registers which can tell us what the processor supports are Chris@69: * accessible in priveleged modes only, so we can't have a general user-space Chris@69: * detection method like on x86.*/ Chris@69: # error "Configured to use ARM asm but no CPU detection method available for " \ Chris@69: "your platform. Reconfigure with --disable-rtcd (or send patches)." Chris@69: #endif Chris@69: Chris@69: int opus_select_arch(void) Chris@69: { Chris@69: opus_uint32 flags = opus_cpu_capabilities(); Chris@69: int arch = 0; Chris@69: Chris@69: if(!(flags & OPUS_CPU_ARM_EDSP_FLAG)) { Chris@69: /* Asserts ensure arch values are sequential */ Chris@69: celt_assert(arch == OPUS_ARCH_ARM_V4); Chris@69: return arch; Chris@69: } Chris@69: arch++; Chris@69: Chris@69: if(!(flags & OPUS_CPU_ARM_MEDIA_FLAG)) { Chris@69: celt_assert(arch == OPUS_ARCH_ARM_EDSP); Chris@69: return arch; Chris@69: } Chris@69: arch++; Chris@69: Chris@69: if(!(flags & OPUS_CPU_ARM_NEON_FLAG)) { Chris@69: celt_assert(arch == OPUS_ARCH_ARM_MEDIA); Chris@69: return arch; Chris@69: } Chris@69: arch++; Chris@69: Chris@69: celt_assert(arch == OPUS_ARCH_ARM_NEON); Chris@69: return arch; Chris@69: } Chris@69: Chris@69: #endif