Mike Klein | 455c747 | 2019-02-05 13:42:46 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/private/SkVx.h" |
| 9 | #include "tests/Test.h" |
Mike Klein | 455c747 | 2019-02-05 13:42:46 -0500 | [diff] [blame] | 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 | |
Mike Klein | 9a885b2 | 2019-04-16 12:07:23 -0500 | [diff] [blame] | 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>; |
Mike Klein | 455c747 | 2019-02-05 13:42:46 -0500 | [diff] [blame] | 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 | DEF_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 Klein | 4292515 | 2019-02-06 11:56:58 -0500 | [diff] [blame] | 80 | |
| 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 Klein | 41b995c | 2019-02-27 10:24:55 -0600 | [diff] [blame] | 91 | REPORTER_ASSERT(r, all( sqrt(float2{2,3}) < float2{2,2})); |
Mike Klein | 41b995c | 2019-02-27 10:24:55 -0600 | [diff] [blame] | 92 | |
Mike Klein | da7b053 | 2019-04-10 12:40:31 -0500 | [diff] [blame] | 93 | REPORTER_ASSERT(r, all(skvx::cast<int>(float4{-1.5f,0.5f,1.0f,1.5f}) == int4{-1,0,1,1})); |
Mike Klein | 4292515 | 2019-02-06 11:56:58 -0500 | [diff] [blame] | 94 | |
Mike Klein | dcfc3ef | 2019-02-07 09:49:17 -0500 | [diff] [blame] | 95 | float buf[] = {1,2,3,4,5,6}; |
Mike Klein | 4292515 | 2019-02-06 11:56:58 -0500 | [diff] [blame] | 96 | REPORTER_ASSERT(r, all(float4::Load(buf) == float4{1,2,3,4})); |
| 97 | float4{2,3,4,5}.store(buf); |
Mike Klein | dcfc3ef | 2019-02-07 09:49:17 -0500 | [diff] [blame] | 98 | 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 Klein | 53a5298 | 2019-02-06 15:48:12 -0500 | [diff] [blame] | 106 | |
Mike Klein | da7b053 | 2019-04-10 12:40:31 -0500 | [diff] [blame] | 107 | 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 Klein | f4438d5 | 2019-03-14 13:30:42 -0500 | [diff] [blame] | 112 | |
| 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 Klein | 4b44a0d | 2019-04-11 11:52:51 -0500 | [diff] [blame] | 122 | |
| 123 | for (int x = 0; x < 256; x++) |
| 124 | for (int y = 0; y < 256; y++) { |
Mike Klein | 1dcc55b | 2019-04-11 13:40:30 -0500 | [diff] [blame] | 125 | uint8_t want = (uint8_t)( 255*(x/255.0 * y/255.0) + 0.5 ); |
Mike Klein | 4b44a0d | 2019-04-11 11:52:51 -0500 | [diff] [blame] | 126 | |
Mike Klein | 1dcc55b | 2019-04-11 13:40:30 -0500 | [diff] [blame] | 127 | { |
| 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 Klein | 4b44a0d | 2019-04-11 11:52:51 -0500 | [diff] [blame] | 132 | |
Mike Klein | 1dcc55b | 2019-04-11 13:40:30 -0500 | [diff] [blame] | 133 | { |
| 134 | uint8_t got = skvx::approx_scale(skvx::Vec<8,uint8_t>(x), |
| 135 | skvx::Vec<8,uint8_t>(y))[0]; |
Mike Klein | 4b44a0d | 2019-04-11 11:52:51 -0500 | [diff] [blame] | 136 | |
Mike Klein | 1dcc55b | 2019-04-11 13:40:30 -0500 | [diff] [blame] | 137 | 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 Klein | 4b44a0d | 2019-04-11 11:52:51 -0500 | [diff] [blame] | 143 | } |
| 144 | } |
Mike Klein | 9a885b2 | 2019-04-16 12:07:23 -0500 | [diff] [blame] | 145 | |
| 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 Klein | 4d680cd | 2020-07-15 09:58:51 -0500 | [diff] [blame] | 156 | |
| 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 Klein | 455c747 | 2019-02-05 13:42:46 -0500 | [diff] [blame] | 166 | } |