001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.truffle.test;
024
025import org.junit.*;
026
027import com.oracle.graal.compiler.test.*;
028import com.oracle.graal.graphbuilderconf.*;
029import com.oracle.graal.truffle.substitutions.*;
030import com.oracle.truffle.api.*;
031
032public class ExactMathTest extends GraalCompilerTest {
033
034    @Override
035    protected GraphBuilderConfiguration editGraphBuilderConfiguration(GraphBuilderConfiguration conf) {
036        TruffleGraphBuilderPlugins.registerExactMathPlugins(conf.getPlugins().getInvocationPlugins());
037        return super.editGraphBuilderConfiguration(conf);
038    }
039
040    @Test
041    public void testAdd() {
042        test("add", 1, 2);
043        test("add", Integer.MAX_VALUE, 2);
044        test("add", Integer.MIN_VALUE, -1);
045        test("add", -1, 2);
046    }
047
048    @Test
049    public void testMul() {
050        test("mul", 1, 2);
051        test("mul", -1, 2);
052        test("mul", Integer.MIN_VALUE, 1);
053        test("mul", Integer.MIN_VALUE, 2);
054        test("mul", Integer.MIN_VALUE, Integer.MIN_VALUE);
055        test("mul", Integer.MAX_VALUE, 1);
056        test("mul", Integer.MAX_VALUE, 2);
057        test("mul", Integer.MAX_VALUE, Integer.MAX_VALUE);
058    }
059
060    @Test
061    public void testSub() {
062        test("sub", 1, 2);
063        test("sub", Integer.MIN_VALUE, 2);
064    }
065
066    @Test
067    public void testMulHigh() {
068        test("mulHigh", 7, 15);
069        test("mulHigh", Integer.MAX_VALUE, 15);
070        test("mulHigh", Integer.MIN_VALUE, 15);
071    }
072
073    @Test
074    public void testMulHighUnsigned() {
075        test("mulHighUnsigned", 7, 15);
076        test("mulHighUnsigned", Integer.MAX_VALUE, 15);
077        test("mulHighUnsigned", 15, Integer.MAX_VALUE);
078        test("mulHighUnsigned", Integer.MAX_VALUE, Integer.MAX_VALUE);
079        test("mulHighUnsigned", 15, Integer.MIN_VALUE);
080        test("mulHighUnsigned", Integer.MIN_VALUE, 15);
081        test("mulHighUnsigned", Integer.MIN_VALUE, Integer.MIN_VALUE);
082    }
083
084    @Test
085    public void testLongAdd() {
086        test("longAdd", (long) Integer.MAX_VALUE, 2L);
087        test("longAdd", Long.MAX_VALUE, 2L);
088    }
089
090    @Test
091    public void testLongMul() {
092        test("longMul", (long) Integer.MAX_VALUE, 2L);
093        test("longMul", (long) Integer.MIN_VALUE, 2L);
094        test("longMul", Long.MAX_VALUE, 2L);
095        test("longMul", Long.MAX_VALUE, 1L);
096        test("longMul", Long.MAX_VALUE, Long.MAX_VALUE);
097        test("longMul", Long.MIN_VALUE, Long.MIN_VALUE);
098        test("longMul", Long.MIN_VALUE, Long.MAX_VALUE);
099    }
100
101    @Test
102    public void testLongSub() {
103        test("longSub", (long) Integer.MIN_VALUE, 2L);
104        test("longSub", Long.MIN_VALUE, 2L);
105    }
106
107    @Test
108    public void testLongMulHigh() {
109        test("longMulHigh", 7L, 15L);
110        test("longMulHigh", Long.MAX_VALUE, 15L);
111        test("longMulHigh", 15L, Long.MAX_VALUE);
112        test("longMulHigh", Long.MAX_VALUE, Long.MAX_VALUE);
113        test("longMulHigh", Long.MIN_VALUE, 15L);
114        test("longMulHigh", 15L, Long.MIN_VALUE);
115        test("longMulHigh", Long.MIN_VALUE, Long.MIN_VALUE);
116    }
117
118    @Test
119    public void testLongMulHighUnsigned() {
120        test("longMulHighUnsigned", 7L, 15L);
121        test("longMulHighUnsigned", Long.MAX_VALUE, 15L);
122        test("longMulHighUnsigned", Long.MIN_VALUE, 15L);
123    }
124
125    public static int add(int a, int b) {
126        return ExactMath.addExact(a, b);
127    }
128
129    public static int mul(int a, int b) {
130        return ExactMath.multiplyExact(a, b);
131    }
132
133    public static int sub(int a, int b) {
134        return ExactMath.subtractExact(a, b);
135    }
136
137    public static int mulHigh(int a, int b) {
138        return ExactMath.multiplyHigh(a, b);
139    }
140
141    public static int mulHighUnsigned(int a, int b) {
142        return ExactMath.multiplyHighUnsigned(a, b);
143    }
144
145    public static long longAdd(long a, long b) {
146        return ExactMath.addExact(a, b);
147    }
148
149    public static long longMul(long a, long b) {
150        return ExactMath.multiplyExact(a, b);
151    }
152
153    public static long longSub(long a, long b) {
154        return ExactMath.subtractExact(a, b);
155    }
156
157    public static long longMulHigh(long a, long b) {
158        return ExactMath.multiplyHigh(a, b);
159    }
160
161    public static long longMulHighUnsigned(long a, long b) {
162        return ExactMath.multiplyHighUnsigned(a, b);
163    }
164}