blob: 60b815a4845d2151d4deb004100fbc240bde34e9 [file] [log] [blame]
Mike Klein455c7472019-02-05 13:42:46 -05001/*
2 * Copyright 2019 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkVx.h"
9#include "tests/Test.h"
Mike Klein455c7472019-02-05 13:42:46 -050010
11using float2 = skvx::Vec<2,float>;
12using float4 = skvx::Vec<4,float>;
13using float8 = skvx::Vec<8,float>;
14
15using double2 = skvx::Vec<2,double>;
16using double4 = skvx::Vec<4,double>;
17using double8 = skvx::Vec<8,double>;
18
Mike Klein9a885b22019-04-16 12:07:23 -050019using byte2 = skvx::Vec< 2,uint8_t>;
20using byte4 = skvx::Vec< 4,uint8_t>;
21using byte8 = skvx::Vec< 8,uint8_t>;
22using byte16 = skvx::Vec<16,uint8_t>;
Mike Klein455c7472019-02-05 13:42:46 -050023
24using int2 = skvx::Vec<2,int32_t>;
25using int4 = skvx::Vec<4,int32_t>;
26using int8 = skvx::Vec<8,int32_t>;
27
28using long2 = skvx::Vec<2,int64_t>;
29using long4 = skvx::Vec<4,int64_t>;
30using long8 = skvx::Vec<8,int64_t>;
31
32DEF_TEST(SkVx, r) {
33 static_assert(sizeof(float2) == 8, "");
34 static_assert(sizeof(float4) == 16, "");
35 static_assert(sizeof(float8) == 32, "");
36
37 static_assert(sizeof(byte2) == 2, "");
38 static_assert(sizeof(byte4) == 4, "");
39 static_assert(sizeof(byte8) == 8, "");
40
41 {
42 int4 mask = float4{1,2,3,4} < float4{1,2,4,8};
43 REPORTER_ASSERT(r, mask[0] == int32_t( 0));
44 REPORTER_ASSERT(r, mask[1] == int32_t( 0));
45 REPORTER_ASSERT(r, mask[2] == int32_t(-1));
46 REPORTER_ASSERT(r, mask[3] == int32_t(-1));
47
48 REPORTER_ASSERT(r, any(mask));
49 REPORTER_ASSERT(r, !all(mask));
50 }
51
52 {
53 long4 mask = double4{1,2,3,4} < double4{1,2,4,8};
54 REPORTER_ASSERT(r, mask[0] == int64_t( 0));
55 REPORTER_ASSERT(r, mask[1] == int64_t( 0));
56 REPORTER_ASSERT(r, mask[2] == int64_t(-1));
57 REPORTER_ASSERT(r, mask[3] == int64_t(-1));
58
59 REPORTER_ASSERT(r, any(mask));
60 REPORTER_ASSERT(r, !all(mask));
61 }
62
63 REPORTER_ASSERT(r, min(float4{1,2,3,4}) == 1);
64 REPORTER_ASSERT(r, max(float4{1,2,3,4}) == 4);
65
66 REPORTER_ASSERT(r, all(int4{1,2,3,4,5} == int4{1,2,3,4}));
67 REPORTER_ASSERT(r, all(int4{1,2,3,4} == int4{1,2,3,4}));
68 REPORTER_ASSERT(r, all(int4{1,2,3} == int4{1,2,3,0}));
69 REPORTER_ASSERT(r, all(int4{1,2} == int4{1,2,0,0}));
70 REPORTER_ASSERT(r, all(int4{1} == int4{1,0,0,0}));
71 REPORTER_ASSERT(r, all(int4(1) == int4{1,1,1,1}));
72 REPORTER_ASSERT(r, all(int4{} == int4{0,0,0,0}));
73 REPORTER_ASSERT(r, all(int4() == int4{0,0,0,0}));
74
75 REPORTER_ASSERT(r, all(int4{1,2,2,1} == min(int4{1,2,3,4}, int4{4,3,2,1})));
76 REPORTER_ASSERT(r, all(int4{4,3,3,4} == max(int4{1,2,3,4}, int4{4,3,2,1})));
77
78 REPORTER_ASSERT(r, all(if_then_else(float4{1,2,3,2} <= float4{2,2,2,2}, float4(42), float4(47))
79 == float4{42,42,47,42}));
Mike Klein42925152019-02-06 11:56:58 -050080
81 REPORTER_ASSERT(r, all(floor(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,1.0f,1.0f,-1.0f}));
82 REPORTER_ASSERT(r, all( ceil(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,2.0f,1.0f,-1.0f}));
83 REPORTER_ASSERT(r, all(trunc(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,1.0f,1.0f,-1.0f}));
84 REPORTER_ASSERT(r, all(round(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,2.0f,1.0f,-1.0f}));
85
86
87 REPORTER_ASSERT(r, all(abs(float4{-2,-1,0,1}) == float4{2,1,0,1}));
88
89 // TODO(mtklein): these tests could be made less loose.
90 REPORTER_ASSERT(r, all( sqrt(float4{2,3,4,5}) < float4{2,2,3,3}));
Mike Klein41b995c2019-02-27 10:24:55 -060091 REPORTER_ASSERT(r, all( sqrt(float2{2,3}) < float2{2,2}));
Mike Klein41b995c2019-02-27 10:24:55 -060092
Mike Kleinda7b0532019-04-10 12:40:31 -050093 REPORTER_ASSERT(r, all(skvx::cast<int>(float4{-1.5f,0.5f,1.0f,1.5f}) == int4{-1,0,1,1}));
Mike Klein42925152019-02-06 11:56:58 -050094
Mike Kleindcfc3ef2019-02-07 09:49:17 -050095 float buf[] = {1,2,3,4,5,6};
Mike Klein42925152019-02-06 11:56:58 -050096 REPORTER_ASSERT(r, all(float4::Load(buf) == float4{1,2,3,4}));
97 float4{2,3,4,5}.store(buf);
Mike Kleindcfc3ef2019-02-07 09:49:17 -050098 REPORTER_ASSERT(r, buf[0] == 2
99 && buf[1] == 3
100 && buf[2] == 4
101 && buf[3] == 5
102 && buf[4] == 5
103 && buf[5] == 6);
104 REPORTER_ASSERT(r, all(float4::Load(buf+0) == float4{2,3,4,5}));
105 REPORTER_ASSERT(r, all(float4::Load(buf+2) == float4{4,5,5,6}));
Mike Klein53a52982019-02-06 15:48:12 -0500106
Mike Kleinda7b0532019-04-10 12:40:31 -0500107 REPORTER_ASSERT(r, all(skvx::shuffle<2,1,0,3> (float4{1,2,3,4}) == float4{3,2,1,4}));
108 REPORTER_ASSERT(r, all(skvx::shuffle<2,1> (float4{1,2,3,4}) == float2{3,2}));
109 REPORTER_ASSERT(r, all(skvx::shuffle<3,3,3,3> (float4{1,2,3,4}) == float4{4,4,4,4}));
110 REPORTER_ASSERT(r, all(skvx::shuffle<2,1,2,1,2,1,2,1>(float4{1,2,3,4})
111 == float8{3,2,3,2,3,2,3,2}));
Mike Kleinf4438d52019-03-14 13:30:42 -0500112
113 // Test that mixed types can be used where they make sense. Mostly about ergonomics.
114 REPORTER_ASSERT(r, all(float4{1,2,3,4} < 5));
115 REPORTER_ASSERT(r, all( byte4{1,2,3,4} < 5));
116 REPORTER_ASSERT(r, all( int4{1,2,3,4} < 5.0f));
117 float4 five = 5;
118 REPORTER_ASSERT(r, all(five == 5.0f));
119 REPORTER_ASSERT(r, all(five == 5));
120
121 REPORTER_ASSERT(r, all(max(2, min(float4{1,2,3,4}, 3)) == float4{2,2,3,3}));
Mike Klein4b44a0d2019-04-11 11:52:51 -0500122
123 for (int x = 0; x < 256; x++)
124 for (int y = 0; y < 256; y++) {
Mike Klein1dcc55b2019-04-11 13:40:30 -0500125 uint8_t want = (uint8_t)( 255*(x/255.0 * y/255.0) + 0.5 );
Mike Klein4b44a0d2019-04-11 11:52:51 -0500126
Mike Klein1dcc55b2019-04-11 13:40:30 -0500127 {
128 uint8_t got = skvx::div255(skvx::Vec<8, uint16_t>(x) *
129 skvx::Vec<8, uint16_t>(y) )[0];
130 REPORTER_ASSERT(r, got == want);
131 }
Mike Klein4b44a0d2019-04-11 11:52:51 -0500132
Mike Klein1dcc55b2019-04-11 13:40:30 -0500133 {
134 uint8_t got = skvx::approx_scale(skvx::Vec<8,uint8_t>(x),
135 skvx::Vec<8,uint8_t>(y))[0];
Mike Klein4b44a0d2019-04-11 11:52:51 -0500136
Mike Klein1dcc55b2019-04-11 13:40:30 -0500137 REPORTER_ASSERT(r, got == want-1 ||
138 got == want ||
139 got == want+1);
140 if (x == 0 || y == 0 || x == 255 || y == 255) {
141 REPORTER_ASSERT(r, got == want);
142 }
Mike Klein4b44a0d2019-04-11 11:52:51 -0500143 }
144 }
Mike Klein9a885b22019-04-16 12:07:23 -0500145
146 for (int x = 0; x < 256; x++)
147 for (int y = 0; y < 256; y++) {
148 uint16_t xy = x*y;
149
150 // Make sure to cover implementation cases N=8, N<8, and N>8.
151 REPORTER_ASSERT(r, all(mull(byte2 (x), byte2 (y)) == xy));
152 REPORTER_ASSERT(r, all(mull(byte4 (x), byte4 (y)) == xy));
153 REPORTER_ASSERT(r, all(mull(byte8 (x), byte8 (y)) == xy));
154 REPORTER_ASSERT(r, all(mull(byte16(x), byte16(y)) == xy));
155 }
Mike Klein4d680cd2020-07-15 09:58:51 -0500156
157 {
158 // Intentionally not testing -0, as we don't care if it's 0x0000 or 0x8000.
159 float8 fs = {+0.0f,+0.5f,+1.0f,+2.0f,
160 -4.0f,-0.5f,-1.0f,-2.0f};
161 skvx::Vec<8,uint16_t> hs = {0x0000,0x3800,0x3c00,0x4000,
162 0xc400,0xb800,0xbc00,0xc000};
163 REPORTER_ASSERT(r, all(skvx:: to_half(fs) == hs));
164 REPORTER_ASSERT(r, all(skvx::from_half(hs) == fs));
165 }
Mike Klein455c7472019-02-05 13:42:46 -0500166}