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 isGreaterThan(long unsignedValue) {
  47         return Long.compareUnsigned(value, unsignedValue) > 0;
  48     }
  49 
  50     public boolean isLessOrEqualTo(long unsignedValue) {
  51         return Long.compareUnsigned(value, unsignedValue) <= 0;
  52     }
  53 
  54     public UnsignedLong times(long unsignedValue) {
  55         if (unsignedValue != 0 && Long.compareUnsigned(value, Long.divideUnsigned(0xffff_ffff_ffff_ffffL, unsignedValue)) > 0) {
  56             throw new ArithmeticException();
  57         }
  58         return new UnsignedLong(value * unsignedValue);
  59     }
  60 
  61     public UnsignedLong minus(long unsignedValue) {
  62         if (Long.compareUnsigned(value, unsignedValue) < 0) {
  63             throw new ArithmeticException();
  64         }
  65         return new UnsignedLong(value - unsignedValue);
  66     }
  67 
  68     public UnsignedLong plus(long unsignedValue) {
  69         if (Long.compareUnsigned(0xffff_ffff_ffff_ffffL - unsignedValue, value) < 0) {
  70             throw new ArithmeticException();
  71         }
  72         return new UnsignedLong(value + unsignedValue);
  73     }
  74 
  75     public UnsignedLong wrappingPlus(long unsignedValue) {
  76         return new UnsignedLong(value + unsignedValue);
  77     }
  78 
  79     public UnsignedLong wrappingTimes(long unsignedValue) {
  80         return new UnsignedLong(value * unsignedValue);
  81     }
  82 
  83     @Override
  84     public String toString() {
  85         return "UnsignedLong(" + Long.toUnsignedString(value) + ")";
  86     }
  87 
  88     @Override
  89     public boolean equals(Object o) {
  90         if (this == o) {
  91             return true;
  92         }
  93         if (o == null || getClass() != o.getClass()) {
  94             return false;
  95         }
  96         UnsignedLong that = (UnsignedLong) o;
  97         return value == that.value;
  98     }
  99 
 100     @Override
 101     public int hashCode() {
 102         return Long.hashCode(value);
 103     }
 104 }