Skip to content

Commit 59ff18f

Browse files
committed
expand operations supported by org.graalvm.word.Word
1 parent eae43aa commit 59ff18f

File tree

6 files changed

+634
-81
lines changed

6 files changed

+634
-81
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright (c) 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.graal.compiler.test;
26+
27+
import static jdk.graal.compiler.core.common.calc.UnsignedMath.aboveOrEqual;
28+
import static jdk.graal.compiler.core.common.calc.UnsignedMath.aboveThan;
29+
import static jdk.graal.compiler.core.common.calc.UnsignedMath.belowOrEqual;
30+
import static jdk.graal.compiler.core.common.calc.UnsignedMath.belowThan;
31+
32+
import org.graalvm.word.Pointer;
33+
import org.graalvm.word.SignedWord;
34+
import org.graalvm.word.UnsignedWord;
35+
import org.junit.Assert;
36+
import org.junit.Test;
37+
38+
import java.util.List;
39+
import java.util.stream.LongStream;
40+
import java.util.stream.Stream;
41+
42+
/**
43+
* Tests word operations on boxed values produced by {@link org.graalvm.word.WordFactory} and
44+
* {@link jdk.graal.compiler.word.WordFactory}.
45+
*/
46+
public class WordTests {
47+
48+
static long[] words = {
49+
Long.MIN_VALUE,
50+
Long.MIN_VALUE + 1,
51+
-1L,
52+
0L,
53+
1L,
54+
Long.MAX_VALUE - 1,
55+
Long.MAX_VALUE,
56+
Integer.MAX_VALUE - 1L,
57+
Integer.MAX_VALUE,
58+
Integer.MAX_VALUE + 1L,
59+
Integer.MIN_VALUE - 1L,
60+
Integer.MIN_VALUE,
61+
Integer.MIN_VALUE + 1L
62+
};
63+
64+
static SignedWord graalSigned(long val) {
65+
return jdk.graal.compiler.word.WordFactory.signed(val);
66+
}
67+
68+
static UnsignedWord graalUnsigned(long val) {
69+
return jdk.graal.compiler.word.WordFactory.unsigned(val);
70+
}
71+
72+
static Pointer graalPointer(long val) {
73+
return jdk.graal.compiler.word.WordFactory.pointer(val);
74+
}
75+
76+
static List<SignedWord> signedWords = Stream.concat(
77+
LongStream.of(words).mapToObj(org.graalvm.word.WordFactory::signed),
78+
LongStream.of(words).mapToObj(WordTests::graalSigned)).toList();
79+
static List<UnsignedWord> unsignedWords = Stream.concat(
80+
LongStream.of(words).mapToObj(org.graalvm.word.WordFactory::unsigned),
81+
LongStream.of(words).mapToObj(WordTests::graalUnsigned)).toList();
82+
static List<Pointer> pointers = Stream.concat(
83+
LongStream.of(words).mapToObj(org.graalvm.word.WordFactory::pointer),
84+
LongStream.of(words).mapToObj(WordTests::graalPointer)).toList();
85+
86+
@Test
87+
public void testSigned() {
88+
for (var x : signedWords) {
89+
Assert.assertEquals(x.not().rawValue(), ~x.rawValue());
90+
91+
for (var y : signedWords) {
92+
Assert.assertEquals(x.equal(y), x.rawValue() == y.rawValue());
93+
Assert.assertEquals(x.notEqual(y), x.rawValue() != y.rawValue());
94+
95+
Assert.assertEquals(x.add(y).rawValue(), x.rawValue() + y.rawValue());
96+
Assert.assertEquals(x.add(y).rawValue(), x.rawValue() + y.rawValue());
97+
Assert.assertEquals(x.subtract(y).rawValue(), x.rawValue() - y.rawValue());
98+
Assert.assertEquals(x.multiply(y).rawValue(), x.rawValue() * y.rawValue());
99+
100+
if (y.rawValue() != 0) {
101+
Assert.assertEquals(x.signedDivide(y).rawValue(), x.rawValue() / y.rawValue());
102+
Assert.assertEquals(x.signedRemainder(y).rawValue(), x.rawValue() % y.rawValue());
103+
}
104+
Assert.assertEquals(x.and(y).rawValue(), x.rawValue() & y.rawValue());
105+
Assert.assertEquals(x.or(y).rawValue(), x.rawValue() | y.rawValue());
106+
Assert.assertEquals(x.xor(y).rawValue(), x.rawValue() ^ y.rawValue());
107+
108+
Assert.assertEquals(x.greaterThan(y), x.rawValue() > y.rawValue());
109+
Assert.assertEquals(x.greaterOrEqual(y), x.rawValue() >= y.rawValue());
110+
Assert.assertEquals(x.lessThan(y), x.rawValue() < y.rawValue());
111+
Assert.assertEquals(x.lessOrEqual(y), x.rawValue() <= y.rawValue());
112+
113+
Assert.assertEquals(x.shiftLeft((UnsignedWord) y).rawValue(), x.rawValue() << y.rawValue());
114+
Assert.assertEquals(x.signedShiftRight((UnsignedWord) y).rawValue(), x.rawValue() >> y.rawValue());
115+
}
116+
}
117+
}
118+
119+
@Test
120+
public void testUnsigned() {
121+
for (var x : unsignedWords) {
122+
Assert.assertEquals(x.not().rawValue(), ~x.rawValue());
123+
124+
for (var y : unsignedWords) {
125+
Assert.assertEquals(x.equal(y), x.rawValue() == y.rawValue());
126+
Assert.assertEquals(x.notEqual(y), x.rawValue() != y.rawValue());
127+
128+
Assert.assertEquals(x.add(y).rawValue(), x.rawValue() + y.rawValue());
129+
Assert.assertEquals(x.subtract(y).rawValue(), x.rawValue() - y.rawValue());
130+
Assert.assertEquals(x.multiply(y).rawValue(), x.rawValue() * y.rawValue());
131+
132+
if (y.rawValue() != 0) {
133+
Assert.assertEquals(x.unsignedDivide(y).rawValue(), Long.divideUnsigned(x.rawValue(), y.rawValue()));
134+
Assert.assertEquals(x.unsignedRemainder(y).rawValue(), Long.remainderUnsigned(x.rawValue(), y.rawValue()));
135+
}
136+
Assert.assertEquals(x.and(y).rawValue(), x.rawValue() & y.rawValue());
137+
Assert.assertEquals(x.or(y).rawValue(), x.rawValue() | y.rawValue());
138+
Assert.assertEquals(x.xor(y).rawValue(), x.rawValue() ^ y.rawValue());
139+
140+
Assert.assertEquals(x.aboveThan(y), aboveThan(x.rawValue(), y.rawValue()));
141+
Assert.assertEquals(x.aboveOrEqual(y), aboveOrEqual(x.rawValue(), y.rawValue()));
142+
Assert.assertEquals(x.belowThan(y), belowThan(x.rawValue(), y.rawValue()));
143+
Assert.assertEquals(x.belowOrEqual(y), belowOrEqual(x.rawValue(), y.rawValue()));
144+
145+
Assert.assertEquals(x.shiftLeft(y).rawValue(), x.rawValue() << y.rawValue());
146+
Assert.assertEquals(x.unsignedShiftRight(y).rawValue(), x.rawValue() >>> y.rawValue());
147+
}
148+
}
149+
}
150+
151+
@Test
152+
public void testPointer() {
153+
for (var x : pointers) {
154+
Assert.assertEquals(x.not().rawValue(), ~x.rawValue());
155+
Assert.assertEquals(x.isNull(), x.rawValue() == 0);
156+
Assert.assertEquals(x.isNonNull(), x.rawValue() != 0);
157+
158+
for (var y : pointers) {
159+
Assert.assertEquals(x.equal(y), x.rawValue() == y.rawValue());
160+
Assert.assertEquals(x.notEqual(y), x.rawValue() != y.rawValue());
161+
162+
Assert.assertEquals(x.add(y).rawValue(), x.rawValue() + y.rawValue());
163+
Assert.assertEquals(x.subtract(y).rawValue(), x.rawValue() - y.rawValue());
164+
Assert.assertEquals(x.multiply(y).rawValue(), x.rawValue() * y.rawValue());
165+
166+
if (y.rawValue() != 0) {
167+
Assert.assertEquals(x.unsignedDivide(y).rawValue(), Long.divideUnsigned(x.rawValue(), y.rawValue()));
168+
Assert.assertEquals(x.unsignedRemainder(y).rawValue(), Long.remainderUnsigned(x.rawValue(), y.rawValue()));
169+
}
170+
Assert.assertEquals(x.and(y).rawValue(), x.rawValue() & y.rawValue());
171+
Assert.assertEquals(x.or(y).rawValue(), x.rawValue() | y.rawValue());
172+
Assert.assertEquals(x.xor(y).rawValue(), x.rawValue() ^ y.rawValue());
173+
174+
Assert.assertEquals(x.aboveThan(y), aboveThan(x.rawValue(), y.rawValue()));
175+
Assert.assertEquals(x.aboveOrEqual(y), aboveOrEqual(x.rawValue(), y.rawValue()));
176+
Assert.assertEquals(x.belowThan(y), belowThan(x.rawValue(), y.rawValue()));
177+
Assert.assertEquals(x.belowOrEqual(y), belowOrEqual(x.rawValue(), y.rawValue()));
178+
179+
Assert.assertEquals(x.shiftLeft(y).rawValue(), x.rawValue() << y.rawValue());
180+
Assert.assertEquals(x.unsignedShiftRight(y).rawValue(), x.rawValue() >>> y.rawValue());
181+
}
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)