001/*
002 * Copyright (c) 2015, 2015, 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 com.oracle.graal.compiler.common.util;
024
025/**
026 * Provides low-level value checks and conversion for signed and unsigned values of size 1, 2, and 4
027 * bytes.
028 */
029public class TypeConversion {
030
031    public static boolean isS1(long value) {
032        return value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE;
033    }
034
035    public static boolean isU1(long value) {
036        return value >= 0 && value <= 0xFF;
037    }
038
039    public static boolean isS2(long value) {
040        return value >= Short.MIN_VALUE && value <= Short.MAX_VALUE;
041    }
042
043    public static boolean isU2(long value) {
044        return value >= 0 && value <= 0xFFFF;
045    }
046
047    public static boolean isS4(long value) {
048        return value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE;
049    }
050
051    public static boolean isU4(long value) {
052        return value >= 0 && value <= 0xFFFFFFFFL;
053    }
054
055    public static byte asS1(long value) {
056        assert isS1(value);
057        return (byte) value;
058    }
059
060    public static byte asU1(long value) {
061        assert isU1(value);
062        return (byte) value;
063    }
064
065    public static short asS2(long value) {
066        assert isS2(value);
067        return (short) value;
068    }
069
070    public static short asU2(long value) {
071        assert isU2(value);
072        return (short) value;
073    }
074
075    public static int asS4(long value) {
076        assert isS4(value);
077        return (int) value;
078    }
079
080    public static int asU4(long value) {
081        assert isU4(value);
082        return (int) value;
083    }
084}