Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8277304: Java support for FP16 #164

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
@@ -116,12 +116,13 @@ public class VectorSupport {

// BasicType codes, for primitives only:
public static final int
T_FLOAT = 6,
T_DOUBLE = 7,
T_BYTE = 8,
T_SHORT = 9,
T_INT = 10,
T_LONG = 11;
T_HALFFLOAT = 5,
T_FLOAT = 6,
T_DOUBLE = 7,
T_BYTE = 8,
T_SHORT = 9,
T_INT = 10,
T_LONG = 11;

/* ============================================================================ */

Original file line number Diff line number Diff line change
@@ -298,7 +298,15 @@ AbstractVector<E> dummyVector() {
return makeDummyVector();
}
private AbstractVector<E> makeDummyVector() {
Object za = Array.newInstance(elementType(), laneCount);
Object za;
// FIXME: Remove the following special handling for
// Halffloat till Valhalla integration when Halffloat
// will become a primitive class.
if (elementType() == Halffloat.class) {
za = Array.newInstance(short.class, laneCount);
} else {
za = Array.newInstance(elementType(), laneCount);
}
return dummyVector = vectorFactory.apply(za);
// This is the only use of vectorFactory.
// All other factory requests are routed
@@ -610,6 +618,8 @@ AbstractSpecies<?> computeSpecies(LaneType laneType,
s = IntVector.species(shape); break;
case LaneType.SK_LONG:
s = LongVector.species(shape); break;
case LaneType.SK_HALFFLOAT:
s = HalffloatVector.species(shape); break;
}
if (s == null) {
// NOTE: The result of this method is guaranteed to be
Original file line number Diff line number Diff line change
@@ -250,6 +250,15 @@ public DoubleVector reinterpretAsDoubles() {
return (DoubleVector) asVectorRaw(LaneType.DOUBLE);
}

/**
* {@inheritDoc} <!--workaround-->
*/
@Override
@ForceInline
public HalffloatVector reinterpretAsHalffloats() {
return (HalffloatVector) asVectorRaw(LaneType.HALFFLOAT);
}

/**
* {@inheritDoc} <!--workaround-->
*/
@@ -521,6 +530,7 @@ AbstractVector<F> defaultReinterpret(AbstractSpecies<F> rsp) {
return FloatVector.fromByteBuffer(rsp.check(float.class), bb, 0, bo, m.check(float.class)).check0(rsp);
case LaneType.SK_DOUBLE:
return DoubleVector.fromByteBuffer(rsp.check(double.class), bb, 0, bo, m.check(double.class)).check0(rsp);
// FIXME: Add lanetype for Halffloat
default:
throw new AssertionError(rsp.toString());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.incubator.vector;

import jdk.internal.vm.annotation.IntrinsicCandidate;

/**
* A specialized {@link Vector} representing an ordered immutable sequence of
* {@code short} values.
* @author abc
* @version 1.0
* @since 10/01/2021
*/
@SuppressWarnings("serial")
public final class Halffloat extends Number implements Comparable<Halffloat>{
/** Definitions for FP16*/
public static final short MAX_VALUE = 0x7bff;
/** Definitions for FP16 */
public static final short MIN_VALUE = 0x400;
/** Definitions for FP16 */
public static final short POSITIVE_INFINITY = 0x7c00;
/** Definitions for FP16 */
public static final short NEGATIVE_INFINITY = (short)0xfc00;
/** Definitions for FP16*/
public static final short NaN = (short)0xffff;
/** Definitions for FP16*/
private static final float MAX_FLOAT_VALUE = 0x1.ffep+15f;
/** Definitions for FP16*/
private static final float MIN_FLOAT_VALUE = 0x1.004p-14f;
/** Definitions for FP16 */
public static final int SIZE = 16;
/** Definitions for FP16 */
public static final int BYTES = SIZE / Byte.SIZE;
/** Definitions for FP16 */
private final short value;

/**
* Returns a new Halffloat.
* @param f the species describing the element type
* @return short value of float provided
*/
public static Halffloat valueOf(short f) {
return new Halffloat(f);
}

/**
* Halffloat constructor
* @param value short value assigned to halffloat
*/
public Halffloat(short value) {
this.value = value;
}

/**
* Halffloat constructor
* @param f float value assigned to halffloat
*/
public Halffloat(float f) {
this.value = valueOf(f);
}

/**
* Returns floatvalue of a given short value.
* @return a float value of short provided
*/
public float floatValue() {
int val = (int)value;
float result;
switch(val) {
case Halffloat.POSITIVE_INFINITY:
result = Float.POSITIVE_INFINITY;
break;
case Halffloat.NEGATIVE_INFINITY:
result = Float.NEGATIVE_INFINITY;
break;
case Halffloat.NaN:
result = Float.NaN;
break;
default:
result = (Float.intBitsToFloat(((val&0x8000)<<16) | (((val&0x7c00)+0x1C000)<<13) | ((val&0x03FF)<<13)));
break;
}
return result;
}

/**
* Returns halffloat value of a given float.
* @param f float value to be converted into halffloat
* @return short value of float provided
*/
public static short valueOf(float f) {
if (f > Halffloat.MAX_FLOAT_VALUE) return Halffloat.POSITIVE_INFINITY;
if (Float.isNaN(f)) return Halffloat.NaN;

if (f < Halffloat.MIN_FLOAT_VALUE) return Halffloat.NEGATIVE_INFINITY;

int val = Float.floatToIntBits(f);
Copy link
Member

Choose a reason for hiding this comment

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

As per JVM specification of d2f semantics, "A finite value too small to be represented as a float is converted
to a zero of the same sign; a finite value too large to be represented
as a float is converted to an infinity of the same sign. A double
NaN is converted to a float NaN."

I think we can apply same semantics for halffloat values if floting point argument is beyond the halffloat range.

val = ((((val>>16)&0x8000)|((((val&0x7f800000)-0x38000000)>>13)&0x7c00)|((val>>13)&0x03ff)));
Copy link
Member

Choose a reason for hiding this comment

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

Kindly also add the link to paper.

return (short)val;
}

/** doublevalue */
public double doubleValue() {
return (double) floatValue();
}

/** longValue */
public long longValue() {
return (long) value;
}

/** IntValue */
public int intValue() {
return (int) value;
}

/**
* Returns the size, in bits, of vectors of this shape.
* @param bits the species describing the element type
* @return short value of float provided
*/
public static short shortBitsToHalffloat(short bits) {
return bits;
}
/**
* Returns the size, in bits, of vectors of this shape.
* @param bits the species describing the element type
* @return short value of float provided
*/
public static short shortToRawShortBits(short bits) {
return bits;
}
/**
* Returns the size, in bits, of vectors of this shape.
* @param bits the species describing the element type
* @return short value of float provided
*/
public static short shortToShortBits(short bits) {
return bits;
}

/**
Compares two halffloats
* @param hf value to be compared
* @return 0, 1, -1
*/
public int compareTo(Halffloat hf) {
float f1 = floatValue();
float f2 = hf.floatValue();
return Float.compare(f1, f2);
}
}
Loading