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