bytes_bsd_x86.inline.hpp revision 2721:f08d439fab8c
178342Sbenno/*
299657Sbenno * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
399657Sbenno * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
478342Sbenno *
578342Sbenno * This code is free software; you can redistribute it and/or modify it
678342Sbenno * under the terms of the GNU General Public License version 2 only, as
778342Sbenno * published by the Free Software Foundation.
878342Sbenno *
978342Sbenno * This code is distributed in the hope that it will be useful, but WITHOUT
1099657Sbenno * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1199657Sbenno * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1299657Sbenno * version 2 for more details (a copy is included in the LICENSE file that
1399657Sbenno * accompanied this code).
1478342Sbenno *
1599657Sbenno * You should have received a copy of the GNU General Public License version
1699657Sbenno * 2 along with this work; if not, write to the Free Software Foundation,
1799657Sbenno * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1899657Sbenno *
1999657Sbenno * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2099657Sbenno * or visit www.oracle.com if you need additional information or have any
2199657Sbenno * questions.
2299657Sbenno *
2399657Sbenno */
2499657Sbenno
2599657Sbenno#ifndef OS_CPU_BSD_X86_VM_BYTES_BSD_X86_INLINE_HPP
2699657Sbenno#define OS_CPU_BSD_X86_VM_BYTES_BSD_X86_INLINE_HPP
2799657Sbenno
2878342Sbenno#ifndef _ALLBSD_SOURCE
2978342Sbenno#include <byteswap.h>
30113038Sobrien#endif
31113038Sobrien
3278342Sbenno#ifdef __APPLE__
3399657Sbenno#include <libkern/OSByteOrder.h>
3499657Sbenno#endif
3599657Sbenno
3678342Sbenno#if defined(AMD64)
3799657Sbenno#  if defined(__APPLE__)
3899657Sbenno#    define bswap_16(x) OSSwapInt16(x)
3999657Sbenno#    define bswap_32(x) OSSwapInt32(x)
4099657Sbenno#    define bswap_64(x) OSSwapInt64(x)
4199657Sbenno#  elif defined(__OpenBSD__)
4299657Sbenno#    define bswap_16(x) swap16(x)
4399657Sbenno#    define bswap_32(x) swap32(x)
4499657Sbenno#    define bswap_64(x) swap64(x)
45108939Sgrehan#  elif defined(__NetBSD__)
46108939Sgrehan#    define bswap_16(x) bswap16(x)
4799657Sbenno#    define bswap_32(x) bswap32(x)
4899657Sbenno#    define bswap_64(x) bswap64(x)
4999657Sbenno#  else
50108939Sgrehan#    define bswap_16(x) __bswap16(x)
5199657Sbenno#    define bswap_32(x) __bswap32(x)
52112436Smux#    define bswap_64(x) __bswap64(x)
5399657Sbenno#  endif
54109919Sbenno#endif
5599657Sbenno
5699657Sbenno// Efficient swapping of data bytes from Java byte
5799657Sbenno// ordering to native byte ordering and vice versa.
5899657Sbennoinline u2   Bytes::swap_u2(u2 x) {
5999657Sbenno#ifdef AMD64
6099657Sbenno  return bswap_16(x);
6199657Sbenno#else
6299657Sbenno  u2 ret;
6399657Sbenno  __asm__ __volatile__ (
6499657Sbenno    "movw %0, %%ax;"
6599657Sbenno    "xchg %%al, %%ah;"
6699657Sbenno    "movw %%ax, %0"
6799657Sbenno    :"=r" (ret)      // output : register 0 => ret
6899657Sbenno    :"0"  (x)        // input  : x => register 0
6999657Sbenno    :"ax", "0"       // clobbered registers
7099657Sbenno  );
7199657Sbenno  return ret;
7299657Sbenno#endif // AMD64
7399657Sbenno}
7499657Sbenno
7599657Sbennoinline u4   Bytes::swap_u4(u4 x) {
7699657Sbenno#ifdef AMD64
7799657Sbenno  return bswap_32(x);
7899657Sbenno#else
7999657Sbenno  u4 ret;
8099657Sbenno  __asm__ __volatile__ (
8199657Sbenno    "bswap %0"
8299657Sbenno    :"=r" (ret)      // output : register 0 => ret
8399657Sbenno    :"0"  (x)        // input  : x => register 0
8499657Sbenno    :"0"             // clobbered register
8599657Sbenno  );
8699657Sbenno  return ret;
8799657Sbenno#endif // AMD64
8899657Sbenno}
8999657Sbenno
9099657Sbenno#ifdef AMD64
9199657Sbennoinline u8 Bytes::swap_u8(u8 x) {
9299657Sbenno#ifdef SPARC_WORKS
9399657Sbenno  // workaround for SunStudio12 CR6615391
9499657Sbenno  __asm__ __volatile__ (
9599657Sbenno    "bswapq %0"
9699657Sbenno    :"=r" (x)        // output : register 0 => x
9799657Sbenno    :"0"  (x)        // input  : x => register 0
9899657Sbenno    :"0"             // clobbered register
9999657Sbenno  );
10099657Sbenno  return x;
10199657Sbenno#else
10299657Sbenno  return bswap_64(x);
10399657Sbenno#endif
10499657Sbenno}
10599657Sbenno#else
10699657Sbenno// Helper function for swap_u8
10799657Sbennoinline u8   Bytes::swap_u8_base(u4 x, u4 y) {
10899657Sbenno  return (((u8)swap_u4(x))<<32) | swap_u4(y);
10999657Sbenno}
11099657Sbenno
11199657Sbennoinline u8 Bytes::swap_u8(u8 x) {
11299657Sbenno  return swap_u8_base(*(u4*)&x, *(((u4*)&x)+1));
11399657Sbenno}
11499657Sbenno#endif // !AMD64
11599657Sbenno
11699657Sbenno#endif // OS_CPU_BSD_X86_VM_BYTES_BSD_X86_INLINE_HPP
11799657Sbenno