Skip to content

Commit

Permalink
8315970: Big-endian issues after JDK-8310929
Browse files Browse the repository at this point in the history
Reviewed-by: mdoerr
  • Loading branch information
wenshao authored and TheRealMDoerr committed Sep 11, 2023
1 parent ae08143 commit 4cb4637
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions src/java.base/share/classes/java/lang/StringUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import jdk.internal.util.ByteArrayLittleEndian;
import jdk.internal.vm.annotation.DontInline;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
Expand Down Expand Up @@ -1543,21 +1542,14 @@ static int getChars(int i, int index, byte[] buf) {
q = i / 100;
r = (q * 100) - i;
i = q;

int packed = (int) StringLatin1.PACKED_DIGITS[r];
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);

charPos -= 2;
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
putPair(buf, charPos, r);
}

// We know there are at most two digits left at this point.
if (i < -9) {
int packed = (int) StringLatin1.PACKED_DIGITS[-i];
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);

charPos -= 2;
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
putPair(buf, charPos, -i);
} else {
putChar(buf, --charPos, '0' - i);
}
Expand Down Expand Up @@ -1590,12 +1582,8 @@ static int getChars(long i, int index, byte[] buf) {
// Get 2 digits/iteration using longs until quotient fits into an int
while (i <= Integer.MIN_VALUE) {
q = i / 100;

int packed = (int) StringLatin1.PACKED_DIGITS[(int)((q * 100) - i)];
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);

charPos -= 2;
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
putPair(buf, charPos, (int)((q * 100) - i));
i = q;
}

Expand All @@ -1604,23 +1592,15 @@ static int getChars(long i, int index, byte[] buf) {
int i2 = (int)i;
while (i2 <= -100) {
q2 = i2 / 100;

int packed = (int) StringLatin1.PACKED_DIGITS[(q2 * 100) - i2];
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);

charPos -= 2;
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
putPair(buf, charPos, (q2 * 100) - i2);
i2 = q2;
}

// We know there are at most two digits left at this point.
if (i2 < -9) {
charPos -= 2;

int packed = (int) StringLatin1.PACKED_DIGITS[-i2];
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);

ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
putPair(buf, charPos, -i2);
} else {
putChar(buf, --charPos, '0' - i2);
}
Expand All @@ -1630,6 +1610,12 @@ static int getChars(long i, int index, byte[] buf) {
}
return charPos;
}

private static void putPair(byte[] buf, int charPos, int v) {
int packed = (int) StringLatin1.PACKED_DIGITS[v];
putChar(buf, charPos, packed & 0xFF);
putChar(buf, charPos + 1, packed >> 8);
}
// End of trusted methods.

public static void checkIndex(int off, byte[] val) {
Expand Down

1 comment on commit 4cb4637

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.