From 9cf34e76156205551b702c671c33609c536fb08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Sat, 1 Mar 2014 01:43:11 +0200 Subject: [PATCH 1/2] Unify the interface for the different variants of WelsCPUFeatureDetect The caller of the function should not need to know exactly which implementation of it is being used. For the variants that don't support detecting the number of cores, the pNumberOfLogicProcessors parameter can be left untouched and the caller will use a higher level API for finding it out. This simplifies all the calling code, and simplifies adding more implementations of cpu feature detection. --- codec/common/cpu.cpp | 20 ++++++++++++------- codec/common/cpu.h | 12 ----------- codec/decoder/core/src/decoder.cpp | 9 --------- codec/encoder/core/src/encoder_ext.cpp | 2 +- codec/processing/src/common/WelsFrameWork.cpp | 4 ---- 5 files changed, 14 insertions(+), 33 deletions(-) diff --git a/codec/common/cpu.cpp b/codec/common/cpu.cpp index 14194ff7..f07d380c 100644 --- a/codec/common/cpu.cpp +++ b/codec/common/cpu.cpp @@ -212,9 +212,7 @@ void WelsCPURestore (const uint32_t kuiCPU) { void WelsXmmRegEmptyOp(void * pSrc) { } -#endif - -#if defined(HAVE_NEON)//For supporting both android platform and iOS platform +#elif defined(HAVE_NEON) //For supporting both android platform and iOS platform #if defined(ANDROID_NDK) uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { @@ -242,10 +240,8 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) return uiCPU; } -#endif - -#if defined(APPLE_IOS) -uint32_t WelsCPUFeatureDetectIOS() //Need to be updated for the new device of APPLE +#elif defined(APPLE_IOS) +uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { uint32_t uiCPU = 0; struct utsname sSystemInfo; @@ -262,7 +258,17 @@ uint32_t WelsCPUFeatureDetectIOS() //Need to be updated for the new device of AP } return uiCPU; } +#else /* HAVE_NEON enabled but no runtime detection */ +uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { + return 0; +} #endif +#else /* Neither X86_ASM nor HAVE_NEON */ + +uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { + return 0; +} + #endif diff --git a/codec/common/cpu.h b/codec/common/cpu.h index 5623cad0..2e0361c7 100644 --- a/codec/common/cpu.h +++ b/codec/common/cpu.h @@ -62,8 +62,6 @@ int32_t WelsCPUSupportFMA (uint32_t eax, uint32_t ecx); void WelsEmms(); -uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors); - /* * clear FPU registers states for potential float based calculation if support */ @@ -80,18 +78,8 @@ void WelsXmmRegLoad(void * src); void WelsXmmRegEmptyOp(void * pSrc); -#if defined(HAVE_NEON) -#if defined(ANDROID_NDK) - uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors); -#endif - -#if defined(APPLE_IOS) - uint32_t WelsCPUFeatureDetectIOS(); -#endif -#endif - #if defined(__cplusplus) } #endif//__cplusplus diff --git a/codec/decoder/core/src/decoder.cpp b/codec/decoder/core/src/decoder.cpp index 6200c942..8b774eef 100644 --- a/codec/decoder/core/src/decoder.cpp +++ b/codec/decoder/core/src/decoder.cpp @@ -144,16 +144,7 @@ void WelsDecoderDefaults (PWelsDecoderContext pCtx) { pCtx->bAuReadyFlag = 0; // au data is not ready -#if defined(X86_ASM) pCtx->uiCpuFlag = WelsCPUFeatureDetect (&iCpuCores); -#elif defined(HAVE_NEON) -#if defined(ANDROID_NDK) - pCtx->uiCpuFlag = WelsCPUFeatureDetect(&iCpuCores); -#endif -#if defined(APPLE_IOS) - pCtx->uiCpuFlag = WelsCPUFeatureDetectIOS(); -#endif -#endif pCtx->iImgWidthInPixel = 0; pCtx->iImgHeightInPixel = 0; // alloc picture data when picture size is available diff --git a/codec/encoder/core/src/encoder_ext.cpp b/codec/encoder/core/src/encoder_ext.cpp index 646d1d96..f9d6be52 100644 --- a/codec/encoder/core/src/encoder_ext.cpp +++ b/codec/encoder/core/src/encoder_ext.cpp @@ -1933,8 +1933,8 @@ int32_t WelsInitEncoderExt (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPar } // for cpu features detection, Only detect once?? -#ifdef X86_ASM uiCpuFeatureFlags = WelsCPUFeatureDetect (&uiCpuCores); // detect cpu capacity features +#ifdef X86_ASM if (uiCpuFeatureFlags & WELS_CPU_CACHELINE_128) iCacheLineSize = 128; else if (uiCpuFeatureFlags & WELS_CPU_CACHELINE_64) diff --git a/codec/processing/src/common/WelsFrameWork.cpp b/codec/processing/src/common/WelsFrameWork.cpp index d444b1ac..d980e032 100644 --- a/codec/processing/src/common/WelsFrameWork.cpp +++ b/codec/processing/src/common/WelsFrameWork.cpp @@ -87,11 +87,7 @@ EResult DestroySpecificVpInterface (IWelsVP* pCtx) { CVpFrameWork::CVpFrameWork (uint32_t uiThreadsNum, EResult& eReturn) { int32_t iCoreNum = 1; -#ifndef X86_ASM - uint32_t uiCPUFlag = 0; -#else uint32_t uiCPUFlag = WelsCPUFeatureDetect (&iCoreNum); -#endif for (int32_t i = 0; i < MAX_STRATEGY_NUM; i++) { IStrategy* pStrategy = m_pStgChain[i]; From d411d768b6cf3d6b5e51f279eaec3d59e6233a2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Sat, 1 Mar 2014 01:51:59 +0200 Subject: [PATCH 2/2] Add cpu feature detection for generic arm/linux For platforms without runtime detection, assume whoever built it with HAVE_NEON actually wanted it. --- codec/common/cpu.cpp | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/codec/common/cpu.cpp b/codec/common/cpu.cpp index f07d380c..e07209b5 100644 --- a/codec/common/cpu.cpp +++ b/codec/common/cpu.cpp @@ -38,6 +38,7 @@ ************************************************************************************* */ #include +#include #ifdef ANDROID_NDK #include #endif @@ -258,9 +259,39 @@ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) } return uiCPU; } -#else /* HAVE_NEON enabled but no runtime detection */ +#elif defined(__linux__) + +/* Generic arm/linux cpu feature detection */ uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { - return 0; + FILE *f = fopen("/proc/cpuinfo", "r"); + + if (!f) + return 0; + + char buf[200]; + int flags = 0; + while (fgets(buf, sizeof(buf), f)) { + if (!strncmp(buf, "Features", strlen("Features"))) { + if (strstr(buf, " neon ")) + flags |= WELS_CPU_NEON; + if (strstr(buf, " vfpv3 ")) + flags |= WELS_CPU_VFPv3; + break; + } + } + fclose(f); + return flags; +} + +#else /* HAVE_NEON enabled but no runtime detection */ + +/* No runtime feature detection available, but built with HAVE_NEON - assume + * that NEON and all associated features are available. */ + +uint32_t WelsCPUFeatureDetect (int32_t* pNumberOfLogicProcessors) { + return WELS_CPU_ARMv7 | + WELS_CPU_VFPv3 | + WELS_CPU_NEON; } #endif #else /* Neither X86_ASM nor HAVE_NEON */