From 6e2a4f847fc91ab25a167ca833bdeaaad5ee8d48 Mon Sep 17 00:00:00 2001 From: Jatin Bhateja Date: Tue, 14 Jul 2026 12:57:49 +0000 Subject: [PATCH] 8386322: Float16Vector.toString should render lane values using Float16.toString Reviewed-by: psandoz, rgiulietti --- .../jdk/incubator/vector/Float16Vector.java | 12 ++++++++---- .../jdk/incubator/vector/X-Vector.java.template | 16 ++++++++++++++++ .../incubator/vector/Float16Vector128Tests.java | 3 ++- .../incubator/vector/Float16Vector256Tests.java | 3 ++- .../incubator/vector/Float16Vector512Tests.java | 3 ++- .../incubator/vector/Float16Vector64Tests.java | 3 ++- .../incubator/vector/Float16VectorMaxTests.java | 3 ++- .../vector/templates/Unit-Miscellaneous.template | 7 ++++++- 8 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Float16Vector.java b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Float16Vector.java index ce3a67357f9..a42fb44dd02 100644 --- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Float16Vector.java +++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Float16Vector.java @@ -3707,8 +3707,10 @@ public abstract sealed class Float16Vector extends AbstractVector * in lane order. * * The string is produced as if by a call to {@link - * java.util.Arrays#toString(short[]) Arrays.toString()}, - * as appropriate to the {@code short} array returned by + * java.util.Arrays#toString(Object[]) Arrays.toString()}, + * as appropriate to a {@code Float16} array whose elements + * are obtained by applying {@link Float16#shortBitsToFloat16(short)} + * to each element of the {@code short[]} array returned by * {@link #toArray this.toArray()}. * * @return a string of the form {@code "[0,1,2...]"} @@ -3718,8 +3720,10 @@ public abstract sealed class Float16Vector extends AbstractVector @ForceInline public final String toString() { - // now that toArray is strongly typed, we can define this - return Arrays.toString(toArray()); + // Render the lanes as Float16 values; Float16.toString produces + // human-readable text and canonicalizes NaN, Infinity and -0.0 + // independent of the underlying bit encoding. + return Arrays.toString(toFloat16Array()); } /** diff --git a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template index f11c6283685..00445cc8ac5 100644 --- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template +++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template @@ -5723,10 +5723,19 @@ public abstract sealed class $abstractvectortype$ extends AbstractVector<$Boxtyp * {@code "[0,1,2...]"}, reporting the lane values of this vector, * in lane order. * +#if[FP16] + * The string is produced as if by a call to {@link + * java.util.Arrays#toString(Object[]) Arrays.toString()}, + * as appropriate to a {@code Float16} array whose elements + * are obtained by applying {@link Float16#shortBitsToFloat16(short)} + * to each element of the {@code short[]} array returned by + * {@link #toArray this.toArray()}. +#else[FP16] * The string is produced as if by a call to {@link * java.util.Arrays#toString($type$[]) Arrays.toString()}, * as appropriate to the {@code $type$} array returned by * {@link #toArray this.toArray()}. +#end[FP16] * * @return a string of the form {@code "[0,1,2...]"} * reporting the lane values of this vector @@ -5735,8 +5744,15 @@ public abstract sealed class $abstractvectortype$ extends AbstractVector<$Boxtyp @ForceInline public final String toString() { +#if[FP16] + // Render the lanes as Float16 values; Float16.toString produces + // human-readable text and canonicalizes NaN, Infinity and -0.0 + // independent of the underlying bit encoding. + return Arrays.toString(toFloat16Array()); +#else[FP16] // now that toArray is strongly typed, we can define this return Arrays.toString(toArray()); +#end[FP16] } /** diff --git a/test/jdk/jdk/incubator/vector/Float16Vector128Tests.java b/test/jdk/jdk/incubator/vector/Float16Vector128Tests.java index a33e83d14ea..215f6ef9c47 100644 --- a/test/jdk/jdk/incubator/vector/Float16Vector128Tests.java +++ b/test/jdk/jdk/incubator/vector/Float16Vector128Tests.java @@ -5412,7 +5412,8 @@ public class Float16Vector128Tests extends AbstractVectorTest { String str = av.toString(); short subarr[] = Arrays.copyOfRange(a, i, i + SPECIES.length()); - Assert.assertTrue(str.equals(Arrays.toString(subarr)), "at index " + i + ", string should be = " + Arrays.toString(subarr) + ", but is = " + str); + String expectedStr = Arrays.toString(toFloat16Array(subarr)); + Assert.assertTrue(str.equals(expectedStr), "at index " + i + ", string should be = " + expectedStr + ", but is = " + str); } } diff --git a/test/jdk/jdk/incubator/vector/Float16Vector256Tests.java b/test/jdk/jdk/incubator/vector/Float16Vector256Tests.java index 99b167d4024..f198a4a970f 100644 --- a/test/jdk/jdk/incubator/vector/Float16Vector256Tests.java +++ b/test/jdk/jdk/incubator/vector/Float16Vector256Tests.java @@ -5412,7 +5412,8 @@ public class Float16Vector256Tests extends AbstractVectorTest { String str = av.toString(); short subarr[] = Arrays.copyOfRange(a, i, i + SPECIES.length()); - Assert.assertTrue(str.equals(Arrays.toString(subarr)), "at index " + i + ", string should be = " + Arrays.toString(subarr) + ", but is = " + str); + String expectedStr = Arrays.toString(toFloat16Array(subarr)); + Assert.assertTrue(str.equals(expectedStr), "at index " + i + ", string should be = " + expectedStr + ", but is = " + str); } } diff --git a/test/jdk/jdk/incubator/vector/Float16Vector512Tests.java b/test/jdk/jdk/incubator/vector/Float16Vector512Tests.java index 1c391497015..3d2b7de23f9 100644 --- a/test/jdk/jdk/incubator/vector/Float16Vector512Tests.java +++ b/test/jdk/jdk/incubator/vector/Float16Vector512Tests.java @@ -5412,7 +5412,8 @@ public class Float16Vector512Tests extends AbstractVectorTest { String str = av.toString(); short subarr[] = Arrays.copyOfRange(a, i, i + SPECIES.length()); - Assert.assertTrue(str.equals(Arrays.toString(subarr)), "at index " + i + ", string should be = " + Arrays.toString(subarr) + ", but is = " + str); + String expectedStr = Arrays.toString(toFloat16Array(subarr)); + Assert.assertTrue(str.equals(expectedStr), "at index " + i + ", string should be = " + expectedStr + ", but is = " + str); } } diff --git a/test/jdk/jdk/incubator/vector/Float16Vector64Tests.java b/test/jdk/jdk/incubator/vector/Float16Vector64Tests.java index 6ef651860ad..c44f0f7c576 100644 --- a/test/jdk/jdk/incubator/vector/Float16Vector64Tests.java +++ b/test/jdk/jdk/incubator/vector/Float16Vector64Tests.java @@ -5412,7 +5412,8 @@ public class Float16Vector64Tests extends AbstractVectorTest { String str = av.toString(); short subarr[] = Arrays.copyOfRange(a, i, i + SPECIES.length()); - Assert.assertTrue(str.equals(Arrays.toString(subarr)), "at index " + i + ", string should be = " + Arrays.toString(subarr) + ", but is = " + str); + String expectedStr = Arrays.toString(toFloat16Array(subarr)); + Assert.assertTrue(str.equals(expectedStr), "at index " + i + ", string should be = " + expectedStr + ", but is = " + str); } } diff --git a/test/jdk/jdk/incubator/vector/Float16VectorMaxTests.java b/test/jdk/jdk/incubator/vector/Float16VectorMaxTests.java index 61efa3de9a0..92945d74ceb 100644 --- a/test/jdk/jdk/incubator/vector/Float16VectorMaxTests.java +++ b/test/jdk/jdk/incubator/vector/Float16VectorMaxTests.java @@ -5418,7 +5418,8 @@ public class Float16VectorMaxTests extends AbstractVectorTest { String str = av.toString(); short subarr[] = Arrays.copyOfRange(a, i, i + SPECIES.length()); - Assert.assertTrue(str.equals(Arrays.toString(subarr)), "at index " + i + ", string should be = " + Arrays.toString(subarr) + ", but is = " + str); + String expectedStr = Arrays.toString(toFloat16Array(subarr)); + Assert.assertTrue(str.equals(expectedStr), "at index " + i + ", string should be = " + expectedStr + ", but is = " + str); } } diff --git a/test/jdk/jdk/incubator/vector/templates/Unit-Miscellaneous.template b/test/jdk/jdk/incubator/vector/templates/Unit-Miscellaneous.template index 0ae9342539f..b673e5e1fab 100644 --- a/test/jdk/jdk/incubator/vector/templates/Unit-Miscellaneous.template +++ b/test/jdk/jdk/incubator/vector/templates/Unit-Miscellaneous.template @@ -87,7 +87,12 @@ String str = av.toString(); $type$ subarr[] = Arrays.copyOfRange(a, i, i + SPECIES.length()); - Assert.assertTrue(str.equals(Arrays.toString(subarr)), "at index " + i + ", string should be = " + Arrays.toString(subarr) + ", but is = " + str); +#if[FP16] + String expectedStr = Arrays.toString(toFloat16Array(subarr)); +#else[FP16] + String expectedStr = Arrays.toString(subarr); +#end[FP16] + Assert.assertTrue(str.equals(expectedStr), "at index " + i + ", string should be = " + expectedStr + ", but is = " + str); } }