This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
TestFPBinop.java
415 lines (318 loc) · 12.3 KB
/
TestFPBinop.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
*
* @summary converted from VM Testbase jit/FloatingPoint/FPCompare/TestFPBinop.
* VM Testbase keywords: [jit, quick]
*
* @library /vmTestbase
* /test/lib
* @run main/othervm jit.FloatingPoint.FPCompare.TestFPBinop.TestFPBinop
*/
package jit.FloatingPoint.FPCompare.TestFPBinop;
import nsk.share.TestFailure;
import nsk.share.GoldChecker;
/** Test of Floating Point Binary Ops.
** This is intended to be run on a known-correct system and the
** answer compared with the golden answer with diff or equivalent.
*/
public class TestFPBinop {
public static final GoldChecker goldChecker = new GoldChecker( "TestFPBinop" );
static float floatValues [] = {
Float.MIN_VALUE, Float.MAX_VALUE,
-Float.MIN_VALUE, -Float.MAX_VALUE,
-1.0f, 1.0f, -0.0f, 0.0f,
Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY,
Float.NaN
};
static double doubleValues [] = {
Double.MIN_VALUE, Double.MAX_VALUE,
-Double.MIN_VALUE, -Double.MAX_VALUE,
-1.0, 1.0, -0.0, 0.0,
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
Double.NaN
};
static int nValues = floatValues.length;
static float fOne, fZero;
static double dOne, dZero;
/* This is intended to thrwart an optimizing compiler
* from simplifying some of the expressions by using algebraic
* identities. */
static {
fOne = new Integer(1).floatValue();
fZero = new Integer(0).floatValue();
dOne = new Integer(1).doubleValue();
dZero = new Integer(0).doubleValue();
}
static final boolean DEBUG = false;
static String operandType = "";
// static values
static float xs, ys;
static double xS, yS;
/** Test of Floating Point Binary operators.
** The following orthogonal variables need to be tested.
** <ul>
** <li> Data type: float or double
** <li> Operator: +, -, *, /
** <li> Data values: +-normal, +-zero, NaN, +-infinity, +- min, +-max
** <li> Operand: variable, parameter, static, field, array element,
** function reference, expression, explicit constant.
** </ul>
*/
public static void main (String [] args) {
testFloats();
testDoubles();
TestFPBinop.goldChecker.check();
}
static void testFloats() {
for (int i = 0; i < floatValues.length; i++) {
float iVal = floatValues[i];
for (int j = 0; j < floatValues.length; j++) {
float jVal = floatValues[j];
testFloat(iVal, jVal);
}
}
}
static void testDoubles() {
for (int i = 0; i < doubleValues.length; i++) {
double iVal = doubleValues[i];
for (int j = 0; j < doubleValues.length; j++) {
double jVal = doubleValues[j];
testDouble(iVal, jVal);
}
}
}
static void testFloat (float x, float y) {
testFloatP(x, y);
testFloatL(x, y);
testFloatS(x, y);
testFloatF(x, y);
testFloatA(x, y);
testFloatM(x, y);
testFloat1(x, y);
testFloat2(x, y);
testFloat3(x, y);
}
static void testFloatP(float x, float y) {
check(x, y, x + y, "param", "+");
check(x, y, x - y, "param", "-");
check(x, y, x * y, "param", "*");
check(x, y, x / y, "param", "/");
}
static void testFloatL(float x, float y) {
float xl = x;
float yl = y;
check(xl, yl, xl + yl, "local", "+");
check(xl, yl, xl - yl, "local", "-");
check(xl, yl, xl * yl, "local", "*");
check(xl, yl, xl / yl, "local", "/");
}
static void testFloatS(float x, float y) {
xs = x;
ys = y;
check(xs, ys, xs + ys, "static", "+");
check(xs, ys, xs - ys, "static", "-");
check(xs, ys, xs * ys, "static", "*");
check(xs, ys, xs / ys, "static", "/");
}
static void testFloatF(float x, float y) {
FloatObject xo = new FloatObject(x);
FloatObject yo = new FloatObject(y);
check(xo.f, yo.f, xo.f + yo.f, "field", "+");
check(xo.f, yo.f, xo.f - yo.f, "field", "-");
check(xo.f, yo.f, xo.f * yo.f, "field", "*");
check(xo.f, yo.f, xo.f / yo.f, "field", "/");
}
static void testFloatA(float x, float y) {
int i = index(x);
int j = index(y);
float a [] = floatValues;
check(a[i], a[j], a[i] + a[j], "a[i]", "+");
check(a[i], a[j], a[i] - a[j], "a[i]", "-");
check(a[i], a[j], a[i] * a[j], "a[i]", "*");
check(a[i], a[j], a[i] / a[j], "a[i]", "/");
}
static void testFloatM(float x, float y) {
check(i(x), i(y), i(x) + i(y), "f(x)", "+");
check(i(x), i(y), i(x) - i(y), "f(x)", "-");
check(i(x), i(y), i(x) * i(y), "f(x)", "*");
check(i(x), i(y), i(x) / i(y), "f(x)", "/");
}
static void testFloat1(float x, float y) {
float zero = fZero;
float one = fOne;
check(((x + zero) * one), y, ((x + zero) * one) + y, "lExpr", "+");
check(((x + zero) * one), y, ((x + zero) * one) - y, "lExpr", "-");
check(((x + zero) * one), y, ((x + zero) * one) * y, "lExpr", "*");
check(((x + zero) * one), y, ((x + zero) * one) / y, "lExpr", "/");
}
static void testFloat3(float x, float y) {
float zero = fZero;
float one = fOne;
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) + (zero + one * y), "exprs", "+");
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) - (zero + one * y), "exprs", "-");
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) * (zero + one * y), "exprs", "*");
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) / (zero + one * y), "exprs", "/");
}
static void testFloat2(float x, float y) {
float zero = fZero;
float one = fOne;
operandType = "rExpr";
check(x, (zero + one * y), x + (zero + one * y), "rExpr", "+");
check(x, (zero + one * y), x - (zero + one * y), "rExpr", "-");
check(x, (zero + one * y), x * (zero + one * y), "rExpr", "*");
check(x, (zero + one * y), x / (zero + one * y), "rExpr", "/");
}
static void testDouble (double x, double y) {
testDoubleP(x, y);
testDoubleL(x, y);
testDoubleS(x, y);
testDoubleF(x, y);
testDoubleA(x, y);
testDoubleM(x, y);
testDouble1(x, y);
testDouble2(x, y);
testDouble3(x, y);
}
static void testDoubleP (double x, double y) {
check(x, y, x + y, "param", "+");
check(x, y, x - y, "param", "-");
check(x, y, x * y, "param", "*");
check(x, y, x / y, "param", "/");
}
static void testDoubleL (double x, double y) {
double xl = x;
double yl = y;
check(xl, yl, xl + yl, "local", "+");
check(xl, yl, xl - yl, "local", "-");
check(xl, yl, xl * yl, "local", "*");
check(xl, yl, xl / yl, "local", "/");
}
static void testDoubleS (double x, double y) {
xS = x;
yS = y;
check(xS, yS, xS + yS, "static", "+");
check(xS, yS, xS - yS, "static", "-");
check(xS, yS, xS * yS, "static", "*");
check(xS, yS, xS / yS, "static", "/");
}
static void testDoubleF (double x, double y) {
DoubleObject xo = new DoubleObject(x);
DoubleObject yo = new DoubleObject(y);
check(xo.f, yo.f, xo.f + yo.f, "field", "+");
check(xo.f, yo.f, xo.f - yo.f, "field", "-");
check(xo.f, yo.f, xo.f * yo.f, "field", "*");
check(xo.f, yo.f, xo.f / yo.f, "field", "/");
}
static void testDoubleA (double x, double y) {
int i = index(x);
int j = index(y);
double a [] = doubleValues;
check(a[i], a[j], a[i] + a[j], "a[i]", "+");
check(a[i], a[j], a[i] - a[j], "a[i]", "-");
check(a[i], a[j], a[i] * a[j], "a[i]", "*");
check(a[i], a[j], a[i] / a[j], "a[i]", "/");
}
static void testDoubleM (double x, double y) {
check(i(x), i(y), i(x) + i(y), "f(x)", "+");
check(i(x), i(y), i(x) - i(y), "f(x)", "-");
check(i(x), i(y), i(x) * i(y), "f(x)", "*");
check(i(x), i(y), i(x) / i(y), "f(x)", "/");
}
static void testDouble1 (double x, double y) {
double zero = dZero;
double one = dOne;
check(((x + zero) * one), y, ((x + zero) * one) + y, "lExpr", "+");
check(((x + zero) * one), y, ((x + zero) * one) - y, "lExpr", "-");
check(((x + zero) * one), y, ((x + zero) * one) * y, "lExpr", "*");
check(((x + zero) * one), y, ((x + zero) * one) / y, "lExpr", "/");
}
static void testDouble3 (double x, double y) {
double zero = dZero;
double one = dOne;
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) + (zero + one * y), "exprs", "+");
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) - (zero + one * y), "exprs", "-");
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) * (zero + one * y), "exprs", "*");
check(((x + zero) * one), (zero + one * y), ((x + zero) * one) / (zero + one * y), "exprs", "/");
}
static void testDouble2 (double x, double y) {
double zero = dZero;
double one = dOne;
check(x, (zero + one * y), x + (zero + one * y), "rExpr", "+");
check(x, (zero + one * y), x - (zero + one * y), "rExpr", "-");
check(x, (zero + one * y), x * (zero + one * y), "rExpr", "*");
check(x, (zero + one * y), x / (zero + one * y), "rExpr", "/");
}
/* The convoluted coding is intended to prevent inlining */
static float i(float x) {
while (Float.isNaN(x) && Float.floatToIntBits(x) == 0) {
x = 0.0f;
}
return x;
}
static double i(double x) {
while (Double.isNaN(x) && Double.doubleToLongBits(x) == 0L) {
x = 0.0;
}
return x;
}
static int index(float x) {
for (int i = 0; i < floatValues.length; i++) {
if (new Float(x).equals(new Float(floatValues[i])))
return i;
}
throw new TestFailure("ERROR: can't find " + x + " in floatValues.");
}
static int index(double x) {
for (int i = 0; i < doubleValues.length; i++) {
if (new Double(x).equals(new Double(doubleValues[i])))
return i;
}
throw new TestFailure("ERROR: can't find " + x + " in doubleValues.");
}
static void check (float x, float y, float result,
String operands, String operator) {
TestFPBinop.goldChecker.println(x + " " + operator + " " + y +
" = " + result + ", with float " +
operands + " operands");
}
static void check (double x, double y, double result,
String operands, String operator) {
TestFPBinop.goldChecker.println(x + " " + operator + " " + y +
" = " + result + ", with double " +
operands + " operands");
}
}
class FloatObject {
public float f;
public FloatObject(float x) {
f = x;
}
}
class DoubleObject {
public double f;
public DoubleObject(double x) {
f = x;
}
}