1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * Tests for last value of a few periodic sequences
19  * (found by fuzz testing).
20  */
21 public class Main {
22 
23   /// CHECK-START: int Main.doitUpInt(int) loop_optimization (before)
24   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
25   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
26   //
27   /// CHECK-START: int Main.doitUpInt(int) loop_optimization (after)
28   /// CHECK-NOT: Phi
doitUpInt(int n)29   static int doitUpInt(int n) {
30     // Complete loop is replaced by last-value.
31     int lI = 1;
32     for (int i1 = 0; i1  < n; i1++) {
33       lI = (1486662021 - lI);
34     }
35     return lI;
36   }
37 
38   /// CHECK-START: int Main.doitDownInt(int) loop_optimization (before)
39   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
40   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
41   //
42   /// CHECK-START: int Main.doitDownInt(int) loop_optimization (after)
43   /// CHECK-NOT: Phi
doitDownInt(int n)44   static int doitDownInt(int n) {
45     // Complete loop is replaced by last-value.
46     int lI = 1;
47     for (int i1 = n - 1; i1 >= 0; i1--) {
48       lI = (1486662021 - lI);
49     }
50     return lI;
51   }
52 
53   /// CHECK-START: float Main.doitUpFloat(int) loop_optimization (before)
54   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
55   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
56   //
57   /// CHECK-START: float Main.doitUpFloat(int) loop_optimization (after)
58   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
59   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
doitUpFloat(int n)60   static float doitUpFloat(int n) {
61     // FP arithmetic is not sufficiently precise.
62     // The loop remains.
63     float lF = 1.0f;
64     for (int i1 = 0; i1  < n; i1++) {
65       lF = (1486662021.0f - lF);
66     }
67     return lF;
68   }
69 
70   /// CHECK-START: float Main.doitDownFloat(int) loop_optimization (before)
71   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
72   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
73   //
74   /// CHECK-START: float Main.doitDownFloat(int) loop_optimization (after)
75   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
76   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
doitDownFloat(int n)77   static float doitDownFloat(int n) {
78     // FP arithmetic is not sufficiently precise.
79     // The loop remains.
80     float lF = 1.0f;
81     for (int i1 = n - 1; i1 >= 0; i1--) {
82       lF = (1486662021.0f - lF);
83     }
84     return lF;
85   }
86 
87   /// CHECK-START: float Main.doitUpFloatAlt(int) loop_optimization (before)
88   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
89   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
90   //
91   /// CHECK-START: float Main.doitUpFloatAlt(int) loop_optimization (after)
92   /// CHECK-NOT: Phi
doitUpFloatAlt(int n)93   static float doitUpFloatAlt(int n) {
94     // Complete loop is replaced by last-value
95     // since the values are now precise.
96     float lF = 1.0f;
97     float l2 = 1486662020.0f;
98     for (int i1 = 0; i1  < n; i1++) {
99       float old = lF;
100       lF = l2;
101       l2 = old;
102     }
103     return lF;
104   }
105 
106   /// CHECK-START: float Main.doitDownFloatAlt(int) loop_optimization (before)
107   /// CHECK-DAG: <<Phi:i\d+>> Phi  loop:<<Loop:B\d+>> outer_loop:none
108   /// CHECK-DAG:              Phi  loop:<<Loop>>      outer_loop:none
109   //
110   /// CHECK-START: float Main.doitDownFloatAlt(int) loop_optimization (after)
111   /// CHECK-NOT: Phi
doitDownFloatAlt(int n)112   static float doitDownFloatAlt(int n) {
113     // Complete loop is replaced by last-value
114     // since the values are now precise.
115     float lF = 1.0f;
116     float l2 = 1486662020.0f;
117     for (int i1 = n - 1; i1 >= 0; i1--) {
118       float old = lF;
119       lF = l2;
120       l2 = old;
121     }
122     return lF;
123   }
124 
125   // Main driver.
main(String[] args)126   public static void main(String[] args) {
127     for (int i = 0; i < 10; i++) {
128       int ei = (i & 1) == 0 ? 1 : 1486662020;
129       int ci = doitUpInt(i);
130       expectEquals(ei, ci);
131     }
132     for (int i = 0; i < 10; i++) {
133       int ei = (i & 1) == 0 ? 1 : 1486662020;
134       int ci = doitDownInt(i);
135       expectEquals(ei, ci);
136     }
137     for (int i = 0; i < 10; i++) {
138       float ef = i == 0 ? 1.0f : ((i & 1) == 0 ? 0.0f : 1486662021.0f);
139       float cf = doitUpFloat(i);
140       expectEquals(ef, cf);
141     }
142     for (int i = 0; i < 10; i++) {
143       float ef = i == 0 ? 1.0f : ((i & 1) == 0 ? 0.0f : 1486662021.0f);
144       float cf = doitDownFloat(i);
145       expectEquals(ef, cf);
146     }
147     for (int i = 0; i < 10; i++) {
148       float ef = (i & 1) == 0 ? 1.0f : 1486662020.0f;
149       float cf = doitUpFloatAlt(i);
150       expectEquals(ef, cf);
151     }
152     for (int i = 0; i < 10; i++) {
153       float ef = (i & 1) == 0 ? 1.0f : 1486662020.0f;
154       float cf = doitDownFloatAlt(i);
155       expectEquals(ef, cf);
156     }
157     System.out.println("passed");
158   }
159 
expectEquals(int expected, int result)160   private static void expectEquals(int expected, int result) {
161     if (expected != result) {
162       throw new Error("Expected: " + expected + ", found: " + result);
163     }
164   }
165 
expectEquals(float expected, float result)166   private static void expectEquals(float expected, float result) {
167     if (expected != result) {
168       throw new Error("Expected: " + expected + ", found: " + result);
169     }
170   }
171 }
172 
173 
174