The answer to this question also depends on whether you want the actual inequality, where you will use something according to what @PaulR shows:
bool fneq128_a (__m128 const& a, __m128 const& b) {
or do you want to use some epsilon to indicate that the elements are still considered "equal" if they do not differ from each other beyond the threshold:
bool fneq128_b (__m128 const& a, __m128 const& b, float epsilon = 1.e-8f) { // epsilon vector auto eps = _mm_set1_ps(epsilon); // absolute of difference of a and b auto abd = _mm_andnot_ps(_mm_set1_ps(-0.0f), _mm_sub_ps(a, b)); // compare abd to eps // returns true if one of the elements in abd is not less than // epsilon return _mm_movemask_ps(_mm_cmplt_ps(abd, eps)) != 0xF; }
Example:
auto a = _mm_set_ps(0.0, 0.0, 0.0, 0.0); auto b = _mm_set_ps(0.0, 0.0, 0.0, 1.e-15); std::cout << fneq128_a(a, b) << ' ' << fneq128_b(a, b) << "\n";
Print
1 0
Pixelchemist
source share