Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

c++ - Use different class upon available CPU support

How do I change the used class upon available CPU features?

For example changing the used Vector4-class by supported SIMD on the client side:

bool has_sse = get_cpuid_info( "sse" );

if( has_sse ) {
    // use Vector4SIMD only in the entire app
} else {
    // fallback to a plain Vector4-class
}

I can think of some ways (edit: added suggestions):

  • using dynamic libraries
  • dynamic cast's
  • virtual dispatch
  • compiler specific attributes

How is this, or similar behaviour done in nowadays software?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It could be compiler specific. Recent GCC offers specific x86 builtins like __builtin_cpu_supports etc. See also the ifunc function attribute

Of course, as Mark Ransom commented, you could use virtual methods. But that might not worth the effort for very short and quick small functions (you want them to be inlined).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...