summaryrefslogtreecommitdiff
path: root/tests/src/JIT/opt/Loops/SearchLoopTail.cs
blob: 10be60c021559deb31bcf9b3cac183c74aae6d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Test for tail merging/duplication of search loops returning constants.

using System.Runtime.CompilerServices;

namespace N
{
    public static class C
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool HasPrimeUnderTwenty(int first, int last)
        {
            for (int n = first; n <= last; ++n)
            {
                if (n == 2) return true;
                if (n == 3) return true;
                if (n == 5) return true;
                if (n == 7) return true;
                if (n == 11) return true;
                if (n == 13) return true;
                if (n == 17) return true;
                if (n == 19) return true;
            }

            return false;
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        static int Distance_abCD(string s)
        {
            int c_index = 0;
            bool sawA = false;
            bool sawB = false;
            int index = 0;

            foreach (char c in s)
            {
                if (c == 'A') sawA = true;
                if (c == 'B')
                {
                    if (!sawA) return -1;
                    sawB = true;
                }
                if (c == 'C')
                {
                    if (!sawB) return -1;
                    c_index = index;
                }
                if (c == 'D')
                {
                    if (c_index == 0) return -1;
                    return (index - c_index);
                }
                ++index;
            }

            return -1;
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool TrueFalseOr(int[] input)
        {
            int n = 0, m = 0;
            for (int i = 0; i < input.Length; ++i)
            {
                m = n;
                n = input[i];
                if (n < 7)
                    return true;
                if (n > 22)
                    return false;
                if (n == 12)
                    return (m == 5);
            }

            return (n == 9 || m == 13);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool TrueFalseAnd(int[] input)
        {
            int n = 0, m = 0;
            for (int i = 0; i < input.Length; ++i)
            {
                m = n;
                n = input[i];
                if (n < 7)
                    return true;
                if (n > 22)
                    return false;
                if (n == 12)
                    return (m == 5);
            }

            return (n == 9 && m == 13);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool FalseTrueOr(int[] input)
        {
            int n = 0, m = 0;
            for (int i = 0; i < input.Length; ++i)
            {
                m = n;
                n = input[i];
                if (n < 7)
                    return false;
                if (n > 22)
                    return true;
                if (n == 12)
                    return (m == 5);
            }

            return (n == 9 || m == 13);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool FalseTrueAnd(int[] input)
        {
            int n = 0, m = 0;
            for (int i = 0; i < input.Length; ++i)
            {
                m = n;
                n = input[i];
                if (n < 7)
                    return false;
                if (n > 22)
                    return true;
                if (n == 12)
                    return (m == 5);
            }

            return (n == 9 && m == 13);
        }

        public static int Main(string[] args)
        {
            if (HasPrimeUnderTwenty(22, 36) || !HasPrimeUnderTwenty(-1, 4))
            {
                return -1;
            }

            if ((Distance_abCD("xxxxAyyyyBzyzyzC1234D") != 5) || (Distance_abCD("nnABmmDC") != -1))
            {
                return -1;
            }

            int[] inputs = new int[] { 8, 20, 11, 13, 15 };
            if (!TrueFalseOr(inputs) || TrueFalseAnd(inputs) || !FalseTrueOr(inputs) || FalseTrueAnd(inputs))
            {
                return -1;
            }

            return 100;
        }
    }
}