• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/private/SkVx.h"
9 #include "tests/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 using byte16 = skvx::Vec<16,uint8_t>;
23 
24 using int2 = skvx::Vec<2,int32_t>;
25 using int4 = skvx::Vec<4,int32_t>;
26 using int8 = skvx::Vec<8,int32_t>;
27 
28 using long2 = skvx::Vec<2,int64_t>;
29 using long4 = skvx::Vec<4,int64_t>;
30 using long8 = skvx::Vec<8,int64_t>;
31 
32 // These are unused, and just here so I can look at the disassembly.
Sqrt(float2 x)33 float2 Sqrt(float2 x) { return sqrt(x); }
Sqrt(float4 x)34 float4 Sqrt(float4 x) { return sqrt(x); }
Sqrt(float8 x)35 float8 Sqrt(float8 x) { return sqrt(x); }
36 
RSqrt(float4 x)37 float4 RSqrt(float4 x) { return rsqrt(x); }
Rcp(float4 x)38 float4   Rcp(float4 x) { return   rcp(x); }
Ceil(float4 x)39 float4  Ceil(float4 x) { return  ceil(x); }
Floor(float4 x)40 float4 Floor(float4 x) { return floor(x); }
Trunc(float4 x)41 float4 Trunc(float4 x) { return trunc(x); }
Round(float4 x)42 float4 Round(float4 x) { return round(x); }
Abs(float4 x)43 float4   Abs(float4 x) { return   abs(x); }
44 
Min(float4 x,float4 y)45 float4 Min(float4 x, float4 y) { return min(x,y); }
Max(float4 x,float4 y)46 float4 Max(float4 x, float4 y) { return max(x,y); }
47 
IfThenElse(int4 c,float4 t,float4 e)48 float4 IfThenElse(int4 c, float4 t, float4 e) { return if_then_else(c,t,e); }
49 
DEF_TEST(SkVx,r)50 DEF_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}));
98 
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 
112     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 
116     REPORTER_ASSERT(r, all(skvx::cast<int>(float4{-1.5f,0.5f,1.0f,1.5f}) == int4{-1,0,1,1}));
117 
118     float buf[] = {1,2,3,4,5,6};
119     REPORTER_ASSERT(r, all(float4::Load(buf) == float4{1,2,3,4}));
120     float4{2,3,4,5}.store(buf);
121     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}));
129 
130     REPORTER_ASSERT(r, all(mad(float4{1,2,3,4}, 2.0f, 3.0f) == float4{5,7,9,11}));
131 
132     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}));
137 
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}));
147 
148     for (int x = 0; x < 256; x++)
149     for (int y = 0; y < 256; y++) {
150         uint8_t want = (uint8_t)( 255*(x/255.0 * y/255.0) + 0.5 );
151 
152         {
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         }
157 
158         {
159             uint8_t got = skvx::approx_scale(skvx::Vec<8,uint8_t>(x),
160                                              skvx::Vec<8,uint8_t>(y))[0];
161 
162             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             }
168         }
169     }
170 
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     }
181 }
182