FloatOptimizationTest.java revision 12651:6ef01bd40ce2
1132718Skan/*
2169689Skan * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3132718Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4132718Skan *
5132718Skan * This code is free software; you can redistribute it and/or modify it
6132718Skan * under the terms of the GNU General Public License version 2 only, as
7132718Skan * published by the Free Software Foundation.
8132718Skan *
9132718Skan * This code is distributed in the hope that it will be useful, but WITHOUT
10132718Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11132718Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12132718Skan * version 2 for more details (a copy is included in the LICENSE file that
13132718Skan * accompanied this code).
14132718Skan *
15132718Skan * You should have received a copy of the GNU General Public License version
16132718Skan * 2 along with this work; if not, write to the Free Software Foundation,
17132718Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169689Skan *
19169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20132718Skan * or visit www.oracle.com if you need additional information or have any
21132718Skan * questions.
22132718Skan */
23132718Skanpackage org.graalvm.compiler.core.test;
24132718Skan
25132718Skanimport org.junit.Assert;
26132718Skanimport org.junit.Test;
27132718Skan
28132718Skan/**
29132718Skan * Check for incorrect elimination of 0.0 and -0.0 from computations. They can affect the sign of
30132718Skan * the result of an add or substract.
31132718Skan */
32132718Skanpublic class FloatOptimizationTest extends GraalCompilerTest {
33132718Skan
34132718Skan    @Test
35132718Skan    public void test1() {
36132718Skan        test("test1Snippet", -0.0);
37132718Skan    }
38132718Skan
39132718Skan    @SuppressWarnings("all")
40132718Skan    public static double test1Snippet(double x) {
41169689Skan        return x + 0.0;
42169689Skan    }
43132718Skan
44132718Skan    @Test
45132718Skan    public void test2() {
46132718Skan        test("test2Snippet", -0.0f);
47132718Skan    }
48132718Skan
49132718Skan    @SuppressWarnings("all")
50132718Skan    public static double test2Snippet(float x) {
51132718Skan        return x + 0.0f;
52169689Skan    }
53169689Skan
54132718Skan    @Test
55132718Skan    public void test3() {
56132718Skan        test("test3Snippet", -0.0);
57132718Skan    }
58169689Skan
59169689Skan    @SuppressWarnings("all")
60132718Skan    public static double test3Snippet(double x) {
61132718Skan        return x - -0.0;
62132718Skan    }
63132718Skan
64169689Skan    @Test
65169689Skan    public void test4() {
66132718Skan        test("test4Snippet", -0.0f);
67132718Skan    }
68132718Skan
69132718Skan    @SuppressWarnings("all")
70169689Skan    public static double test4Snippet(float x) {
71169689Skan        return x - -0.0f;
72132718Skan    }
73132718Skan
74132718Skan    @Override
75132718Skan    protected void assertDeepEquals(String message, Object expected, Object actual, double delta) {
76132718Skan        if (expected instanceof Double && actual instanceof Double) {
77132718Skan            double e = (double) expected;
78132718Skan            double a = (double) actual;
79132718Skan            if (Double.doubleToRawLongBits(a) != Double.doubleToRawLongBits(e)) {
80132718Skan                Assert.fail((message == null ? "" : message) + "raw double bits not equal " + Double.doubleToRawLongBits(a) + " != " + Double.doubleToRawLongBits(e));
81132718Skan            }
82132718Skan        } else {
83132718Skan            super.assertDeepEquals(message, expected, actual, delta);
84132718Skan        }
85132718Skan    }
86132718Skan}
87132718Skan