1/*
2 * Copyright (c) 2015, 2015, 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.word;
24
25/**
26 * Utility methods on Unsigned values.
27 */
28public final class UnsignedUtils {
29
30    private UnsignedUtils() {
31        // This is a class of static methods, so no need for any instances.
32    }
33
34    /**
35     * Round an Unsigned down to the nearest smaller multiple.
36     *
37     * @param that The Unsigned to be rounded down.
38     * @param multiple The multiple to which that Unsigned should be decreased.
39     * @return That Unsigned, but rounded down.
40     */
41    public static Unsigned roundDown(Unsigned that, Unsigned multiple) {
42        return that.unsignedDivide(multiple).multiply(multiple);
43    }
44
45    /**
46     * Round an Unsigned up to the nearest larger multiple.
47     *
48     * @param that The Unsigned to be rounded up.
49     * @param multiple The multiple to which that Unsigned should be increased.
50     * @return That Unsigned, but rounded up.
51     */
52    public static Unsigned roundUp(Unsigned that, Unsigned multiple) {
53        return UnsignedUtils.roundDown(that.add(multiple.subtract(1)), multiple);
54    }
55
56    /**
57     * Check that an Unsigned is an even multiple.
58     *
59     * @param that The Unsigned to be verified as a multiple.
60     * @param multiple The multiple against which the Unsigned should be verified.
61     * @return true if that Unsigned is a multiple, false otherwise.
62     */
63    public static boolean isAMultiple(Unsigned that, Unsigned multiple) {
64        return that.equal(UnsignedUtils.roundDown(that, multiple));
65    }
66
67    /**
68     * The minimum of two Unsigneds.
69     *
70     * @param x An Unsigned.
71     * @param y Another Unsigned.
72     * @return The whichever Unsigned is smaller.
73     */
74    public static Unsigned min(Unsigned x, Unsigned y) {
75        return (x.belowOrEqual(y)) ? x : y;
76    }
77
78    /**
79     * The maximum of two Unsigneds.
80     *
81     * @param x An Unsigned.
82     * @param y Another Unsigned.
83     * @return The whichever Unsigned is larger.
84     */
85    public static Unsigned max(Unsigned x, Unsigned y) {
86        return (x.aboveOrEqual(y)) ? x : y;
87    }
88}
89