Linker errors when using intrinsic function via function pointer

痴心易碎 提交于 2019-12-11 04:04:49

问题


The code below doesn't compile with visual studio 2013. I get linker Error unresolved external symbol(LNK 2019) for the mm functions. If i use the functions directly, it all links fine. Why it doesn't compile? And is there a work-around

        #include "emmintrin.h"
        #include <smmintrin.h>
        #include <intrin.h>


        __m128i (*load)(const __m128i*) = NULL;

        if (it::isAligned<16>(ucpSrc, iXOffset * sizeof(unsigned char)) )
            load = &_mm_load_si128;
        else
            load = &_mm_lddqu_si128;

回答1:


Where some compilers such as gcc and clang use some special annotation on such methods (static extern __inline __m128d __attribute__((__gnu_inline__, __always_inline__, __artificial__)) for gcc, or static __inline__ __attribute__((__always_inline__, __nodebug__)) for clang), others (like Intel on Windows and cl, don't and probably do something special under the hood.

Key thing is that these functions are not meant to be considered as functions. They will not show any preamble, implement standard ABI. Those are just a C-syntax way to invoke some assembly instruction, more readable than a __asm (...)

I believe you can accomplish this function pointer thing with:

__m128i load_aligned (const __m128i* p)
{
    return _mm_load_si128(p);
}

__m128i load_unaligned (const __m128i* p)
{
    return _mm_lddqu_si128(p);
}


__m128i (*load)(const __m128i*) = NULL;

void f(bool a)
{
    if (a)
        load = load_aligned;
    else
        load = load_unaligned;
}

int main()
{
    __m128i a, b ;
    f(argc != 0);
    return 0;
}

I would highlight a performance note though: using a function pointer is going to be immensely more expensive than simply using the unaligned load all the time. The overhead of unaligned loads is about a few percents when memory is aligned where calling a function pointer will force you respect the ABI, hence store registers on the stack, most probably go through a few cache misses, etc.



来源:https://stackoverflow.com/questions/32219721/linker-errors-when-using-intrinsic-function-via-function-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!