Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "src/tint/transform/builtin_polyfill.h" |
| 16 | |
| 17 | #include <utility> |
| 18 | |
| 19 | #include "src/tint/transform/test_helper.h" |
| 20 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 21 | namespace tint::transform { |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 24 | using Level = BuiltinPolyfill::Level; |
| 25 | |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 26 | using BuiltinPolyfillTest = TransformTest; |
| 27 | |
| 28 | TEST_F(BuiltinPolyfillTest, ShouldRunEmptyModule) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 29 | auto* src = R"()"; |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 30 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 31 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src)); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | TEST_F(BuiltinPolyfillTest, EmptyModule) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 35 | auto* src = R"()"; |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 36 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 37 | auto* expect = src; |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 38 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 39 | auto got = Run<BuiltinPolyfill>(src); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 40 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 41 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | //////////////////////////////////////////////////////////////////////////////// |
| 45 | // countLeadingZeros |
| 46 | //////////////////////////////////////////////////////////////////////////////// |
| 47 | DataMap polyfillCountLeadingZeros() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | BuiltinPolyfill::Builtins builtins; |
| 49 | builtins.count_leading_zeros = true; |
| 50 | DataMap data; |
| 51 | data.Add<BuiltinPolyfill::Config>(builtins); |
| 52 | return data; |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST_F(BuiltinPolyfillTest, ShouldRunCountLeadingZeros) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 56 | auto* src = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 57 | fn f() { |
| 58 | countLeadingZeros(0xf); |
| 59 | } |
| 60 | )"; |
| 61 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 62 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src)); |
| 63 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillCountLeadingZeros())); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST_F(BuiltinPolyfillTest, CountLeadingZeros_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 67 | auto* src = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 68 | fn f() { |
| 69 | let r : i32 = countLeadingZeros(15); |
| 70 | } |
| 71 | )"; |
| 72 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 73 | auto* expect = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 74 | fn tint_count_leading_zeros(v : i32) -> i32 { |
| 75 | var x = u32(v); |
| 76 | let b16 = select(0u, 16u, (x <= 65535u)); |
| 77 | x = (x << b16); |
| 78 | let b8 = select(0u, 8u, (x <= 16777215u)); |
| 79 | x = (x << b8); |
| 80 | let b4 = select(0u, 4u, (x <= 268435455u)); |
| 81 | x = (x << b4); |
| 82 | let b2 = select(0u, 2u, (x <= 1073741823u)); |
| 83 | x = (x << b2); |
| 84 | let b1 = select(0u, 1u, (x <= 2147483647u)); |
| 85 | let is_zero = select(0u, 1u, (x == 0u)); |
| 86 | return i32((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 87 | } |
| 88 | |
| 89 | fn f() { |
| 90 | let r : i32 = tint_count_leading_zeros(15); |
| 91 | } |
| 92 | )"; |
| 93 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | auto got = Run<BuiltinPolyfill>(src, polyfillCountLeadingZeros()); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 95 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(BuiltinPolyfillTest, CountLeadingZeros_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 100 | auto* src = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 101 | fn f() { |
| 102 | let r : u32 = countLeadingZeros(15u); |
| 103 | } |
| 104 | )"; |
| 105 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 106 | auto* expect = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 107 | fn tint_count_leading_zeros(v : u32) -> u32 { |
| 108 | var x = u32(v); |
| 109 | let b16 = select(0u, 16u, (x <= 65535u)); |
| 110 | x = (x << b16); |
| 111 | let b8 = select(0u, 8u, (x <= 16777215u)); |
| 112 | x = (x << b8); |
| 113 | let b4 = select(0u, 4u, (x <= 268435455u)); |
| 114 | x = (x << b4); |
| 115 | let b2 = select(0u, 2u, (x <= 1073741823u)); |
| 116 | x = (x << b2); |
| 117 | let b1 = select(0u, 1u, (x <= 2147483647u)); |
| 118 | let is_zero = select(0u, 1u, (x == 0u)); |
| 119 | return u32((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 120 | } |
| 121 | |
| 122 | fn f() { |
| 123 | let r : u32 = tint_count_leading_zeros(15u); |
| 124 | } |
| 125 | )"; |
| 126 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 127 | auto got = Run<BuiltinPolyfill>(src, polyfillCountLeadingZeros()); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 128 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 129 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | TEST_F(BuiltinPolyfillTest, CountLeadingZeros_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 133 | auto* src = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 134 | fn f() { |
| 135 | let r : vec3<i32> = countLeadingZeros(vec3<i32>(15)); |
| 136 | } |
| 137 | )"; |
| 138 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 139 | auto* expect = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 140 | fn tint_count_leading_zeros(v : vec3<i32>) -> vec3<i32> { |
| 141 | var x = vec3<u32>(v); |
| 142 | let b16 = select(vec3<u32>(0u), vec3<u32>(16u), (x <= vec3<u32>(65535u))); |
| 143 | x = (x << b16); |
| 144 | let b8 = select(vec3<u32>(0u), vec3<u32>(8u), (x <= vec3<u32>(16777215u))); |
| 145 | x = (x << b8); |
| 146 | let b4 = select(vec3<u32>(0u), vec3<u32>(4u), (x <= vec3<u32>(268435455u))); |
| 147 | x = (x << b4); |
| 148 | let b2 = select(vec3<u32>(0u), vec3<u32>(2u), (x <= vec3<u32>(1073741823u))); |
| 149 | x = (x << b2); |
| 150 | let b1 = select(vec3<u32>(0u), vec3<u32>(1u), (x <= vec3<u32>(2147483647u))); |
| 151 | let is_zero = select(vec3<u32>(0u), vec3<u32>(1u), (x == vec3<u32>(0u))); |
| 152 | return vec3<i32>((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 153 | } |
| 154 | |
| 155 | fn f() { |
| 156 | let r : vec3<i32> = tint_count_leading_zeros(vec3<i32>(15)); |
| 157 | } |
| 158 | )"; |
| 159 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 160 | auto got = Run<BuiltinPolyfill>(src, polyfillCountLeadingZeros()); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 161 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 162 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(BuiltinPolyfillTest, CountLeadingZeros_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 166 | auto* src = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 167 | fn f() { |
| 168 | let r : vec3<u32> = countLeadingZeros(vec3<u32>(15u)); |
| 169 | } |
| 170 | )"; |
| 171 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 172 | auto* expect = R"( |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 173 | fn tint_count_leading_zeros(v : vec3<u32>) -> vec3<u32> { |
| 174 | var x = vec3<u32>(v); |
| 175 | let b16 = select(vec3<u32>(0u), vec3<u32>(16u), (x <= vec3<u32>(65535u))); |
| 176 | x = (x << b16); |
| 177 | let b8 = select(vec3<u32>(0u), vec3<u32>(8u), (x <= vec3<u32>(16777215u))); |
| 178 | x = (x << b8); |
| 179 | let b4 = select(vec3<u32>(0u), vec3<u32>(4u), (x <= vec3<u32>(268435455u))); |
| 180 | x = (x << b4); |
| 181 | let b2 = select(vec3<u32>(0u), vec3<u32>(2u), (x <= vec3<u32>(1073741823u))); |
| 182 | x = (x << b2); |
| 183 | let b1 = select(vec3<u32>(0u), vec3<u32>(1u), (x <= vec3<u32>(2147483647u))); |
| 184 | let is_zero = select(vec3<u32>(0u), vec3<u32>(1u), (x == vec3<u32>(0u))); |
| 185 | return vec3<u32>((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 186 | } |
| 187 | |
| 188 | fn f() { |
| 189 | let r : vec3<u32> = tint_count_leading_zeros(vec3<u32>(15u)); |
| 190 | } |
| 191 | )"; |
| 192 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 193 | auto got = Run<BuiltinPolyfill>(src, polyfillCountLeadingZeros()); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 194 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 195 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 198 | //////////////////////////////////////////////////////////////////////////////// |
| 199 | // countTrailingZeros |
| 200 | //////////////////////////////////////////////////////////////////////////////// |
| 201 | DataMap polyfillCountTrailingZeros() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 202 | BuiltinPolyfill::Builtins builtins; |
| 203 | builtins.count_trailing_zeros = true; |
| 204 | DataMap data; |
| 205 | data.Add<BuiltinPolyfill::Config>(builtins); |
| 206 | return data; |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(BuiltinPolyfillTest, ShouldRunCountTrailingZeros) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 210 | auto* src = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 211 | fn f() { |
| 212 | countTrailingZeros(0xf); |
| 213 | } |
| 214 | )"; |
| 215 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 216 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src)); |
| 217 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillCountTrailingZeros())); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | TEST_F(BuiltinPolyfillTest, CountTrailingZeros_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 221 | auto* src = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 222 | fn f() { |
| 223 | let r : i32 = countTrailingZeros(15); |
| 224 | } |
| 225 | )"; |
| 226 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 227 | auto* expect = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 228 | fn tint_count_trailing_zeros(v : i32) -> i32 { |
| 229 | var x = u32(v); |
| 230 | let b16 = select(16u, 0u, bool((x & 65535u))); |
| 231 | x = (x >> b16); |
| 232 | let b8 = select(8u, 0u, bool((x & 255u))); |
| 233 | x = (x >> b8); |
| 234 | let b4 = select(4u, 0u, bool((x & 15u))); |
| 235 | x = (x >> b4); |
| 236 | let b2 = select(2u, 0u, bool((x & 3u))); |
| 237 | x = (x >> b2); |
| 238 | let b1 = select(1u, 0u, bool((x & 1u))); |
| 239 | let is_zero = select(0u, 1u, (x == 0u)); |
| 240 | return i32((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 241 | } |
| 242 | |
| 243 | fn f() { |
| 244 | let r : i32 = tint_count_trailing_zeros(15); |
| 245 | } |
| 246 | )"; |
| 247 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 248 | auto got = Run<BuiltinPolyfill>(src, polyfillCountTrailingZeros()); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 249 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 250 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | TEST_F(BuiltinPolyfillTest, CountTrailingZeros_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 254 | auto* src = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 255 | fn f() { |
| 256 | let r : u32 = countTrailingZeros(15u); |
| 257 | } |
| 258 | )"; |
| 259 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 260 | auto* expect = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 261 | fn tint_count_trailing_zeros(v : u32) -> u32 { |
| 262 | var x = u32(v); |
| 263 | let b16 = select(16u, 0u, bool((x & 65535u))); |
| 264 | x = (x >> b16); |
| 265 | let b8 = select(8u, 0u, bool((x & 255u))); |
| 266 | x = (x >> b8); |
| 267 | let b4 = select(4u, 0u, bool((x & 15u))); |
| 268 | x = (x >> b4); |
| 269 | let b2 = select(2u, 0u, bool((x & 3u))); |
| 270 | x = (x >> b2); |
| 271 | let b1 = select(1u, 0u, bool((x & 1u))); |
| 272 | let is_zero = select(0u, 1u, (x == 0u)); |
| 273 | return u32((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 274 | } |
| 275 | |
| 276 | fn f() { |
| 277 | let r : u32 = tint_count_trailing_zeros(15u); |
| 278 | } |
| 279 | )"; |
| 280 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 281 | auto got = Run<BuiltinPolyfill>(src, polyfillCountTrailingZeros()); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 282 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 283 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | TEST_F(BuiltinPolyfillTest, CountTrailingZeros_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 287 | auto* src = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 288 | fn f() { |
| 289 | let r : vec3<i32> = countTrailingZeros(vec3<i32>(15)); |
| 290 | } |
| 291 | )"; |
| 292 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 293 | auto* expect = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 294 | fn tint_count_trailing_zeros(v : vec3<i32>) -> vec3<i32> { |
| 295 | var x = vec3<u32>(v); |
| 296 | let b16 = select(vec3<u32>(16u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(65535u)))); |
| 297 | x = (x >> b16); |
| 298 | let b8 = select(vec3<u32>(8u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(255u)))); |
| 299 | x = (x >> b8); |
| 300 | let b4 = select(vec3<u32>(4u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(15u)))); |
| 301 | x = (x >> b4); |
| 302 | let b2 = select(vec3<u32>(2u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(3u)))); |
| 303 | x = (x >> b2); |
| 304 | let b1 = select(vec3<u32>(1u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(1u)))); |
| 305 | let is_zero = select(vec3<u32>(0u), vec3<u32>(1u), (x == vec3<u32>(0u))); |
| 306 | return vec3<i32>((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 307 | } |
| 308 | |
| 309 | fn f() { |
| 310 | let r : vec3<i32> = tint_count_trailing_zeros(vec3<i32>(15)); |
| 311 | } |
| 312 | )"; |
| 313 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 314 | auto got = Run<BuiltinPolyfill>(src, polyfillCountTrailingZeros()); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 315 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 316 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | TEST_F(BuiltinPolyfillTest, CountTrailingZeros_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 320 | auto* src = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 321 | fn f() { |
| 322 | let r : vec3<u32> = countTrailingZeros(vec3<u32>(15u)); |
| 323 | } |
| 324 | )"; |
| 325 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 326 | auto* expect = R"( |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 327 | fn tint_count_trailing_zeros(v : vec3<u32>) -> vec3<u32> { |
| 328 | var x = vec3<u32>(v); |
| 329 | let b16 = select(vec3<u32>(16u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(65535u)))); |
| 330 | x = (x >> b16); |
| 331 | let b8 = select(vec3<u32>(8u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(255u)))); |
| 332 | x = (x >> b8); |
| 333 | let b4 = select(vec3<u32>(4u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(15u)))); |
| 334 | x = (x >> b4); |
| 335 | let b2 = select(vec3<u32>(2u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(3u)))); |
| 336 | x = (x >> b2); |
| 337 | let b1 = select(vec3<u32>(1u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(1u)))); |
| 338 | let is_zero = select(vec3<u32>(0u), vec3<u32>(1u), (x == vec3<u32>(0u))); |
| 339 | return vec3<u32>((((((b16 | b8) | b4) | b2) | b1) + is_zero)); |
| 340 | } |
| 341 | |
| 342 | fn f() { |
| 343 | let r : vec3<u32> = tint_count_trailing_zeros(vec3<u32>(15u)); |
| 344 | } |
| 345 | )"; |
| 346 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 347 | auto got = Run<BuiltinPolyfill>(src, polyfillCountTrailingZeros()); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 348 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 349 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | f8672d8 | 2022-02-22 23:16:39 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 352 | //////////////////////////////////////////////////////////////////////////////// |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 353 | // extractBits |
| 354 | //////////////////////////////////////////////////////////////////////////////// |
| 355 | DataMap polyfillExtractBits(Level level) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 356 | BuiltinPolyfill::Builtins builtins; |
| 357 | builtins.extract_bits = level; |
| 358 | DataMap data; |
| 359 | data.Add<BuiltinPolyfill::Config>(builtins); |
| 360 | return data; |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | TEST_F(BuiltinPolyfillTest, ShouldRunExtractBits) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 364 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 365 | fn f() { |
| 366 | extractBits(1234, 5u, 6u); |
| 367 | } |
| 368 | )"; |
| 369 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 370 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src)); |
| 371 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src, polyfillExtractBits(Level::kNone))); |
| 372 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillExtractBits(Level::kClampParameters))); |
| 373 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillExtractBits(Level::kFull))); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | TEST_F(BuiltinPolyfillTest, ExtractBits_Full_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 377 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 378 | fn f() { |
| 379 | let r : i32 = extractBits(1234, 5u, 6u); |
| 380 | } |
| 381 | )"; |
| 382 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 383 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 384 | fn tint_extract_bits(v : i32, offset : u32, count : u32) -> i32 { |
| 385 | let s = min(offset, 32u); |
| 386 | let e = min(32u, (s + count)); |
| 387 | let shl = (32u - e); |
| 388 | let shr = (shl + s); |
| 389 | return ((v << shl) >> shr); |
| 390 | } |
| 391 | |
| 392 | fn f() { |
| 393 | let r : i32 = tint_extract_bits(1234, 5u, 6u); |
| 394 | } |
| 395 | )"; |
| 396 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 397 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kFull)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 398 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 399 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | TEST_F(BuiltinPolyfillTest, ExtractBits_Full_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 403 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 404 | fn f() { |
| 405 | let r : u32 = extractBits(1234u, 5u, 6u); |
| 406 | } |
| 407 | )"; |
| 408 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 409 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 410 | fn tint_extract_bits(v : u32, offset : u32, count : u32) -> u32 { |
| 411 | let s = min(offset, 32u); |
| 412 | let e = min(32u, (s + count)); |
| 413 | let shl = (32u - e); |
| 414 | let shr = (shl + s); |
| 415 | return ((v << shl) >> shr); |
| 416 | } |
| 417 | |
| 418 | fn f() { |
| 419 | let r : u32 = tint_extract_bits(1234u, 5u, 6u); |
| 420 | } |
| 421 | )"; |
| 422 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 423 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kFull)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 424 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 425 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | TEST_F(BuiltinPolyfillTest, ExtractBits_Full_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 429 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 430 | fn f() { |
| 431 | let r : vec3<i32> = extractBits(vec3<i32>(1234), 5u, 6u); |
| 432 | } |
| 433 | )"; |
| 434 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 435 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 436 | fn tint_extract_bits(v : vec3<i32>, offset : u32, count : u32) -> vec3<i32> { |
| 437 | let s = min(offset, 32u); |
| 438 | let e = min(32u, (s + count)); |
| 439 | let shl = (32u - e); |
| 440 | let shr = (shl + s); |
| 441 | return ((v << vec3<u32>(shl)) >> vec3<u32>(shr)); |
| 442 | } |
| 443 | |
| 444 | fn f() { |
| 445 | let r : vec3<i32> = tint_extract_bits(vec3<i32>(1234), 5u, 6u); |
| 446 | } |
| 447 | )"; |
| 448 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 449 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kFull)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 450 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 451 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | TEST_F(BuiltinPolyfillTest, ExtractBits_Full_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 455 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 456 | fn f() { |
| 457 | let r : vec3<u32> = extractBits(vec3<u32>(1234u), 5u, 6u); |
| 458 | } |
| 459 | )"; |
| 460 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 461 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 462 | fn tint_extract_bits(v : vec3<u32>, offset : u32, count : u32) -> vec3<u32> { |
| 463 | let s = min(offset, 32u); |
| 464 | let e = min(32u, (s + count)); |
| 465 | let shl = (32u - e); |
| 466 | let shr = (shl + s); |
| 467 | return ((v << vec3<u32>(shl)) >> vec3<u32>(shr)); |
| 468 | } |
| 469 | |
| 470 | fn f() { |
| 471 | let r : vec3<u32> = tint_extract_bits(vec3<u32>(1234u), 5u, 6u); |
| 472 | } |
| 473 | )"; |
| 474 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 475 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kFull)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 476 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 477 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | TEST_F(BuiltinPolyfillTest, ExtractBits_Clamp_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 481 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 482 | fn f() { |
| 483 | let r : i32 = extractBits(1234, 5u, 6u); |
| 484 | } |
| 485 | )"; |
| 486 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 487 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 488 | fn tint_extract_bits(v : i32, offset : u32, count : u32) -> i32 { |
| 489 | let s = min(offset, 32u); |
| 490 | let e = min(32u, (s + count)); |
| 491 | return extractBits(v, s, (e - s)); |
| 492 | } |
| 493 | |
| 494 | fn f() { |
| 495 | let r : i32 = tint_extract_bits(1234, 5u, 6u); |
| 496 | } |
| 497 | )"; |
| 498 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 499 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kClampParameters)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 500 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 501 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | TEST_F(BuiltinPolyfillTest, ExtractBits_Clamp_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 505 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 506 | fn f() { |
| 507 | let r : u32 = extractBits(1234u, 5u, 6u); |
| 508 | } |
| 509 | )"; |
| 510 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 511 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 512 | fn tint_extract_bits(v : u32, offset : u32, count : u32) -> u32 { |
| 513 | let s = min(offset, 32u); |
| 514 | let e = min(32u, (s + count)); |
| 515 | return extractBits(v, s, (e - s)); |
| 516 | } |
| 517 | |
| 518 | fn f() { |
| 519 | let r : u32 = tint_extract_bits(1234u, 5u, 6u); |
| 520 | } |
| 521 | )"; |
| 522 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 523 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kClampParameters)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 524 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 525 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | TEST_F(BuiltinPolyfillTest, ExtractBits_Clamp_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 529 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 530 | fn f() { |
| 531 | let r : vec3<i32> = extractBits(vec3<i32>(1234), 5u, 6u); |
| 532 | } |
| 533 | )"; |
| 534 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 535 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 536 | fn tint_extract_bits(v : vec3<i32>, offset : u32, count : u32) -> vec3<i32> { |
| 537 | let s = min(offset, 32u); |
| 538 | let e = min(32u, (s + count)); |
| 539 | return extractBits(v, s, (e - s)); |
| 540 | } |
| 541 | |
| 542 | fn f() { |
| 543 | let r : vec3<i32> = tint_extract_bits(vec3<i32>(1234), 5u, 6u); |
| 544 | } |
| 545 | )"; |
| 546 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 547 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kClampParameters)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 548 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 549 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | TEST_F(BuiltinPolyfillTest, ExtractBits_Clamp_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 553 | auto* src = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 554 | fn f() { |
| 555 | let r : vec3<u32> = extractBits(vec3<u32>(1234u), 5u, 6u); |
| 556 | } |
| 557 | )"; |
| 558 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 559 | auto* expect = R"( |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 560 | fn tint_extract_bits(v : vec3<u32>, offset : u32, count : u32) -> vec3<u32> { |
| 561 | let s = min(offset, 32u); |
| 562 | let e = min(32u, (s + count)); |
| 563 | return extractBits(v, s, (e - s)); |
| 564 | } |
| 565 | |
| 566 | fn f() { |
| 567 | let r : vec3<u32> = tint_extract_bits(vec3<u32>(1234u), 5u, 6u); |
| 568 | } |
| 569 | )"; |
| 570 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 571 | auto got = Run<BuiltinPolyfill>(src, polyfillExtractBits(Level::kClampParameters)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 572 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 573 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | d868e86 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | //////////////////////////////////////////////////////////////////////////////// |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 577 | // firstLeadingBit |
| 578 | //////////////////////////////////////////////////////////////////////////////// |
| 579 | DataMap polyfillFirstLeadingBit() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 580 | BuiltinPolyfill::Builtins builtins; |
| 581 | builtins.first_leading_bit = true; |
| 582 | DataMap data; |
| 583 | data.Add<BuiltinPolyfill::Config>(builtins); |
| 584 | return data; |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | TEST_F(BuiltinPolyfillTest, ShouldRunFirstLeadingBit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 588 | auto* src = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 589 | fn f() { |
| 590 | firstLeadingBit(0xf); |
| 591 | } |
| 592 | )"; |
| 593 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 594 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src)); |
| 595 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillFirstLeadingBit())); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | TEST_F(BuiltinPolyfillTest, FirstLeadingBit_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 599 | auto* src = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 600 | fn f() { |
| 601 | let r : i32 = firstLeadingBit(15); |
| 602 | } |
| 603 | )"; |
| 604 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 605 | auto* expect = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 606 | fn tint_first_leading_bit(v : i32) -> i32 { |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 607 | var x = select(u32(v), u32(~(v)), (v < 0i)); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 608 | let b16 = select(0u, 16u, bool((x & 4294901760u))); |
| 609 | x = (x >> b16); |
| 610 | let b8 = select(0u, 8u, bool((x & 65280u))); |
| 611 | x = (x >> b8); |
| 612 | let b4 = select(0u, 4u, bool((x & 240u))); |
| 613 | x = (x >> b4); |
| 614 | let b2 = select(0u, 2u, bool((x & 12u))); |
| 615 | x = (x >> b2); |
| 616 | let b1 = select(0u, 1u, bool((x & 2u))); |
| 617 | let is_zero = select(0u, 4294967295u, (x == 0u)); |
| 618 | return i32((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 619 | } |
| 620 | |
| 621 | fn f() { |
| 622 | let r : i32 = tint_first_leading_bit(15); |
| 623 | } |
| 624 | )"; |
| 625 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 626 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstLeadingBit()); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 627 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 628 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | TEST_F(BuiltinPolyfillTest, FirstLeadingBit_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 632 | auto* src = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 633 | fn f() { |
| 634 | let r : u32 = firstLeadingBit(15u); |
| 635 | } |
| 636 | )"; |
| 637 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 638 | auto* expect = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 639 | fn tint_first_leading_bit(v : u32) -> u32 { |
| 640 | var x = v; |
| 641 | let b16 = select(0u, 16u, bool((x & 4294901760u))); |
| 642 | x = (x >> b16); |
| 643 | let b8 = select(0u, 8u, bool((x & 65280u))); |
| 644 | x = (x >> b8); |
| 645 | let b4 = select(0u, 4u, bool((x & 240u))); |
| 646 | x = (x >> b4); |
| 647 | let b2 = select(0u, 2u, bool((x & 12u))); |
| 648 | x = (x >> b2); |
| 649 | let b1 = select(0u, 1u, bool((x & 2u))); |
| 650 | let is_zero = select(0u, 4294967295u, (x == 0u)); |
| 651 | return u32((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 652 | } |
| 653 | |
| 654 | fn f() { |
| 655 | let r : u32 = tint_first_leading_bit(15u); |
| 656 | } |
| 657 | )"; |
| 658 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 659 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstLeadingBit()); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 660 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 661 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | TEST_F(BuiltinPolyfillTest, FirstLeadingBit_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 665 | auto* src = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 666 | fn f() { |
| 667 | let r : vec3<i32> = firstLeadingBit(vec3<i32>(15)); |
| 668 | } |
| 669 | )"; |
| 670 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 671 | auto* expect = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 672 | fn tint_first_leading_bit(v : vec3<i32>) -> vec3<i32> { |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 673 | var x = select(vec3<u32>(v), vec3<u32>(~(v)), (v < vec3<i32>(0i))); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 674 | let b16 = select(vec3<u32>(0u), vec3<u32>(16u), vec3<bool>((x & vec3<u32>(4294901760u)))); |
| 675 | x = (x >> b16); |
| 676 | let b8 = select(vec3<u32>(0u), vec3<u32>(8u), vec3<bool>((x & vec3<u32>(65280u)))); |
| 677 | x = (x >> b8); |
| 678 | let b4 = select(vec3<u32>(0u), vec3<u32>(4u), vec3<bool>((x & vec3<u32>(240u)))); |
| 679 | x = (x >> b4); |
| 680 | let b2 = select(vec3<u32>(0u), vec3<u32>(2u), vec3<bool>((x & vec3<u32>(12u)))); |
| 681 | x = (x >> b2); |
| 682 | let b1 = select(vec3<u32>(0u), vec3<u32>(1u), vec3<bool>((x & vec3<u32>(2u)))); |
| 683 | let is_zero = select(vec3<u32>(0u), vec3<u32>(4294967295u), (x == vec3<u32>(0u))); |
| 684 | return vec3<i32>((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 685 | } |
| 686 | |
| 687 | fn f() { |
| 688 | let r : vec3<i32> = tint_first_leading_bit(vec3<i32>(15)); |
| 689 | } |
| 690 | )"; |
| 691 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 692 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstLeadingBit()); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 693 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 694 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | TEST_F(BuiltinPolyfillTest, FirstLeadingBit_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 698 | auto* src = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 699 | fn f() { |
| 700 | let r : vec3<u32> = firstLeadingBit(vec3<u32>(15u)); |
| 701 | } |
| 702 | )"; |
| 703 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 704 | auto* expect = R"( |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 705 | fn tint_first_leading_bit(v : vec3<u32>) -> vec3<u32> { |
| 706 | var x = v; |
| 707 | let b16 = select(vec3<u32>(0u), vec3<u32>(16u), vec3<bool>((x & vec3<u32>(4294901760u)))); |
| 708 | x = (x >> b16); |
| 709 | let b8 = select(vec3<u32>(0u), vec3<u32>(8u), vec3<bool>((x & vec3<u32>(65280u)))); |
| 710 | x = (x >> b8); |
| 711 | let b4 = select(vec3<u32>(0u), vec3<u32>(4u), vec3<bool>((x & vec3<u32>(240u)))); |
| 712 | x = (x >> b4); |
| 713 | let b2 = select(vec3<u32>(0u), vec3<u32>(2u), vec3<bool>((x & vec3<u32>(12u)))); |
| 714 | x = (x >> b2); |
| 715 | let b1 = select(vec3<u32>(0u), vec3<u32>(1u), vec3<bool>((x & vec3<u32>(2u)))); |
| 716 | let is_zero = select(vec3<u32>(0u), vec3<u32>(4294967295u), (x == vec3<u32>(0u))); |
| 717 | return vec3<u32>((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 718 | } |
| 719 | |
| 720 | fn f() { |
| 721 | let r : vec3<u32> = tint_first_leading_bit(vec3<u32>(15u)); |
| 722 | } |
| 723 | )"; |
| 724 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 725 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstLeadingBit()); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 726 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 727 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | 8169693 | 2022-02-23 18:20:30 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | //////////////////////////////////////////////////////////////////////////////// |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 731 | // firstTrailingBit |
| 732 | //////////////////////////////////////////////////////////////////////////////// |
| 733 | DataMap polyfillFirstTrailingBit() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 734 | BuiltinPolyfill::Builtins builtins; |
| 735 | builtins.first_trailing_bit = true; |
| 736 | DataMap data; |
| 737 | data.Add<BuiltinPolyfill::Config>(builtins); |
| 738 | return data; |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | TEST_F(BuiltinPolyfillTest, ShouldRunFirstTrailingBit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 742 | auto* src = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 743 | fn f() { |
| 744 | firstTrailingBit(0xf); |
| 745 | } |
| 746 | )"; |
| 747 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 748 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src)); |
| 749 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillFirstTrailingBit())); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | TEST_F(BuiltinPolyfillTest, FirstTrailingBit_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 753 | auto* src = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 754 | fn f() { |
| 755 | let r : i32 = firstTrailingBit(15); |
| 756 | } |
| 757 | )"; |
| 758 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 759 | auto* expect = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 760 | fn tint_first_trailing_bit(v : i32) -> i32 { |
| 761 | var x = u32(v); |
| 762 | let b16 = select(16u, 0u, bool((x & 65535u))); |
| 763 | x = (x >> b16); |
| 764 | let b8 = select(8u, 0u, bool((x & 255u))); |
| 765 | x = (x >> b8); |
| 766 | let b4 = select(4u, 0u, bool((x & 15u))); |
| 767 | x = (x >> b4); |
| 768 | let b2 = select(2u, 0u, bool((x & 3u))); |
| 769 | x = (x >> b2); |
| 770 | let b1 = select(1u, 0u, bool((x & 1u))); |
| 771 | let is_zero = select(0u, 4294967295u, (x == 0u)); |
| 772 | return i32((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 773 | } |
| 774 | |
| 775 | fn f() { |
| 776 | let r : i32 = tint_first_trailing_bit(15); |
| 777 | } |
| 778 | )"; |
| 779 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 780 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstTrailingBit()); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 781 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 782 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | TEST_F(BuiltinPolyfillTest, FirstTrailingBit_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 786 | auto* src = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 787 | fn f() { |
| 788 | let r : u32 = firstTrailingBit(15u); |
| 789 | } |
| 790 | )"; |
| 791 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 792 | auto* expect = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 793 | fn tint_first_trailing_bit(v : u32) -> u32 { |
| 794 | var x = u32(v); |
| 795 | let b16 = select(16u, 0u, bool((x & 65535u))); |
| 796 | x = (x >> b16); |
| 797 | let b8 = select(8u, 0u, bool((x & 255u))); |
| 798 | x = (x >> b8); |
| 799 | let b4 = select(4u, 0u, bool((x & 15u))); |
| 800 | x = (x >> b4); |
| 801 | let b2 = select(2u, 0u, bool((x & 3u))); |
| 802 | x = (x >> b2); |
| 803 | let b1 = select(1u, 0u, bool((x & 1u))); |
| 804 | let is_zero = select(0u, 4294967295u, (x == 0u)); |
| 805 | return u32((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 806 | } |
| 807 | |
| 808 | fn f() { |
| 809 | let r : u32 = tint_first_trailing_bit(15u); |
| 810 | } |
| 811 | )"; |
| 812 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 813 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstTrailingBit()); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 814 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 815 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | TEST_F(BuiltinPolyfillTest, FirstTrailingBit_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 819 | auto* src = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 820 | fn f() { |
| 821 | let r : vec3<i32> = firstTrailingBit(vec3<i32>(15)); |
| 822 | } |
| 823 | )"; |
| 824 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 825 | auto* expect = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 826 | fn tint_first_trailing_bit(v : vec3<i32>) -> vec3<i32> { |
| 827 | var x = vec3<u32>(v); |
| 828 | let b16 = select(vec3<u32>(16u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(65535u)))); |
| 829 | x = (x >> b16); |
| 830 | let b8 = select(vec3<u32>(8u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(255u)))); |
| 831 | x = (x >> b8); |
| 832 | let b4 = select(vec3<u32>(4u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(15u)))); |
| 833 | x = (x >> b4); |
| 834 | let b2 = select(vec3<u32>(2u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(3u)))); |
| 835 | x = (x >> b2); |
| 836 | let b1 = select(vec3<u32>(1u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(1u)))); |
| 837 | let is_zero = select(vec3<u32>(0u), vec3<u32>(4294967295u), (x == vec3<u32>(0u))); |
| 838 | return vec3<i32>((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 839 | } |
| 840 | |
| 841 | fn f() { |
| 842 | let r : vec3<i32> = tint_first_trailing_bit(vec3<i32>(15)); |
| 843 | } |
| 844 | )"; |
| 845 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 846 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstTrailingBit()); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 847 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 848 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | TEST_F(BuiltinPolyfillTest, FirstTrailingBit_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 852 | auto* src = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 853 | fn f() { |
| 854 | let r : vec3<u32> = firstTrailingBit(vec3<u32>(15u)); |
| 855 | } |
| 856 | )"; |
| 857 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 858 | auto* expect = R"( |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 859 | fn tint_first_trailing_bit(v : vec3<u32>) -> vec3<u32> { |
| 860 | var x = vec3<u32>(v); |
| 861 | let b16 = select(vec3<u32>(16u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(65535u)))); |
| 862 | x = (x >> b16); |
| 863 | let b8 = select(vec3<u32>(8u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(255u)))); |
| 864 | x = (x >> b8); |
| 865 | let b4 = select(vec3<u32>(4u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(15u)))); |
| 866 | x = (x >> b4); |
| 867 | let b2 = select(vec3<u32>(2u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(3u)))); |
| 868 | x = (x >> b2); |
| 869 | let b1 = select(vec3<u32>(1u), vec3<u32>(0u), vec3<bool>((x & vec3<u32>(1u)))); |
| 870 | let is_zero = select(vec3<u32>(0u), vec3<u32>(4294967295u), (x == vec3<u32>(0u))); |
| 871 | return vec3<u32>((((((b16 | b8) | b4) | b2) | b1) | is_zero)); |
| 872 | } |
| 873 | |
| 874 | fn f() { |
| 875 | let r : vec3<u32> = tint_first_trailing_bit(vec3<u32>(15u)); |
| 876 | } |
| 877 | )"; |
| 878 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 879 | auto got = Run<BuiltinPolyfill>(src, polyfillFirstTrailingBit()); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 880 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 881 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | df3630c | 2022-02-23 14:32:14 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 884 | //////////////////////////////////////////////////////////////////////////////// |
| 885 | // insertBits |
| 886 | //////////////////////////////////////////////////////////////////////////////// |
| 887 | DataMap polyfillInsertBits(Level level) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 888 | BuiltinPolyfill::Builtins builtins; |
| 889 | builtins.insert_bits = level; |
| 890 | DataMap data; |
| 891 | data.Add<BuiltinPolyfill::Config>(builtins); |
| 892 | return data; |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | TEST_F(BuiltinPolyfillTest, ShouldRunInsertBits) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 896 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 897 | fn f() { |
| 898 | insertBits(1234, 5678, 5u, 6u); |
| 899 | } |
| 900 | )"; |
| 901 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 902 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src)); |
| 903 | EXPECT_FALSE(ShouldRun<BuiltinPolyfill>(src, polyfillInsertBits(Level::kNone))); |
| 904 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillInsertBits(Level::kClampParameters))); |
| 905 | EXPECT_TRUE(ShouldRun<BuiltinPolyfill>(src, polyfillInsertBits(Level::kFull))); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | TEST_F(BuiltinPolyfillTest, InsertBits_Full_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 909 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 910 | fn f() { |
| 911 | let r : i32 = insertBits(1234, 5678, 5u, 6u); |
| 912 | } |
| 913 | )"; |
| 914 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 915 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 916 | fn tint_insert_bits(v : i32, n : i32, offset : u32, count : u32) -> i32 { |
| 917 | let s = min(offset, 32u); |
| 918 | let e = min(32u, (s + count)); |
| 919 | let mask = (((1u << s) - 1u) ^ ((1u << e) - 1u)); |
| 920 | return (((n << s) & i32(mask)) | (v & i32(~(mask)))); |
| 921 | } |
| 922 | |
| 923 | fn f() { |
| 924 | let r : i32 = tint_insert_bits(1234, 5678, 5u, 6u); |
| 925 | } |
| 926 | )"; |
| 927 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 928 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kFull)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 929 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 930 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | TEST_F(BuiltinPolyfillTest, InsertBits_Full_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 934 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 935 | fn f() { |
| 936 | let r : u32 = insertBits(1234u, 5678u, 5u, 6u); |
| 937 | } |
| 938 | )"; |
| 939 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 940 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 941 | fn tint_insert_bits(v : u32, n : u32, offset : u32, count : u32) -> u32 { |
| 942 | let s = min(offset, 32u); |
| 943 | let e = min(32u, (s + count)); |
| 944 | let mask = (((1u << s) - 1u) ^ ((1u << e) - 1u)); |
| 945 | return (((n << s) & mask) | (v & ~(mask))); |
| 946 | } |
| 947 | |
| 948 | fn f() { |
| 949 | let r : u32 = tint_insert_bits(1234u, 5678u, 5u, 6u); |
| 950 | } |
| 951 | )"; |
| 952 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 953 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kFull)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 954 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 955 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | TEST_F(BuiltinPolyfillTest, InsertBits_Full_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 959 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 960 | fn f() { |
| 961 | let r : vec3<i32> = insertBits(vec3<i32>(1234), vec3<i32>(5678), 5u, 6u); |
| 962 | } |
| 963 | )"; |
| 964 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 965 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 966 | fn tint_insert_bits(v : vec3<i32>, n : vec3<i32>, offset : u32, count : u32) -> vec3<i32> { |
| 967 | let s = min(offset, 32u); |
| 968 | let e = min(32u, (s + count)); |
| 969 | let mask = (((1u << s) - 1u) ^ ((1u << e) - 1u)); |
| 970 | return (((n << vec3<u32>(s)) & vec3<i32>(i32(mask))) | (v & vec3<i32>(i32(~(mask))))); |
| 971 | } |
| 972 | |
| 973 | fn f() { |
| 974 | let r : vec3<i32> = tint_insert_bits(vec3<i32>(1234), vec3<i32>(5678), 5u, 6u); |
| 975 | } |
| 976 | )"; |
| 977 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 978 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kFull)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 979 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 980 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | TEST_F(BuiltinPolyfillTest, InsertBits_Full_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 984 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 985 | fn f() { |
| 986 | let r : vec3<u32> = insertBits(vec3<u32>(1234u), vec3<u32>(5678u), 5u, 6u); |
| 987 | } |
| 988 | )"; |
| 989 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 990 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 991 | fn tint_insert_bits(v : vec3<u32>, n : vec3<u32>, offset : u32, count : u32) -> vec3<u32> { |
| 992 | let s = min(offset, 32u); |
| 993 | let e = min(32u, (s + count)); |
| 994 | let mask = (((1u << s) - 1u) ^ ((1u << e) - 1u)); |
| 995 | return (((n << vec3<u32>(s)) & vec3<u32>(mask)) | (v & vec3<u32>(~(mask)))); |
| 996 | } |
| 997 | |
| 998 | fn f() { |
| 999 | let r : vec3<u32> = tint_insert_bits(vec3<u32>(1234u), vec3<u32>(5678u), 5u, 6u); |
| 1000 | } |
| 1001 | )"; |
| 1002 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1003 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kFull)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1004 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1005 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | TEST_F(BuiltinPolyfillTest, InsertBits_Clamp_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1009 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1010 | fn f() { |
| 1011 | let r : i32 = insertBits(1234, 5678, 5u, 6u); |
| 1012 | } |
| 1013 | )"; |
| 1014 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1015 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1016 | fn tint_insert_bits(v : i32, n : i32, offset : u32, count : u32) -> i32 { |
| 1017 | let s = min(offset, 32u); |
| 1018 | let e = min(32u, (s + count)); |
| 1019 | return insertBits(v, n, s, (e - s)); |
| 1020 | } |
| 1021 | |
| 1022 | fn f() { |
| 1023 | let r : i32 = tint_insert_bits(1234, 5678, 5u, 6u); |
| 1024 | } |
| 1025 | )"; |
| 1026 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1027 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kClampParameters)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1028 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1029 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | TEST_F(BuiltinPolyfillTest, InsertBits_Clamp_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1033 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1034 | fn f() { |
| 1035 | let r : u32 = insertBits(1234u, 5678u, 5u, 6u); |
| 1036 | } |
| 1037 | )"; |
| 1038 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1039 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1040 | fn tint_insert_bits(v : u32, n : u32, offset : u32, count : u32) -> u32 { |
| 1041 | let s = min(offset, 32u); |
| 1042 | let e = min(32u, (s + count)); |
| 1043 | return insertBits(v, n, s, (e - s)); |
| 1044 | } |
| 1045 | |
| 1046 | fn f() { |
| 1047 | let r : u32 = tint_insert_bits(1234u, 5678u, 5u, 6u); |
| 1048 | } |
| 1049 | )"; |
| 1050 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1051 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kClampParameters)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1052 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1053 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | TEST_F(BuiltinPolyfillTest, InsertBits_Clamp_vec3_i32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1057 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1058 | fn f() { |
| 1059 | let r : vec3<i32> = insertBits(vec3<i32>(1234), vec3<i32>(5678), 5u, 6u); |
| 1060 | } |
| 1061 | )"; |
| 1062 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1063 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1064 | fn tint_insert_bits(v : vec3<i32>, n : vec3<i32>, offset : u32, count : u32) -> vec3<i32> { |
| 1065 | let s = min(offset, 32u); |
| 1066 | let e = min(32u, (s + count)); |
| 1067 | return insertBits(v, n, s, (e - s)); |
| 1068 | } |
| 1069 | |
| 1070 | fn f() { |
| 1071 | let r : vec3<i32> = tint_insert_bits(vec3<i32>(1234), vec3<i32>(5678), 5u, 6u); |
| 1072 | } |
| 1073 | )"; |
| 1074 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1075 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kClampParameters)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1076 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1077 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | TEST_F(BuiltinPolyfillTest, InsertBits_Clamp_vec3_u32) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1081 | auto* src = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1082 | fn f() { |
| 1083 | let r : vec3<u32> = insertBits(vec3<u32>(1234u), vec3<u32>(5678u), 5u, 6u); |
| 1084 | } |
| 1085 | )"; |
| 1086 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1087 | auto* expect = R"( |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1088 | fn tint_insert_bits(v : vec3<u32>, n : vec3<u32>, offset : u32, count : u32) -> vec3<u32> { |
| 1089 | let s = min(offset, 32u); |
| 1090 | let e = min(32u, (s + count)); |
| 1091 | return insertBits(v, n, s, (e - s)); |
| 1092 | } |
| 1093 | |
| 1094 | fn f() { |
| 1095 | let r : vec3<u32> = tint_insert_bits(vec3<u32>(1234u), vec3<u32>(5678u), 5u, 6u); |
| 1096 | } |
| 1097 | )"; |
| 1098 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1099 | auto got = Run<BuiltinPolyfill>(src, polyfillInsertBits(Level::kClampParameters)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1100 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1101 | EXPECT_EQ(expect, str(got)); |
Ben Clayton | fe08ba4 | 2022-02-23 21:18:09 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 1104 | } // namespace |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 1105 | } // namespace tint::transform |