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