Skip to content

Commit

Permalink
* Provide ByteIndexer with value getters and setters for unsigned …
Browse files Browse the repository at this point in the history
…`byte` or `short`, `half`, `bfloat16`, and `boolean` types as well
  • Loading branch information
saudet committed Oct 30, 2019
1 parent 8cc4144 commit 9239a17
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Provide `ByteIndexer` with value getters and setters for unsigned `byte` or `short`, `half`, `bfloat16`, and `boolean` types as well
* Introduce `PointerScope.extend()` to prevent deallocation on the next call to `close()`
* Make `Generator` avoid ambiguous conversion errors from `UniquePtrAdapter` to `std::unique_ptr` ([pull #353](https://github.com/bytedeco/javacpp/pull/353))
* Fix `Parser` using fully qualified names for `@Name` annotations of nested classes ([issue #352](https://github.com/bytedeco/javacpp/issues/352))
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/ByteArrayIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,85 @@ ByteBuffer getBuffer() {
return this;
}

@Override public int getUByte(long i) {
if (RAW != null) {
return RAW.getByte(array, checkIndex(i, array.length - 1)) & 0xFF;
} else {
return getBuffer().get((int)i) & 0xFF;
}
}
@Override public ByteIndexer putUByte(long i, int b) {
if (RAW != null) {
RAW.putByte(array, checkIndex(i, array.length - 1), (byte)b);
} else {
getBuffer().put((int)i, (byte)b);
}
return this;
}

@Override public int getUShort(long i) {
if (RAW != null) {
return RAW.getShort(array, checkIndex(i, array.length - 1)) & 0xFFFF;
} else {
return getBuffer().getShort((int)i) & 0xFFFF;
}
}
@Override public ByteIndexer putUShort(long i, int s) {
if (RAW != null) {
RAW.putShort(array, checkIndex(i, array.length - 1), (short)s);
} else {
getBuffer().putShort((int)i, (short)s);
}
return this;
}

@Override public float getHalf(long i) {
if (RAW != null) {
return HalfIndexer.toFloat(RAW.getShort(array, checkIndex(i, array.length - 1)));
} else {
return HalfIndexer.toFloat(getBuffer().getShort((int)i));
}
}
@Override public ByteIndexer putHalf(long i, float h) {
if (RAW != null) {
RAW.putShort(array, checkIndex(i, array.length - 1), (short)HalfIndexer.fromFloat(h));
} else {
getBuffer().putShort((int)i, (short)HalfIndexer.fromFloat(h));
}
return this;
}

@Override public float getBfloat16(long i) {
if (RAW != null) {
return Bfloat16Indexer.toFloat(RAW.getShort(array, checkIndex(i, array.length - 1)));
} else {
return Bfloat16Indexer.toFloat(getBuffer().getShort((int)i));
}
}
@Override public ByteIndexer putBfloat16(long i, float h) {
if (RAW != null) {
RAW.putShort(array, checkIndex(i, array.length - 1), (short)Bfloat16Indexer.fromFloat(h));
} else {
getBuffer().putShort((int)i, (short)Bfloat16Indexer.fromFloat(h));
}
return this;
}

@Override public boolean getBoolean(long i) {
if (RAW != null) {
return RAW.getByte(array, checkIndex(i, array.length - 1)) != 0;
} else {
return getBuffer().get((int)i) != 0;
}
}
@Override public ByteIndexer putBoolean(long i, boolean b) {
if (RAW != null) {
RAW.putByte(array, checkIndex(i, array.length - 1), b ? (byte)1 : (byte)0);
} else {
getBuffer().put((int)i, b ? (byte)1 : (byte)0);
}
return this;
}

@Override public void release() { array = null; }
}
40 changes: 40 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/ByteBufferIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,45 @@ public ByteBufferIndexer(ByteBuffer buffer, long[] sizes, long[] strides) {
return this;
}

@Override public int getUByte(long i) {
return buffer.get((int)i) & 0xFF;
}
@Override public ByteIndexer putUByte(long i, int b) {
buffer.put((int)i, (byte)b);
return this;
}

@Override public int getUShort(long i) {
return buffer.getShort((int)i) & 0xFFFF;
}
@Override public ByteIndexer putUShort(long i, int s) {
buffer.putShort((int)i, (short)s);
return this;
}

@Override public float getHalf(long i) {
return HalfIndexer.toFloat(buffer.getShort((int)i));
}
@Override public ByteIndexer putHalf(long i, float h) {
buffer.putShort((int)i, (short)HalfIndexer.fromFloat(h));
return this;
}

@Override public float getBfloat16(long i) {
return Bfloat16Indexer.toFloat(buffer.getShort((int)i));
}
@Override public ByteIndexer putBfloat16(long i, float h) {
buffer.putShort((int)i, (short)Bfloat16Indexer.fromFloat(h));
return this;
}

@Override public boolean getBoolean(long i) {
return buffer.get((int)i) != 0;
}
@Override public ByteIndexer putBoolean(long i, boolean b) {
buffer.put((int)i, b ? (byte)1 : (byte)0);
return this;
}

@Override public void release() { buffer = null; }
}
25 changes: 25 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/ByteIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,31 @@ public static ByteIndexer create(final BytePointer pointer, long[] sizes, long[]
/** Sets the {@code char} value at {@code array/buffer[i]} */
public abstract ByteIndexer putChar(long i, char c);

/** Returns the {@code byte} value at {@code array/buffer[i]}, treated as unsigned */
public abstract int getUByte(long i);
/** Sets the {@code byte} value at {@code array/buffer[i]}, treated as unsigned */
public abstract ByteIndexer putUByte(long i, int b);

/** Returns the {@code short} value at {@code array/buffer[i]}, treated as unsigned */
public abstract int getUShort(long i);
/** Sets the {@code short} value at {@code array/buffer[i]}, treated as unsigned */
public abstract ByteIndexer putUShort(long i, int s);

/** Returns the {@code short} value at {@code array/buffer[i]}, treated as half-precision float */
public abstract float getHalf(long i);
/** Sets the {@code short} value at {@code array/buffer[i]}, treated as half-precision float */
public abstract ByteIndexer putHalf(long i, float h);

/** Returns the {@code short} value at {@code array/buffer[i]}, treated as bfloat16 */
public abstract float getBfloat16(long i);
/** Sets the {@code short} value at {@code array/buffer[i]}, treated as bfloat16 */
public abstract ByteIndexer putBfloat16(long i, float h);

/** Returns the {@code boolean} value at {@code array/buffer[i]} */
public abstract boolean getBoolean(long i);
/** Sets the {@code boolean} value at {@code array/buffer[i]} */
public abstract ByteIndexer putBoolean(long i, boolean b);

@Override public double getDouble(long... indices) { return get(indices); }
@Override public ByteIndexer putDouble(long[] indices, double b) { return put(indices, (byte)b); }
}
40 changes: 40 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/ByteRawIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,45 @@ public ByteRawIndexer(BytePointer pointer, long[] sizes, long[] strides) {
return this;
}

@Override public int getUByte(long i) {
return RAW.getByte(base + checkIndex(i, size - 1)) & 0xFF;
}
@Override public ByteIndexer putUByte(long i, int b) {
RAW.putByte(base + checkIndex(i, size - 1), (byte)b);
return this;
}

@Override public int getUShort(long i) {
return RAW.getShort(base + checkIndex(i, size - 1)) & 0xFFFF;
}
@Override public ByteIndexer putUShort(long i, int s) {
RAW.putShort(base + checkIndex(i, size - 1), (short)s);
return this;
}

@Override public float getHalf(long i) {
return HalfIndexer.toFloat(RAW.getShort(base + checkIndex(i, size - 1)));
}
@Override public ByteIndexer putHalf(long i, float h) {
RAW.putShort(base + checkIndex(i, size - 1), (short)HalfIndexer.fromFloat(h));
return this;
}

@Override public float getBfloat16(long i) {
return Bfloat16Indexer.toFloat(RAW.getShort(base + checkIndex(i, size - 1)));
}
@Override public ByteIndexer putBfloat16(long i, float h) {
RAW.putShort(base + checkIndex(i, size - 1), (short)Bfloat16Indexer.fromFloat(h));
return this;
}

@Override public boolean getBoolean(long i) {
return RAW.getByte(base + checkIndex(i, size - 1)) != 0;
}
@Override public ByteIndexer putBoolean(long i, boolean b) {
RAW.putByte(base + checkIndex(i, size - 1), b ? (byte)1 : (byte)0);
return this;
}

@Override public void release() { pointer = null; }
}
16 changes: 15 additions & 1 deletion src/test/java/org/bytedeco/javacpp/IndexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public class IndexerTest {
fail("IndexOutOfBoundsException should have been thrown.");
} catch (IndexOutOfBoundsException e) { }

byte byteValue = 0x02;
short shortValue = 0x0203;
int intValue = 0x02030405;
long longValue = 0x0203040506070809L;
Expand All @@ -134,13 +135,21 @@ public class IndexerTest {
}
float floatValue = Float.intBitsToFloat(intValue);
double doubleValue = Double.longBitsToDouble(longValue);
float halfValue = HalfIndexer.toFloat(shortValue);
float bfloat16Value = Bfloat16Indexer.toFloat(shortValue);
boolean booleanValue = byteValue != 0;

assertEquals(shortValue, arrayIndexer.getShort(1));
assertEquals(intValue, arrayIndexer.getInt(1));
assertEquals(longValue, arrayIndexer.getLong(1));
assertEquals(floatValue, arrayIndexer.getFloat(1), 0.0);
assertEquals(doubleValue, arrayIndexer.getDouble(1), 0.0);
assertEquals(shortValue, arrayIndexer.getChar(1));
assertEquals(byteValue, arrayIndexer.getUByte(1));
assertEquals(shortValue, arrayIndexer.getUShort(1));
assertEquals(halfValue, arrayIndexer.getHalf(1), 0.0);
assertEquals(bfloat16Value, arrayIndexer.getBfloat16(1), 0.0);
assertEquals(booleanValue, arrayIndexer.getBoolean(1));

System.out.println("arrayIndexer" + arrayIndexer);
System.out.println("directIndexer" + directIndexer);
Expand All @@ -157,7 +166,12 @@ public class IndexerTest {
assertEquals(longValue, directIndexer.getLong(1));
assertEquals(floatValue, directIndexer.getFloat(1), 0.0);
assertEquals(doubleValue, directIndexer.getDouble(1), 0.0);
assertEquals((char)shortValue, directIndexer.getChar(1));
assertEquals(shortValue, directIndexer.getChar(1));
assertEquals(byteValue, directIndexer.getUByte(1));
assertEquals(shortValue, directIndexer.getUShort(1));
assertEquals(halfValue, directIndexer.getHalf(1), 0.0);
assertEquals(bfloat16Value, directIndexer.getBfloat16(1), 0.0);
assertEquals(booleanValue, directIndexer.getBoolean(1));

System.gc();

Expand Down

0 comments on commit 9239a17

Please sign in to comment.