c9adb05b64
The primary feature this delivers is SkNf and SkNd for arbitrary power-of-two N. Non-specialized types or types larger than 128 bits should now Just Work (and we can drop in a specialization to make them faster). Sk4s is now just a typedef for SkNf<4, SkScalar>; Sk4d is SkNf<4, double>, Sk2f SkNf<2, float>, etc. This also makes implementing new specializations easier and more encapsulated. We're now using template specialization, which means the specialized versions don't have to leak out so much from SkNx_sse.h and SkNx_neon.h. This design leaves us room to grow up, e.g to SkNf<8, SkScalar> == Sk8s, and to grown down too, to things like SkNi<8, uint16_t> == Sk8h. To simplify things, I've stripped away most APIs (swizzles, casts, reinterpret_casts) that no one's using yet. I will happily add them back if they seem useful. You shouldn't feel bad about using any of the typedef Sk4s, Sk4f, Sk4d, Sk2s, Sk2f, Sk2d, Sk4i, etc. Here's how you should feel: - Sk4f, Sk4s, Sk2d: feel awesome - Sk2f, Sk2s, Sk4d: feel pretty good No public API changes. TBR=reed@google.com BUG=skia:3592 Review URL: https://codereview.chromium.org/1048593002
79 lines
2.3 KiB
C++
79 lines
2.3 KiB
C++
/*
|
|
* Copyright 2015 Google Inc.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#include "SkNx.h"
|
|
#include "Test.h"
|
|
|
|
template <int N, typename T>
|
|
static void test_Nf(skiatest::Reporter* r) {
|
|
|
|
auto assert_nearly_eq = [&](double eps, const SkNf<N,T>& v, T a, T b, T c, T d) {
|
|
auto close = [=](T a, T b) { return fabs(a-b) <= eps; };
|
|
T vals[4];
|
|
v.store(vals);
|
|
REPORTER_ASSERT(r, close(vals[0], a) && close(vals[1], b));
|
|
REPORTER_ASSERT(r, close( v[0], a) && close( v[1], b));
|
|
|
|
if (N == 4) {
|
|
REPORTER_ASSERT(r, close(vals[2], c) && close(vals[3], d));
|
|
REPORTER_ASSERT(r, close( v[2], c) && close( v[3], d));
|
|
}
|
|
};
|
|
auto assert_eq = [&](const SkNf<N,T>& v, T a, T b, T c, T d) {
|
|
return assert_nearly_eq(0, v, a,b,c,d);
|
|
};
|
|
|
|
T vals[] = {3, 4, 5, 6};
|
|
SkNf<N,T> a = SkNf<N,T>::Load(vals),
|
|
b(a),
|
|
c = a;
|
|
SkNf<N,T> d;
|
|
d = a;
|
|
|
|
assert_eq(a, 3, 4, 5, 6);
|
|
assert_eq(b, 3, 4, 5, 6);
|
|
assert_eq(c, 3, 4, 5, 6);
|
|
assert_eq(d, 3, 4, 5, 6);
|
|
|
|
assert_eq(a+b, 6, 8, 10, 12);
|
|
assert_eq(a*b, 9, 16, 25, 36);
|
|
assert_eq(a*b-b, 6, 12, 20, 30);
|
|
assert_eq((a*b).sqrt(), 3, 4, 5, 6);
|
|
assert_eq(a/b, 1, 1, 1, 1);
|
|
assert_eq(-a, -3, -4, -5, -6);
|
|
|
|
SkNf<N,T> fours(4);
|
|
|
|
assert_eq(fours.sqrt(), 2,2,2,2);
|
|
assert_nearly_eq(0.001, fours.rsqrt(), 0.5, 0.5, 0.5, 0.5);
|
|
|
|
assert_eq( fours. invert(), 0.25, 0.25, 0.25, 0.25);
|
|
assert_nearly_eq(0.001, fours.approxInvert(), 0.25, 0.25, 0.25, 0.25);
|
|
|
|
assert_eq(SkNf<N,T>::Min(a, fours), 3, 4, 4, 4);
|
|
assert_eq(SkNf<N,T>::Max(a, fours), 4, 4, 5, 6);
|
|
|
|
// Test some comparisons. This is not exhaustive.
|
|
REPORTER_ASSERT(r, (a == b).allTrue());
|
|
REPORTER_ASSERT(r, (a+b == a*b-b).anyTrue());
|
|
REPORTER_ASSERT(r, !(a+b == a*b-b).allTrue());
|
|
REPORTER_ASSERT(r, !(a+b == a*b).anyTrue());
|
|
REPORTER_ASSERT(r, !(a != b).anyTrue());
|
|
REPORTER_ASSERT(r, (a < fours).anyTrue());
|
|
REPORTER_ASSERT(r, (a <= fours).anyTrue());
|
|
REPORTER_ASSERT(r, !(a > fours).allTrue());
|
|
REPORTER_ASSERT(r, !(a >= fours).allTrue());
|
|
}
|
|
|
|
DEF_TEST(SkNf, r) {
|
|
test_Nf<2, float>(r);
|
|
test_Nf<2, double>(r);
|
|
|
|
test_Nf<4, float>(r);
|
|
test_Nf<4, double>(r);
|
|
}
|