Log in

View Full Version : How to detect CPU instruction set


MysteryX
14th June 2021, 22:38
Simple question. While writing VapourSynth plugins, how do I detect supported CPU instruction sets like AVX or SSE2?

or do I have to use an external library like this
https://github.com/HomeOfVapourSynthEvolution/VapourSynth-Yadifmod/blob/master/Yadifmod/vectorclass/instrset_detect.cpp

DJATOM
14th June 2021, 22:48
https://github.com/vapoursynth/vapoursynth/blob/doodle1/src/core/mergefilters.cpp#L547
Is that an API you asked for?

MysteryX
15th June 2021, 03:10
https://github.com/vapoursynth/vapoursynth/blob/doodle1/src/core/mergefilters.cpp#L547
Is that an API you asked for?
Unfortunately it's not a public API, so it looks like the only option is to import either library files into the project. Why wasn't it made public? .... AND this code is x64-only

#ifdef VS_TARGET_CPU_X86

While being at it, could someone help me with this function to throw an error using PrintF formatting syntax? One option is finding a std::string format function, and then copying it into a char*, but what would be a cleaner and more efficient way to do it?

template<typename... Args>
void ThrowError(const char* msg, Args... args)
{
std::string str = format(std::string(msg), args...);
char c[str.size() + 1];
str.copy(c, str.size() + 1);
c[str.size()] = '\0';
ThrowError(c);
}

ChaosKing
15th June 2021, 08:30
Avsmeter detects cpu instruction sets with the help of libcpuid https://forum.doom9.org/showthread.php?t=174797

MysteryX
15th June 2021, 18:13
The first library code I posted is just as good as any other one; might as well use that.

Found the answer to the 2nd question: vsnprintf

template<typename... Args>
void ThrowError(const char* format, Args... args)
{
const int MaxLength = 1024;
const char buffer[MaxLength]{};
vsnprintf(buffer, MaxLength, format, args);
ThrowError(buffer);
}

CrendKing
18th June 2021, 23:26
If there's no public API from VapourSynth, you can always implement yourself: https://github.com/CrendKing/avisynth_filter/blob/master/filter_common/src/environment.cpp#L162 .