1 /*
   2  * Copyright (c) 2011, 2011, 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 package jdk.internal.jvmci.code;
  24 
  25 import java.math.*;
  26 
  27 //JaCoCo Exclude
  28 
  29 /**
  30  * Utilities for unsigned comparisons. All methods have correct, but slow, standard Java
  31  * implementations so that they can be used with compilers not supporting the intrinsics.
  32  */
  33 public class UnsignedMath {
  34 
  35     private static final long MASK = 0xffffffffL;
  36 
  37     /**
  38      * Unsigned comparison aboveThan for two numbers.
  39      */
  40     public static boolean aboveThan(int a, int b) {
  41         return (a & MASK) > (b & MASK);
  42     }
  43 
  44     /**
  45      * Unsigned comparison aboveOrEqual for two numbers.
  46      */
  47     public static boolean aboveOrEqual(int a, int b) {
  48         return (a & MASK) >= (b & MASK);
  49     }
  50 
  51     /**
  52      * Unsigned comparison belowThan for two numbers.
  53      */
  54     public static boolean belowThan(int a, int b) {
  55         return (a & MASK) < (b & MASK);
  56     }
  57 
  58     /**
  59      * Unsigned comparison belowOrEqual for two numbers.
  60      */
  61     public static boolean belowOrEqual(int a, int b) {
  62         return (a & MASK) <= (b & MASK);
  63     }
  64 
  65     /**
  66      * Unsigned comparison aboveThan for two numbers.
  67      */
  68     public static boolean aboveThan(long a, long b) {
  69         return (a > b) ^ ((a < 0) != (b < 0));
  70     }
  71 
  72     /**
  73      * Unsigned comparison aboveOrEqual for two numbers.
  74      */
  75     public static boolean aboveOrEqual(long a, long b) {
  76         return (a >= b) ^ ((a < 0) != (b < 0));
  77     }
  78 
  79     /**
  80      * Unsigned comparison belowThan for two numbers.
  81      */
  82     public static boolean belowThan(long a, long b) {
  83         return (a < b) ^ ((a < 0) != (b < 0));
  84     }
  85 
  86     /**
  87      * Unsigned comparison belowOrEqual for two numbers.
  88      */
  89     public static boolean belowOrEqual(long a, long b) {
  90         return (a <= b) ^ ((a < 0) != (b < 0));
  91     }
  92 
  93     /**
  94      * Unsigned division for two numbers.
  95      */
  96     public static int divide(int a, int b) {
  97         return (int) ((a & MASK) / (b & MASK));
  98     }
  99 
 100     /**
 101      * Unsigned remainder for two numbers.
 102      */
 103     public static int remainder(int a, int b) {
 104         return (int) ((a & MASK) % (b & MASK));
 105     }
 106 
 107     /**
 108      * Unsigned division for two numbers.
 109      */
 110     public static long divide(long a, long b) {
 111         return bi(a).divide(bi(b)).longValue();
 112     }
 113 
 114     /**
 115      * Unsigned remainder for two numbers.
 116      */
 117     public static long remainder(long a, long b) {
 118         return bi(a).remainder(bi(b)).longValue();
 119     }
 120 
 121     private static BigInteger bi(long unsigned) {
 122         return unsigned >= 0 ? BigInteger.valueOf(unsigned) : BigInteger.valueOf(unsigned & 0x7fffffffffffffffL).setBit(63);
 123     }
 124 }