Debian Codegraph @

clang/test/SemaHLSL/Types/ElementwiseCastConstantExpr.hlsl

raw
1 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -fnative-half-type -fnative-int16-type -std=hlsl202x -verify %s
2
3 // expected-no-diagnostics
4
5 struct Base {
6 double D;
7 uint64_t2 U;
8 int16_t I : 5;
9 uint16_t I2: 5;
10 };
11
12 struct R : Base {
13 int G : 10;
14 int : 30;
15 float F;
16 };
17
18 struct B1 {
19 float A;
20 float B;
21 };
22
23 struct B2 : B1 {
24 int C;
25 int D;
26 bool BB;
27 };
28
29 export void fn() {
30 _Static_assert(((float4)(int[6]){1,2,3,4,5,6}).x == 1.0, "Woo!");
31
32 // This compiling successfully verifies that the array constant expression
33 // gets truncated to a float at compile time for instantiation via the
34 // flat cast
35 _Static_assert(((int)(int[2]){1,2}) == 1, "Woo!");
36
37 // truncation tests
38 // result type int
39 // truncate from struct
40 constexpr B1 SB1 = {1.0, 3.0};
41 constexpr int X = (int)SB1;
42 _Static_assert(X == 1, "Woo!");
43
44 // result type float
45 // truncate from array
46 constexpr B1 Arr[2] = {4.0, 3.0, 2.0, 1.0};
47 constexpr float F = (float)Arr;
48 _Static_assert(F == 4.0, "Woo!");
49
50 // result type vector
51 // truncate from array of vector
52 constexpr int2 Arr2[2] = {5,6,7,8};
53 constexpr int2 I2 = (int2)Arr2;
54 _Static_assert(I2[0] == 5, "Woo!");
55 _Static_assert(I2[1] == 6, "Woo!");
56
57 // lhs and rhs are same "size" tests
58
59 // result type vector from array
60 constexpr int4 I4 = (int4)Arr;
61 _Static_assert(I4[0] == 4, "Woo!");
62 _Static_assert(I4[1] == 3, "Woo!");
63 _Static_assert(I4[2] == 2, "Woo!");
64 _Static_assert(I4[3] == 1, "Woo!");
65
66 // result type array from vector
67 constexpr double3 D3 = {100.11, 200.11, 300.11};
68 constexpr float FArr[3] = (float[3])D3;
69 _Static_assert(FArr[0] == 100.11, "Woo!");
70 _Static_assert(FArr[1] == 200.11, "Woo!");
71 _Static_assert(FArr[2] == 300.11, "Woo!");
72
73 // result type struct from struct
74 constexpr B2 SB2 = {5.5, 6.5, 1000, 5000, false};
75 constexpr Base SB = (Base)SB2;
76 _Static_assert(SB.D == 5.5, "Woo!");
77 _Static_assert(SB.U[0] == 6, "Woo!");
78 _Static_assert(SB.U[1] == 1000, "Woo!");
79 _Static_assert(SB.I == 8, "Woo!");
80 _Static_assert(SB.I2 == 0, "Woo!");
81
82 // Make sure we read bitfields correctly
83 constexpr Base BB = {222.22, {100, 200}, -2, 7};
84 constexpr int Arr3[5] = (int[5])BB;
85 _Static_assert(Arr3[0] == 222, "Woo!");
86 _Static_assert(Arr3[1] == 100, "Woo!");
87 _Static_assert(Arr3[2] == 200, "Woo!");
88 _Static_assert(Arr3[3] == -2, "Woo!");
89 _Static_assert(Arr3[4] == 7, "Woo!");
90 }