8369312: Refactor Float.toHexString() to avoid use of regex

Reviewed-by: rgiulietti
This commit is contained in:
Joe Darcy 2025-10-14 16:04:27 +00:00
parent 64ff7062c1
commit 7ed93cf40e
2 changed files with 10 additions and 6 deletions

View File

@ -420,10 +420,12 @@ public final class Float extends Number
// replace subnormal double exponent with subnormal float
// exponent
String s = Double.toHexString(Math.scalb((double)f,
/* -1022+126 */
Double.MIN_EXPONENT-
// -1022 + 126
Double.MIN_EXPONENT -
Float.MIN_EXPONENT));
return s.replaceFirst("p-1022$", "p-126");
// The char sequence "-1022" can only appear in the
// representation of the exponent, not in the (hex) significand.
return s.replace("-1022", "-126");
}
else // double string will be the same as float string
return Double.toHexString(f);

View File

@ -295,10 +295,12 @@ public final class Float16
// replace subnormal double exponent with subnormal Float16
// exponent
String s = Double.toHexString(Math.scalb((double)f,
/* -1022+14 */
Double.MIN_EXPONENT-
// -1022 + 14
Double.MIN_EXPONENT -
MIN_EXPONENT));
return s.replaceFirst("p-1022$", "p-14");
// The char sequence "-1022" can only appear in the
// representation of the exponent, not in the (hex) significand.
return s.replace("-1022", "-14");
} else {// double string will be the same as Float16 string
return Double.toHexString(f);
}