blob: 2860e6db9d44b7c569f4a68e7a6ca4d3c6234559 [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
Mike Kleindcfc3ef2019-02-07 09:49:17 -050032// These are unused, and just here so I can look at the disassembly.
33float2 Sqrt(float2 x) { return sqrt(x); }
34float4 Sqrt(float4 x) { return sqrt(x); }
35float8 Sqrt(float8 x) { return sqrt(x); }
36
37float4 RSqrt(float4 x) { return rsqrt(x); }
38float4 Rcp(float4 x) { return rcp(x); }
39float4 Ceil(float4 x) { return ceil(x); }
40float4 Floor(float4 x) { return floor(x); }
41float4 Trunc(float4 x) { return trunc(x); }
42float4 Round(float4 x) { return round(x); }
43float4 Abs(float4 x) { return abs(x); }
44
45float4 Min(float4 x, float4 y) { return min(x,y); }
46float4 Max(float4 x, float4 y) { return max(x,y); }
47
Mike Klein41b995c2019-02-27 10:24:55 -060048float4 IfThenElse(int4 c, float4 t, float4 e) { return if_then_else(c,t,e); }
49
Mike Klein455c7472019-02-05 13:42:46 -050050DEF_TEST(SkVx, r) {
51 static_assert(sizeof(float2) == 8, "");
52 static_assert(sizeof(float4) == 16, "");
53 static_assert(sizeof(float8) == 32, "");
54
55 static_assert(sizeof(byte2) == 2, "");
56 static_assert(sizeof(byte4) == 4, "");
57 static_assert(sizeof(byte8) == 8, "");
58
59 {
60 int4 mask = float4{1,2,3,4} < float4{1,2,4,8};
61 REPORTER_ASSERT(r, mask[0] == int32_t( 0));
62 REPORTER_ASSERT(r, mask[1] == int32_t( 0));
63 REPORTER_ASSERT(r, mask[2] == int32_t(-1));
64 REPORTER_ASSERT(r, mask[3] == int32_t(-1));
65
66 REPORTER_ASSERT(r, any(mask));
67 REPORTER_ASSERT(r, !all(mask));
68 }
69
70 {
71 long4 mask = double4{1,2,3,4} < double4{1,2,4,8};
72 REPORTER_ASSERT(r, mask[0] == int64_t( 0));
73 REPORTER_ASSERT(r, mask[1] == int64_t( 0));
74 REPORTER_ASSERT(r, mask[2] == int64_t(-1));
75 REPORTER_ASSERT(r, mask[3] == int64_t(-1));
76
77 REPORTER_ASSERT(r, any(mask));
78 REPORTER_ASSERT(r, !all(mask));
79 }
80
81 REPORTER_ASSERT(r, min(float4{1,2,3,4}) == 1);
82 REPORTER_ASSERT(r, max(float4{1,2,3,4}) == 4);
83
84 REPORTER_ASSERT(r, all(int4{1,2,3,4,5} == int4{1,2,3,4}));
85 REPORTER_ASSERT(r, all(int4{1,2,3,4} == int4{1,2,3,4}));
86 REPORTER_ASSERT(r, all(int4{1,2,3} == int4{1,2,3,0}));
87 REPORTER_ASSERT(r, all(int4{1,2} == int4{1,2,0,0}));
88 REPORTER_ASSERT(r, all(int4{1} == int4{1,0,0,0}));
89 REPORTER_ASSERT(r, all(int4(1) == int4{1,1,1,1}));
90 REPORTER_ASSERT(r, all(int4{} == int4{0,0,0,0}));
91 REPORTER_ASSERT(r, all(int4() == int4{0,0,0,0}));
92
93 REPORTER_ASSERT(r, all(int4{1,2,2,1} == min(int4{1,2,3,4}, int4{4,3,2,1})));
94 REPORTER_ASSERT(r, all(int4{4,3,3,4} == max(int4{1,2,3,4}, int4{4,3,2,1})));
95
96 REPORTER_ASSERT(r, all(if_then_else(float4{1,2,3,2} <= float4{2,2,2,2}, float4(42), float4(47))
97 == float4{42,42,47,42}));
Mike Klein42925152019-02-06 11:56:58 -050098
99 REPORTER_ASSERT(r, all(floor(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,1.0f,1.0f,-1.0f}));
100 REPORTER_ASSERT(r, all( ceil(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,2.0f,1.0f,-1.0f}));
101 REPORTER_ASSERT(r, all(trunc(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,1.0f,1.0f,-1.0f}));
102 REPORTER_ASSERT(r, all(round(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,2.0f,1.0f,-1.0f}));
103
104
105 REPORTER_ASSERT(r, all(abs(float4{-2,-1,0,1}) == float4{2,1,0,1}));
106
107 // TODO(mtklein): these tests could be made less loose.
108 REPORTER_ASSERT(r, all( sqrt(float4{2,3,4,5}) < float4{2,2,3,3}));
109 REPORTER_ASSERT(r, all( rcp(float4{2,3,4,5}) < float4{1.0f,0.5f,0.5f,0.3f}));
110 REPORTER_ASSERT(r, all(rsqrt(float4{2,3,4,5}) < float4{1.0f,1.0f,1.0f,0.5f}));
111
Mike Klein41b995c2019-02-27 10:24:55 -0600112 REPORTER_ASSERT(r, all( sqrt(float2{2,3}) < float2{2,2}));
113 REPORTER_ASSERT(r, all( rcp(float2{2,3}) < float2{1.0f,0.5f}));
114 REPORTER_ASSERT(r, all(rsqrt(float2{2,3}) < float2{1.0f,1.0f}));
115
Mike Kleinda7b0532019-04-10 12:40:31 -0500116 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 -0500117
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500118 float buf[] = {1,2,3,4,5,6};
Mike Klein42925152019-02-06 11:56:58 -0500119 REPORTER_ASSERT(r, all(float4::Load(buf) == float4{1,2,3,4}));
120 float4{2,3,4,5}.store(buf);
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500121 REPORTER_ASSERT(r, buf[0] == 2
122 && buf[1] == 3
123 && buf[2] == 4
124 && buf[3] == 5
125 && buf[4] == 5
126 && buf[5] == 6);
127 REPORTER_ASSERT(r, all(float4::Load(buf+0) == float4{2,3,4,5}));
128 REPORTER_ASSERT(r, all(float4::Load(buf+2) == float4{4,5,5,6}));
Mike Klein53a52982019-02-06 15:48:12 -0500129
130 REPORTER_ASSERT(r, all(mad(float4{1,2,3,4}, 2.0f, 3.0f) == float4{5,7,9,11}));
131
Mike Kleinda7b0532019-04-10 12:40:31 -0500132 REPORTER_ASSERT(r, all(skvx::shuffle<2,1,0,3> (float4{1,2,3,4}) == float4{3,2,1,4}));
133 REPORTER_ASSERT(r, all(skvx::shuffle<2,1> (float4{1,2,3,4}) == float2{3,2}));
134 REPORTER_ASSERT(r, all(skvx::shuffle<3,3,3,3> (float4{1,2,3,4}) == float4{4,4,4,4}));
135 REPORTER_ASSERT(r, all(skvx::shuffle<2,1,2,1,2,1,2,1>(float4{1,2,3,4})
136 == float8{3,2,3,2,3,2,3,2}));
Mike Kleinf4438d52019-03-14 13:30:42 -0500137
138 // Test that mixed types can be used where they make sense. Mostly about ergonomics.
139 REPORTER_ASSERT(r, all(float4{1,2,3,4} < 5));
140 REPORTER_ASSERT(r, all( byte4{1,2,3,4} < 5));
141 REPORTER_ASSERT(r, all( int4{1,2,3,4} < 5.0f));
142 float4 five = 5;
143 REPORTER_ASSERT(r, all(five == 5.0f));
144 REPORTER_ASSERT(r, all(five == 5));
145
146 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 -0500147
148 for (int x = 0; x < 256; x++)
149 for (int y = 0; y < 256; y++) {
Mike Klein1dcc55b2019-04-11 13:40:30 -0500150 uint8_t want = (uint8_t)( 255*(x/255.0 * y/255.0) + 0.5 );
Mike Klein4b44a0d2019-04-11 11:52:51 -0500151
Mike Klein1dcc55b2019-04-11 13:40:30 -0500152 {
153 uint8_t got = skvx::div255(skvx::Vec<8, uint16_t>(x) *
154 skvx::Vec<8, uint16_t>(y) )[0];
155 REPORTER_ASSERT(r, got == want);
156 }
Mike Klein4b44a0d2019-04-11 11:52:51 -0500157
Mike Klein1dcc55b2019-04-11 13:40:30 -0500158 {
159 uint8_t got = skvx::approx_scale(skvx::Vec<8,uint8_t>(x),
160 skvx::Vec<8,uint8_t>(y))[0];
Mike Klein4b44a0d2019-04-11 11:52:51 -0500161
Mike Klein1dcc55b2019-04-11 13:40:30 -0500162 REPORTER_ASSERT(r, got == want-1 ||
163 got == want ||
164 got == want+1);
165 if (x == 0 || y == 0 || x == 255 || y == 255) {
166 REPORTER_ASSERT(r, got == want);
167 }
Mike Klein4b44a0d2019-04-11 11:52:51 -0500168 }
169 }
Mike Klein9a885b22019-04-16 12:07:23 -0500170
171 for (int x = 0; x < 256; x++)
172 for (int y = 0; y < 256; y++) {
173 uint16_t xy = x*y;
174
175 // Make sure to cover implementation cases N=8, N<8, and N>8.
176 REPORTER_ASSERT(r, all(mull(byte2 (x), byte2 (y)) == xy));
177 REPORTER_ASSERT(r, all(mull(byte4 (x), byte4 (y)) == xy));
178 REPORTER_ASSERT(r, all(mull(byte8 (x), byte8 (y)) == xy));
179 REPORTER_ASSERT(r, all(mull(byte16(x), byte16(y)) == xy));
180 }
Mike Klein455c7472019-02-05 13:42:46 -0500181}