changeset 22740:a5ff8a589802

Add utility classes and methods for Word types
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 30 Sep 2015 12:57:48 -0700
parents f7693dc4e341
children b67cd5b560be a19722cd3dd2
files graal/com.oracle.graal.word/src/com/oracle/graal/word/AtomicUnsigned.java graal/com.oracle.graal.word/src/com/oracle/graal/word/AtomicWord.java graal/com.oracle.graal.word/src/com/oracle/graal/word/PointerUtils.java graal/com.oracle.graal.word/src/com/oracle/graal/word/UnsignedUtils.java
diffstat 4 files changed, 387 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.word/src/com/oracle/graal/word/AtomicUnsigned.java	Wed Sep 30 12:57:48 2015 -0700
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2015, 2015, 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.
+ */
+package com.oracle.graal.word;
+
+/**
+ * A {@link Unsigned} value that may be updated atomically. See the
+ * {@link java.util.concurrent.atomic} package specification for description of the properties of
+ * atomic variables.
+ */
+public class AtomicUnsigned extends AtomicWord<Unsigned> {
+
+    /**
+     * Atomically adds the given value to the current value.
+     *
+     * @param delta the value to add
+     * @return the previous value
+     */
+    public final Unsigned getAndAdd(Unsigned delta) {
+        return Word.unsigned(value.getAndAdd(delta.rawValue()));
+    }
+
+    /**
+     * Atomically adds the given value to the current value.
+     *
+     * @param delta the value to add
+     * @return the updated value
+     */
+    public final Unsigned addAndGet(Unsigned delta) {
+        return Word.unsigned(value.addAndGet(delta.rawValue()));
+    }
+
+    /**
+     * Atomically subtracts the given value from the current value.
+     *
+     * @param delta the value to add
+     * @return the previous value
+     */
+    public final Unsigned getAndSubtract(Unsigned delta) {
+        return Word.unsigned(value.getAndAdd(-delta.rawValue()));
+    }
+
+    /**
+     * Atomically subtracts the given value from the current value.
+     *
+     * @param delta the value to add
+     * @return the updated value
+     */
+    public final Unsigned subtractAndGet(Unsigned delta) {
+        return Word.unsigned(value.addAndGet(-delta.rawValue()));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.word/src/com/oracle/graal/word/AtomicWord.java	Wed Sep 30 12:57:48 2015 -0700
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2015, 2015, 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.
+ */
+package com.oracle.graal.word;
+
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A {@link WordBase word} value that may be updated atomically. See the
+ * {@link java.util.concurrent.atomic} package specification for description of the properties of
+ * atomic variables.
+ *
+ * Similar to {@link AtomicReference}, but for {@link WordBase word} types. A dedicated
+ * implementation is necessary because Object and word types cannot be mixed.
+ */
+public class AtomicWord<T extends WordBase> {
+
+    /**
+     * For simplicity, we convert the word value to a long and delegate to existing atomic
+     * operations.
+     */
+    protected final AtomicLong value;
+
+    /**
+     * Creates a new AtomicLong with initial value {@link Word#zero}.
+     */
+    public AtomicWord() {
+        value = new AtomicLong();
+    }
+
+    /**
+     * Gets the current value.
+     *
+     * @return the current value
+     */
+    @SuppressWarnings("unchecked")
+    public final T get() {
+        return (T) Word.unsigned(value.get());
+    }
+
+    /**
+     * Sets to the given value.
+     *
+     * @param newValue the new value
+     */
+    public final void set(T newValue) {
+        value.set(newValue.rawValue());
+    }
+
+    /**
+     * Atomically sets to the given value and returns the old value.
+     *
+     * @param newValue the new value
+     * @return the previous value
+     */
+    @SuppressWarnings("unchecked")
+    public final T getAndSet(T newValue) {
+        return (T) Word.unsigned(value.getAndSet(newValue.rawValue()));
+    }
+
+    /**
+     * Atomically sets the value to the given updated value if the current value {@code ==} the
+     * expected value.
+     *
+     * @param expect the expected value
+     * @param update the new value
+     * @return {@code true} if successful. False return indicates that the actual value was not
+     *         equal to the expected value.
+     */
+    public final boolean compareAndSet(T expect, T update) {
+        return value.compareAndSet(expect.rawValue(), update.rawValue());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.word/src/com/oracle/graal/word/PointerUtils.java	Wed Sep 30 12:57:48 2015 -0700
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2015, 2015, 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.
+ */
+package com.oracle.graal.word;
+
+/**
+ * Utility methods on Pointers.
+ */
+public final class PointerUtils {
+
+    private PointerUtils() {
+        // This is a class of static methods, so no need for any instances.
+    }
+
+    /**
+     * The value of a null Pointer.
+     *
+     * @return A null Pointer value.
+     */
+    @SuppressWarnings("unchecked")
+    public static <T extends PointerBase> T nullPointer() {
+        return (T) Word.zero();
+    }
+
+    /**
+     * Predicate to check for the null Pointer value.
+     *
+     * @return Whether that Pointer is the null Pointer.
+     */
+    public static boolean isNull(ComparableWord that) {
+        return that.equal(nullPointer());
+    }
+
+    /**
+     * Predicate to check for a non-null Pointer value.
+     *
+     * @return Whether that Pointer is not the null Pointer.
+     */
+    public static boolean isNonNull(ComparableWord that) {
+        return that.notEqual(nullPointer());
+    }
+
+    /**
+     * Round a Pointer down to the nearest smaller multiple.
+     *
+     * @param that The Pointer to be rounded up.
+     * @param multiple The multiple to which that Pointer should be decreased.
+     * @return That Pointer, but rounded down.
+     */
+    public static Pointer roundDown(PointerBase that, Unsigned multiple) {
+        return (Pointer) UnsignedUtils.roundDown((Unsigned) that, multiple);
+    }
+
+    /**
+     * Round a Pointer up to the nearest larger multiple.
+     *
+     * @param that The Pointer to be rounded up.
+     * @param multiple The multiple to which that Pointer should be increased.
+     * @return That Pointer, but rounded up.
+     */
+    public static Pointer roundUp(PointerBase that, Unsigned multiple) {
+        return (Pointer) UnsignedUtils.roundUp((Unsigned) that, multiple);
+    }
+
+    /**
+     * Check that a Pointer is an even multiple.
+     *
+     * @param that The Pointer to be verified as a multiple.
+     * @param multiple The multiple against which the Pointer should be verified.
+     * @return true if that Pointer is a multiple, false otherwise.
+     */
+    public static boolean isAMultiple(PointerBase that, Unsigned multiple) {
+        return that.equal(PointerUtils.roundDown(that, multiple));
+    }
+
+    /**
+     * Return the distance between two Pointers.
+     *
+     * @param pointer1 A first Pointer.
+     * @param pointer2 A second Pointer.
+     * @return The distance in bytes between the two Pointers.
+     */
+    public static Unsigned absoluteDifference(PointerBase pointer1, PointerBase pointer2) {
+        Pointer p1 = (Pointer) pointer1;
+        Pointer p2 = (Pointer) pointer2;
+        final Unsigned result;
+        if (p1.aboveOrEqual(p2)) {
+            result = p1.subtract(p2);
+        } else {
+            result = p2.subtract(p1);
+        }
+        return result;
+    }
+
+    /**
+     * The minimum of two Pointers.
+     *
+     * @param x A Pointer.
+     * @param y Another Pointer.
+     * @return The whichever Pointer is smaller.
+     */
+    public static <T extends PointerBase> T min(T x, T y) {
+        return (((Pointer) x).belowOrEqual((Pointer) y)) ? x : y;
+    }
+
+    /**
+     * The maximum of two Pointers.
+     *
+     * @param x A Pointer.
+     * @param y Another Pointer.
+     * @return The whichever Pointer is larger.
+     */
+    public static <T extends PointerBase> T max(T x, T y) {
+        return (((Pointer) x).aboveOrEqual((Pointer) y)) ? x : y;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.word/src/com/oracle/graal/word/UnsignedUtils.java	Wed Sep 30 12:57:48 2015 -0700
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2015, 2015, 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.
+ */
+package com.oracle.graal.word;
+
+/**
+ * Utility methods on Unsigned values.
+ */
+public final class UnsignedUtils {
+
+    private UnsignedUtils() {
+        // This is a class of static methods, so no need for any instances.
+    }
+
+    /**
+     * Round an Unsigned down to the nearest smaller multiple.
+     *
+     * @param that The Unsigned to be rounded down.
+     * @param multiple The multiple to which that Unsigned should be decreased.
+     * @return That Unsigned, but rounded down.
+     */
+    public static Unsigned roundDown(Unsigned that, Unsigned multiple) {
+        return that.unsignedDivide(multiple).multiply(multiple);
+    }
+
+    /**
+     * Round an Unsigned up to the nearest larger multiple.
+     *
+     * @param that The Unsigned to be rounded up.
+     * @param multiple The multiple to which that Unsigned should be increased.
+     * @return That Unsigned, but rounded up.
+     */
+    public static Unsigned roundUp(Unsigned that, Unsigned multiple) {
+        return UnsignedUtils.roundDown(that.add(multiple.subtract(1)), multiple);
+    }
+
+    /**
+     * Check that an Unsigned is an even multiple.
+     *
+     * @param that The Unsigned to be verified as a multiple.
+     * @param multiple The multiple against which the Unsigned should be verified.
+     * @return true if that Unsigned is a multiple, false otherwise.
+     */
+    public static boolean isAMultiple(Unsigned that, Unsigned multiple) {
+        return that.equal(UnsignedUtils.roundDown(that, multiple));
+    }
+
+    /**
+     * The minimum of two Unsigneds.
+     *
+     * @param x An Unsigned.
+     * @param y Another Unsigned.
+     * @return The whichever Unsigned is smaller.
+     */
+    public static Unsigned min(Unsigned x, Unsigned y) {
+        return (x.belowOrEqual(y)) ? x : y;
+    }
+
+    /**
+     * The maximum of two Unsigneds.
+     *
+     * @param x An Unsigned.
+     * @param y Another Unsigned.
+     * @return The whichever Unsigned is larger.
+     */
+    public static Unsigned max(Unsigned x, Unsigned y) {
+        return (x.aboveOrEqual(y)) ? x : y;
+    }
+}