K. G. - 13 days ago 3x

C++ Question

UPDATE: I guess the question can be summarized as: **"Is there a modern C++ approach that is equivalent to polymorphic function-like macros?"**

I wonder if it is possible to program in C++ to write one kernel with the abstract operations, and automatically produce ISA-specific codes. For example, a generic kernel can be:

`RET_TYPE kernel(IN_TYPE a, IN_TYPE b)`

{

RET_TYPE res = ADD(a, b);

return res;

}

And the kernel can be transformed into

`float kernel(float a, float b)`

{

float res = a + b;

return res;

}

and a vectorized version:

`__m128 kernel(__m128 a, __m128 b)`

{

__m128 res = _mm_add_ps(a, b);

return res;

}

In reality, the generic kernels would be much more complex. The genericity in the types can be handled by template parameters. But the genericity of the instructions got me stuck.

Usually, this kind of problem is addressed via

However, I have to do it within

Answer

Generally it's achievable with templates and type traits:

```
template <typename T>
T kernel(T a, T b)
{
return MathTraits<T>::add (a, b);
}
template <typename T>
class MathTraits
{
}
// specialization for float
template <>
class MathTraits <float>
{
float add(float a, float b)
{
return a+b;
}
}
// specialization of MathTraits for __m128 etc.
```

However, this approach may fail when you may want to treat the same type differently in different situations. But that's general limit of overloading...

With the example given it's actually possible to directly specialize the function but the way described is more common as it's more clear and reusable.

Source (Stackoverflow)

Comments