001/* 002 * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package jdk.internal.jvmci.code; 024 025import java.math.*; 026 027//JaCoCo Exclude 028 029/** 030 * Utilities for unsigned comparisons. All methods have correct, but slow, standard Java 031 * implementations so that they can be used with compilers not supporting the intrinsics. 032 */ 033public class UnsignedMath { 034 035 private static final long MASK = 0xffffffffL; 036 037 /** 038 * Unsigned comparison aboveThan for two numbers. 039 */ 040 public static boolean aboveThan(int a, int b) { 041 return (a & MASK) > (b & MASK); 042 } 043 044 /** 045 * Unsigned comparison aboveOrEqual for two numbers. 046 */ 047 public static boolean aboveOrEqual(int a, int b) { 048 return (a & MASK) >= (b & MASK); 049 } 050 051 /** 052 * Unsigned comparison belowThan for two numbers. 053 */ 054 public static boolean belowThan(int a, int b) { 055 return (a & MASK) < (b & MASK); 056 } 057 058 /** 059 * Unsigned comparison belowOrEqual for two numbers. 060 */ 061 public static boolean belowOrEqual(int a, int b) { 062 return (a & MASK) <= (b & MASK); 063 } 064 065 /** 066 * Unsigned comparison aboveThan for two numbers. 067 */ 068 public static boolean aboveThan(long a, long b) { 069 return (a > b) ^ ((a < 0) != (b < 0)); 070 } 071 072 /** 073 * Unsigned comparison aboveOrEqual for two numbers. 074 */ 075 public static boolean aboveOrEqual(long a, long b) { 076 return (a >= b) ^ ((a < 0) != (b < 0)); 077 } 078 079 /** 080 * Unsigned comparison belowThan for two numbers. 081 */ 082 public static boolean belowThan(long a, long b) { 083 return (a < b) ^ ((a < 0) != (b < 0)); 084 } 085 086 /** 087 * Unsigned comparison belowOrEqual for two numbers. 088 */ 089 public static boolean belowOrEqual(long a, long b) { 090 return (a <= b) ^ ((a < 0) != (b < 0)); 091 } 092 093 /** 094 * Unsigned division for two numbers. 095 */ 096 public static int divide(int a, int b) { 097 return (int) ((a & MASK) / (b & MASK)); 098 } 099 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}