1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Don't edit this file!  It is auto-generated by frameworks/rs/api/generate.sh.
18 
19 package android.renderscript.cts;
20 
21 import android.renderscript.Allocation;
22 import android.renderscript.RSRuntimeException;
23 import android.renderscript.Element;
24 import android.renderscript.cts.Target;
25 
26 import java.util.Arrays;
27 
28 public class TestPown extends RSBaseCompute {
29 
30     private ScriptC_TestPown script;
31     private ScriptC_TestPownRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestPown(mRS);
37         scriptRelaxed = new ScriptC_TestPownRelaxed(mRS);
38     }
39 
40     @Override
tearDown()41     protected void tearDown() throws Exception {
42         script.destroy();
43         scriptRelaxed.destroy();
44         super.tearDown();
45     }
46 
47     public class ArgumentsFloatIntFloat {
48         public float inBase;
49         public int inExponent;
50         public Target.Floaty out;
51     }
52 
checkPownFloatIntFloat()53     private void checkPownFloatIntFloat() {
54         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xe020952d622f0405l, false);
55         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 1, 0x9c3888e9096b9f1bl, false);
56         try {
57             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
58             script.set_gAllocInExponent(inExponent);
59             script.forEach_testPownFloatIntFloat(inBase, out);
60             verifyResultsPownFloatIntFloat(inBase, inExponent, out, false);
61             out.destroy();
62         } catch (Exception e) {
63             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloatIntFloat: " + e.toString());
64         }
65         try {
66             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
67             scriptRelaxed.set_gAllocInExponent(inExponent);
68             scriptRelaxed.forEach_testPownFloatIntFloat(inBase, out);
69             verifyResultsPownFloatIntFloat(inBase, inExponent, out, true);
70             out.destroy();
71         } catch (Exception e) {
72             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloatIntFloat: " + e.toString());
73         }
74         inBase.destroy();
75         inExponent.destroy();
76     }
77 
verifyResultsPownFloatIntFloat(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)78     private void verifyResultsPownFloatIntFloat(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
79         float[] arrayInBase = new float[INPUTSIZE * 1];
80         Arrays.fill(arrayInBase, (float) 42);
81         inBase.copyTo(arrayInBase);
82         int[] arrayInExponent = new int[INPUTSIZE * 1];
83         Arrays.fill(arrayInExponent, (int) 42);
84         inExponent.copyTo(arrayInExponent);
85         float[] arrayOut = new float[INPUTSIZE * 1];
86         Arrays.fill(arrayOut, (float) 42);
87         out.copyTo(arrayOut);
88         StringBuilder message = new StringBuilder();
89         boolean errorFound = false;
90         for (int i = 0; i < INPUTSIZE; i++) {
91             for (int j = 0; j < 1 ; j++) {
92                 // Extract the inputs.
93                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
94                 args.inBase = arrayInBase[i];
95                 args.inExponent = arrayInExponent[i];
96                 // Figure out what the outputs should have been.
97                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
98                 CoreMathVerifier.computePown(args, target);
99                 // Validate the outputs.
100                 boolean valid = true;
101                 if (!args.out.couldBe(arrayOut[i * 1 + j])) {
102                     valid = false;
103                 }
104                 if (!valid) {
105                     if (!errorFound) {
106                         errorFound = true;
107                         message.append("Input inBase: ");
108                         appendVariableToMessage(message, args.inBase);
109                         message.append("\n");
110                         message.append("Input inExponent: ");
111                         appendVariableToMessage(message, args.inExponent);
112                         message.append("\n");
113                         message.append("Expected output out: ");
114                         appendVariableToMessage(message, args.out);
115                         message.append("\n");
116                         message.append("Actual   output out: ");
117                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
118                         if (!args.out.couldBe(arrayOut[i * 1 + j])) {
119                             message.append(" FAIL");
120                         }
121                         message.append("\n");
122                         message.append("Errors at");
123                     }
124                     message.append(" [");
125                     message.append(Integer.toString(i));
126                     message.append(", ");
127                     message.append(Integer.toString(j));
128                     message.append("]");
129                 }
130             }
131         }
132         assertFalse("Incorrect output for checkPownFloatIntFloat" +
133                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
134     }
135 
checkPownFloat2Int2Float2()136     private void checkPownFloat2Int2Float2() {
137         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x7571c02be438467l, false);
138         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 2, 0xe6177b3249076ddl, false);
139         try {
140             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
141             script.set_gAllocInExponent(inExponent);
142             script.forEach_testPownFloat2Int2Float2(inBase, out);
143             verifyResultsPownFloat2Int2Float2(inBase, inExponent, out, false);
144             out.destroy();
145         } catch (Exception e) {
146             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat2Int2Float2: " + e.toString());
147         }
148         try {
149             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
150             scriptRelaxed.set_gAllocInExponent(inExponent);
151             scriptRelaxed.forEach_testPownFloat2Int2Float2(inBase, out);
152             verifyResultsPownFloat2Int2Float2(inBase, inExponent, out, true);
153             out.destroy();
154         } catch (Exception e) {
155             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat2Int2Float2: " + e.toString());
156         }
157         inBase.destroy();
158         inExponent.destroy();
159     }
160 
verifyResultsPownFloat2Int2Float2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)161     private void verifyResultsPownFloat2Int2Float2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
162         float[] arrayInBase = new float[INPUTSIZE * 2];
163         Arrays.fill(arrayInBase, (float) 42);
164         inBase.copyTo(arrayInBase);
165         int[] arrayInExponent = new int[INPUTSIZE * 2];
166         Arrays.fill(arrayInExponent, (int) 42);
167         inExponent.copyTo(arrayInExponent);
168         float[] arrayOut = new float[INPUTSIZE * 2];
169         Arrays.fill(arrayOut, (float) 42);
170         out.copyTo(arrayOut);
171         StringBuilder message = new StringBuilder();
172         boolean errorFound = false;
173         for (int i = 0; i < INPUTSIZE; i++) {
174             for (int j = 0; j < 2 ; j++) {
175                 // Extract the inputs.
176                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
177                 args.inBase = arrayInBase[i * 2 + j];
178                 args.inExponent = arrayInExponent[i * 2 + j];
179                 // Figure out what the outputs should have been.
180                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
181                 CoreMathVerifier.computePown(args, target);
182                 // Validate the outputs.
183                 boolean valid = true;
184                 if (!args.out.couldBe(arrayOut[i * 2 + j])) {
185                     valid = false;
186                 }
187                 if (!valid) {
188                     if (!errorFound) {
189                         errorFound = true;
190                         message.append("Input inBase: ");
191                         appendVariableToMessage(message, args.inBase);
192                         message.append("\n");
193                         message.append("Input inExponent: ");
194                         appendVariableToMessage(message, args.inExponent);
195                         message.append("\n");
196                         message.append("Expected output out: ");
197                         appendVariableToMessage(message, args.out);
198                         message.append("\n");
199                         message.append("Actual   output out: ");
200                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
201                         if (!args.out.couldBe(arrayOut[i * 2 + j])) {
202                             message.append(" FAIL");
203                         }
204                         message.append("\n");
205                         message.append("Errors at");
206                     }
207                     message.append(" [");
208                     message.append(Integer.toString(i));
209                     message.append(", ");
210                     message.append(Integer.toString(j));
211                     message.append("]");
212                 }
213             }
214         }
215         assertFalse("Incorrect output for checkPownFloat2Int2Float2" +
216                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
217     }
218 
checkPownFloat3Int3Float3()219     private void checkPownFloat3Int3Float3() {
220         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x5e88c45be35ff8a2l, false);
221         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 3, 0xaf710734144a81a8l, false);
222         try {
223             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
224             script.set_gAllocInExponent(inExponent);
225             script.forEach_testPownFloat3Int3Float3(inBase, out);
226             verifyResultsPownFloat3Int3Float3(inBase, inExponent, out, false);
227             out.destroy();
228         } catch (Exception e) {
229             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat3Int3Float3: " + e.toString());
230         }
231         try {
232             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
233             scriptRelaxed.set_gAllocInExponent(inExponent);
234             scriptRelaxed.forEach_testPownFloat3Int3Float3(inBase, out);
235             verifyResultsPownFloat3Int3Float3(inBase, inExponent, out, true);
236             out.destroy();
237         } catch (Exception e) {
238             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat3Int3Float3: " + e.toString());
239         }
240         inBase.destroy();
241         inExponent.destroy();
242     }
243 
verifyResultsPownFloat3Int3Float3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)244     private void verifyResultsPownFloat3Int3Float3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
245         float[] arrayInBase = new float[INPUTSIZE * 4];
246         Arrays.fill(arrayInBase, (float) 42);
247         inBase.copyTo(arrayInBase);
248         int[] arrayInExponent = new int[INPUTSIZE * 4];
249         Arrays.fill(arrayInExponent, (int) 42);
250         inExponent.copyTo(arrayInExponent);
251         float[] arrayOut = new float[INPUTSIZE * 4];
252         Arrays.fill(arrayOut, (float) 42);
253         out.copyTo(arrayOut);
254         StringBuilder message = new StringBuilder();
255         boolean errorFound = false;
256         for (int i = 0; i < INPUTSIZE; i++) {
257             for (int j = 0; j < 3 ; j++) {
258                 // Extract the inputs.
259                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
260                 args.inBase = arrayInBase[i * 4 + j];
261                 args.inExponent = arrayInExponent[i * 4 + j];
262                 // Figure out what the outputs should have been.
263                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
264                 CoreMathVerifier.computePown(args, target);
265                 // Validate the outputs.
266                 boolean valid = true;
267                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
268                     valid = false;
269                 }
270                 if (!valid) {
271                     if (!errorFound) {
272                         errorFound = true;
273                         message.append("Input inBase: ");
274                         appendVariableToMessage(message, args.inBase);
275                         message.append("\n");
276                         message.append("Input inExponent: ");
277                         appendVariableToMessage(message, args.inExponent);
278                         message.append("\n");
279                         message.append("Expected output out: ");
280                         appendVariableToMessage(message, args.out);
281                         message.append("\n");
282                         message.append("Actual   output out: ");
283                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
284                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
285                             message.append(" FAIL");
286                         }
287                         message.append("\n");
288                         message.append("Errors at");
289                     }
290                     message.append(" [");
291                     message.append(Integer.toString(i));
292                     message.append(", ");
293                     message.append(Integer.toString(j));
294                     message.append("]");
295                 }
296             }
297         }
298         assertFalse("Incorrect output for checkPownFloat3Int3Float3" +
299                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
300     }
301 
checkPownFloat4Int4Float4()302     private void checkPownFloat4Int4Float4() {
303         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xb5ba6cb5087c6cddl, false);
304         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 4, 0x508096b504048c73l, false);
305         try {
306             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
307             script.set_gAllocInExponent(inExponent);
308             script.forEach_testPownFloat4Int4Float4(inBase, out);
309             verifyResultsPownFloat4Int4Float4(inBase, inExponent, out, false);
310             out.destroy();
311         } catch (Exception e) {
312             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat4Int4Float4: " + e.toString());
313         }
314         try {
315             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
316             scriptRelaxed.set_gAllocInExponent(inExponent);
317             scriptRelaxed.forEach_testPownFloat4Int4Float4(inBase, out);
318             verifyResultsPownFloat4Int4Float4(inBase, inExponent, out, true);
319             out.destroy();
320         } catch (Exception e) {
321             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat4Int4Float4: " + e.toString());
322         }
323         inBase.destroy();
324         inExponent.destroy();
325     }
326 
verifyResultsPownFloat4Int4Float4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)327     private void verifyResultsPownFloat4Int4Float4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
328         float[] arrayInBase = new float[INPUTSIZE * 4];
329         Arrays.fill(arrayInBase, (float) 42);
330         inBase.copyTo(arrayInBase);
331         int[] arrayInExponent = new int[INPUTSIZE * 4];
332         Arrays.fill(arrayInExponent, (int) 42);
333         inExponent.copyTo(arrayInExponent);
334         float[] arrayOut = new float[INPUTSIZE * 4];
335         Arrays.fill(arrayOut, (float) 42);
336         out.copyTo(arrayOut);
337         StringBuilder message = new StringBuilder();
338         boolean errorFound = false;
339         for (int i = 0; i < INPUTSIZE; i++) {
340             for (int j = 0; j < 4 ; j++) {
341                 // Extract the inputs.
342                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
343                 args.inBase = arrayInBase[i * 4 + j];
344                 args.inExponent = arrayInExponent[i * 4 + j];
345                 // Figure out what the outputs should have been.
346                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
347                 CoreMathVerifier.computePown(args, target);
348                 // Validate the outputs.
349                 boolean valid = true;
350                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
351                     valid = false;
352                 }
353                 if (!valid) {
354                     if (!errorFound) {
355                         errorFound = true;
356                         message.append("Input inBase: ");
357                         appendVariableToMessage(message, args.inBase);
358                         message.append("\n");
359                         message.append("Input inExponent: ");
360                         appendVariableToMessage(message, args.inExponent);
361                         message.append("\n");
362                         message.append("Expected output out: ");
363                         appendVariableToMessage(message, args.out);
364                         message.append("\n");
365                         message.append("Actual   output out: ");
366                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
367                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
368                             message.append(" FAIL");
369                         }
370                         message.append("\n");
371                         message.append("Errors at");
372                     }
373                     message.append(" [");
374                     message.append(Integer.toString(i));
375                     message.append(", ");
376                     message.append(Integer.toString(j));
377                     message.append("]");
378                 }
379             }
380         }
381         assertFalse("Incorrect output for checkPownFloat4Int4Float4" +
382                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
383     }
384 
385     public class ArgumentsHalfIntHalf {
386         public short inBase;
387         public double inBaseDouble;
388         public int inExponent;
389         public Target.Floaty out;
390     }
391 
checkPownHalfIntHalf()392     private void checkPownHalfIntHalf() {
393         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0x89409624858a42bbl, false);
394         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 1, 0xfdb4ee1e30c3d6f1l, false);
395         try {
396             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
397             script.set_gAllocInExponent(inExponent);
398             script.forEach_testPownHalfIntHalf(inBase, out);
399             verifyResultsPownHalfIntHalf(inBase, inExponent, out, false);
400             out.destroy();
401         } catch (Exception e) {
402             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalfIntHalf: " + e.toString());
403         }
404         try {
405             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
406             scriptRelaxed.set_gAllocInExponent(inExponent);
407             scriptRelaxed.forEach_testPownHalfIntHalf(inBase, out);
408             verifyResultsPownHalfIntHalf(inBase, inExponent, out, true);
409             out.destroy();
410         } catch (Exception e) {
411             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalfIntHalf: " + e.toString());
412         }
413         inBase.destroy();
414         inExponent.destroy();
415     }
416 
verifyResultsPownHalfIntHalf(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)417     private void verifyResultsPownHalfIntHalf(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
418         short[] arrayInBase = new short[INPUTSIZE * 1];
419         Arrays.fill(arrayInBase, (short) 42);
420         inBase.copyTo(arrayInBase);
421         int[] arrayInExponent = new int[INPUTSIZE * 1];
422         Arrays.fill(arrayInExponent, (int) 42);
423         inExponent.copyTo(arrayInExponent);
424         short[] arrayOut = new short[INPUTSIZE * 1];
425         Arrays.fill(arrayOut, (short) 42);
426         out.copyTo(arrayOut);
427         StringBuilder message = new StringBuilder();
428         boolean errorFound = false;
429         for (int i = 0; i < INPUTSIZE; i++) {
430             for (int j = 0; j < 1 ; j++) {
431                 // Extract the inputs.
432                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
433                 args.inBase = arrayInBase[i];
434                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
435                 args.inExponent = arrayInExponent[i];
436                 // Figure out what the outputs should have been.
437                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
438                 CoreMathVerifier.computePown(args, target);
439                 // Validate the outputs.
440                 boolean valid = true;
441                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
442                     valid = false;
443                 }
444                 if (!valid) {
445                     if (!errorFound) {
446                         errorFound = true;
447                         message.append("Input inBase: ");
448                         appendVariableToMessage(message, args.inBase);
449                         message.append("\n");
450                         message.append("Input inExponent: ");
451                         appendVariableToMessage(message, args.inExponent);
452                         message.append("\n");
453                         message.append("Expected output out: ");
454                         appendVariableToMessage(message, args.out);
455                         message.append("\n");
456                         message.append("Actual   output out: ");
457                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
458                         message.append("\n");
459                         message.append("Actual   output out (in double): ");
460                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]));
461                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
462                             message.append(" FAIL");
463                         }
464                         message.append("\n");
465                         message.append("Errors at");
466                     }
467                     message.append(" [");
468                     message.append(Integer.toString(i));
469                     message.append(", ");
470                     message.append(Integer.toString(j));
471                     message.append("]");
472                 }
473             }
474         }
475         assertFalse("Incorrect output for checkPownHalfIntHalf" +
476                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
477     }
478 
checkPownHalf2Int2Half2()479     private void checkPownHalf2Int2Half2() {
480         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x218972b229ccb2d5l, false);
481         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 2, 0x90a067e6e5f9cebl, false);
482         try {
483             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
484             script.set_gAllocInExponent(inExponent);
485             script.forEach_testPownHalf2Int2Half2(inBase, out);
486             verifyResultsPownHalf2Int2Half2(inBase, inExponent, out, false);
487             out.destroy();
488         } catch (Exception e) {
489             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf2Int2Half2: " + e.toString());
490         }
491         try {
492             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
493             scriptRelaxed.set_gAllocInExponent(inExponent);
494             scriptRelaxed.forEach_testPownHalf2Int2Half2(inBase, out);
495             verifyResultsPownHalf2Int2Half2(inBase, inExponent, out, true);
496             out.destroy();
497         } catch (Exception e) {
498             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf2Int2Half2: " + e.toString());
499         }
500         inBase.destroy();
501         inExponent.destroy();
502     }
503 
verifyResultsPownHalf2Int2Half2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)504     private void verifyResultsPownHalf2Int2Half2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
505         short[] arrayInBase = new short[INPUTSIZE * 2];
506         Arrays.fill(arrayInBase, (short) 42);
507         inBase.copyTo(arrayInBase);
508         int[] arrayInExponent = new int[INPUTSIZE * 2];
509         Arrays.fill(arrayInExponent, (int) 42);
510         inExponent.copyTo(arrayInExponent);
511         short[] arrayOut = new short[INPUTSIZE * 2];
512         Arrays.fill(arrayOut, (short) 42);
513         out.copyTo(arrayOut);
514         StringBuilder message = new StringBuilder();
515         boolean errorFound = false;
516         for (int i = 0; i < INPUTSIZE; i++) {
517             for (int j = 0; j < 2 ; j++) {
518                 // Extract the inputs.
519                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
520                 args.inBase = arrayInBase[i * 2 + j];
521                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
522                 args.inExponent = arrayInExponent[i * 2 + j];
523                 // Figure out what the outputs should have been.
524                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
525                 CoreMathVerifier.computePown(args, target);
526                 // Validate the outputs.
527                 boolean valid = true;
528                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
529                     valid = false;
530                 }
531                 if (!valid) {
532                     if (!errorFound) {
533                         errorFound = true;
534                         message.append("Input inBase: ");
535                         appendVariableToMessage(message, args.inBase);
536                         message.append("\n");
537                         message.append("Input inExponent: ");
538                         appendVariableToMessage(message, args.inExponent);
539                         message.append("\n");
540                         message.append("Expected output out: ");
541                         appendVariableToMessage(message, args.out);
542                         message.append("\n");
543                         message.append("Actual   output out: ");
544                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
545                         message.append("\n");
546                         message.append("Actual   output out (in double): ");
547                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]));
548                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
549                             message.append(" FAIL");
550                         }
551                         message.append("\n");
552                         message.append("Errors at");
553                     }
554                     message.append(" [");
555                     message.append(Integer.toString(i));
556                     message.append(", ");
557                     message.append(Integer.toString(j));
558                     message.append("]");
559                 }
560             }
561         }
562         assertFalse("Incorrect output for checkPownHalf2Int2Half2" +
563                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
564     }
565 
checkPownHalf3Int3Half3()566     private void checkPownHalf3Int3Half3() {
567         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0x82d20b742f8523b4l, false);
568         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 3, 0x3cb4a30b92b63d1al, false);
569         try {
570             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
571             script.set_gAllocInExponent(inExponent);
572             script.forEach_testPownHalf3Int3Half3(inBase, out);
573             verifyResultsPownHalf3Int3Half3(inBase, inExponent, out, false);
574             out.destroy();
575         } catch (Exception e) {
576             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf3Int3Half3: " + e.toString());
577         }
578         try {
579             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
580             scriptRelaxed.set_gAllocInExponent(inExponent);
581             scriptRelaxed.forEach_testPownHalf3Int3Half3(inBase, out);
582             verifyResultsPownHalf3Int3Half3(inBase, inExponent, out, true);
583             out.destroy();
584         } catch (Exception e) {
585             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf3Int3Half3: " + e.toString());
586         }
587         inBase.destroy();
588         inExponent.destroy();
589     }
590 
verifyResultsPownHalf3Int3Half3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)591     private void verifyResultsPownHalf3Int3Half3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
592         short[] arrayInBase = new short[INPUTSIZE * 4];
593         Arrays.fill(arrayInBase, (short) 42);
594         inBase.copyTo(arrayInBase);
595         int[] arrayInExponent = new int[INPUTSIZE * 4];
596         Arrays.fill(arrayInExponent, (int) 42);
597         inExponent.copyTo(arrayInExponent);
598         short[] arrayOut = new short[INPUTSIZE * 4];
599         Arrays.fill(arrayOut, (short) 42);
600         out.copyTo(arrayOut);
601         StringBuilder message = new StringBuilder();
602         boolean errorFound = false;
603         for (int i = 0; i < INPUTSIZE; i++) {
604             for (int j = 0; j < 3 ; j++) {
605                 // Extract the inputs.
606                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
607                 args.inBase = arrayInBase[i * 4 + j];
608                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
609                 args.inExponent = arrayInExponent[i * 4 + j];
610                 // Figure out what the outputs should have been.
611                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
612                 CoreMathVerifier.computePown(args, target);
613                 // Validate the outputs.
614                 boolean valid = true;
615                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
616                     valid = false;
617                 }
618                 if (!valid) {
619                     if (!errorFound) {
620                         errorFound = true;
621                         message.append("Input inBase: ");
622                         appendVariableToMessage(message, args.inBase);
623                         message.append("\n");
624                         message.append("Input inExponent: ");
625                         appendVariableToMessage(message, args.inExponent);
626                         message.append("\n");
627                         message.append("Expected output out: ");
628                         appendVariableToMessage(message, args.out);
629                         message.append("\n");
630                         message.append("Actual   output out: ");
631                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
632                         message.append("\n");
633                         message.append("Actual   output out (in double): ");
634                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
635                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
636                             message.append(" FAIL");
637                         }
638                         message.append("\n");
639                         message.append("Errors at");
640                     }
641                     message.append(" [");
642                     message.append(Integer.toString(i));
643                     message.append(", ");
644                     message.append(Integer.toString(j));
645                     message.append("]");
646                 }
647             }
648         }
649         assertFalse("Incorrect output for checkPownHalf3Int3Half3" +
650                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
651     }
652 
checkPownHalf4Int4Half4()653     private void checkPownHalf4Int4Half4() {
654         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0xe41aa436353d9493l, false);
655         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 4, 0x705f3f98b70cdd49l, false);
656         try {
657             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
658             script.set_gAllocInExponent(inExponent);
659             script.forEach_testPownHalf4Int4Half4(inBase, out);
660             verifyResultsPownHalf4Int4Half4(inBase, inExponent, out, false);
661             out.destroy();
662         } catch (Exception e) {
663             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf4Int4Half4: " + e.toString());
664         }
665         try {
666             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
667             scriptRelaxed.set_gAllocInExponent(inExponent);
668             scriptRelaxed.forEach_testPownHalf4Int4Half4(inBase, out);
669             verifyResultsPownHalf4Int4Half4(inBase, inExponent, out, true);
670             out.destroy();
671         } catch (Exception e) {
672             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf4Int4Half4: " + e.toString());
673         }
674         inBase.destroy();
675         inExponent.destroy();
676     }
677 
verifyResultsPownHalf4Int4Half4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)678     private void verifyResultsPownHalf4Int4Half4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
679         short[] arrayInBase = new short[INPUTSIZE * 4];
680         Arrays.fill(arrayInBase, (short) 42);
681         inBase.copyTo(arrayInBase);
682         int[] arrayInExponent = new int[INPUTSIZE * 4];
683         Arrays.fill(arrayInExponent, (int) 42);
684         inExponent.copyTo(arrayInExponent);
685         short[] arrayOut = new short[INPUTSIZE * 4];
686         Arrays.fill(arrayOut, (short) 42);
687         out.copyTo(arrayOut);
688         StringBuilder message = new StringBuilder();
689         boolean errorFound = false;
690         for (int i = 0; i < INPUTSIZE; i++) {
691             for (int j = 0; j < 4 ; j++) {
692                 // Extract the inputs.
693                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
694                 args.inBase = arrayInBase[i * 4 + j];
695                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
696                 args.inExponent = arrayInExponent[i * 4 + j];
697                 // Figure out what the outputs should have been.
698                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
699                 CoreMathVerifier.computePown(args, target);
700                 // Validate the outputs.
701                 boolean valid = true;
702                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
703                     valid = false;
704                 }
705                 if (!valid) {
706                     if (!errorFound) {
707                         errorFound = true;
708                         message.append("Input inBase: ");
709                         appendVariableToMessage(message, args.inBase);
710                         message.append("\n");
711                         message.append("Input inExponent: ");
712                         appendVariableToMessage(message, args.inExponent);
713                         message.append("\n");
714                         message.append("Expected output out: ");
715                         appendVariableToMessage(message, args.out);
716                         message.append("\n");
717                         message.append("Actual   output out: ");
718                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
719                         message.append("\n");
720                         message.append("Actual   output out (in double): ");
721                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
722                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
723                             message.append(" FAIL");
724                         }
725                         message.append("\n");
726                         message.append("Errors at");
727                     }
728                     message.append(" [");
729                     message.append(Integer.toString(i));
730                     message.append(", ");
731                     message.append(Integer.toString(j));
732                     message.append("]");
733                 }
734             }
735         }
736         assertFalse("Incorrect output for checkPownHalf4Int4Half4" +
737                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
738     }
739 
testPown()740     public void testPown() {
741         checkPownFloatIntFloat();
742         checkPownFloat2Int2Float2();
743         checkPownFloat3Int3Float3();
744         checkPownFloat4Int4Float4();
745         checkPownHalfIntHalf();
746         checkPownHalf2Int2Half2();
747         checkPownHalf3Int3Half3();
748         checkPownHalf4Int4Half4();
749     }
750 }
751