Android build system, NEON and non-NEON builds

后端 未结 6 2203
情话喂你
情话喂你 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:15

    If you put the NEON code in a separate module (static library or shared library), you can manually tune CFLAGS for that module in your Android.mk to your wanting.

    If you have C files that use #ifdef __ARM_NEON__ around intrinsics, your best choice would be to put these files in shared libraries - one compiled for v6, another for neon.

    I usually load such "supplimentary" libraries directly from Java, so that the main native code does not care about these changes at all.


    Update: here is a simple example that uses static library:

    Android.mk

    LOCAL_PATH:= $(call my-dir)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE    := neon_utils
    LOCAL_SRC_FILES := neon_add.c
    LOCAL_CFLAGS += -mfloat-abi=softfp -mfpu=neon -march=armv7
    include $(BUILD_STATIC_LIBRARY)
    
    NDK_PATH:=$(call my-dir)/../..
    
    include $(CLEAR_VARS)
    LOCAL_MODULE    := test_conditional_load
    LOCAL_C_INCLUDES := $(NDK_PATH)/sources/cpufeatures
    LOCAL_SRC_FILES := main.c
    LOCAL_STATIC_LIBRARIES  :=  neon_utils cpufeatures
    
    include $(BUILD_EXECUTABLE)
    
    include $(NDK_PATH)/sources/cpufeatures/Android.mk
    

    main.c

    #include 
    #include 
    
    void neon_add(int32_t * ptr);
    
    int main()
    {
        int32_t int32_4[] = {2,3,4,5};
    
        if (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON)
        {
            neon_add(int32_4);
            printf("neon\n");
        }
        else
        {
            printf("not neon\n");
        }
        printf("values = %d, %d, %d, %d\n", int32_4[0], int32_4[1], int32_4[2], int32_4[3]);
        return 0;
    }
    

    neon_add.c

    #include 
    
    void neon_add(int32_t * ptr)
    {
        int32x4_t vin = vld1q_s32(ptr);
        int32x4_t vout = vaddq_s32(vin, vin);
        vst1q_s32(ptr, vout);
    }
    

提交回复
热议问题