-
Notifications
You must be signed in to change notification settings - Fork 42
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
+9,182
−82
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
afa7567
FP16 Java support
smita-kamath cda8b06
Fixed space and tabs in code
smita-kamath 89c872d
Updated code as per review comment
smita-kamath 2479ce4
Modified gen-src to add Halffloat as new element type. This change re…
smita-kamath 102a4ee
changes to Halffloat class
smita-kamath 84033ba
Added new variable in gen-src that cleans up redundant code
smita-kamath 0d391f7
Code cleanup
smita-kamath 17aaf80
Code cleanup
smita-kamath ffb9a62
Changed file mode of gen-src.sh
smita-kamath 8e849c2
Added handling for special cases
smita-kamath 4f5b106
Changes to Halffloat class
smita-kamath 536b40a
Fixing jtreg failures
smita-kamath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Halffloat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
val = ((((val>>16)&0x8000)|((((val&0x7f800000)-0x38000000)>>13)&0x7c00)|((val>>13)&0x03ff))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.