Code: Select all
template<typename T, typename U>
struct common_type {
using intermediate_type = typename get_higher_value_<T, U>::type;
static constexpr auto use_float = is_simd_float<intermediate_type>::value;
using integral_type = std::conditional_t<
( !use_float ) && ( is_unsigned_v<T> || is_unsigned_v<U> ),
make_simd_unsigned_t<intermediate_type>,
intermediate_type
>;
using type = std::conditional_t<
use_float, // ?
mm_float_t, // :
integral_type
>;
};
I'm making an SSE library. SIMD ( SSE/AVX ) doesn't distinguish between the different integral types, so it's kind of impossible to create math operator overloads for them. SSE has only one integral type, __m128i. There are add and subtract functions that handle this type as 4 distinct types, a few multiplication functions and no divide functions. So this library will attempt to separate and provide overloads for the integer types as well as interaction between integral types and floating point types.
For instance, when blending two colors the most straight forward approach is to have the alpha in the range of 0.f to 1.f. If you aren't comfortable or aren't familiar with bit manipulation it's just easier to convert the four color channels to floats, do the math then convert back to chars. While this is less efficient than just manipulating bits and approximation, it is more readable and straight forward.
With SSE, it becomes even more convoluted. You have to convert the chars to shorts manually, then do the normal stuff like extract the alpha, multiply alpha with the three color channels, add those results together, then shift right by 8 ( like dividing by 256 ), then convert the shorts back to chars. The steps are simple and straightforward, but figuring out which intrinsics to use and how to use them was a pain to learn and still are a pain to write and an eye sore in the code.
As the title jokingly states, this isn't my first attempt at making an SSE library, but I have a better understanding of templates and with features like constexpr-if it is a bit easier or less tedious.