Mercurial > hg > truffle
comparison test/compiler/6603011/Test.java @ 567:bbef4344adb2
6800154: Add comments to long_by_long_mulhi() for better understandability
Summary: This patch adds a comment pointing to the Hacker's Delight version of the algorithm plus a verbatim copy of it. Furthermore it adds inline comments.
Reviewed-by: kvn, jrose
author | twisti |
---|---|
date | Fri, 13 Feb 2009 09:09:35 -0800 |
parents | |
children | c18cbe5936b8 |
comparison
equal
deleted
inserted
replaced
566:91263420e1c6 | 567:bbef4344adb2 |
---|---|
1 /* | |
2 * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
20 * CA 95054 USA or visit www.sun.com if you need additional information or | |
21 * have any questions. | |
22 */ | |
23 | |
24 /** | |
25 * @test | |
26 * @bug 6603011 | |
27 * @summary long/int division by constant | |
28 * | |
29 * @run main/othervm -Xcomp -Xbatch -XX:-Inline Test | |
30 */ | |
31 | |
32 // | |
33 // -XX:-Inline is essential to this test so that verification functions | |
34 // divi, modi, divl and modl generate "plain" divides. | |
35 // -Xcomp -Xbatch are also useful to ensure the full range of | |
36 // dividend and divisor combinations are tested | |
37 // | |
38 | |
39 import java.net.*; | |
40 | |
41 class s { | |
42 static int divi(int dividend, int divisor) { return dividend / divisor; } | |
43 static int modi(int dividend, int divisor) { return dividend % divisor; } | |
44 static long divl(long dividend, long divisor) { return dividend / divisor; } | |
45 static long modl(long dividend, long divisor) { return dividend % divisor; } | |
46 } | |
47 | |
48 public class Test implements Runnable { | |
49 // Report verbose messages on failure; turn off to suppress | |
50 // too much output with gross numbers of failures. | |
51 static final boolean VERBOSE = true; | |
52 | |
53 // Initailize DIVISOR so that it is final in this class. | |
54 static final int DIVISOR; | |
55 static { | |
56 int value = 0; | |
57 try { | |
58 value = Integer.decode(System.getProperty("divisor")); | |
59 } catch (Throwable e) { | |
60 } | |
61 DIVISOR = value; | |
62 } | |
63 | |
64 // The methods of interest. We want the JIT to compile these | |
65 // and convert the divide into a multiply. | |
66 public int divbyI (int dividend) { return dividend / DIVISOR; } | |
67 public int modbyI (int dividend) { return dividend % DIVISOR; } | |
68 public long divbyL (long dividend) { return dividend / DIVISOR; } | |
69 public long modbyL (long dividend) { return dividend % DIVISOR; } | |
70 | |
71 public int divisor() { return DIVISOR; } | |
72 | |
73 public boolean checkI (int dividend) { | |
74 int quo = divbyI(dividend); | |
75 int rem = modbyI(dividend); | |
76 int quo0 = s.divi(dividend, divisor()); | |
77 int rem0 = s.modi(dividend, divisor()); | |
78 | |
79 if (quo != quo0 || rem != rem0) { | |
80 if (VERBOSE) { | |
81 System.out.println("Computed: " + dividend + " / " + divisor() + " = " + | |
82 quo + ", " + dividend + " % " + divisor() + " = " + rem ); | |
83 System.out.println("expected: " + dividend + " / " + divisor() + " = " + | |
84 quo0 + ", " + dividend + " % " + divisor() + " = " + rem0); | |
85 // Report sign of rem failure | |
86 if (rem != 0 && (rem ^ dividend) < 0) { | |
87 System.out.println(" rem & dividend have different signs"); | |
88 } | |
89 // Report range of rem failure | |
90 if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) { | |
91 System.out.println(" remainder out of range"); | |
92 } | |
93 // Report quo/rem identity relationship failure | |
94 if ((quo * divisor()) + rem != dividend) { | |
95 System.out.println(" quotien/remainder invariant broken"); | |
96 } | |
97 } | |
98 return false; | |
99 } | |
100 return true; | |
101 } | |
102 | |
103 public boolean checkL (long dividend) { | |
104 long quo = divbyL(dividend); | |
105 long rem = modbyL(dividend); | |
106 long quo0 = s.divl(dividend, divisor()); | |
107 long rem0 = s.modl(dividend, divisor()); | |
108 | |
109 if (quo != quo0 || rem != rem0) { | |
110 if (VERBOSE) { | |
111 System.out.println(" " + dividend + " / " + divisor() + " = " + | |
112 quo + ", " + dividend + " % " + divisor() + " = " + rem); | |
113 // Report sign of rem failure | |
114 if (rem != 0 && (rem ^ dividend) < 0) { | |
115 System.out.println(" rem & dividend have different signs"); | |
116 } | |
117 // Report range of rem failure | |
118 if (java.lang.Math.abs(rem) >= java.lang.Math.abs(divisor())) { | |
119 System.out.println(" remainder out of range"); | |
120 } | |
121 // Report quo/rem identity relationship failure | |
122 if ((quo * divisor()) + rem != dividend) { | |
123 System.out.println(" (" + quo + " * " + divisor() + ") + " + rem + " != " | |
124 + dividend); | |
125 } | |
126 } | |
127 return false; | |
128 } | |
129 return true; | |
130 } | |
131 | |
132 public void run() { | |
133 // Don't try to divide by zero | |
134 if (divisor() == 0) return; | |
135 | |
136 // Range of dividends to check. Try dividends from start to end | |
137 // inclusive, as well as variations on those values as shifted | |
138 // left. | |
139 int start = -1024; | |
140 int end = 1024; | |
141 | |
142 // Test int division using a variety of dividends. | |
143 int wrong = 0; | |
144 int total = 0; | |
145 | |
146 outerloop: | |
147 for (int i = start; i <= end; i++) { | |
148 for (int s = 0; s < 32; s += 4) { | |
149 total++; | |
150 int dividend = i << s; | |
151 if (!checkI(dividend)) { | |
152 wrong++; | |
153 // Stop on the first failure | |
154 // break outerloop; | |
155 } | |
156 } | |
157 } | |
158 if (wrong > 0) { | |
159 System.out.println("divisor " + divisor() + ": " + | |
160 wrong + "/" + total + " wrong int divisions"); | |
161 } | |
162 | |
163 // Test long division using a variety of dividends. | |
164 wrong = 0; | |
165 total = 0; | |
166 | |
167 outerloop: | |
168 for (int i = start; i <= end; i++) { | |
169 for (int s = 0; s < 64; s += 4) { | |
170 total++; | |
171 long dividend = i << s; | |
172 if (!checkL(dividend)) { | |
173 wrong++; | |
174 // Stop on the first failure | |
175 // break outerloop; | |
176 } | |
177 } | |
178 } | |
179 if (wrong > 0) { | |
180 System.out.println("divisor " + divisor() + ": " + | |
181 wrong + "/" + total + " wrong long divisions"); | |
182 } | |
183 | |
184 } | |
185 | |
186 // Reload this class with the "divisor" property set to the input parameter. | |
187 // This allows the JIT to see q.DIVISOR as a final constant, and change | |
188 // any divisions or mod operations into multiplies. | |
189 public static void test_divisor(int divisor, | |
190 URLClassLoader apploader) throws Exception { | |
191 System.setProperty("divisor", "" + divisor); | |
192 ClassLoader loader = new URLClassLoader(apploader.getURLs(), | |
193 apploader.getParent()); | |
194 Class c = loader.loadClass("Test"); | |
195 Runnable r = (Runnable)c.newInstance(); | |
196 r.run(); | |
197 } | |
198 | |
199 public static void main(String[] args) throws Exception { | |
200 Class cl = Class.forName("Test"); | |
201 URLClassLoader apploader = (URLClassLoader)cl.getClassLoader(); | |
202 | |
203 | |
204 // Test every divisor between -100 and 100. | |
205 for (int i = -100; i <= 100; i++) { | |
206 test_divisor(i, apploader); | |
207 } | |
208 | |
209 // Try a few divisors outside the typical range. | |
210 // The values below have been observed in rt.jar. | |
211 test_divisor(101, apploader); | |
212 test_divisor(400, apploader); | |
213 test_divisor(1000, apploader); | |
214 test_divisor(3600, apploader); | |
215 test_divisor(9973, apploader); | |
216 test_divisor(86400, apploader); | |
217 test_divisor(1000000, apploader); | |
218 } | |
219 | |
220 } |