0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.unsafe.array;
0019
0020 import org.apache.spark.unsafe.Platform;
0021
0022 public class ByteArrayMethods {
0023
0024 private ByteArrayMethods() {
0025
0026 }
0027
0028
0029 public static long nextPowerOf2(long num) {
0030 final long highBit = Long.highestOneBit(num);
0031 return (highBit == num) ? num : highBit << 1;
0032 }
0033
0034 public static int roundNumberOfBytesToNearestWord(int numBytes) {
0035 return (int)roundNumberOfBytesToNearestWord((long)numBytes);
0036 }
0037
0038 public static long roundNumberOfBytesToNearestWord(long numBytes) {
0039 long remainder = numBytes & 0x07;
0040 if (remainder == 0) {
0041 return numBytes;
0042 } else {
0043 return numBytes + (8 - remainder);
0044 }
0045 }
0046
0047
0048
0049
0050
0051
0052 public static int MAX_ROUNDED_ARRAY_LENGTH = Integer.MAX_VALUE - 15;
0053
0054 private static final boolean unaligned = Platform.unaligned();
0055
0056
0057
0058
0059 public static boolean arrayEquals(
0060 Object leftBase, long leftOffset, Object rightBase, long rightOffset, final long length) {
0061 int i = 0;
0062
0063
0064 if ((leftOffset % 8) == (rightOffset % 8)) {
0065 while ((leftOffset + i) % 8 != 0 && i < length) {
0066 if (Platform.getByte(leftBase, leftOffset + i) !=
0067 Platform.getByte(rightBase, rightOffset + i)) {
0068 return false;
0069 }
0070 i += 1;
0071 }
0072 }
0073
0074 if (unaligned || (((leftOffset + i) % 8 == 0) && ((rightOffset + i) % 8 == 0))) {
0075 while (i <= length - 8) {
0076 if (Platform.getLong(leftBase, leftOffset + i) !=
0077 Platform.getLong(rightBase, rightOffset + i)) {
0078 return false;
0079 }
0080 i += 8;
0081 }
0082 }
0083
0084
0085 while (i < length) {
0086 if (Platform.getByte(leftBase, leftOffset + i) !=
0087 Platform.getByte(rightBase, rightOffset + i)) {
0088 return false;
0089 }
0090 i += 1;
0091 }
0092 return true;
0093 }
0094 }