diff --git a/CHANGELOG.md b/CHANGELOG.md index cf3d97b38278601afdc3079ada8aecbd554166d3..f19e71383b5b28b004e417e43cc1984b07bcc8c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Updated parent pon to v6. * createProgram had the wrong signature producing a unsatisfied link exception that is now fixed. * Build now requires version 3.5.0 of maven due to changes in surefire plugin. +* Added the functions popcount and clz ## 1.4.1 diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 42337edff8cb49c6777857cb72210d38785f4bf2..edc9e67bc4a54812eef8b01fe20637c7c57d72a5 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -20,6 +20,7 @@ * Fernando Marino` * AMD Corporation * Dmitriy Shabanov <shabanovd@gmail.com> +* Toon Baeyens <toon.baeyens@gmail.com> # Details diff --git a/src/main/java/com/aparapi/Kernel.java b/src/main/java/com/aparapi/Kernel.java index f697ff4a202298d391dee7851014a2218b448c4c..72bac0cadcbdae629dc8efd27764feb39a08c805 100644 --- a/src/main/java/com/aparapi/Kernel.java +++ b/src/main/java/com/aparapi/Kernel.java @@ -1316,7 +1316,64 @@ public abstract class Kernel implements Cloneable { return Math.abs(_f); } + /** + * Delegates to either {@link java.lang.Integer#bitCount(int)} (Java) or <code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/popcount.html">popcount(int)</a></code> (OpenCL). + * + * @param _i value to delegate to {@link java.lang.Integer#bitCount(int)}/<code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/popcount.html">popcount(int)</a></code> + * @return {@link java.lang.Integer#bitCount(int)}/<code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/popcount.html">popcount(int)</a></code> + * @see java.lang.Integer#bitCount(int) + * @see <code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/popcount.html">popcount(int)</a></code> + */ + @OpenCLMapping(mapTo = "popcount") + protected int popcount(int _i) { + return Integer.bitCount(_i); + } + + /** + * Delegates to either {@link java.lang.Long#bitCount(long)} (Java) or <code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/popcount.html">popcount(long)</a></code> (OpenCL). + * + * @param _i value to delegate to {@link java.lang.Long#bitCount(long)}/<code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/popcount.html">popcount(long)</a></code> + * @return {@link java.lang.Long#bitCount(long)}/<code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/popcount.html">popcount(long)</a></code> + * @see java.lang.Long#bitCount(long) + * @see <code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/popcount.html">popcount(long)</a></code> + */ + @OpenCLMapping(mapTo = "popcount") + protected long popcount(long _i) { + return Long.bitCount(_i); + } + + /** + * Delegates to either {@link java.lang.Integer#numberOfLeadingZeros(int)} (Java) or <code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.1/docs/man/xhtml/clz.html">clz(int)</a></code> (OpenCL). + * + * @param _i value to delegate to {@link java.lang.Integer#numberOfLeadingZeros(int)}/<code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clz.html">clz(int)</a></code> + * @return {@link java.lang.Integer#numberOfLeadingZeros(int)}/<code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clz.html">clz(int)</a></code> + * @see java.lang.Integer#numberOfLeadingZeros(int) + * @see <code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clz.html">clz(int)</a></code> + */ + + @OpenCLMapping(mapTo = "clz") + protected int clz(int _i) { + return Integer.numberOfLeadingZeros(_i); + } + + + /** + * Delegates to either {@link java.lang.Long#numberOfLeadingZeros(long)} (Java) or <code><a href="https://www.khronos.org/registry/OpenCL/sdk/1.1/docs/man/xhtml/clz.html">clz(long)</a></code> (OpenCL). + * + * @param _l value to delegate to {@link java.lang.Long#numberOfLeadingZeros(long)}/<code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clz.html">clz(long)</a></code> + * @return {@link java.lang.Long#numberOfLeadingZeros(long)}/<code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clz.html">clz(long)</a></code> + * @see java.lang.Long#numberOfLeadingZeros(long) + * @see <code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clz.html">clz(long)</a></code> + */ + + + @OpenCLMapping(mapTo = "clz") + protected long clz(long _l) { + return Long.numberOfLeadingZeros(_l); + } + + /** * Delegates to either {@link java.lang.Math#abs(double)} (Java) or <code><a href="http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/fabs.html">fabs(double)</a></code> (OpenCL). * * User should note the differences in precision between Java and OpenCL's implementation of arithmetic functions to determine whether the difference in precision is acceptable. diff --git a/src/test/java/com/aparapi/codegen/test/MathFallThru.java b/src/test/java/com/aparapi/codegen/test/MathFallThru.java index d6b6875d92e3b5e806b58951020e624294c05ae0..76a2dd893f0c4d62bc8d3225c982d34bccc2618a 100644 --- a/src/test/java/com/aparapi/codegen/test/MathFallThru.java +++ b/src/test/java/com/aparapi/codegen/test/MathFallThru.java @@ -19,14 +19,18 @@ import com.aparapi.Kernel; public class MathFallThru extends Kernel { - long longout[] = new long[1]; - int intout[] = new int[1]; + long longout[] = new long[3]; + int intout[] = new int[3]; public void run() { float f1 = 1.0f; double d1 = 1.0; longout[0] = round(ceil(cos(exp(floor(log(pow(d1, d1)))))) + tan(sqrt(sin(rint(acos(asin(atan(atan2(d1, d1))))))))); + longout[1] = popcount(longout[0]); + longout[2] = clz(longout[0]); intout[0] = round(ceil(cos(exp(floor(log(pow(f1, f1)))))) + tan(sqrt(sin(rint(acos(asin(atan(atan2(f1, f1))))))))); + intout[1] = popcount(intout[0]); + intout[2] = clz(intout[0]); @SuppressWarnings("unused") boolean pass = false; } } @@ -56,7 +60,11 @@ public class MathFallThru extends Kernel { float f1 = 1.0f; double d1 = 1.0; this->longout[0] = round((ceil(cos(exp(floor(log(pow(d1, d1)))))) + tan(sqrt(sin(rint(acos(asin(atan(atan2(d1, d1)))))))))); + this->longout[1] = popcount(this->longout[0]); + this->longout[2] = clz(this->longout[0]); this->intout[0] = round((ceil(cos(exp(floor(log(pow(f1, f1)))))) + tan(sqrt(sin(rint(acos(asin(atan(atan2(f1, f1)))))))))); + this->intout[1] = popcount(this->intout[0]); + this->intout[2] = clz(this->intout[0]); char pass = 0; return; } diff --git a/src/test/java/com/aparapi/codegen/test/MathFallThruTest.java b/src/test/java/com/aparapi/codegen/test/MathFallThruTest.java index ced44207788fd5b877d9e2ce3b299533493e6788..0c7f28939c0d65816df40d3fcae864051f7b4288 100644 --- a/src/test/java/com/aparapi/codegen/test/MathFallThruTest.java +++ b/src/test/java/com/aparapi/codegen/test/MathFallThruTest.java @@ -44,7 +44,11 @@ public class MathFallThruTest extends com.aparapi.codegen.CodeGenJUnitBase { " float f1 = 1.0f;\n" + " double d1 = 1.0;\n" + " this->longout[0] = round((ceil(cos(exp(floor(log(pow(d1, d1)))))) + tan(sqrt(sin(rint(acos(asin(atan(atan2(d1, d1))))))))));\n" + +" this->longout[1] = popcount(this->longout[0]);\n" + +" this->longout[2] = clz(this->longout[0]);\n" + " this->intout[0] = round((ceil(cos(exp(floor(log(pow(f1, f1)))))) + tan(sqrt(sin(rint(acos(asin(atan(atan2(f1, f1))))))))));\n" + +" this->intout[1] = popcount(this->intout[0]);\n" + +" this->intout[2] = clz(this->intout[0]);\n" + " char pass = 0;\n" + " return;\n" + " }\n" +