-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add suport for double16 and VectorDouble16 types
- Loading branch information
1 parent
bb6c226
commit 15872c1
Showing
4 changed files
with
326 additions
and
7 deletions.
There are no files selected for viewing
236 changes: 236 additions & 0 deletions
236
tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/collections/VectorDouble16.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,236 @@ | ||
/* | ||
* Copyright (c) 2023, APT Group, Department of Computer Science, | ||
* The University of Manchester. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* GNU Classpath is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* GNU Classpath 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 for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with GNU Classpath; see the file COPYING. If not, write to the | ||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301 USA. | ||
* | ||
* Linking this library statically or dynamically with other modules is | ||
* making a combined work based on this library. Thus, the terms and | ||
* conditions of the GNU General Public License cover the whole | ||
* combination. | ||
* | ||
* As a special exception, the copyright holders of this library give you | ||
* permission to link this library with independent modules to produce an | ||
* executable, regardless of the license terms of these independent | ||
* modules, and to copy and distribute the resulting executable under | ||
* terms of your choice, provided that you also meet, for each linked | ||
* independent module, the terms and conditions of the license of that | ||
* module. An independent module is a module which is not derived from | ||
* or based on this library. If you modify this library, you may extend | ||
* this exception to your version of the library, but you are not | ||
* obligated to do so. If you do not wish to do so, delete this | ||
* exception statement from your version. | ||
* | ||
*/ | ||
package uk.ac.manchester.tornado.api.types.collections; | ||
|
||
import java.nio.DoubleBuffer; | ||
|
||
import uk.ac.manchester.tornado.api.types.arrays.DoubleArray; | ||
import uk.ac.manchester.tornado.api.types.common.PrimitiveStorage; | ||
import uk.ac.manchester.tornado.api.types.vectors.Double16; | ||
|
||
public class VectorDouble16 implements PrimitiveStorage<DoubleBuffer> { | ||
|
||
public static final Class<VectorDouble16> TYPE = VectorDouble16.class; | ||
|
||
private static final int ELEMENT_SIZE = 16; | ||
/** | ||
* backing array. | ||
*/ | ||
protected final DoubleArray storage; | ||
/** | ||
* number of elements in the storage. | ||
*/ | ||
private final int numElements; | ||
|
||
/** | ||
* Creates a vector using the provided backing array. | ||
* | ||
* @param numElements | ||
* @param array | ||
*/ | ||
protected VectorDouble16(int numElements, DoubleArray array) { | ||
this.numElements = numElements; | ||
this.storage = array; | ||
} | ||
|
||
/** | ||
* Creates a vector using the provided backing array. | ||
*/ | ||
public VectorDouble16(DoubleArray array) { | ||
this(array.getSize() / ELEMENT_SIZE, array); | ||
} | ||
|
||
/** | ||
* Creates an empty vector with. | ||
* | ||
* @param numElements | ||
*/ | ||
public VectorDouble16(int numElements) { | ||
this(numElements, new DoubleArray(numElements * ELEMENT_SIZE)); | ||
} | ||
|
||
public int vectorWidth() { | ||
return ELEMENT_SIZE; | ||
} | ||
|
||
private int toIndex(int index) { | ||
return (index * ELEMENT_SIZE); | ||
} | ||
|
||
/** | ||
* Returns the float at the given index of this vector. | ||
* | ||
* @param index | ||
* @return value | ||
*/ | ||
public Double16 get(int index) { | ||
return loadFromArray(storage, toIndex(index)); | ||
} | ||
|
||
private Double16 loadFromArray(final DoubleArray array, int index) { | ||
final Double16 result = new Double16(); | ||
for (int i = 0; i < ELEMENT_SIZE; i++) { | ||
result.set(i, array.get(index + i)); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Sets the float at the given index of this vector. | ||
* | ||
* @param index | ||
* @param value | ||
*/ | ||
public void set(int index, Double16 value) { | ||
storeToArray(value, storage, toIndex(index)); | ||
} | ||
|
||
private void storeToArray(Double16 value, DoubleArray array, int index) { | ||
for (int i = 0; i < ELEMENT_SIZE; i++) { | ||
array.set(index + i, value.get(i)); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the elements of this vector to that of the provided vector. | ||
* | ||
* @param values | ||
*/ | ||
public void set(VectorDouble16 values) { | ||
for (int i = 0; i < numElements; i++) { | ||
set(i, values.get(i)); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the elements of this vector to that of the provided array. | ||
* | ||
* @param values | ||
*/ | ||
public void set(DoubleArray values) { | ||
VectorDouble16 vector = new VectorDouble16(values); | ||
for (int i = 0; i < numElements; i++) { | ||
set(i, vector.get(i)); | ||
} | ||
} | ||
|
||
public void fill(double value) { | ||
for (int i = 0; i < storage.getSize(); i++) { | ||
storage.set(i, value); | ||
} | ||
} | ||
|
||
/** | ||
* Duplicates this vector. | ||
* | ||
* @return | ||
*/ | ||
public VectorDouble16 duplicate() { | ||
VectorDouble16 vector = new VectorDouble16(numElements); | ||
vector.set(this); | ||
return vector; | ||
} | ||
|
||
public String toString() { | ||
if (this.numElements > ELEMENT_SIZE) { | ||
return String.format("VectorDouble16 <%d>", this.numElements); | ||
} | ||
StringBuilder tempString = new StringBuilder(); | ||
for (int i = 0; i < numElements; i++) { | ||
tempString.append(" ").append(this.get(i).toString()); | ||
} | ||
return tempString.toString(); | ||
} | ||
|
||
public Double16 sum() { | ||
Double16 result = new Double16(); | ||
for (int i = 0; i < numElements; i++) { | ||
result = Double16.add(result, get(i)); | ||
} | ||
return result; | ||
} | ||
|
||
public Double16 min() { | ||
Double16 result = new Double16(); | ||
for (int i = 0; i < numElements; i++) { | ||
result = Double16.min(result, get(i)); | ||
} | ||
return result; | ||
} | ||
|
||
public Double16 max() { | ||
Double16 result = new Double16(); | ||
for (int i = 0; i < numElements; i++) { | ||
result = Double16.max(result, get(i)); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void loadFromBuffer(DoubleBuffer buffer) { | ||
asBuffer().put(buffer); | ||
} | ||
|
||
@Override | ||
public DoubleBuffer asBuffer() { | ||
return DoubleBuffer.wrap(storage.toHeapArray()); | ||
} | ||
|
||
public DoubleBuffer asBuffer(DoubleBuffer buffer) { | ||
return asBuffer().put(buffer); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return storage.getSize(); | ||
} | ||
|
||
public int getLength() { | ||
return numElements; | ||
} | ||
|
||
public DoubleArray getArray() { | ||
return storage; | ||
} | ||
|
||
public void clear() { | ||
storage.clear(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.