1 /*
   2  * Copyright (c) 2015, 2015, 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 org.graalvm.compiler.core.common.util;
  24 
  25 /**
  26  * Provides low-level value checks and conversion for signed and unsigned values of size 1, 2, and 4
  27  * bytes.
  28  */
  29 public class TypeConversion {
  30 
  31     public static boolean isS1(long value) {
  32         return value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE;
  33     }
  34 
  35     public static boolean isU1(long value) {
  36         return value >= 0 && value <= 0xFF;
  37     }
  38 
  39     public static boolean isS2(long value) {
  40         return value >= Short.MIN_VALUE && value <= Short.MAX_VALUE;
  41     }
  42 
  43     public static boolean isU2(long value) {
  44         return value >= 0 && value <= 0xFFFF;
  45     }
  46 
  47     public static boolean isS4(long value) {
  48         return value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE;
  49     }
  50 
  51     public static boolean isU4(long value) {
  52         return value >= 0 && value <= 0xFFFFFFFFL;
  53     }
  54 
  55     public static byte asS1(long value) {
  56         assert isS1(value);
  57         return (byte) value;
  58     }
  59 
  60     public static byte asU1(long value) {
  61         assert isU1(value);
  62         return (byte) value;
  63     }
  64 
  65     public static short asS2(long value) {
  66         assert isS2(value);
  67         return (short) value;
  68     }
  69 
  70     public static short asU2(long value) {
  71         assert isU2(value);
  72         return (short) value;
  73     }
  74 
  75     public static int asS4(long value) {
  76         assert isS4(value);
  77         return (int) value;
  78     }
  79 
  80     public static int asU4(long value) {
  81         assert isU4(value);
  82         return (int) value;
  83     }
  84 }