NumUtil.java revision 12651:6ef01bd40ce2
1199482Srdivacky/* 2199482Srdivacky * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3199482Srdivacky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4199482Srdivacky * 5199482Srdivacky * This code is free software; you can redistribute it and/or modify it 6199482Srdivacky * under the terms of the GNU General Public License version 2 only, as 7199482Srdivacky * published by the Free Software Foundation. 8199482Srdivacky * 9199482Srdivacky * This code is distributed in the hope that it will be useful, but WITHOUT 10199482Srdivacky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11199482Srdivacky * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12199482Srdivacky * version 2 for more details (a copy is included in the LICENSE file that 13199482Srdivacky * accompanied this code). 14199482Srdivacky * 15199482Srdivacky * You should have received a copy of the GNU General Public License version 16199482Srdivacky * 2 along with this work; if not, write to the Free Software Foundation, 17199482Srdivacky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18199482Srdivacky * 19199482Srdivacky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20199482Srdivacky * or visit www.oracle.com if you need additional information or have any 21205408Srdivacky * questions. 22199482Srdivacky */ 23199482Srdivackypackage org.graalvm.compiler.asm; 24199482Srdivacky 25199482Srdivacky// JaCoCo Exclude 26199482Srdivacky 27199482Srdivacky/** 28199482Srdivacky * A collection of static utility functions that check ranges of numbers. 29199482Srdivacky */ 30199482Srdivackypublic class NumUtil { 31199482Srdivacky 32207619Srdivacky public static boolean isShiftCount(int x) { 33218893Sdim return 0 <= x && x < 32; 34218893Sdim } 35204962Srdivacky 36218893Sdim /** 37199482Srdivacky * Determines if a given {@code int} value is the range of unsigned byte values. 38199482Srdivacky */ 39218893Sdim public static boolean isUByte(int x) { 40199482Srdivacky return (x & 0xff) == x; 41199482Srdivacky } 42199482Srdivacky 43199482Srdivacky /** 44199482Srdivacky * Determines if a given {@code int} value is the range of signed byte values. 45199482Srdivacky */ 46199482Srdivacky public static boolean isByte(int x) { 47199482Srdivacky return (byte) x == x; 48199482Srdivacky } 49199482Srdivacky 50199482Srdivacky /** 51199482Srdivacky * Determines if a given {@code long} value is the range of unsigned byte values. 52199482Srdivacky */ 53199482Srdivacky public static boolean isUByte(long x) { 54199482Srdivacky return (x & 0xffL) == x; 55199482Srdivacky } 56199482Srdivacky 57199482Srdivacky /** 58199482Srdivacky * Determines if a given {@code long} value is the range of signed byte values. 59218893Sdim */ 60218893Sdim public static boolean isByte(long l) { 61218893Sdim return (byte) l == l; 62218893Sdim } 63218893Sdim 64218893Sdim /** 65218893Sdim * Determines if a given {@code long} value is the range of unsigned int values. 66218893Sdim */ 67199482Srdivacky public static boolean isUInt(long x) { 68199482Srdivacky return (x & 0xffffffffL) == x; 69199482Srdivacky } 70199482Srdivacky 71199482Srdivacky /** 72199482Srdivacky * Determines if a given {@code long} value is the range of signed int values. 73199482Srdivacky */ 74218893Sdim public static boolean isInt(long l) { 75218893Sdim return (int) l == l; 76218893Sdim } 77218893Sdim 78218893Sdim /** 79218893Sdim * Determines if a given {@code int} value is the range of signed short values. 80218893Sdim */ 81218893Sdim public static boolean isShort(int x) { 82218893Sdim return (short) x == x; 83218893Sdim } 84218893Sdim 85218893Sdim /** 86218893Sdim * Determines if a given {@code long} value is the range of signed short values. 87218893Sdim */ 88218893Sdim public static boolean isShort(long x) { 89218893Sdim return (short) x == x; 90218893Sdim } 91218893Sdim 92218893Sdim public static boolean isUShort(int s) { 93218893Sdim return s == (s & 0xFFFF); 94218893Sdim } 95218893Sdim 96218893Sdim public static boolean isUShort(long s) { 97223017Sdim return s == (s & 0xFFFF); 98218893Sdim } 99218893Sdim 100218893Sdim public static boolean is32bit(long x) { 101218893Sdim return -0x80000000L <= x && x < 0x80000000L; 102218893Sdim } 103218893Sdim 104218893Sdim public static short safeToShort(int v) { 105218893Sdim assert isShort(v); 106218893Sdim return (short) v; 107218893Sdim } 108218893Sdim 109218893Sdim public static int roundUp(int number, int mod) { 110218893Sdim return ((number + mod - 1) / mod) * mod; 111218893Sdim } 112218893Sdim 113218893Sdim public static long roundUp(long number, long mod) { 114218893Sdim return ((number + mod - 1L) / mod) * mod; 115218893Sdim } 116218893Sdim 117218893Sdim public static int roundDown(int number, int mod) { 118218893Sdim return number / mod * mod; 119218893Sdim } 120218893Sdim 121218893Sdim public static long roundDown(long number, long mod) { 122218893Sdim return number / mod * mod; 123218893Sdim } 124218893Sdim 125218893Sdim public static int log2Ceil(int val) { 126218893Sdim int x = 1; 127218893Sdim int log2 = 0; 128218893Sdim while (x < val) { 129218893Sdim log2++; 130218893Sdim x *= 2; 131218893Sdim } 132218893Sdim return log2; 133218893Sdim } 134218893Sdim 135218893Sdim public static boolean isUnsignedNbit(int n, int value) { 136218893Sdim assert n > 0 && n < 32; 137218893Sdim return 32 - Integer.numberOfLeadingZeros(value) <= n; 138218893Sdim } 139218893Sdim 140218893Sdim public static boolean isUnsignedNbit(int n, long value) { 141218893Sdim assert n > 0 && n < 64; 142218893Sdim return 64 - Long.numberOfLeadingZeros(value) <= n; 143199482Srdivacky } 144218893Sdim 145218893Sdim public static boolean isSignedNbit(int n, int value) { 146218893Sdim assert n > 0 && n < 32; 147199482Srdivacky int min = -(1 << (n - 1)); 148218893Sdim int max = (1 << (n - 1)) - 1; 149199482Srdivacky return value >= min && value <= max; 150199482Srdivacky } 151199482Srdivacky 152199482Srdivacky public static boolean isSignedNbit(int n, long value) { 153218893Sdim assert n > 0 && n < 64; 154218893Sdim long min = -(1L << (n - 1)); 155199482Srdivacky long max = (1L << (n - 1)) - 1; 156199482Srdivacky return value >= min && value <= max; 157199482Srdivacky } 158218893Sdim 159218893Sdim /** 160218893Sdim * 161218893Sdim * @param n Number of bits that should be set to 1. Must be between 0 and 32 (inclusive). 162218893Sdim * @return A number with n bits set to 1. 163218893Sdim */ 164218893Sdim public static int getNbitNumberInt(int n) { 165218893Sdim assert n >= 0 && n <= 32 : "0 <= n <= 32; instead: " + n; 166218893Sdim if (n < 32) { 167218893Sdim return (1 << n) - 1; 168218893Sdim } else { 169218893Sdim return 0xFFFFFFFF; 170199482Srdivacky } 171199482Srdivacky } 172218893Sdim 173199482Srdivacky /** 174199482Srdivacky * 175199482Srdivacky * @param n Number of bits that should be set to 1. Must be between 0 and 64 (inclusive). 176199482Srdivacky * @return A number with n bits set to 1. 177199482Srdivacky */ 178199482Srdivacky public static long getNbitNumberLong(int n) { 179199482Srdivacky assert n >= 0 && n <= 64; 180199482Srdivacky if (n < 64) { 181199482Srdivacky return (1L << n) - 1; 182218893Sdim } else { 183218893Sdim return 0xFFFFFFFFFFFFFFFFL; 184199482Srdivacky } 185199482Srdivacky } 186199482Srdivacky} 187218893Sdim