Android build system, NEON and non-NEON builds

后端 未结 6 2201
情话喂你
情话喂你 2020-12-01 08:26

I want to build my library for armv6, and there is some neon code that I enable at runtime if the device supports it. The neon code uses neon intrinsics, and to be able to c

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 09:24

    If you're looking for a simpler implementation:

    First, ensure all NEON-capable code is conditionally compiled only for ABI armeabi-v7a, and additionally will only be executed if at runtime it's running on an ARMv7 implementation which includes NEON:

    /* 
       bar_better_on_neon.c
     */
    
    #ifdef HAVE_ARMV7
    #  include 
    #  ifdef ANDROID
    #    include "cpu-features.h"
    #  endif
    #endif
    
    #ifdef HAVE_ARMV7 
    static int check_for_neon(void)
    {
    #  ifdef ANDROID
        // Not all Android devices with ARMv7 are guaranteed to have NEON, so check.
        uint64_t features = android_getCpuFeatures();
        return (features & ANDROID_CPU_ARM_FEATURE_ARMv7) && (features & ANDROID_CPU_ARM_FEATURE_NEON);
    #  elif defined(__APPLE__)
        return 1;
    #  else
        return 0;
    #  endif
    }
    #endif
    
    void bar(void)
    {
    #ifdef HAVE_ARMV7
        if (check_for_neon()) {
    
            /* here put neon code */
    
        } else {
    #endif
    
            /* here put non-neon code */
    
    #ifdef HAVE_ARMV7
        }
    #endif
    }
    

    Here, the check_for_neon() uses the NDK's cpufeatures library. Then, in your Android.mk file:

    LOCAL_SRC_FILES := foo.c bar_better_on_neon.c
    ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
      # Compile the one file in NEON mode.
      LOCAL_SRC_FILES := $(subst bar_better_on_neon.c, bar_better_on_neon.c.neon,$(LOCAL_SRC_FILES))
      LOCAL_CFLAGS += -DHAVE_ARMV7=1
    endif
    

提交回复
热议问题