Path: blob/master/src/java.base/share/native/libjimage/endian.cpp
41149 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031#include "endian.hpp"32#include "inttypes.hpp"3334// Most modern compilers optimize the bswap routines to native instructions.35inline static u2 bswap_16(u2 x) {36return ((x & 0xFF) << 8) |37((x >> 8) & 0xFF);38}3940inline static u4 bswap_32(u4 x) {41return ((x & 0xFF) << 24) |42((x & 0xFF00) << 8) |43((x >> 8) & 0xFF00) |44((x >> 24) & 0xFF);45}4647inline static u8 bswap_64(u8 x) {48return (u8)bswap_32((u4)x) << 32 |49(u8)bswap_32((u4)(x >> 32));50}5152u2 NativeEndian::get(u2 x) { return x; }53u4 NativeEndian::get(u4 x) { return x; }54u8 NativeEndian::get(u8 x) { return x; }55s2 NativeEndian::get(s2 x) { return x; }56s4 NativeEndian::get(s4 x) { return x; }57s8 NativeEndian::get(s8 x) { return x; }5859void NativeEndian::set(u2& x, u2 y) { x = y; }60void NativeEndian::set(u4& x, u4 y) { x = y; }61void NativeEndian::set(u8& x, u8 y) { x = y; }62void NativeEndian::set(s2& x, s2 y) { x = y; }63void NativeEndian::set(s4& x, s4 y) { x = y; }64void NativeEndian::set(s8& x, s8 y) { x = y; }6566NativeEndian NativeEndian::_native;6768u2 SwappingEndian::get(u2 x) { return bswap_16(x); }69u4 SwappingEndian::get(u4 x) { return bswap_32(x); }70u8 SwappingEndian::get(u8 x) { return bswap_64(x); }71s2 SwappingEndian::get(s2 x) { return bswap_16(x); }72s4 SwappingEndian::get(s4 x) { return bswap_32(x); }73s8 SwappingEndian::get(s8 x) { return bswap_64(x); }7475void SwappingEndian::set(u2& x, u2 y) { x = bswap_16(y); }76void SwappingEndian::set(u4& x, u4 y) { x = bswap_32(y); }77void SwappingEndian::set(u8& x, u8 y) { x = bswap_64(y); }78void SwappingEndian::set(s2& x, s2 y) { x = bswap_16(y); }79void SwappingEndian::set(s4& x, s4 y) { x = bswap_32(y); }80void SwappingEndian::set(s8& x, s8 y) { x = bswap_64(y); }8182SwappingEndian SwappingEndian::_swapping;8384Endian* Endian::get_handler(bool big_endian) {85// If requesting little endian on a little endian machine or86// big endian on a big endian machine use native handler87if (big_endian == is_big_endian()) {88return NativeEndian::get_native();89} else {90// Use swapping handler.91return SwappingEndian::get_swapping();92}93}9495// Return a platform u2 from an array in which Big Endian is applied.96u2 Endian::get_java(u1* x) {97return (u2) (x[0]<<8 | x[1]);98}99100// Add a platform u2 to the array as a Big Endian u2101void Endian::set_java(u1* p, u2 x) {102p[0] = (x >> 8) & 0xff;103p[1] = x & 0xff;104}105106Endian* Endian::get_native_handler() {107return NativeEndian::get_native();108}109110111