Have different optimizations (plain, SSE, AVX) in the same executable with C/C++

前端 未结 3 1710
生来不讨喜
生来不讨喜 2021-01-02 16:46

I\'m developing optimizations for my 3D calculations and I now have:

  • a \"plain\" version using the standard C language libraries,
  • an
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 17:23

    One way is to implement three libraries conforming to the same interface. With dynamic libraries, you can just swap the library file and the executable will use whatever it finds. For example on Windows, you could compile three DLLs:

    • PlainImpl.dll
    • SSEImpl.dll
    • AVXImpl.dll

    And then make the executable link against Impl.dll. Now just put one of the three specific DLLs into the same directory as the .exe, rename it to Impl.dll, and it will use that version. The same principle should basically be applicable on a UNIX-like OS.

    The next step would be to load the libraries programmatically, which is probably the most flexible, but it is OS specific and requires some more work (like opening the library, obtaining function pointers etc.)

    Edit: But of course, you could just implement the function three times and select one at runtime, depending on some parameter/config file setting etc., as lined out in the other answers.

提交回复
热议问题