1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 SAP AG. All rights reserved.
   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 #ifndef CPU_PPC_VM_BYTES_PPC_HPP
  27 #define CPU_PPC_VM_BYTES_PPC_HPP
  28 
  29 #include "memory/allocation.hpp"
  30 
  31 class Bytes: AllStatic {
  32  public:
  33   // Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering
  34   // PowerPC needs to check for alignment.
  35 
  36   // can I count on address always being a pointer to an unsigned char? Yes
  37 
  38   // Returns true, if the byte ordering used by Java is different from the nativ byte ordering
  39   // of the underlying machine. For example, true for Intel x86, False, for Solaris on Sparc.
  40   static inline bool is_Java_byte_ordering_different() { return false; }
  41 
  42   // Thus, a swap between native and Java ordering is always a no-op:
  43   static inline u2   swap_u2(u2 x)  { return x; }
  44   static inline u4   swap_u4(u4 x)  { return x; }
  45   static inline u8   swap_u8(u8 x)  { return x; }
  46 
  47   static inline u2   get_native_u2(address p) {
  48     return (intptr_t(p) & 1) == 0
  49              ?   *(u2*)p
  50              :   ( u2(p[0]) << 8 )
  51                | ( u2(p[1])      );
  52   }
  53 
  54   static inline u4   get_native_u4(address p) {
  55     switch (intptr_t(p) & 3) {
  56      case 0:  return *(u4*)p;
  57 
  58      case 2:  return (  u4( ((u2*)p)[0] ) << 16  )
  59                    | (  u4( ((u2*)p)[1] )        );
  60 
  61     default:  return ( u4(p[0]) << 24 )
  62                    | ( u4(p[1]) << 16 )
  63                    | ( u4(p[2]) <<  8 )
  64                    |   u4(p[3]);
  65     }
  66   }
  67 
  68   static inline u8   get_native_u8(address p) {
  69     switch (intptr_t(p) & 7) {
  70       case 0:  return *(u8*)p;
  71 
  72       case 4:  return (  u8( ((u4*)p)[0] ) << 32  )
  73                     | (  u8( ((u4*)p)[1] )        );
  74 
  75       case 2:  return (  u8( ((u2*)p)[0] ) << 48  )
  76                     | (  u8( ((u2*)p)[1] ) << 32  )
  77                     | (  u8( ((u2*)p)[2] ) << 16  )
  78                     | (  u8( ((u2*)p)[3] )        );
  79 
  80      default:  return ( u8(p[0]) << 56 )
  81                     | ( u8(p[1]) << 48 )
  82                     | ( u8(p[2]) << 40 )
  83                     | ( u8(p[3]) << 32 )
  84                     | ( u8(p[4]) << 24 )
  85                     | ( u8(p[5]) << 16 )
  86                     | ( u8(p[6]) <<  8 )
  87                     |   u8(p[7]);
  88     }
  89   }
  90 
  91 
  92 
  93   static inline void put_native_u2(address p, u2 x) {
  94     if ( (intptr_t(p) & 1) == 0 ) { *(u2*)p = x; }
  95     else {
  96       p[0] = x >> 8;
  97       p[1] = x;
  98     }
  99   }
 100 
 101   static inline void put_native_u4(address p, u4 x) {
 102     switch ( intptr_t(p) & 3 ) {
 103     case 0:  *(u4*)p = x;
 104               break;
 105 
 106     case 2:  ((u2*)p)[0] = x >> 16;
 107              ((u2*)p)[1] = x;
 108              break;
 109 
 110     default: ((u1*)p)[0] = x >> 24;
 111              ((u1*)p)[1] = x >> 16;
 112              ((u1*)p)[2] = x >>  8;
 113              ((u1*)p)[3] = x;
 114              break;
 115     }
 116   }
 117 
 118   static inline void put_native_u8(address p, u8 x) {
 119     switch ( intptr_t(p) & 7 ) {
 120     case 0:  *(u8*)p = x;
 121              break;
 122 
 123     case 4:  ((u4*)p)[0] = x >> 32;
 124              ((u4*)p)[1] = x;
 125              break;
 126 
 127     case 2:  ((u2*)p)[0] = x >> 48;
 128              ((u2*)p)[1] = x >> 32;
 129              ((u2*)p)[2] = x >> 16;
 130              ((u2*)p)[3] = x;
 131              break;
 132 
 133     default: ((u1*)p)[0] = x >> 56;
 134              ((u1*)p)[1] = x >> 48;
 135              ((u1*)p)[2] = x >> 40;
 136              ((u1*)p)[3] = x >> 32;
 137              ((u1*)p)[4] = x >> 24;
 138              ((u1*)p)[5] = x >> 16;
 139              ((u1*)p)[6] = x >>  8;
 140              ((u1*)p)[7] = x;
 141     }
 142   }
 143 
 144 
 145   // Efficient reading and writing of unaligned unsigned data in Java byte ordering (i.e. big-endian ordering)
 146   // (no byte-order reversal is needed since Power CPUs are big-endian oriented).
 147   static inline u2   get_Java_u2(address p) { return get_native_u2(p); }
 148   static inline u4   get_Java_u4(address p) { return get_native_u4(p); }
 149   static inline u8   get_Java_u8(address p) { return get_native_u8(p); }
 150 
 151   static inline void put_Java_u2(address p, u2 x)     { put_native_u2(p, x); }
 152   static inline void put_Java_u4(address p, u4 x)     { put_native_u4(p, x); }
 153   static inline void put_Java_u8(address p, u8 x)     { put_native_u8(p, x); }
 154 };
 155 
 156 #endif // CPU_PPC_VM_BYTES_PPC_HPP