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