1 /*
   2  * Copyright (c) 1997, 2002, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2009 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 typedef union unaligned {
  27   u4 u;
  28   u2 us;
  29   u8 ul;
  30 } __attribute__((packed)) unaligned;
  31 
  32 class Bytes: AllStatic {
  33  public:
  34   // Returns true if the byte ordering used by Java is different
  35   // from the native byte ordering of the underlying machine.
  36   static inline bool is_Java_byte_ordering_different() {
  37 #ifdef VM_LITTLE_ENDIAN
  38     return true;
  39 #else
  40     return false;
  41 #endif
  42   }
  43 
  44   // Efficient reading and writing of unaligned unsigned data in
  45   // platform-specific byte ordering.
  46   static inline u2 get_native_u2(address p){
  47     unaligned *up = (unaligned *) p;
  48     return up->us;
  49   }
  50 
  51   static inline u4 get_native_u4(address p) {
  52     unaligned *up = (unaligned *) p;
  53     return up->u;
  54   }
  55 
  56   static inline u8 get_native_u8(address p) {
  57     unaligned *up = (unaligned *) p;
  58     return up->ul;
  59   }
  60 
  61   static inline void put_native_u2(address p, u2 x) {
  62     unaligned *up = (unaligned *) p;
  63     up->us = x;
  64   }
  65 
  66   static inline void put_native_u4(address p, u4 x) {
  67     unaligned *up = (unaligned *) p;
  68     up->u = x;
  69   }
  70 
  71   static inline void put_native_u8(address p, u8 x) {
  72     unaligned *up = (unaligned *) p;
  73     up->ul = x;
  74   }
  75 
  76   // Efficient reading and writing of unaligned unsigned data in Java
  77   // byte ordering (i.e. big-endian ordering).
  78 #ifdef VM_LITTLE_ENDIAN
  79   // Byte-order reversal is needed
  80   static inline u2 get_Java_u2(address p) {
  81     return (u2(p[0]) << 8) |
  82            (u2(p[1])     );
  83   }
  84   static inline u4 get_Java_u4(address p) {
  85     return (u4(p[0]) << 24) |
  86            (u4(p[1]) << 16) |
  87            (u4(p[2]) <<  8) |
  88            (u4(p[3])      );
  89   }
  90   static inline u8 get_Java_u8(address p) {
  91     u4 hi, lo;
  92     hi = (u4(p[0]) << 24) |
  93          (u4(p[1]) << 16) |
  94          (u4(p[2]) <<  8) |
  95          (u4(p[3])      );
  96     lo = (u4(p[4]) << 24) |
  97          (u4(p[5]) << 16) |
  98          (u4(p[6]) <<  8) |
  99          (u4(p[7])      );
 100     return u8(lo) | (u8(hi) << 32);
 101   }
 102 
 103   static inline void put_Java_u2(address p, u2 x) {
 104     p[0] = x >> 8;
 105     p[1] = x;
 106   }
 107   static inline void put_Java_u4(address p, u4 x) {
 108     p[0] = x >> 24;
 109     p[1] = x >> 16;
 110     p[2] = x >> 8;
 111     p[3] = x;
 112   }
 113   static inline void put_Java_u8(address p, u8 x) {
 114     u4 hi, lo;
 115     lo = x;
 116     hi = x >> 32;
 117     p[0] = hi >> 24;
 118     p[1] = hi >> 16;
 119     p[2] = hi >> 8;
 120     p[3] = hi;
 121     p[4] = lo >> 24;
 122     p[5] = lo >> 16;
 123     p[6] = lo >> 8;
 124     p[7] = lo;
 125   }
 126 
 127   // Efficient swapping of byte ordering
 128   static inline u2 swap_u2(u2 x);
 129   static inline u4 swap_u4(u4 x);
 130   static inline u8 swap_u8(u8 x);
 131 #else
 132   // No byte-order reversal is needed
 133   static inline u2 get_Java_u2(address p) {
 134     return get_native_u2(p);
 135   }
 136   static inline u4 get_Java_u4(address p) {
 137     return get_native_u4(p);
 138   }
 139   static inline u8 get_Java_u8(address p) {
 140     return get_native_u8(p);
 141   }
 142 
 143   static inline void put_Java_u2(address p, u2 x) {
 144     put_native_u2(p, x);
 145   }
 146   static inline void put_Java_u4(address p, u4 x) {
 147     put_native_u4(p, x);
 148   }
 149   static inline void put_Java_u8(address p, u8 x) {
 150     put_native_u8(p, x);
 151   }
 152 
 153   // No byte-order reversal is needed
 154   static inline u2 swap_u2(u2 x) { return x; }
 155   static inline u4 swap_u4(u4 x) { return x; }
 156   static inline u8 swap_u8(u8 x) { return x; }
 157 #endif // VM_LITTLE_ENDIAN
 158 };
 159 
 160 #ifdef VM_LITTLE_ENDIAN
 161 // The following header contains the implementations of swap_u2,
 162 // swap_u4, and swap_u8
 163 #include "incls/_bytes_pd.inline.hpp.incl"
 164 #endif // VM_LITTLE_ENDIAN