WordTest.java revision 13083:b9a173f12fe6
1/* 2 * Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.graalvm.compiler.replacements.test; 24 25import org.graalvm.api.word.Pointer; 26import org.graalvm.api.word.Unsigned; 27import org.graalvm.api.word.WordBase; 28import org.graalvm.api.word.WordFactory; 29import org.graalvm.compiler.api.replacements.Snippet; 30import org.graalvm.compiler.core.common.CompilationIdentifier; 31import org.graalvm.compiler.nodes.StructuredGraph; 32import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 33import org.graalvm.compiler.options.OptionValues; 34import org.graalvm.compiler.word.Word; 35import org.junit.Test; 36 37import jdk.vm.ci.meta.ResolvedJavaMethod; 38 39/** 40 * Tests for the {@link Word} type. 41 */ 42public class WordTest extends SnippetsTest { 43 44 @Override 45 protected StructuredGraph parseEager(ResolvedJavaMethod m, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, OptionValues options) { 46 // create a copy to assign a valid compilation id 47 return installer.makeGraph(bytecodeProvider, m, null, null).copyWithIdentifier(compilationId); 48 } 49 50 @Test 51 public void construction() { 52 long[] words = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1L, 0L, 1L, Long.MAX_VALUE - 1, Long.MAX_VALUE, Integer.MAX_VALUE - 1L, Integer.MAX_VALUE, Integer.MAX_VALUE + 1L, 53 Integer.MIN_VALUE - 1L, Integer.MIN_VALUE, Integer.MIN_VALUE + 1L}; 54 for (long word : words) { 55 test("unsignedLong", word); 56 test("unsignedInt", (int) word); 57 test("signedLong", word); 58 test("signedInt", (int) word); 59 } 60 } 61 62 @Test 63 public void testArithmetic() { 64 long[] words = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1L, 0L, 1L, Long.MAX_VALUE - 1, Long.MAX_VALUE, Integer.MAX_VALUE - 1L, Integer.MAX_VALUE, Integer.MAX_VALUE + 1L, 65 Integer.MIN_VALUE - 1L, Integer.MIN_VALUE, Integer.MIN_VALUE + 1L}; 66 for (long word : words) { 67 test("unsignedNot", word); 68 test("signedNot", word); 69 for (long addend : words) { 70 test("unsignedPlusInt", word, (int) addend); 71 test("unsignedMinusInt", word, (int) addend); 72 test("unsignedPlusInt", word, -((int) addend)); 73 test("unsignedMinusInt", word, -((int) addend)); 74 test("unsignedPlusLong", word, addend); 75 test("unsignedMinusLong", word, addend); 76 test("unsignedPlusLong", word, -addend); 77 test("unsignedMinusLong", word, -addend); 78 test("signedPlusInt", word, (int) addend); 79 test("signedMinusInt", word, (int) addend); 80 test("signedPlusInt", word, -((int) addend)); 81 test("signedMinusInt", word, -((int) addend)); 82 test("signedPlusLong", word, addend); 83 test("signedMinusLong", word, addend); 84 test("signedPlusLong", word, -addend); 85 test("signedMinusLong", word, -addend); 86 87 test("andInt", word, (int) addend); 88 test("orInt", word, (int) addend); 89 test("andInt", word, -((int) addend)); 90 test("orInt", word, -((int) addend)); 91 test("andLong", word, addend); 92 test("orLong", word, addend); 93 test("andLong", word, -addend); 94 test("orLong", word, -addend); 95 } 96 } 97 } 98 99 @Test 100 public void testCompare() { 101 long[] words = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1L, 0L, 1L, Long.MAX_VALUE - 1, Long.MAX_VALUE}; 102 for (long word1 : words) { 103 for (long word2 : words) { 104 for (String method : new String[]{"aboveOrEqual", "above", "belowOrEqual", "below"}) { 105 test(method, word1, word2); 106 test(method, word2, word1); 107 } 108 } 109 } 110 } 111 112 @Test 113 public void testCast() { 114 test("cast", 1234L); 115 } 116 117 @Snippet 118 public static long cast(long input) { 119 WordBase base = WordFactory.signed(input); 120 Unsigned unsigned = (Unsigned) base; 121 Pointer pointer = (Pointer) unsigned; 122 Word word = (Word) pointer; 123 return word.rawValue(); 124 } 125 126 @Snippet 127 public static long unsignedLong(long word) { 128 return WordFactory.unsigned(word).rawValue(); 129 } 130 131 @Snippet 132 public static long unsignedInt(int word) { 133 return WordFactory.unsigned(word).rawValue(); 134 } 135 136 @Snippet 137 public static long signedLong(long word) { 138 return WordFactory.signed(word).rawValue(); 139 } 140 141 @Snippet 142 public static long signedInt(int word) { 143 return WordFactory.signed(word).rawValue(); 144 } 145 146 @Snippet 147 public static long unsignedPlusInt(long word, int addend) { 148 return WordFactory.unsigned(word).add(addend).rawValue(); 149 } 150 151 @Snippet 152 public static long unsignedMinusInt(long word, int addend) { 153 return WordFactory.unsigned(word).subtract(addend).rawValue(); 154 } 155 156 @Snippet 157 public static long unsignedPlusLong(long word, long addend) { 158 return WordFactory.unsigned(word).add(WordFactory.unsigned(addend)).rawValue(); 159 } 160 161 @Snippet 162 public static long unsignedMinusLong(long word, long addend) { 163 return WordFactory.unsigned(word).subtract(WordFactory.unsigned(addend)).rawValue(); 164 } 165 166 @Snippet 167 public static long signedPlusInt(long word, int addend) { 168 return WordFactory.signed(word).add(addend).rawValue(); 169 } 170 171 @Snippet 172 public static long signedMinusInt(long word, int addend) { 173 return WordFactory.signed(word).subtract(addend).rawValue(); 174 } 175 176 @Snippet 177 public static long signedPlusLong(long word, long addend) { 178 return WordFactory.signed(word).add(WordFactory.signed(addend)).rawValue(); 179 } 180 181 @Snippet 182 public static long signedMinusLong(long word, long addend) { 183 return WordFactory.signed(word).subtract(WordFactory.signed(addend)).rawValue(); 184 } 185 186 @Snippet 187 public static long signedNot(long word) { 188 return WordFactory.signed(word).not().rawValue(); 189 } 190 191 @Snippet 192 public static long unsignedNot(long word) { 193 return WordFactory.unsigned(word).not().rawValue(); 194 } 195 196 @Snippet 197 public static boolean aboveOrEqual(long word1, long word2) { 198 return WordFactory.unsigned(word1).aboveOrEqual(WordFactory.unsigned(word2)); 199 } 200 201 @Snippet 202 public static boolean above(long word1, long word2) { 203 return WordFactory.unsigned(word1).aboveThan(WordFactory.unsigned(word2)); 204 } 205 206 @Snippet 207 public static boolean belowOrEqual(long word1, long word2) { 208 return WordFactory.unsigned(word1).belowOrEqual(WordFactory.unsigned(word2)); 209 } 210 211 @Snippet 212 public static boolean below(long word1, long word2) { 213 return WordFactory.unsigned(word1).belowThan(WordFactory.unsigned(word2)); 214 } 215 216 @Snippet 217 public static long andInt(long word, int addend) { 218 return WordFactory.unsigned(word).and(addend).rawValue(); 219 } 220 221 @Snippet 222 public static long orInt(long word, int addend) { 223 return WordFactory.unsigned(word).or(addend).rawValue(); 224 } 225 226 @Snippet 227 public static long andLong(long word, long addend) { 228 return WordFactory.unsigned(word).and(WordFactory.unsigned(addend)).rawValue(); 229 } 230 231 @Snippet 232 public static long orLong(long word, long addend) { 233 return WordFactory.unsigned(word).or(WordFactory.unsigned(addend)).rawValue(); 234 } 235} 236