1010N/A/*
1879N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1010N/A * Copyright 2007, 2008, 2009 Red Hat, Inc.
1010N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1010N/A *
1010N/A * This code is free software; you can redistribute it and/or modify it
1010N/A * under the terms of the GNU General Public License version 2 only, as
1010N/A * published by the Free Software Foundation.
1010N/A *
1010N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1010N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1010N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1010N/A * version 2 for more details (a copy is included in the LICENSE file that
1010N/A * accompanied this code).
1010N/A *
1010N/A * You should have received a copy of the GNU General Public License version
1010N/A * 2 along with this work; if not, write to the Free Software Foundation,
1010N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1010N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
1010N/A *
1010N/A */
1010N/A
1879N/A#ifndef CPU_ZERO_VM_BYTES_ZERO_HPP
1879N/A#define CPU_ZERO_VM_BYTES_ZERO_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A
1010N/Atypedef union unaligned {
1010N/A u4 u;
1010N/A u2 us;
1010N/A u8 ul;
1010N/A} __attribute__((packed)) unaligned;
1010N/A
1010N/Aclass Bytes: AllStatic {
1010N/A public:
1010N/A // Returns true if the byte ordering used by Java is different
1010N/A // from the native byte ordering of the underlying machine.
1010N/A static inline bool is_Java_byte_ordering_different() {
1010N/A#ifdef VM_LITTLE_ENDIAN
1010N/A return true;
1010N/A#else
1010N/A return false;
1010N/A#endif
1010N/A }
1010N/A
1010N/A // Efficient reading and writing of unaligned unsigned data in
1010N/A // platform-specific byte ordering.
1010N/A static inline u2 get_native_u2(address p){
1010N/A unaligned *up = (unaligned *) p;
1010N/A return up->us;
1010N/A }
1010N/A
1010N/A static inline u4 get_native_u4(address p) {
1010N/A unaligned *up = (unaligned *) p;
1010N/A return up->u;
1010N/A }
1010N/A
1010N/A static inline u8 get_native_u8(address p) {
1010N/A unaligned *up = (unaligned *) p;
1010N/A return up->ul;
1010N/A }
1010N/A
1010N/A static inline void put_native_u2(address p, u2 x) {
1010N/A unaligned *up = (unaligned *) p;
1010N/A up->us = x;
1010N/A }
1010N/A
1010N/A static inline void put_native_u4(address p, u4 x) {
1010N/A unaligned *up = (unaligned *) p;
1010N/A up->u = x;
1010N/A }
1010N/A
1010N/A static inline void put_native_u8(address p, u8 x) {
1010N/A unaligned *up = (unaligned *) p;
1010N/A up->ul = x;
1010N/A }
1010N/A
1010N/A // Efficient reading and writing of unaligned unsigned data in Java
1010N/A // byte ordering (i.e. big-endian ordering).
1010N/A#ifdef VM_LITTLE_ENDIAN
1010N/A // Byte-order reversal is needed
1010N/A static inline u2 get_Java_u2(address p) {
1010N/A return (u2(p[0]) << 8) |
1010N/A (u2(p[1]) );
1010N/A }
1010N/A static inline u4 get_Java_u4(address p) {
1010N/A return (u4(p[0]) << 24) |
1010N/A (u4(p[1]) << 16) |
1010N/A (u4(p[2]) << 8) |
1010N/A (u4(p[3]) );
1010N/A }
1010N/A static inline u8 get_Java_u8(address p) {
1010N/A u4 hi, lo;
1010N/A hi = (u4(p[0]) << 24) |
1010N/A (u4(p[1]) << 16) |
1010N/A (u4(p[2]) << 8) |
1010N/A (u4(p[3]) );
1010N/A lo = (u4(p[4]) << 24) |
1010N/A (u4(p[5]) << 16) |
1010N/A (u4(p[6]) << 8) |
1010N/A (u4(p[7]) );
1010N/A return u8(lo) | (u8(hi) << 32);
1010N/A }
1010N/A
1010N/A static inline void put_Java_u2(address p, u2 x) {
1010N/A p[0] = x >> 8;
1010N/A p[1] = x;
1010N/A }
1010N/A static inline void put_Java_u4(address p, u4 x) {
1010N/A p[0] = x >> 24;
1010N/A p[1] = x >> 16;
1010N/A p[2] = x >> 8;
1010N/A p[3] = x;
1010N/A }
1010N/A static inline void put_Java_u8(address p, u8 x) {
1010N/A u4 hi, lo;
1010N/A lo = x;
1010N/A hi = x >> 32;
1010N/A p[0] = hi >> 24;
1010N/A p[1] = hi >> 16;
1010N/A p[2] = hi >> 8;
1010N/A p[3] = hi;
1010N/A p[4] = lo >> 24;
1010N/A p[5] = lo >> 16;
1010N/A p[6] = lo >> 8;
1010N/A p[7] = lo;
1010N/A }
1010N/A
1010N/A // Efficient swapping of byte ordering
1010N/A static inline u2 swap_u2(u2 x);
1010N/A static inline u4 swap_u4(u4 x);
1010N/A static inline u8 swap_u8(u8 x);
1010N/A#else
1010N/A // No byte-order reversal is needed
1010N/A static inline u2 get_Java_u2(address p) {
1010N/A return get_native_u2(p);
1010N/A }
1010N/A static inline u4 get_Java_u4(address p) {
1010N/A return get_native_u4(p);
1010N/A }
1010N/A static inline u8 get_Java_u8(address p) {
1010N/A return get_native_u8(p);
1010N/A }
1010N/A
1010N/A static inline void put_Java_u2(address p, u2 x) {
1010N/A put_native_u2(p, x);
1010N/A }
1010N/A static inline void put_Java_u4(address p, u4 x) {
1010N/A put_native_u4(p, x);
1010N/A }
1010N/A static inline void put_Java_u8(address p, u8 x) {
1010N/A put_native_u8(p, x);
1010N/A }
1010N/A
1010N/A // No byte-order reversal is needed
1010N/A static inline u2 swap_u2(u2 x) { return x; }
1010N/A static inline u4 swap_u4(u4 x) { return x; }
1010N/A static inline u8 swap_u8(u8 x) { return x; }
1010N/A#endif // VM_LITTLE_ENDIAN
1010N/A};
1010N/A
1010N/A#ifdef VM_LITTLE_ENDIAN
1010N/A// The following header contains the implementations of swap_u2,
1010N/A// swap_u4, and swap_u8
1879N/A#ifdef TARGET_OS_ARCH_linux_zero
1879N/A# include "bytes_linux_zero.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_ARCH_bsd_zero
2796N/A# include "bytes_bsd_zero.inline.hpp"
2796N/A#endif
1879N/A
1010N/A#endif // VM_LITTLE_ENDIAN
1879N/A
1879N/A#endif // CPU_ZERO_VM_BYTES_ZERO_HPP