1 /*
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 
8 #include "SkVx.h"
9 #include "Test.h"
10 
11 using float2 = skvx::Vec<2,float>;
12 using float4 = skvx::Vec<4,float>;
13 using float8 = skvx::Vec<8,float>;
14 
15 using double2 = skvx::Vec<2,double>;
16 using double4 = skvx::Vec<4,double>;
17 using double8 = skvx::Vec<8,double>;
18 
19 using byte2 = skvx::Vec<2,uint8_t>;
20 using byte4 = skvx::Vec<4,uint8_t>;
21 using byte8 = skvx::Vec<8,uint8_t>;
22 
23 using int2 = skvx::Vec<2,int32_t>;
24 using int4 = skvx::Vec<4,int32_t>;
25 using int8 = skvx::Vec<8,int32_t>;
26 
27 using long2 = skvx::Vec<2,int64_t>;
28 using long4 = skvx::Vec<4,int64_t>;
29 using long8 = skvx::Vec<8,int64_t>;
30 
31 // These are unused, and just here so I can look at the disassembly.
Sqrt(float2 x)32 float2 Sqrt(float2 x) { return sqrt(x); }
Sqrt(float4 x)33 float4 Sqrt(float4 x) { return sqrt(x); }
Sqrt(float8 x)34 float8 Sqrt(float8 x) { return sqrt(x); }
35 
RSqrt(float4 x)36 float4 RSqrt(float4 x) { return rsqrt(x); }
Rcp(float4 x)37 float4   Rcp(float4 x) { return   rcp(x); }
Ceil(float4 x)38 float4  Ceil(float4 x) { return  ceil(x); }
Floor(float4 x)39 float4 Floor(float4 x) { return floor(x); }
Trunc(float4 x)40 float4 Trunc(float4 x) { return trunc(x); }
Round(float4 x)41 float4 Round(float4 x) { return round(x); }
Abs(float4 x)42 float4   Abs(float4 x) { return   abs(x); }
43 
Min(float4 x,float4 y)44 float4 Min(float4 x, float4 y) { return min(x,y); }
Max(float4 x,float4 y)45 float4 Max(float4 x, float4 y) { return max(x,y); }
46 
IfThenElse(int4 c,float4 t,float4 e)47 float4 IfThenElse(int4 c, float4 t, float4 e) { return if_then_else(c,t,e); }
48 
DEF_TEST(SkVx,r)49 DEF_TEST(SkVx, r) {
50     static_assert(sizeof(float2) ==  8, "");
51     static_assert(sizeof(float4) == 16, "");
52     static_assert(sizeof(float8) == 32, "");
53 
54     static_assert(sizeof(byte2) == 2, "");
55     static_assert(sizeof(byte4) == 4, "");
56     static_assert(sizeof(byte8) == 8, "");
57 
58     {
59         int4 mask = float4{1,2,3,4} < float4{1,2,4,8};
60         REPORTER_ASSERT(r, mask[0] == int32_t( 0));
61         REPORTER_ASSERT(r, mask[1] == int32_t( 0));
62         REPORTER_ASSERT(r, mask[2] == int32_t(-1));
63         REPORTER_ASSERT(r, mask[3] == int32_t(-1));
64 
65         REPORTER_ASSERT(r,  any(mask));
66         REPORTER_ASSERT(r, !all(mask));
67     }
68 
69     {
70         long4 mask = double4{1,2,3,4} < double4{1,2,4,8};
71         REPORTER_ASSERT(r, mask[0] == int64_t( 0));
72         REPORTER_ASSERT(r, mask[1] == int64_t( 0));
73         REPORTER_ASSERT(r, mask[2] == int64_t(-1));
74         REPORTER_ASSERT(r, mask[3] == int64_t(-1));
75 
76         REPORTER_ASSERT(r,  any(mask));
77         REPORTER_ASSERT(r, !all(mask));
78     }
79 
80     REPORTER_ASSERT(r, min(float4{1,2,3,4}) == 1);
81     REPORTER_ASSERT(r, max(float4{1,2,3,4}) == 4);
82 
83     REPORTER_ASSERT(r, all(int4{1,2,3,4,5} == int4{1,2,3,4}));
84     REPORTER_ASSERT(r, all(int4{1,2,3,4}   == int4{1,2,3,4}));
85     REPORTER_ASSERT(r, all(int4{1,2,3}     == int4{1,2,3,0}));
86     REPORTER_ASSERT(r, all(int4{1,2}       == int4{1,2,0,0}));
87     REPORTER_ASSERT(r, all(int4{1}         == int4{1,0,0,0}));
88     REPORTER_ASSERT(r, all(int4(1)         == int4{1,1,1,1}));
89     REPORTER_ASSERT(r, all(int4{}          == int4{0,0,0,0}));
90     REPORTER_ASSERT(r, all(int4()          == int4{0,0,0,0}));
91 
92     REPORTER_ASSERT(r, all(int4{1,2,2,1} == min(int4{1,2,3,4}, int4{4,3,2,1})));
93     REPORTER_ASSERT(r, all(int4{4,3,3,4} == max(int4{1,2,3,4}, int4{4,3,2,1})));
94 
95     REPORTER_ASSERT(r, all(if_then_else(float4{1,2,3,2} <= float4{2,2,2,2}, float4(42), float4(47))
96                            == float4{42,42,47,42}));
97 
98     REPORTER_ASSERT(r, all(floor(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,1.0f,1.0f,-1.0f}));
99     REPORTER_ASSERT(r, all( ceil(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,2.0f,1.0f,-1.0f}));
100     REPORTER_ASSERT(r, all(trunc(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-1.0f,1.0f,1.0f,-1.0f}));
101     REPORTER_ASSERT(r, all(round(float4{-1.5f,1.5f,1.0f,-1.0f}) == float4{-2.0f,2.0f,1.0f,-1.0f}));
102 
103 
104     REPORTER_ASSERT(r, all(abs(float4{-2,-1,0,1}) == float4{2,1,0,1}));
105 
106     // TODO(mtklein): these tests could be made less loose.
107     REPORTER_ASSERT(r, all( sqrt(float4{2,3,4,5}) < float4{2,2,3,3}));
108     REPORTER_ASSERT(r, all(  rcp(float4{2,3,4,5}) < float4{1.0f,0.5f,0.5f,0.3f}));
109     REPORTER_ASSERT(r, all(rsqrt(float4{2,3,4,5}) < float4{1.0f,1.0f,1.0f,0.5f}));
110 
111     REPORTER_ASSERT(r, all( sqrt(float2{2,3}) < float2{2,2}));
112     REPORTER_ASSERT(r, all(  rcp(float2{2,3}) < float2{1.0f,0.5f}));
113     REPORTER_ASSERT(r, all(rsqrt(float2{2,3}) < float2{1.0f,1.0f}));
114 
115     REPORTER_ASSERT(r, all(cast<int>(float4{-1.5f,0.5f,1.0f,1.5f}) == int4{-1,0,1,1}));
116 
117     float buf[] = {1,2,3,4,5,6};
118     REPORTER_ASSERT(r, all(float4::Load(buf) == float4{1,2,3,4}));
119     float4{2,3,4,5}.store(buf);
120     REPORTER_ASSERT(r, buf[0] == 2
121                     && buf[1] == 3
122                     && buf[2] == 4
123                     && buf[3] == 5
124                     && buf[4] == 5
125                     && buf[5] == 6);
126     REPORTER_ASSERT(r, all(float4::Load(buf+0) == float4{2,3,4,5}));
127     REPORTER_ASSERT(r, all(float4::Load(buf+2) == float4{4,5,5,6}));
128 
129     REPORTER_ASSERT(r, all(mad(float4{1,2,3,4}, 2.0f, 3.0f) == float4{5,7,9,11}));
130 
131     REPORTER_ASSERT(r, all(shuffle<2,1,0,3>        (float4{1,2,3,4}) == float4{3,2,1,4}));
132     REPORTER_ASSERT(r, all(shuffle<2,1>            (float4{1,2,3,4}) == float2{3,2}));
133     REPORTER_ASSERT(r, all(shuffle<2,1,2,1,2,1,2,1>(float4{1,2,3,4}) == float8{3,2,3,2,3,2,3,2}));
134     REPORTER_ASSERT(r, all(shuffle<3,3,3,3>        (float4{1,2,3,4}) == float4{4,4,4,4}));
135 }
136