Skip to content

Commit 6b7b107

Browse files
Smita KamathSandhya Viswanathan
authored andcommitted
8277304: Java support for FP16
Reviewed-by: sviswanathan
1 parent 302d35f commit 6b7b107

File tree

19 files changed

+9182
-82
lines changed

19 files changed

+9182
-82
lines changed

src/java.base/share/classes/jdk/internal/vm/vector/VectorSupport.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ public class VectorSupport {
116116

117117
// BasicType codes, for primitives only:
118118
public static final int
119-
T_FLOAT = 6,
120-
T_DOUBLE = 7,
121-
T_BYTE = 8,
122-
T_SHORT = 9,
123-
T_INT = 10,
124-
T_LONG = 11;
119+
T_HALFFLOAT = 5,
120+
T_FLOAT = 6,
121+
T_DOUBLE = 7,
122+
T_BYTE = 8,
123+
T_SHORT = 9,
124+
T_INT = 10,
125+
T_LONG = 11;
125126

126127
/* ============================================================================ */
127128

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/AbstractSpecies.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,15 @@ AbstractVector<E> dummyVector() {
298298
return makeDummyVector();
299299
}
300300
private AbstractVector<E> makeDummyVector() {
301-
Object za = Array.newInstance(elementType(), laneCount);
301+
Object za;
302+
// FIXME: Remove the following special handling for
303+
// Halffloat till Valhalla integration when Halffloat
304+
// will become a primitive class.
305+
if (elementType() == Halffloat.class) {
306+
za = Array.newInstance(short.class, laneCount);
307+
} else {
308+
za = Array.newInstance(elementType(), laneCount);
309+
}
302310
return dummyVector = vectorFactory.apply(za);
303311
// This is the only use of vectorFactory.
304312
// All other factory requests are routed
@@ -610,6 +618,8 @@ AbstractSpecies<?> computeSpecies(LaneType laneType,
610618
s = IntVector.species(shape); break;
611619
case LaneType.SK_LONG:
612620
s = LongVector.species(shape); break;
621+
case LaneType.SK_HALFFLOAT:
622+
s = HalffloatVector.species(shape); break;
613623
}
614624
if (s == null) {
615625
// NOTE: The result of this method is guaranteed to be

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/AbstractVector.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ public DoubleVector reinterpretAsDoubles() {
250250
return (DoubleVector) asVectorRaw(LaneType.DOUBLE);
251251
}
252252

253+
/**
254+
* {@inheritDoc} <!--workaround-->
255+
*/
256+
@Override
257+
@ForceInline
258+
public HalffloatVector reinterpretAsHalffloats() {
259+
return (HalffloatVector) asVectorRaw(LaneType.HALFFLOAT);
260+
}
261+
253262
/**
254263
* {@inheritDoc} <!--workaround-->
255264
*/
@@ -521,6 +530,7 @@ AbstractVector<F> defaultReinterpret(AbstractSpecies<F> rsp) {
521530
return FloatVector.fromByteBuffer(rsp.check(float.class), bb, 0, bo, m.check(float.class)).check0(rsp);
522531
case LaneType.SK_DOUBLE:
523532
return DoubleVector.fromByteBuffer(rsp.check(double.class), bb, 0, bo, m.check(double.class)).check0(rsp);
533+
// FIXME: Add lanetype for Halffloat
524534
default:
525535
throw new AssertionError(rsp.toString());
526536
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
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+
package jdk.incubator.vector;
26+
27+
import jdk.internal.vm.annotation.IntrinsicCandidate;
28+
29+
/**
30+
* A specialized {@link Vector} representing an ordered immutable sequence of
31+
* {@code short} values.
32+
* @author abc
33+
* @version 1.0
34+
* @since 10/01/2021
35+
*/
36+
@SuppressWarnings("serial")
37+
public final class Halffloat extends Number implements Comparable<Halffloat>{
38+
/** Definitions for FP16*/
39+
public static final short MAX_VALUE = 0x7bff;
40+
/** Definitions for FP16 */
41+
public static final short MIN_VALUE = 0x400;
42+
/** Definitions for FP16 */
43+
public static final short POSITIVE_INFINITY = 0x7c00;
44+
/** Definitions for FP16 */
45+
public static final short NEGATIVE_INFINITY = (short)0xfc00;
46+
/** Definitions for FP16*/
47+
public static final short NaN = (short)0xffff;
48+
/** Definitions for FP16*/
49+
private static final float MAX_FLOAT_VALUE = 0x1.ffep+15f;
50+
/** Definitions for FP16*/
51+
private static final float MIN_FLOAT_VALUE = 0x1.004p-14f;
52+
/** Definitions for FP16 */
53+
public static final int SIZE = 16;
54+
/** Definitions for FP16 */
55+
public static final int BYTES = SIZE / Byte.SIZE;
56+
/** Definitions for FP16 */
57+
private final short value;
58+
59+
/**
60+
* Returns a new Halffloat.
61+
* @param f the species describing the element type
62+
* @return short value of float provided
63+
*/
64+
public static Halffloat valueOf(short f) {
65+
return new Halffloat(f);
66+
}
67+
68+
/**
69+
* Halffloat constructor
70+
* @param value short value assigned to halffloat
71+
*/
72+
public Halffloat(short value) {
73+
this.value = value;
74+
}
75+
76+
/**
77+
* Halffloat constructor
78+
* @param f float value assigned to halffloat
79+
*/
80+
public Halffloat(float f) {
81+
this.value = valueOf(f);
82+
}
83+
84+
/**
85+
* Returns floatvalue of a given short value.
86+
* @return a float value of short provided
87+
*/
88+
public float floatValue() {
89+
int val = (int)value;
90+
float result;
91+
switch(val) {
92+
case Halffloat.POSITIVE_INFINITY:
93+
result = Float.POSITIVE_INFINITY;
94+
break;
95+
case Halffloat.NEGATIVE_INFINITY:
96+
result = Float.NEGATIVE_INFINITY;
97+
break;
98+
case Halffloat.NaN:
99+
result = Float.NaN;
100+
break;
101+
default:
102+
result = (Float.intBitsToFloat(((val&0x8000)<<16) | (((val&0x7c00)+0x1C000)<<13) | ((val&0x03FF)<<13)));
103+
break;
104+
}
105+
return result;
106+
}
107+
108+
/**
109+
* Returns halffloat value of a given float.
110+
* @param f float value to be converted into halffloat
111+
* @return short value of float provided
112+
*/
113+
public static short valueOf(float f) {
114+
if (f > Halffloat.MAX_FLOAT_VALUE) return Halffloat.POSITIVE_INFINITY;
115+
if (Float.isNaN(f)) return Halffloat.NaN;
116+
117+
if (f < Halffloat.MIN_FLOAT_VALUE) return Halffloat.NEGATIVE_INFINITY;
118+
119+
int val = Float.floatToIntBits(f);
120+
val = ((((val>>16)&0x8000)|((((val&0x7f800000)-0x38000000)>>13)&0x7c00)|((val>>13)&0x03ff)));
121+
return (short)val;
122+
}
123+
124+
/** doublevalue */
125+
public double doubleValue() {
126+
return (double) floatValue();
127+
}
128+
129+
/** longValue */
130+
public long longValue() {
131+
return (long) value;
132+
}
133+
134+
/** IntValue */
135+
public int intValue() {
136+
return (int) value;
137+
}
138+
139+
/**
140+
* Returns the size, in bits, of vectors of this shape.
141+
* @param bits the species describing the element type
142+
* @return short value of float provided
143+
*/
144+
public static short shortBitsToHalffloat(short bits) {
145+
return bits;
146+
}
147+
/**
148+
* Returns the size, in bits, of vectors of this shape.
149+
* @param bits the species describing the element type
150+
* @return short value of float provided
151+
*/
152+
public static short shortToRawShortBits(short bits) {
153+
return bits;
154+
}
155+
/**
156+
* Returns the size, in bits, of vectors of this shape.
157+
* @param bits the species describing the element type
158+
* @return short value of float provided
159+
*/
160+
public static short shortToShortBits(short bits) {
161+
return bits;
162+
}
163+
164+
/**
165+
Compares two halffloats
166+
* @param hf value to be compared
167+
* @return 0, 1, -1
168+
*/
169+
public int compareTo(Halffloat hf) {
170+
float f1 = floatValue();
171+
float f2 = hf.floatValue();
172+
return Float.compare(f1, f2);
173+
}
174+
}

0 commit comments

Comments
 (0)