OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkNx.h" |
| 9 #include "Test.h" |
| 10 |
| 11 template <int N, typename T> |
| 12 static void test_Nf(skiatest::Reporter* r) { |
| 13 |
| 14 auto assert_nearly_eq = [&](double eps, const SkNf<N,T>& v, T a, T b, T c, T
d) { |
| 15 auto close = [=](T a, T b) { return fabs(a-b) <= eps; }; |
| 16 T vals[4]; |
| 17 v.store(vals); |
| 18 REPORTER_ASSERT(r, close(vals[0], a) && close(vals[1], b)); |
| 19 REPORTER_ASSERT(r, close( v[0], a) && close( v[1], b)); |
| 20 |
| 21 if (N == 4) { |
| 22 REPORTER_ASSERT(r, close(vals[2], c) && close(vals[3], d)); |
| 23 REPORTER_ASSERT(r, close( v[2], c) && close( v[3], d)); |
| 24 } |
| 25 }; |
| 26 auto assert_eq = [&](const SkNf<N,T>& v, T a, T b, T c, T d) { |
| 27 return assert_nearly_eq(0, v, a,b,c,d); |
| 28 }; |
| 29 |
| 30 T vals[] = {3, 4, 5, 6}; |
| 31 SkNf<N,T> a = SkNf<N,T>::Load(vals), |
| 32 b(a), |
| 33 c = a; |
| 34 SkNf<N,T> d; |
| 35 d = a; |
| 36 |
| 37 assert_eq(a, 3, 4, 5, 6); |
| 38 assert_eq(b, 3, 4, 5, 6); |
| 39 assert_eq(c, 3, 4, 5, 6); |
| 40 assert_eq(d, 3, 4, 5, 6); |
| 41 |
| 42 assert_eq(a+b, 6, 8, 10, 12); |
| 43 assert_eq(a*b, 9, 16, 25, 36); |
| 44 assert_eq(a*b-b, 6, 12, 20, 30); |
| 45 assert_eq((a*b).sqrt(), 3, 4, 5, 6); |
| 46 assert_eq(a/b, 1, 1, 1, 1); |
| 47 assert_eq(-a, -3, -4, -5, -6); |
| 48 |
| 49 SkNf<N,T> fours(4); |
| 50 |
| 51 assert_eq(fours.sqrt(), 2,2,2,2); |
| 52 assert_nearly_eq(0.001, fours.rsqrt(), 0.5, 0.5, 0.5, 0.5); |
| 53 |
| 54 assert_eq( fours. invert(), 0.25, 0.25, 0.25, 0.25); |
| 55 assert_nearly_eq(0.001, fours.approxInvert(), 0.25, 0.25, 0.25, 0.25); |
| 56 |
| 57 assert_eq(SkNf<N,T>::Min(a, fours), 3, 4, 4, 4); |
| 58 assert_eq(SkNf<N,T>::Max(a, fours), 4, 4, 5, 6); |
| 59 |
| 60 // Test some comparisons. This is not exhaustive. |
| 61 REPORTER_ASSERT(r, (a == b).allTrue()); |
| 62 REPORTER_ASSERT(r, (a+b == a*b-b).anyTrue()); |
| 63 REPORTER_ASSERT(r, !(a+b == a*b-b).allTrue()); |
| 64 REPORTER_ASSERT(r, !(a+b == a*b).anyTrue()); |
| 65 REPORTER_ASSERT(r, !(a != b).anyTrue()); |
| 66 REPORTER_ASSERT(r, (a < fours).anyTrue()); |
| 67 REPORTER_ASSERT(r, (a <= fours).anyTrue()); |
| 68 REPORTER_ASSERT(r, !(a > fours).allTrue()); |
| 69 REPORTER_ASSERT(r, !(a >= fours).allTrue()); |
| 70 } |
| 71 |
| 72 DEF_TEST(SkNf, r) { |
| 73 test_Nf<2, float>(r); |
| 74 test_Nf<2, double>(r); |
| 75 |
| 76 test_Nf<4, float>(r); |
| 77 test_Nf<4, double>(r); |
| 78 } |
OLD | NEW |