public static int compare(short x, short y) {
    boolean xIsNaN = isNaN(x);
    boolean yIsNaN = isNaN(y);

    if (!xIsNaN && !yIsNaN) {
      int first = ((x & SIGN_MASK) != 0 ? 0x8000 - (x & 0xffff) : x & 0xffff);
      int second = ((y & SIGN_MASK) != 0 ? 0x8000 - (y & 0xffff) : y & 0xffff);
      // Returns true if the first half-precision float value is less
      // (smaller toward negative infinity) than the second half-precision float value.
      if (first < second) {
        return -1;
      }

      // Returns true if the first half-precision float value is greater
      // (larger toward positive infinity) than the second half-precision float value.
      if (first > second) {
        return 1;
      }
    }

    // Collapse NaNs, akin to halfToIntBits(), but we want to keep
    // (signed) short value types to preserve the ordering of -0.0
    // and +0.0
    short xBits = xIsNaN ? NaN : x;
    short yBits = yIsNaN ? NaN : y;
    return (xBits == yBits ? 0 : (xBits < yBits ? -1 : 1));
  }
@Test
  public void testCompare() {
    assertEquals(0, Float16.compare(NaN, NaN));
    assertEquals(0, Float16.compare(NaN, (short) 0xfc98));
    assertEquals(1, Float16.compare(NaN, POSITIVE_INFINITY));
    assertEquals(-1, Float16.compare(POSITIVE_INFINITY, NaN));
    assertEquals(0, Float16.compare(POSITIVE_INFINITY, POSITIVE_INFINITY));
    assertEquals(0, Float16.compare(NEGATIVE_INFINITY, NEGATIVE_INFINITY));
    assertEquals(1, Float16.compare(POSITIVE_INFINITY, NEGATIVE_INFINITY));
    assertEquals(-1, Float16.compare(NEGATIVE_INFINITY, POSITIVE_INFINITY));
    assertEquals(0, Float16.compare(POSITIVE_ZERO, POSITIVE_ZERO));
    assertEquals(0, Float16.compare(NEGATIVE_ZERO, NEGATIVE_ZERO));
    assertEquals(1, Float16.compare(POSITIVE_ZERO, NEGATIVE_ZERO));
    assertEquals(-1, Float16.compare(NEGATIVE_ZERO, POSITIVE_ZERO));
    assertEquals(0, Float16.compare(Float16.toFloat16(12.462f), Float16.toFloat16(12.462f)));
    assertEquals(0, Float16.compare(Float16.toFloat16(-12.462f), Float16.toFloat16(-12.462f)));
    assertEquals(1, Float16.compare(Float16.toFloat16(12.462f), Float16.toFloat16(-12.462f)));
    assertEquals(-1, Float16.compare(Float16.toFloat16(-12.462f), Float16.toFloat16(12.462f)));
  }