|
| 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