-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVulnBaseDataBuffer.java
1971 lines (1743 loc) · 64.6 KB
/
VulnBaseDataBuffer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.nd4j.linalg.api.buffer;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.indexer.*;
import org.nd4j.common.config.ND4JSystemProperties;
import org.nd4j.linalg.api.memory.MemoryWorkspace;
import org.nd4j.common.primitives.AtomicBoolean;
import org.nd4j.common.primitives.AtomicDouble;
import org.nd4j.common.primitives.Triple;
import org.nd4j.common.util.ArrayUtil;
import java.io.*;
import java.nio.*;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
@Slf4j
public abstract class BaseDataBuffer implements DataBuffer {
/**
* @deprecated Use {@link ND4JSystemProperties#DATABUFFER_TO_STRING_MAX_ELEMENTS}
*/
public static String TO_STRING_MAX_ELEMENTS = ND4JSystemProperties.DATABUFFER_TO_STRING_MAX_ELEMENTS;
private static int TO_STRING_MAX;
static {
String s = System.getProperty(ND4JSystemProperties.DATABUFFER_TO_STRING_MAX_ELEMENTS);
if(s != null ){
try {
TO_STRING_MAX = Integer.parseInt(s);
} catch (NumberFormatException e){
log.warn("Invalid value for key {}: \"{}\"", ND4JSystemProperties.DATABUFFER_TO_STRING_MAX_ELEMENTS, s);
TO_STRING_MAX = 1000;
}
} else {
TO_STRING_MAX = 1000;
}
}
protected DataType type;
protected long length;
protected long underlyingLength;
protected long offset;
protected byte elementSize;
//protected transient ByteBuffer wrappedBuffer;
protected transient DataBuffer wrappedDataBuffer;
protected transient long workspaceGenerationId = 0L;
protected AllocationMode allocationMode;
protected transient Indexer indexer = null;
protected transient Pointer pointer = null;
protected transient boolean attached = false;
protected transient MemoryWorkspace parentWorkspace;
// Allocator-related stuff. Moved down here to avoid opType casting.
protected transient DataBuffer originalBuffer;
protected transient long originalOffset = 0;
protected transient boolean constant = false;
protected transient boolean released = false;
protected transient AtomicBoolean referenced = new AtomicBoolean(false);
//protected transient Collection<WeakReference<BaseDataBuffer>> references = new ArrayList<>();
public BaseDataBuffer() {}
/**
* Initialize the opType of this buffer
*/
protected abstract void initTypeAndSize();
@Override
public int getElementSize() {
return elementSize;
}
@Override
public long getGenerationId() {
if(parentWorkspace != null){
return workspaceGenerationId;
} else if(wrappedDataBuffer != null && wrappedDataBuffer.isAttached()){
return wrappedDataBuffer.getGenerationId();
} else if(originalBuffer != null && originalBuffer.isAttached()){
return originalBuffer.getGenerationId();
}
return workspaceGenerationId;
}
/**
*
* Meant for creating another view of a buffer
* @param pointer the underlying buffer to create a view from
* @param indexer the indexer for the pointer
* @param length the length of the view
*/
public BaseDataBuffer(Pointer pointer, Indexer indexer, long length) {
if (length < 0)
throw new IllegalArgumentException("Length must be >= 0");
initTypeAndSize();
this.length = length;
this.allocationMode = AllocationMode.MIXED_DATA_TYPES;
this.underlyingLength = length;
this.wrappedDataBuffer = this;
if (length > 0) {
this.pointer = pointer;
setIndexer(indexer);
}
}
protected void setIndexer(Indexer indexer) {
this.indexer = indexer;
}
protected void pickReferent(BaseDataBuffer referent) {
referenced.compareAndSet(false, true);
//references.add(new WeakReference<BaseDataBuffer>(this));
}
/**
*
* Meant for creating another view of a buffer
* @param underlyingBuffer the underlying buffer to create a view from
* @param length the length of the view
* @param offset the offset for the view
*/
protected BaseDataBuffer(DataBuffer underlyingBuffer, long length, long offset) {
if (length < 0)
throw new IllegalArgumentException("Length must be >= 0");
if (length == 0)
length = 1;
initTypeAndSize();
this.length = length;
this.offset = offset;
this.allocationMode = underlyingBuffer.allocationMode();
this.elementSize = (byte) underlyingBuffer.getElementSize();
this.underlyingLength = underlyingBuffer.underlyingLength();
this.wrappedDataBuffer = underlyingBuffer;
// we're not referencing constant buffers
if (!underlyingBuffer.isConstant())
((BaseDataBuffer) underlyingBuffer).pickReferent(this);
// Adding link to original databuffer
if (underlyingBuffer.originalDataBuffer() == null) {
this.originalBuffer = underlyingBuffer;
this.originalOffset = offset;
} else {
this.originalBuffer = underlyingBuffer.originalDataBuffer();
// FIXME: please don't remove this comment, since there's probably a bug in current offset() impl,
// and this line will change originalOffset accroding to proper offset() impl
// FIXME: [email protected]
this.originalOffset = offset; // + underlyingBuffer.originalOffset();
}
pointer = underlyingBuffer.pointer();
setIndexer(underlyingBuffer.indexer());
}
/**
* Original DataBuffer.
* In case if we have a view derived from another view, derived from some other view, original DataBuffer will point to the originating DataBuffer, where all views come from.
*/
@Override
public DataBuffer originalDataBuffer() {
return originalBuffer;
}
//sets the nio wrapped buffer (allows to be overridden for other use cases like cuda)
protected void setNioBuffer() {
if (elementSize * length >= Integer.MAX_VALUE)
throw new IllegalArgumentException("Unable to create buffer of length " + length);
//wrappedBuffer = pointer().asByteBuffer();
}
/**
* Returns the indexer for the buffer
*
* @return
*/
@Override
public Indexer indexer() {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
return indexer;
}
@Override
public Pointer pointer() {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
if (underlyingDataBuffer() != null && underlyingDataBuffer() != this) {
if (underlyingDataBuffer().wasClosed())
throw new IllegalStateException("You can't use DataBuffer once it was released");
return underlyingDataBuffer().pointer();
} else {
if (underlyingDataBuffer() != null)
if (((BaseDataBuffer) underlyingDataBuffer()).released)
throw new IllegalStateException("Underlying buffer was released via close() call");
if (released)
throw new IllegalStateException("This buffer was already released via close() call");
return pointer;
}
}
@Override
public DataBuffer underlyingDataBuffer() {
return wrappedDataBuffer;
}
@Override
public long offset() {
return offset;
}
@Override
public AllocationMode allocationMode() {
return allocationMode;
}
@Override
@Deprecated
public void persist() {
throw new UnsupportedOperationException();
}
@Override
@Deprecated
public boolean isPersist() {
throw new UnsupportedOperationException();
}
@Override
@Deprecated
public void unPersist() {
throw new UnsupportedOperationException();
}
protected void fillPointerWithZero() {
Pointer.memset(this.pointer(), 0, getElementSize() * length());
}
@Override
public void copyAtStride(DataBuffer buf, long n, long stride, long yStride, long offset, long yOffset) {
if (dataType() == DataType.FLOAT) {
for (int i = 0; i < n; i++) {
put(offset + i * stride, buf.getFloat(yOffset + i * yStride));
}
} else {
for (int i = 0; i < n; i++) {
put(offset + i * stride, buf.getDouble(yOffset + i * yStride));
}
}
}
@Override
@Deprecated
public void removeReferencing(String id) {
//referencing.remove(id);
}
@Override
@Deprecated
public Collection<String> references() {
throw new UnsupportedOperationException();
//return referencing;
}
public abstract Pointer addressPointer();
/*
@Override
public Pointer addressPointer() {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
if (offset() > 0) {
Pointer ret;
// offset is accounted at native side
final long retAddress = pointer().address();
// directly set address at construction since Pointer.address has not setter.
if (dataType() == DataType.DOUBLE) {
ret = new DoublePointer(pointer()) {
{
address = retAddress;
}
};
} else if (dataType() == DataType.FLOAT) {
ret = new FloatPointer(pointer()) {
{
address = retAddress;
}
};
} else if (dataType() == DataType.INT) {
ret = new IntPointer(pointer()) {
{
address = retAddress;
}
};
} else if (dataType() == DataType.LONG) {
ret = new LongPointer(pointer()) {
{
address = retAddress;
}
};
} else {
ret = new Pointer(pointer()) {
{
address = retAddress;
}
};
}
ret.limit(ret.limit() - offset());
ret.capacity(ret.capacity() - offset());
return ret;
}
return pointer();
}
*/
@Override
public long address() {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
return pointer().address();
}
@Override
@Deprecated
public void addReferencing(String id) {
//referencing.add(id);
}
@Override
public void assign(long[] indices, float[] data, boolean contiguous, long inc) {
if (indices.length != data.length)
throw new IllegalArgumentException("Indices and data length must be the same");
if (indices.length > length())
throw new IllegalArgumentException("More elements than space to assign. This buffer is of length "
+ length() + " where the indices are of length " + data.length);
for (int i = 0; i < indices.length; i++) {
put(indices[i], data[i]);
}
}
@Override
public void setData(int[] data) {
for (int i = 0; i < data.length; i++) {
put(i, data[i]);
}
}
@Override
public void setData(float[] data) {
for (int i = 0; i < data.length; i++) {
put(i, data[i]);
}
}
@Override
public void setData(double[] data) {
for (int i = 0; i < data.length; i++) {
put(i, data[i]);
}
}
@Override
public void setData(long[] data) {
for (int i = 0; i < data.length; i++) {
put(i, data[i]);
}
}
@Override
public void setData(byte[] data) {
for (int i = 0; i < data.length; i++) {
put(i, data[i]);
}
}
@Override
public void setData(short[] data) {
for (int i = 0; i < data.length; i++) {
put(i, data[i]);
}
}
@Override
public void setData(boolean[] data) {
for (int i = 0; i < data.length; i++) {
put(i, data[i]);
}
}
@Override
public void assign(long[] indices, double[] data, boolean contiguous, long inc) {
if (indices.length != data.length)
throw new IllegalArgumentException("Indices and data length must be the same");
if (indices.length > length())
throw new IllegalArgumentException("More elements than space to assign. This buffer is of length "
+ length() + " where the indices are of length " + data.length);
for (int i = 0; i < indices.length; i += inc) {
put(indices[i], data[i]);
}
}
@Override
public void assign(DataBuffer data) {
if (data.length() != length())
throw new IllegalArgumentException("Unable to assign buffer of length " + data.length()
+ " to this buffer of length " + length());
for (int i = 0; i < data.length(); i++) {
put(i, data.getDouble(i));
}
}
@Override
public void assign(long[] indices, float[] data, boolean contiguous) {
assign(indices, data, contiguous, 1);
}
@Override
public void assign(long[] indices, double[] data, boolean contiguous) {
assign(indices, data, contiguous, 1);
}
@Override
public long underlyingLength() {
return underlyingLength;
}
@Override
public long length() {
return length;
}
@Override
public void assign(Number value) {
assign(value, 0);
}
@Override
public double[] getDoublesAt(long offset, long inc, int length) {
if (offset + length > length())
length -= offset;
double[] ret = new double[length];
for (int i = 0; i < length; i++) {
ret[i] = getDouble(i * inc + offset);
}
return ret;
}
@Override
public double[] getDoublesAt(long offset, int length) {
return getDoublesAt(offset, 1, length);
}
@Override
public float[] getFloatsAt(long offset, int length) {
return getFloatsAt(offset, 1, length);
}
@Override
public float[] getFloatsAt(long offset, long inc, int length) {
if (offset + length > length())
length -= offset;
float[] ret = new float[length];
for (int i = 0; i < length; i ++) {
ret[i] = getFloat(i * inc + offset);
}
return ret;
}
@Override
public long[] getLongsAt(long offset, int length) {
return getLongsAt(offset, 1, length);
}
@Override
public long[] getLongsAt(long offset, long inc, int length) {
if (offset + length > length())
length -= offset;
long[] ret = new long[length];
for (int i = 0; i < length; i++) {
ret[i] = getLong(i * inc + offset);
}
return ret;
}
@Override
public int[] getIntsAt(long offset, int length) {
return getIntsAt(offset, 1, length);
}
@Override
public int[] getIntsAt(long offset, long inc, int length) {
if (offset + length > length())
length -= offset;
int[] ret = new int[length];
for (int i = 0; i < length; i++) {
ret[i] = getInt(i * inc + offset);
}
return ret;
}
@Override
public DataBuffer dup() {
DataBuffer ret = create(length);
for (int i = 0; i < ret.length(); i++)
ret.put(i, getDouble(i));
return ret;
}
/**
* Create with length
* @param length a databuffer of the same opType as
* this with the given length
* @return a data buffer with the same length and datatype as this one
*/
protected abstract DataBuffer create(long length);
/**
* Create the data buffer
* with respect to the given byte buffer
* @param data the buffer to create
* @return the data buffer based on the given buffer
*/
public abstract DataBuffer create(double[] data);
/**
* Create the data buffer
* with respect to the given byte buffer
* @param data the buffer to create
* @return the data buffer based on the given buffer
*/
public abstract DataBuffer create(float[] data);
/**
* Create the data buffer
* with respect to the given byte buffer
* @param data the buffer to create
* @return the data buffer based on the given buffer
*/
public abstract DataBuffer create(int[] data);
@Override
public void assign(long[] offsets, long[] strides, DataBuffer... buffers) {
assign(offsets, strides, length(), buffers);
}
@Override
public byte[] asBytes() {
//NOTE: DataOutputStream is big endian
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
val dataType = dataType();
switch (dataType) {
case DOUBLE:
try {
for (int i = 0; i < length(); i++) {
dos.writeDouble(getDouble(i));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case FLOAT:
try {
for (int i = 0; i < length(); i++) {
dos.writeFloat(getFloat(i));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case HALF:
try {
for (int i = 0; i < length(); i++) {
dos.writeShort(HalfIndexer.fromFloat(getFloat(i)));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case BOOL:
try {
for (int i = 0; i < length(); i++) {
dos.writeByte(getInt(i) == 0 ? (byte) 0 : (byte) 1);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case BYTE:
try {
for (int i = 0; i < length(); i++) {
dos.writeByte((byte) getShort(i));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case UBYTE:
try {
UByteIndexer u = (UByteIndexer) indexer;
for (int i = 0; i < length(); i++) {
dos.writeByte(u.get(i));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case SHORT:
try{
for (int i = 0; i < length(); i++) {
dos.writeShort(getShort(i));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case INT:
try {
for (int i = 0; i < length(); i++) {
dos.writeInt(getInt(i));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case LONG:
try {
for (int i = 0; i < length(); i++) {
dos.writeLong(getLong(i));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case BFLOAT16:
case UINT16:
//Treat BFloat16 and UINT16 as bytes
byte[] temp = new byte[(int)(2*length)];
asNio().get(temp);
try {
if(ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
//Switch endianness to big endian
for (int i = 0; i < temp.length / 2; i++) {
dos.write(temp[2 * i + 1]);
dos.write(temp[2 * i]);
}
} else {
//Keep as big endian
dos.write(temp);
}
} catch (IOException e){
throw new RuntimeException(e);
}
break;
case UINT64:
//Treat unsigned long (UINT64) as 8 bytes
byte[] temp2 = new byte[(int)(8*length)];
asNio().get(temp2);
try {
if(ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
//Switch endianness to big endian
for (int i = 0; i < temp2.length / 8; i++) {
for( int j=0; j<8; j++ ){
dos.write(temp2[8 * i + (7-j)]);
}
}
} else {
//Keep as big endian
dos.write(temp2);
}
} catch (IOException e){
throw new RuntimeException(e);
}
break;
case UINT32:
//Treat unsigned integer (UINT32) as 4 bytes
byte[] temp3 = new byte[(int)(4*length)];
asNio().get(temp3);
try {
if(ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
//Switch endianness to big endian
for (int i = 0; i < temp3.length / 4; i++) {
for( int j=0; j<4; j++ ){
dos.write(temp3[4 * i + (3-j)]);
}
}
} else {
//Keep as big endian
dos.write(temp3);
}
} catch (IOException e){
throw new RuntimeException(e);
}
break;
case UTF8:
byte[] temp4 = new byte[(int)length];
asNio().get(temp4);
try {
dos.write(temp4);
} catch (IOException e){
throw new RuntimeException(e);
}
break;
default:
throw new UnsupportedOperationException("Unknown data type: [" + dataType + "]");
}
return bos.toByteArray();
}
@Override
public float[] asFloat() {
if (length >= Integer.MAX_VALUE)
throw new IllegalArgumentException("Unable to create array of length " + length);
float[] ret = new float[(int) length];
for (int i = 0; i < length; i++)
ret[i] = getFloatUnsynced(i);
return ret;
}
@Override
public double[] asDouble() {
if (length >= Integer.MAX_VALUE)
throw new IllegalArgumentException("Unable to create array of length " + length);
double[] ret = new double[(int) length];
for (int i = 0; i < length; i++)
ret[i] = getDoubleUnsynced(i);
return ret;
}
@Override
public int[] asInt() {
if (length >= Integer.MAX_VALUE)
throw new IllegalArgumentException("Unable to create array of length " + length);
int[] ret = new int[(int) length];
for (int i = 0; i < length; i++)
ret[i] = getIntUnsynced(i);
return ret;
}
@Override
public long[] asLong() {
if (length >= Integer.MAX_VALUE)
throw new IllegalArgumentException("Unable to create array of length " + length);
long[] ret = new long[(int) length];
for (int i = 0; i < length; i++)
ret[i] = getLongUnsynced(i);
return ret;
}
@Override
public double getDouble(long i) {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
if (indexer == null) {
throw new IllegalStateException("Indexer must never be null");
}
switch (dataType()) {
case FLOAT:
return ((FloatIndexer) indexer).get(i);
case UINT32:
return ((UIntIndexer) indexer).get(i);
case INT:
return ((IntIndexer) indexer).get(i);
case BFLOAT16:
return ((Bfloat16Indexer) indexer).get(i);
case HALF:
return ((HalfIndexer) indexer).get(i);
case UINT16:
return ((UShortIndexer) indexer).get(i);
case SHORT:
return ((ShortIndexer) indexer).get(i);
case UINT64:
case LONG:
return ((LongIndexer) indexer).get(i);
case BOOL:
return ((BooleanIndexer) indexer).get(i) ? 1.0 : 0.0;
case DOUBLE:
return ((DoubleIndexer) indexer).get(i);
case BYTE:
return ((ByteIndexer) indexer).get(i);
case UBYTE:
return ((UByteIndexer) indexer).get(i);
default:
throw new UnsupportedOperationException("Cannot get double value from buffer of type " + dataType());
}
}
@Override
public long getLong(long i) {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
switch (dataType()) {
case FLOAT:
return (long) ((FloatIndexer) indexer).get(i);
case DOUBLE:
return (long) ((DoubleIndexer) indexer).get(i);
case BFLOAT16:
return (long) ((Bfloat16Indexer) indexer).get(i);
case HALF:
return (long) ((HalfIndexer) indexer).get( i);
case UINT64: //Fall through
case LONG:
return ((LongIndexer) indexer).get(i);
case UINT32:
return (long) ((UIntIndexer) indexer).get(i);
case INT:
return (long) ((IntIndexer) indexer).get(i);
case UINT16:
return (long) ((UShortIndexer) indexer).get(i);
case SHORT:
return (long) ((ShortIndexer) indexer).get(i);
case BYTE:
return (long) ((ByteIndexer) indexer).get(i);
case UBYTE:
return (long) ((UByteIndexer) indexer).get(i);
case BOOL:
return ((BooleanIndexer) indexer).get(i) ? 1L : 0L;
default:
throw new UnsupportedOperationException("Cannot get long value from buffer of type " + dataType());
}
}
/**
* Special method for
* @param i
* @return
*/
protected short getShort(long i) {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
switch (dataType()) {
case DOUBLE:
return (short) ((DoubleIndexer) indexer).get(i);
case BFLOAT16:
return (short) ((Bfloat16Indexer) indexer).get(i);
case HALF:
return (short) ((HalfIndexer) indexer).get(i);
case BOOL:
return (short) (((BooleanIndexer) indexer).get(i) ? 1 : 0);
case UINT32:
return (short) ((UIntIndexer)indexer).get(i);
case INT:
return (short) ((IntIndexer) indexer).get(i);
case UINT16:
case SHORT:
return ((ShortIndexer) indexer).get(i);
case BYTE:
return (short) ((ByteIndexer) indexer).get(i);
case UINT64:
case LONG:
return (short) ((LongIndexer) indexer).get(i);
case FLOAT:
return (short) ((FloatIndexer) indexer).get(i);
default:
throw new UnsupportedOperationException("Cannot get short value from buffer of type " + dataType());
}
}
/**
*
* @param v
* @return
*/
public static short fromFloat(float v) {
return ArrayUtil.fromFloat(v);
}
@Override
public float getFloat(long i) {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
switch (dataType()) {
case DOUBLE:
return (float) ((DoubleIndexer) indexer).get(i);
case BOOL:
return ((BooleanIndexer) indexer).get(i) ? 1.f : 0.f;
case UINT32:
return (float) ((UIntIndexer)indexer).get(i);
case INT:
return (float) ((IntIndexer) indexer).get(i);
case UINT16:
return ((UShortIndexer) indexer).get(i);
case SHORT:
return (float) ((ShortIndexer) indexer).get(i);
case BFLOAT16:
return ((Bfloat16Indexer) indexer).get(i);
case HALF:
return ((HalfIndexer) indexer).get(i);
case UBYTE:
return (float) ((UByteIndexer) indexer).get(i);
case BYTE:
return (float) ((ByteIndexer) indexer).get(i);
case UINT64: //Fall through
case LONG:
return (float) ((LongIndexer) indexer).get(i);
case FLOAT:
return ((FloatIndexer) indexer).get(i);
default:
throw new UnsupportedOperationException("Cannot get float value from buffer of type " + dataType());
}
}
@Override
public int getInt(long i) {
if (released)
throw new IllegalStateException("You can't use DataBuffer once it was released");
switch (dataType()) {
case DOUBLE:
return (int) ((DoubleIndexer) indexer).get(i);
case BOOL:
return ((BooleanIndexer) indexer).get(i) ? 1 : 0;
case UINT32:
return (int)((UIntIndexer) indexer).get(i);
case INT:
return ((IntIndexer) indexer).get(i);
case BFLOAT16:
return (int) ((Bfloat16Indexer) indexer).get(i);
case HALF:
return (int) ((HalfIndexer) indexer).get(i);
case UINT16:
return ((UShortIndexer) indexer).get(i);
case SHORT:
return ((ShortIndexer) indexer).get(i);
case UBYTE:
return ((UByteIndexer) indexer).get(i);
case BYTE:
return ((ByteIndexer) indexer).get(i);
case UINT64: //Fall through
case LONG:
return (int) ((LongIndexer) indexer).get(i);
case FLOAT:
return (int) ((FloatIndexer) indexer).get(i);
default:
throw new UnsupportedOperationException("Cannot get integer value from buffer of type " + dataType());
}