Path: blob/master/src/hotspot/cpu/x86/bytes_x86.hpp
41144 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef CPU_X86_BYTES_X86_HPP25#define CPU_X86_BYTES_X86_HPP2627#include "memory/allocation.hpp"28#include "utilities/align.hpp"29#include "utilities/macros.hpp"3031class Bytes: AllStatic {32private:33#ifndef AMD6434// Helper function for swap_u835static inline u8 swap_u8_base(u4 x, u4 y); // compiler-dependent implementation36#endif // AMD643738public:39// Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering40template <typename T>41static inline T get_native(const void* p) {42assert(p != NULL, "null pointer");4344T x;4546if (is_aligned(p, sizeof(T))) {47x = *(T*)p;48} else {49memcpy(&x, p, sizeof(T));50}5152return x;53}5455template <typename T>56static inline void put_native(void* p, T x) {57assert(p != NULL, "null pointer");5859if (is_aligned(p, sizeof(T))) {60*(T*)p = x;61} else {62memcpy(p, &x, sizeof(T));63}64}6566static inline u2 get_native_u2(address p) { return get_native<u2>((void*)p); }67static inline u4 get_native_u4(address p) { return get_native<u4>((void*)p); }68static inline u8 get_native_u8(address p) { return get_native<u8>((void*)p); }69static inline void put_native_u2(address p, u2 x) { put_native<u2>((void*)p, x); }70static inline void put_native_u4(address p, u4 x) { put_native<u4>((void*)p, x); }71static inline void put_native_u8(address p, u8 x) { put_native<u8>((void*)p, x); }7273// Efficient reading and writing of unaligned unsigned data in Java74// byte ordering (i.e. big-endian ordering). Byte-order reversal is75// needed since x86 CPUs use little-endian format.76template <typename T>77static inline T get_Java(const address p) {78T x = get_native<T>(p);7980if (Endian::is_Java_byte_ordering_different()) {81x = swap<T>(x);82}8384return x;85}8687template <typename T>88static inline void put_Java(address p, T x) {89if (Endian::is_Java_byte_ordering_different()) {90x = swap<T>(x);91}9293put_native<T>(p, x);94}9596static inline u2 get_Java_u2(address p) { return get_Java<u2>(p); }97static inline u4 get_Java_u4(address p) { return get_Java<u4>(p); }98static inline u8 get_Java_u8(address p) { return get_Java<u8>(p); }99100static inline void put_Java_u2(address p, u2 x) { put_Java<u2>(p, x); }101static inline void put_Java_u4(address p, u4 x) { put_Java<u4>(p, x); }102static inline void put_Java_u8(address p, u8 x) { put_Java<u8>(p, x); }103104// Efficient swapping of byte ordering105template <typename T>106static T swap(T x) {107switch (sizeof(T)) {108case sizeof(u1): return x;109case sizeof(u2): return swap_u2(x);110case sizeof(u4): return swap_u4(x);111case sizeof(u8): return swap_u8(x);112default:113guarantee(false, "invalid size: " SIZE_FORMAT "\n", sizeof(T));114return 0;115}116}117118static inline u2 swap_u2(u2 x); // compiler-dependent implementation119static inline u4 swap_u4(u4 x); // compiler-dependent implementation120static inline u8 swap_u8(u8 x);121};122123// The following header contains the implementations of swap_u2, swap_u4, and swap_u8[_base]124#include OS_CPU_HEADER(bytes)125126#endif // CPU_X86_BYTES_X86_HPP127128129