diff options
| author | kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> | 2024-12-12 16:50:44 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-12 14:50:44 -0800 |
| commit | 78c9bd1c2fbd55889e62a2032e9bc96684ced3b5 (patch) | |
| tree | 63332e647aa597450b642751c8c6b2fd7f66439e /tests/language-feature | |
| parent | b4e63d7bc44fc969d24202fc51a774378a489294 (diff) | |
Bit extract (#5847)
* promoting bitfield extraction and insertion to become intrinsics for internal compiler use
* removing duplicate intrinsics from glsl.meta.slang
* refactor: update function signatures of bitfield extraction and insertion to use uint as the parameter type for offset and bits.
---------
Co-authored-by: Nate Morrical <natemorrical@gmail.com>
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tests/language-feature')
| -rw-r--r-- | tests/language-feature/bitfield/bitfield-extract.slang | 64 | ||||
| -rw-r--r-- | tests/language-feature/bitfield/bitfield-insert.slang | 68 |
2 files changed, 132 insertions, 0 deletions
diff --git a/tests/language-feature/bitfield/bitfield-extract.slang b/tests/language-feature/bitfield/bitfield-extract.slang new file mode 100644 index 000000000..c8cfaace4 --- /dev/null +++ b/tests/language-feature/bitfield/bitfield-extract.slang @@ -0,0 +1,64 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk -compile-arg -skip-spirv-validation -emit-spirv-directly +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-dx12 -use-dxil +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-mtl +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cuda +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-wgpu + +// CHECK: 1 +// CHECK-NEXT: 2 +// CHECK-NEXT: 3 +// CHECK-NEXT: 4 +// CHECK-NEXT: 5 +// CHECK-NEXT: 6 +// CHECK-NEXT: 7 +// CHECK-NEXT: 8 +// CHECK-NEXT: 21 +// CHECK-NEXT: 7A +// CHECK-NEXT: FFFFFFFA +// CHECK-NEXT: A +// CHECK-NEXT: 67 +// CHECK-NEXT: FFFFFFEF +// CHECK-NEXT: 32 +// CHECK-NEXT: FFFFFFA9 + +//TEST_INPUT:ubuffer(data=[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1], stride=4):out,name=i32Buffer +RWStructuredBuffer<int> i32Buffer; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // 32-bit tests + { + // Simple hex extraction to test, varying the offset. + uint value = 0x87654321; + i32Buffer[0] = bitfieldExtract(value, 4 * 0, 4); + i32Buffer[1] = bitfieldExtract(value, 4 * 1, 4); + i32Buffer[2] = bitfieldExtract(value, 4 * 2, 4); + i32Buffer[3] = bitfieldExtract(value, 4 * 3, 4); + i32Buffer[4] = bitfieldExtract(value, 4 * 4, 4); + i32Buffer[5] = bitfieldExtract(value, 4 * 5, 4); + i32Buffer[6] = bitfieldExtract(value, 4 * 6, 4); + i32Buffer[7] = bitfieldExtract(value, 4 * 7, 4); + + // Now varying the bit length + value = 0b00111011111011110001111010100001; + i32Buffer[8] = bitfieldExtract(value, 0, 6); + i32Buffer[9] = bitfieldExtract(value, 6, 8); + + // Sign extension case + // - For unsigned data types, the most significant bits of the result will be set to zero. + // - For signed data types, the most significant bits will be set to the value of bit offset + base - 1 + // (i.e., it is sign extended to the width of the return type). + i32Buffer[10] = bitfieldExtract(0b1010111, 3, 4); // 0b1010 -> 0b11111111111111111111111111111010 + i32Buffer[11] = bitfieldExtract(0b1010111u, 3, 4); // 0b1111 -> 0b00000000000000000000000000001010 + + // Component-wise extraction + int4 val4 = int4(0x12345678, 0x9abcdef0, 0x87654321, 0xfedcba98); + int4 ext4 = bitfieldExtract(val4, 4, 8); + i32Buffer[12] = ext4.x; + i32Buffer[13] = ext4.y; + i32Buffer[14] = ext4.z; + i32Buffer[15] = ext4.w; + } +} diff --git a/tests/language-feature/bitfield/bitfield-insert.slang b/tests/language-feature/bitfield/bitfield-insert.slang new file mode 100644 index 000000000..bc3db0b8d --- /dev/null +++ b/tests/language-feature/bitfield/bitfield-insert.slang @@ -0,0 +1,68 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk -compile-arg -skip-spirv-validation -emit-spirv-directly +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-dx12 -use-dxil +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-mtl +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cuda +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-wgpu + +// CHECK: 8765432F +// CHECK-NEXT: 876543F1 +// CHECK-NEXT: 87654F21 +// CHECK-NEXT: 8765F321 +// CHECK-NEXT: A8 +// CHECK-NEXT: 3FC0 +// CHECK-NEXT: AF +// CHECK-NEXT: 123456F8 +// CHECK-NEXT: 9ABCDE60 +// CHECK-NEXT: 87654331 +// CHECK-NEXT: FEDCBA68 +// CHECK-NEXT: 12345EF8 +// CHECK-NEXT: 9ABCD560 +// CHECK-NEXT: 87654431 +// CHECK-NEXT: FEDCB568 + +//TEST_INPUT:ubuffer(data=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14], stride=4):out,name=u32Buffer +RWStructuredBuffer<uint> u32Buffer; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // 32-bit tests + { + // Simple hex insertion to test, varying the offset. + uint base = 0x87654321; + uint value = 0xABCDEF; + u32Buffer[0] = bitfieldInsert(base, value, 4 * 0, 4); // 0x8765432F + u32Buffer[1] = bitfieldInsert(base, value, 4 * 1, 4); // 0x876543F1 + u32Buffer[2] = bitfieldInsert(base, value, 4 * 2, 4); // 0x8765F321 + u32Buffer[3] = bitfieldInsert(base, value, 4 * 3, 4); // 0x87F54321 + + // Test with varying bit length + base = 0; + value = 0b101010; + u32Buffer[4] = bitfieldInsert(base, value, 2, 6); // 0b10101000 + value = 0b11111111; + u32Buffer[5] = bitfieldInsert(base, value, 6, 8); // 0b11111111000000 + + // Test with int input + u32Buffer[6] = bitfieldInsert(0b10100000, 0b1111, 0, 4); // 0b10101111 + + // Test with a vector + uint4 base4 = uint4(0x12345678, 0x9abcdef0, 0x87654321, 0xfedcba98); + uint4 value4 = uint4(0xABCDEF, 0x123456, 0x876543, 0x123456); + uint4 output4 = bitfieldInsert(base4, value4, 4, 4); + u32Buffer[7] = output4.x; + u32Buffer[8] = output4.y; + u32Buffer[9] = output4.z; + u32Buffer[10] = output4.w; + + // Test with a int vector + int4 ibase4 = int4(0x12345678, 0x9abcdef0, 0x87654321, 0xfedcba98); + int4 ivalue4 = int4(0xABCDEF, 0x123456, 0x876543, 0x123456); + int4 ioutput4 = bitfieldInsert(ibase4, ivalue4, 4, 8); + u32Buffer[11] = ioutput4.x; + u32Buffer[12] = ioutput4.y; + u32Buffer[13] = ioutput4.z; + u32Buffer[14] = ioutput4.w; + } +} |
