1 /* 2 * Copyright (c) 2017, 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 #ifndef SHARE_VM_UTILITIES_TRAITS_INTEGER_HPP 26 #define SHARE_VM_UTILITIES_TRAITS_INTEGER_HPP 27 28 #include "memory/allocation.hpp" 29 30 namespace IntegerTypeImpl { 31 template <size_t size> 32 struct SignedType: AllStatic {}; 33 34 template <> 35 struct SignedType<sizeof(int64_t)>: AllStatic { 36 typedef int64_t type; 37 }; 38 39 template <> 40 struct SignedType<sizeof(int32_t)>: AllStatic { 41 typedef int32_t type; 42 }; 43 44 template <> 45 struct SignedType<sizeof(int16_t)>: AllStatic { 46 typedef int16_t type; 47 }; 48 49 template <> 50 struct SignedType<sizeof(int8_t)>: AllStatic { 51 typedef signed char type; 52 }; 53 54 template <size_t size> 55 struct UnsignedType: AllStatic {}; 56 57 template <> 58 struct UnsignedType<sizeof(uint64_t)>: AllStatic { 59 typedef uint64_t type; 60 }; 61 62 template <> 63 struct UnsignedType<sizeof(uint32_t)>: AllStatic { 64 typedef uint32_t type; 65 }; 66 67 template <> 68 struct UnsignedType<sizeof(uint16_t)>: AllStatic { 69 typedef uint16_t type; 70 }; 71 72 template <> 73 struct UnsignedType<sizeof(uint8_t)>: AllStatic { 74 typedef unsigned char type; 75 }; 76 } 77 78 template <typename T> 79 struct IsPointerSize: AllStatic { 80 enum { 81 value = sizeof(T) == sizeof(void*) 82 }; 83 }; 84 85 template <typename T> 86 struct IntegerType: AllStatic { 87 typedef typename IntegerTypeImpl::SignedType<sizeof(T)>::type signed_type; 88 typedef typename IntegerTypeImpl::UnsignedType<sizeof(T)>::type unsigned_type; 89 90 static signed_type cast_to_signed(T val) { 91 return *(signed_type*)&val; 92 } 93 94 static T cast_from_signed(signed_type val) { 95 return *(T*)&val; 96 } 97 98 static unsigned_type cast_to_unsigned(T val) { 99 return *(unsigned_type*)&val; 100 } 101 102 static T cast_from_unsigned(unsigned_type val) { 103 return *(T*)&val; 104 } 105 }; 106 107 #endif // SHARE_VM_UTILITIES_TRAITS_INTEGER_HPP