1 /* 2 * Copyright (c) 2018, 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 */ 23 24 25 package org.graalvm.compiler.core.common.util; 26 27 public final class UnsignedLong { 28 private final long value; 29 30 public UnsignedLong(long value) { 31 this.value = value; 32 } 33 34 public long asLong() { 35 return value; 36 } 37 38 public boolean equals(long unsignedValue) { 39 return value == unsignedValue; 40 } 41 42 public boolean isLessThan(long unsignedValue) { 43 return Long.compareUnsigned(value, unsignedValue) < 0; 44 } 45 46 public boolean isLessOrEqualTo(long unsignedValue) { 47 return Long.compareUnsigned(value, unsignedValue) <= 0; 48 } 49 50 public UnsignedLong times(long unsignedValue) { 51 if (unsignedValue != 0 && Long.compareUnsigned(value, Long.divideUnsigned(0xffff_ffff_ffff_ffffL, unsignedValue)) > 0) { 52 throw new ArithmeticException(); 53 } 54 return new UnsignedLong(value * unsignedValue); 55 } 56 57 public UnsignedLong minus(long unsignedValue) { 58 if (Long.compareUnsigned(value, unsignedValue) < 0) { 59 throw new ArithmeticException(); 60 } 61 return new UnsignedLong(value - unsignedValue); 62 } 63 64 public UnsignedLong plus(long unsignedValue) { 65 if (Long.compareUnsigned(0xffff_ffff_ffff_ffffL - unsignedValue, value) < 0) { 66 throw new ArithmeticException(); 67 } 68 return new UnsignedLong(value + unsignedValue); 69 } 70 71 public UnsignedLong wrappingPlus(long unsignedValue) { 72 return new UnsignedLong(value + unsignedValue); 73 } 74 75 public UnsignedLong wrappingTimes(long unsignedValue) { 76 return new UnsignedLong(value * unsignedValue); 77 } 78 79 @Override 80 public String toString() { 81 return "UnsignedLong(" + Long.toUnsignedString(value) + ")"; 82 } 83 84 @Override 85 public boolean equals(Object o) { 86 if (this == o) { 87 return true; 88 } 89 if (o == null || getClass() != o.getClass()) { 90 return false; 91 } 92 UnsignedLong that = (UnsignedLong) o; 93 return value == that.value; 94 } 95 96 @Override 97 public int hashCode() { 98 return Long.hashCode(value); 99 } 100 }