xref: /aosp_15_r20/external/skia/resources/sksl/runtime/LoopInt.rts (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker/*#pragma settings DebugTrace*/
2*c8dee2aaSAndroid Build Coastguard Worker
3*c8dee2aaSAndroid Build Coastguard Workeruniform half4 colorRed, colorGreen;
4*c8dee2aaSAndroid Build Coastguard Worker
5*c8dee2aaSAndroid Build Coastguard Worker// Should return 5
6*c8dee2aaSAndroid Build Coastguard Workerconst int kZero = 0;
7*c8dee2aaSAndroid Build Coastguard Workerint return_loop(int five) {
8*c8dee2aaSAndroid Build Coastguard Worker    for (int i = kZero; i < 10; ++i) {
9*c8dee2aaSAndroid Build Coastguard Worker        if (i == five) { return i; }
10*c8dee2aaSAndroid Build Coastguard Worker    }
11*c8dee2aaSAndroid Build Coastguard Worker    return 0;
12*c8dee2aaSAndroid Build Coastguard Worker}
13*c8dee2aaSAndroid Build Coastguard Worker
14*c8dee2aaSAndroid Build Coastguard Worker// Should return 35
15*c8dee2aaSAndroid Build Coastguard Workerconst int kTen = kZero + 10;
16*c8dee2aaSAndroid Build Coastguard Workerint continue_loop(int five) {
17*c8dee2aaSAndroid Build Coastguard Worker    int sum = 0;
18*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < kTen; ++i) {
19*c8dee2aaSAndroid Build Coastguard Worker        if (i < five) { continue; }
20*c8dee2aaSAndroid Build Coastguard Worker        sum += i;
21*c8dee2aaSAndroid Build Coastguard Worker    }
22*c8dee2aaSAndroid Build Coastguard Worker    return sum;
23*c8dee2aaSAndroid Build Coastguard Worker}
24*c8dee2aaSAndroid Build Coastguard Worker
25*c8dee2aaSAndroid Build Coastguard Worker// Should return 15
26*c8dee2aaSAndroid Build Coastguard Workerint break_loop(int five) {
27*c8dee2aaSAndroid Build Coastguard Worker    int sum = 0;
28*c8dee2aaSAndroid Build Coastguard Worker    const int kOne = 1;
29*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < 10; i += kOne) {
30*c8dee2aaSAndroid Build Coastguard Worker        if (i > five) { break; }
31*c8dee2aaSAndroid Build Coastguard Worker        sum += i;
32*c8dee2aaSAndroid Build Coastguard Worker    }
33*c8dee2aaSAndroid Build Coastguard Worker    return sum;
34*c8dee2aaSAndroid Build Coastguard Worker}
35*c8dee2aaSAndroid Build Coastguard Worker
36*c8dee2aaSAndroid Build Coastguard Workerbool loop_operator_le() {
37*c8dee2aaSAndroid Build Coastguard Worker    // These loops are inside-out and execute zero times.
38*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 3; i <= 1; ++i) { return false; }
39*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 3; i <= 1; --i) { return false; }
40*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i <= 0; ++i) { return false; }
41*c8dee2aaSAndroid Build Coastguard Worker
42*c8dee2aaSAndroid Build Coastguard Worker    // This loop is not inside-out and should execute exactly one time.
43*c8dee2aaSAndroid Build Coastguard Worker    int4 result = int4(8);
44*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i <= 0; ++i) { result += int4(1); }
45*c8dee2aaSAndroid Build Coastguard Worker
46*c8dee2aaSAndroid Build Coastguard Worker    // This loop executes three times.
47*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i <= 3; ++i) {
48*c8dee2aaSAndroid Build Coastguard Worker        result = int4(result.yzw, i);
49*c8dee2aaSAndroid Build Coastguard Worker    }
50*c8dee2aaSAndroid Build Coastguard Worker    return result == int4(9, 1, 2, 3);
51*c8dee2aaSAndroid Build Coastguard Worker}
52*c8dee2aaSAndroid Build Coastguard Worker
53*c8dee2aaSAndroid Build Coastguard Workerbool loop_operator_lt() {
54*c8dee2aaSAndroid Build Coastguard Worker    // These loops are inside-out and execute zero times.
55*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 4; i < 1; ++i) { return false; }
56*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 4; i < 1; --i) { return false; }
57*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i < 1; ++i) { return false; }
58*c8dee2aaSAndroid Build Coastguard Worker
59*c8dee2aaSAndroid Build Coastguard Worker    // This loop is not inside-out and should execute exactly one time.
60*c8dee2aaSAndroid Build Coastguard Worker    int4 result = int4(8);
61*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < 1; ++i) { result += int4(1); }
62*c8dee2aaSAndroid Build Coastguard Worker
63*c8dee2aaSAndroid Build Coastguard Worker    // This loop executes three times.
64*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i < 4; ++i) {
65*c8dee2aaSAndroid Build Coastguard Worker        result = int4(result.yzw, i);
66*c8dee2aaSAndroid Build Coastguard Worker    }
67*c8dee2aaSAndroid Build Coastguard Worker    return result == int4(9, 1, 2, 3);
68*c8dee2aaSAndroid Build Coastguard Worker}
69*c8dee2aaSAndroid Build Coastguard Worker
70*c8dee2aaSAndroid Build Coastguard Workerbool loop_operator_ge() {
71*c8dee2aaSAndroid Build Coastguard Worker    // These loops are inside-out and execute zero times.
72*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i >= 3; ++i) { return false; }
73*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i >= 3; --i) { return false; }
74*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i >= 1; --i) { return false; }
75*c8dee2aaSAndroid Build Coastguard Worker
76*c8dee2aaSAndroid Build Coastguard Worker    // This loop is not inside-out and should execute exactly one time.
77*c8dee2aaSAndroid Build Coastguard Worker    int4 result = int4(8);
78*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i >= 0; --i) { result += int4(1); }
79*c8dee2aaSAndroid Build Coastguard Worker
80*c8dee2aaSAndroid Build Coastguard Worker    // This loop executes three times.
81*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 3; i >= 1; --i) {
82*c8dee2aaSAndroid Build Coastguard Worker        result = int4(result.yzw, i);
83*c8dee2aaSAndroid Build Coastguard Worker    }
84*c8dee2aaSAndroid Build Coastguard Worker    return result == int4(9, 3, 2, 1);
85*c8dee2aaSAndroid Build Coastguard Worker}
86*c8dee2aaSAndroid Build Coastguard Worker
87*c8dee2aaSAndroid Build Coastguard Workerbool loop_operator_gt() {
88*c8dee2aaSAndroid Build Coastguard Worker    // These loops are inside-out and execute zero times.
89*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i > 3; ++i) { return false; }
90*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i > 3; --i) { return false; }
91*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i > 1; --i) { return false; }
92*c8dee2aaSAndroid Build Coastguard Worker
93*c8dee2aaSAndroid Build Coastguard Worker    // This loop is not inside-out and should execute exactly one time.
94*c8dee2aaSAndroid Build Coastguard Worker    int4 result = int4(8);
95*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i > 0; --i) { result += int4(1); }
96*c8dee2aaSAndroid Build Coastguard Worker
97*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 3; i > 0; --i) {
98*c8dee2aaSAndroid Build Coastguard Worker        result = int4(result.yzw, i);
99*c8dee2aaSAndroid Build Coastguard Worker    }
100*c8dee2aaSAndroid Build Coastguard Worker    return result == int4(9, 3, 2, 1);
101*c8dee2aaSAndroid Build Coastguard Worker}
102*c8dee2aaSAndroid Build Coastguard Worker
103*c8dee2aaSAndroid Build Coastguard Workerbool loop_operator_ne() {
104*c8dee2aaSAndroid Build Coastguard Worker    // This loop executes zero times.
105*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i != 1; ++i) { return false; }
106*c8dee2aaSAndroid Build Coastguard Worker
107*c8dee2aaSAndroid Build Coastguard Worker    // This loop should execute exactly one time.
108*c8dee2aaSAndroid Build Coastguard Worker    int4 result = int4(8);
109*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i != 2; ++i) { result += int4(1); }
110*c8dee2aaSAndroid Build Coastguard Worker
111*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i != 4; ++i) {
112*c8dee2aaSAndroid Build Coastguard Worker        result = int4(result.yzw, i);
113*c8dee2aaSAndroid Build Coastguard Worker    }
114*c8dee2aaSAndroid Build Coastguard Worker    return result == int4(9, 1, 2, 3);
115*c8dee2aaSAndroid Build Coastguard Worker}
116*c8dee2aaSAndroid Build Coastguard Worker
117*c8dee2aaSAndroid Build Coastguard Workerbool loop_operator_eq() {
118*c8dee2aaSAndroid Build Coastguard Worker    // This loop executes zero times.
119*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i == 2; ++i) { return false; }
120*c8dee2aaSAndroid Build Coastguard Worker
121*c8dee2aaSAndroid Build Coastguard Worker    // This loop should execute exactly one time.
122*c8dee2aaSAndroid Build Coastguard Worker    int4 result = int4(9);
123*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 1; i == 1; ++i) {
124*c8dee2aaSAndroid Build Coastguard Worker        result = int4(result.yzw, i);
125*c8dee2aaSAndroid Build Coastguard Worker    }
126*c8dee2aaSAndroid Build Coastguard Worker    return result == int4(9, 9, 9, 1);
127*c8dee2aaSAndroid Build Coastguard Worker}
128*c8dee2aaSAndroid Build Coastguard Worker
129*c8dee2aaSAndroid Build Coastguard Workerhalf4 main(float2 pos) {
130*c8dee2aaSAndroid Build Coastguard Worker    int five = int(clamp(pos.x, colorGreen.g, colorGreen.a)) * 5;
131*c8dee2aaSAndroid Build Coastguard Worker
132*c8dee2aaSAndroid Build Coastguard Worker    // We pass a literal 5 into `break_loop` instead of the variable `five` as a workaround for
133*c8dee2aaSAndroid Build Coastguard Worker    // a Metal crash bug in macOS 12. (skia:13005; Apple FB9937818)
134*c8dee2aaSAndroid Build Coastguard Worker    return (return_loop(five) == 5 && continue_loop(five) == 35 && break_loop(5) == 15 &&
135*c8dee2aaSAndroid Build Coastguard Worker            loop_operator_le() && loop_operator_lt() &&
136*c8dee2aaSAndroid Build Coastguard Worker            loop_operator_ge() && loop_operator_gt() &&
137*c8dee2aaSAndroid Build Coastguard Worker            loop_operator_eq() && loop_operator_ne()) ? colorGreen : colorRed;
138*c8dee2aaSAndroid Build Coastguard Worker}
139