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     public class ArgumentsFloatIntFloat {
41         public float inBase;
42         public int inExponent;
43         public Target.Floaty out;
44     }
45 
checkPownFloatIntFloat()46     private void checkPownFloatIntFloat() {
47         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x622f0405l, false);
48         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 1, 0x96b9f1bl, false);
49         try {
50             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
51             script.set_gAllocInExponent(inExponent);
52             script.forEach_testPownFloatIntFloat(inBase, out);
53             verifyResultsPownFloatIntFloat(inBase, inExponent, out, false);
54         } catch (Exception e) {
55             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloatIntFloat: " + e.toString());
56         }
57         try {
58             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
59             scriptRelaxed.set_gAllocInExponent(inExponent);
60             scriptRelaxed.forEach_testPownFloatIntFloat(inBase, out);
61             verifyResultsPownFloatIntFloat(inBase, inExponent, out, true);
62         } catch (Exception e) {
63             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloatIntFloat: " + e.toString());
64         }
65     }
66 
verifyResultsPownFloatIntFloat(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)67     private void verifyResultsPownFloatIntFloat(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
68         float[] arrayInBase = new float[INPUTSIZE * 1];
69         Arrays.fill(arrayInBase, (float) 42);
70         inBase.copyTo(arrayInBase);
71         int[] arrayInExponent = new int[INPUTSIZE * 1];
72         Arrays.fill(arrayInExponent, (int) 42);
73         inExponent.copyTo(arrayInExponent);
74         float[] arrayOut = new float[INPUTSIZE * 1];
75         Arrays.fill(arrayOut, (float) 42);
76         out.copyTo(arrayOut);
77         StringBuilder message = new StringBuilder();
78         boolean errorFound = false;
79         for (int i = 0; i < INPUTSIZE; i++) {
80             for (int j = 0; j < 1 ; j++) {
81                 // Extract the inputs.
82                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
83                 args.inBase = arrayInBase[i];
84                 args.inExponent = arrayInExponent[i];
85                 // Figure out what the outputs should have been.
86                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
87                 CoreMathVerifier.computePown(args, target);
88                 // Validate the outputs.
89                 boolean valid = true;
90                 if (!args.out.couldBe(arrayOut[i * 1 + j])) {
91                     valid = false;
92                 }
93                 if (!valid) {
94                     if (!errorFound) {
95                         errorFound = true;
96                         message.append("Input inBase: ");
97                         appendVariableToMessage(message, args.inBase);
98                         message.append("\n");
99                         message.append("Input inExponent: ");
100                         appendVariableToMessage(message, args.inExponent);
101                         message.append("\n");
102                         message.append("Expected output out: ");
103                         appendVariableToMessage(message, args.out);
104                         message.append("\n");
105                         message.append("Actual   output out: ");
106                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
107                         if (!args.out.couldBe(arrayOut[i * 1 + j])) {
108                             message.append(" FAIL");
109                         }
110                         message.append("\n");
111                         message.append("Errors at");
112                     }
113                     message.append(" [");
114                     message.append(Integer.toString(i));
115                     message.append(", ");
116                     message.append(Integer.toString(j));
117                     message.append("]");
118                 }
119             }
120         }
121         assertFalse("Incorrect output for checkPownFloatIntFloat" +
122                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
123     }
124 
checkPownFloat2Int2Float2()125     private void checkPownFloat2Int2Float2() {
126         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0xbe438467l, false);
127         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 2, 0x249076ddl, false);
128         try {
129             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
130             script.set_gAllocInExponent(inExponent);
131             script.forEach_testPownFloat2Int2Float2(inBase, out);
132             verifyResultsPownFloat2Int2Float2(inBase, inExponent, out, false);
133         } catch (Exception e) {
134             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat2Int2Float2: " + e.toString());
135         }
136         try {
137             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
138             scriptRelaxed.set_gAllocInExponent(inExponent);
139             scriptRelaxed.forEach_testPownFloat2Int2Float2(inBase, out);
140             verifyResultsPownFloat2Int2Float2(inBase, inExponent, out, true);
141         } catch (Exception e) {
142             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat2Int2Float2: " + e.toString());
143         }
144     }
145 
verifyResultsPownFloat2Int2Float2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)146     private void verifyResultsPownFloat2Int2Float2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
147         float[] arrayInBase = new float[INPUTSIZE * 2];
148         Arrays.fill(arrayInBase, (float) 42);
149         inBase.copyTo(arrayInBase);
150         int[] arrayInExponent = new int[INPUTSIZE * 2];
151         Arrays.fill(arrayInExponent, (int) 42);
152         inExponent.copyTo(arrayInExponent);
153         float[] arrayOut = new float[INPUTSIZE * 2];
154         Arrays.fill(arrayOut, (float) 42);
155         out.copyTo(arrayOut);
156         StringBuilder message = new StringBuilder();
157         boolean errorFound = false;
158         for (int i = 0; i < INPUTSIZE; i++) {
159             for (int j = 0; j < 2 ; j++) {
160                 // Extract the inputs.
161                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
162                 args.inBase = arrayInBase[i * 2 + j];
163                 args.inExponent = arrayInExponent[i * 2 + j];
164                 // Figure out what the outputs should have been.
165                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
166                 CoreMathVerifier.computePown(args, target);
167                 // Validate the outputs.
168                 boolean valid = true;
169                 if (!args.out.couldBe(arrayOut[i * 2 + j])) {
170                     valid = false;
171                 }
172                 if (!valid) {
173                     if (!errorFound) {
174                         errorFound = true;
175                         message.append("Input inBase: ");
176                         appendVariableToMessage(message, args.inBase);
177                         message.append("\n");
178                         message.append("Input inExponent: ");
179                         appendVariableToMessage(message, args.inExponent);
180                         message.append("\n");
181                         message.append("Expected output out: ");
182                         appendVariableToMessage(message, args.out);
183                         message.append("\n");
184                         message.append("Actual   output out: ");
185                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
186                         if (!args.out.couldBe(arrayOut[i * 2 + j])) {
187                             message.append(" FAIL");
188                         }
189                         message.append("\n");
190                         message.append("Errors at");
191                     }
192                     message.append(" [");
193                     message.append(Integer.toString(i));
194                     message.append(", ");
195                     message.append(Integer.toString(j));
196                     message.append("]");
197                 }
198             }
199         }
200         assertFalse("Incorrect output for checkPownFloat2Int2Float2" +
201                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
202     }
203 
checkPownFloat3Int3Float3()204     private void checkPownFloat3Int3Float3() {
205         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0xe35ff8a2l, false);
206         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 3, 0x144a81a8l, false);
207         try {
208             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
209             script.set_gAllocInExponent(inExponent);
210             script.forEach_testPownFloat3Int3Float3(inBase, out);
211             verifyResultsPownFloat3Int3Float3(inBase, inExponent, out, false);
212         } catch (Exception e) {
213             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat3Int3Float3: " + e.toString());
214         }
215         try {
216             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
217             scriptRelaxed.set_gAllocInExponent(inExponent);
218             scriptRelaxed.forEach_testPownFloat3Int3Float3(inBase, out);
219             verifyResultsPownFloat3Int3Float3(inBase, inExponent, out, true);
220         } catch (Exception e) {
221             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat3Int3Float3: " + e.toString());
222         }
223     }
224 
verifyResultsPownFloat3Int3Float3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)225     private void verifyResultsPownFloat3Int3Float3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
226         float[] arrayInBase = new float[INPUTSIZE * 4];
227         Arrays.fill(arrayInBase, (float) 42);
228         inBase.copyTo(arrayInBase);
229         int[] arrayInExponent = new int[INPUTSIZE * 4];
230         Arrays.fill(arrayInExponent, (int) 42);
231         inExponent.copyTo(arrayInExponent);
232         float[] arrayOut = new float[INPUTSIZE * 4];
233         Arrays.fill(arrayOut, (float) 42);
234         out.copyTo(arrayOut);
235         StringBuilder message = new StringBuilder();
236         boolean errorFound = false;
237         for (int i = 0; i < INPUTSIZE; i++) {
238             for (int j = 0; j < 3 ; j++) {
239                 // Extract the inputs.
240                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
241                 args.inBase = arrayInBase[i * 4 + j];
242                 args.inExponent = arrayInExponent[i * 4 + j];
243                 // Figure out what the outputs should have been.
244                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
245                 CoreMathVerifier.computePown(args, target);
246                 // Validate the outputs.
247                 boolean valid = true;
248                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
249                     valid = false;
250                 }
251                 if (!valid) {
252                     if (!errorFound) {
253                         errorFound = true;
254                         message.append("Input inBase: ");
255                         appendVariableToMessage(message, args.inBase);
256                         message.append("\n");
257                         message.append("Input inExponent: ");
258                         appendVariableToMessage(message, args.inExponent);
259                         message.append("\n");
260                         message.append("Expected output out: ");
261                         appendVariableToMessage(message, args.out);
262                         message.append("\n");
263                         message.append("Actual   output out: ");
264                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
265                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
266                             message.append(" FAIL");
267                         }
268                         message.append("\n");
269                         message.append("Errors at");
270                     }
271                     message.append(" [");
272                     message.append(Integer.toString(i));
273                     message.append(", ");
274                     message.append(Integer.toString(j));
275                     message.append("]");
276                 }
277             }
278         }
279         assertFalse("Incorrect output for checkPownFloat3Int3Float3" +
280                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
281     }
282 
checkPownFloat4Int4Float4()283     private void checkPownFloat4Int4Float4() {
284         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x87c6cddl, false);
285         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 4, 0x4048c73l, false);
286         try {
287             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
288             script.set_gAllocInExponent(inExponent);
289             script.forEach_testPownFloat4Int4Float4(inBase, out);
290             verifyResultsPownFloat4Int4Float4(inBase, inExponent, out, false);
291         } catch (Exception e) {
292             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat4Int4Float4: " + e.toString());
293         }
294         try {
295             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
296             scriptRelaxed.set_gAllocInExponent(inExponent);
297             scriptRelaxed.forEach_testPownFloat4Int4Float4(inBase, out);
298             verifyResultsPownFloat4Int4Float4(inBase, inExponent, out, true);
299         } catch (Exception e) {
300             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownFloat4Int4Float4: " + e.toString());
301         }
302     }
303 
verifyResultsPownFloat4Int4Float4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)304     private void verifyResultsPownFloat4Int4Float4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
305         float[] arrayInBase = new float[INPUTSIZE * 4];
306         Arrays.fill(arrayInBase, (float) 42);
307         inBase.copyTo(arrayInBase);
308         int[] arrayInExponent = new int[INPUTSIZE * 4];
309         Arrays.fill(arrayInExponent, (int) 42);
310         inExponent.copyTo(arrayInExponent);
311         float[] arrayOut = new float[INPUTSIZE * 4];
312         Arrays.fill(arrayOut, (float) 42);
313         out.copyTo(arrayOut);
314         StringBuilder message = new StringBuilder();
315         boolean errorFound = false;
316         for (int i = 0; i < INPUTSIZE; i++) {
317             for (int j = 0; j < 4 ; j++) {
318                 // Extract the inputs.
319                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
320                 args.inBase = arrayInBase[i * 4 + j];
321                 args.inExponent = arrayInExponent[i * 4 + j];
322                 // Figure out what the outputs should have been.
323                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
324                 CoreMathVerifier.computePown(args, target);
325                 // Validate the outputs.
326                 boolean valid = true;
327                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
328                     valid = false;
329                 }
330                 if (!valid) {
331                     if (!errorFound) {
332                         errorFound = true;
333                         message.append("Input inBase: ");
334                         appendVariableToMessage(message, args.inBase);
335                         message.append("\n");
336                         message.append("Input inExponent: ");
337                         appendVariableToMessage(message, args.inExponent);
338                         message.append("\n");
339                         message.append("Expected output out: ");
340                         appendVariableToMessage(message, args.out);
341                         message.append("\n");
342                         message.append("Actual   output out: ");
343                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
344                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
345                             message.append(" FAIL");
346                         }
347                         message.append("\n");
348                         message.append("Errors at");
349                     }
350                     message.append(" [");
351                     message.append(Integer.toString(i));
352                     message.append(", ");
353                     message.append(Integer.toString(j));
354                     message.append("]");
355                 }
356             }
357         }
358         assertFalse("Incorrect output for checkPownFloat4Int4Float4" +
359                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
360     }
361 
362     public class ArgumentsHalfIntHalf {
363         public short inBase;
364         public double inBaseDouble;
365         public int inExponent;
366         public Target.Floaty out;
367     }
368 
checkPownHalfIntHalf()369     private void checkPownHalfIntHalf() {
370         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0x858a42bbl, false);
371         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 1, 0x30c3d6f1l, false);
372         try {
373             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
374             script.set_gAllocInExponent(inExponent);
375             script.forEach_testPownHalfIntHalf(inBase, out);
376             verifyResultsPownHalfIntHalf(inBase, inExponent, out, false);
377         } catch (Exception e) {
378             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalfIntHalf: " + e.toString());
379         }
380         try {
381             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
382             scriptRelaxed.set_gAllocInExponent(inExponent);
383             scriptRelaxed.forEach_testPownHalfIntHalf(inBase, out);
384             verifyResultsPownHalfIntHalf(inBase, inExponent, out, true);
385         } catch (Exception e) {
386             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalfIntHalf: " + e.toString());
387         }
388     }
389 
verifyResultsPownHalfIntHalf(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)390     private void verifyResultsPownHalfIntHalf(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
391         short[] arrayInBase = new short[INPUTSIZE * 1];
392         Arrays.fill(arrayInBase, (short) 42);
393         inBase.copyTo(arrayInBase);
394         int[] arrayInExponent = new int[INPUTSIZE * 1];
395         Arrays.fill(arrayInExponent, (int) 42);
396         inExponent.copyTo(arrayInExponent);
397         short[] arrayOut = new short[INPUTSIZE * 1];
398         Arrays.fill(arrayOut, (short) 42);
399         out.copyTo(arrayOut);
400         StringBuilder message = new StringBuilder();
401         boolean errorFound = false;
402         for (int i = 0; i < INPUTSIZE; i++) {
403             for (int j = 0; j < 1 ; j++) {
404                 // Extract the inputs.
405                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
406                 args.inBase = arrayInBase[i];
407                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
408                 args.inExponent = arrayInExponent[i];
409                 // Figure out what the outputs should have been.
410                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
411                 CoreMathVerifier.computePown(args, target);
412                 // Validate the outputs.
413                 boolean valid = true;
414                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
415                     valid = false;
416                 }
417                 if (!valid) {
418                     if (!errorFound) {
419                         errorFound = true;
420                         message.append("Input inBase: ");
421                         appendVariableToMessage(message, args.inBase);
422                         message.append("\n");
423                         message.append("Input inExponent: ");
424                         appendVariableToMessage(message, args.inExponent);
425                         message.append("\n");
426                         message.append("Expected output out: ");
427                         appendVariableToMessage(message, args.out);
428                         message.append("\n");
429                         message.append("Actual   output out: ");
430                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
431                         message.append("\n");
432                         message.append("Actual   output out (in double): ");
433                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]));
434                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
435                             message.append(" FAIL");
436                         }
437                         message.append("\n");
438                         message.append("Errors at");
439                     }
440                     message.append(" [");
441                     message.append(Integer.toString(i));
442                     message.append(", ");
443                     message.append(Integer.toString(j));
444                     message.append("]");
445                 }
446             }
447         }
448         assertFalse("Incorrect output for checkPownHalfIntHalf" +
449                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
450     }
451 
checkPownHalf2Int2Half2()452     private void checkPownHalf2Int2Half2() {
453         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x29ccb2d5l, false);
454         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 2, 0x6e5f9cebl, false);
455         try {
456             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
457             script.set_gAllocInExponent(inExponent);
458             script.forEach_testPownHalf2Int2Half2(inBase, out);
459             verifyResultsPownHalf2Int2Half2(inBase, inExponent, out, false);
460         } catch (Exception e) {
461             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf2Int2Half2: " + e.toString());
462         }
463         try {
464             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
465             scriptRelaxed.set_gAllocInExponent(inExponent);
466             scriptRelaxed.forEach_testPownHalf2Int2Half2(inBase, out);
467             verifyResultsPownHalf2Int2Half2(inBase, inExponent, out, true);
468         } catch (Exception e) {
469             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf2Int2Half2: " + e.toString());
470         }
471     }
472 
verifyResultsPownHalf2Int2Half2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)473     private void verifyResultsPownHalf2Int2Half2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
474         short[] arrayInBase = new short[INPUTSIZE * 2];
475         Arrays.fill(arrayInBase, (short) 42);
476         inBase.copyTo(arrayInBase);
477         int[] arrayInExponent = new int[INPUTSIZE * 2];
478         Arrays.fill(arrayInExponent, (int) 42);
479         inExponent.copyTo(arrayInExponent);
480         short[] arrayOut = new short[INPUTSIZE * 2];
481         Arrays.fill(arrayOut, (short) 42);
482         out.copyTo(arrayOut);
483         StringBuilder message = new StringBuilder();
484         boolean errorFound = false;
485         for (int i = 0; i < INPUTSIZE; i++) {
486             for (int j = 0; j < 2 ; j++) {
487                 // Extract the inputs.
488                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
489                 args.inBase = arrayInBase[i * 2 + j];
490                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
491                 args.inExponent = arrayInExponent[i * 2 + j];
492                 // Figure out what the outputs should have been.
493                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
494                 CoreMathVerifier.computePown(args, target);
495                 // Validate the outputs.
496                 boolean valid = true;
497                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
498                     valid = false;
499                 }
500                 if (!valid) {
501                     if (!errorFound) {
502                         errorFound = true;
503                         message.append("Input inBase: ");
504                         appendVariableToMessage(message, args.inBase);
505                         message.append("\n");
506                         message.append("Input inExponent: ");
507                         appendVariableToMessage(message, args.inExponent);
508                         message.append("\n");
509                         message.append("Expected output out: ");
510                         appendVariableToMessage(message, args.out);
511                         message.append("\n");
512                         message.append("Actual   output out: ");
513                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
514                         message.append("\n");
515                         message.append("Actual   output out (in double): ");
516                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]));
517                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
518                             message.append(" FAIL");
519                         }
520                         message.append("\n");
521                         message.append("Errors at");
522                     }
523                     message.append(" [");
524                     message.append(Integer.toString(i));
525                     message.append(", ");
526                     message.append(Integer.toString(j));
527                     message.append("]");
528                 }
529             }
530         }
531         assertFalse("Incorrect output for checkPownHalf2Int2Half2" +
532                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
533     }
534 
checkPownHalf3Int3Half3()535     private void checkPownHalf3Int3Half3() {
536         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0x2f8523b4l, false);
537         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 3, 0x92b63d1al, false);
538         try {
539             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
540             script.set_gAllocInExponent(inExponent);
541             script.forEach_testPownHalf3Int3Half3(inBase, out);
542             verifyResultsPownHalf3Int3Half3(inBase, inExponent, out, false);
543         } catch (Exception e) {
544             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf3Int3Half3: " + e.toString());
545         }
546         try {
547             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
548             scriptRelaxed.set_gAllocInExponent(inExponent);
549             scriptRelaxed.forEach_testPownHalf3Int3Half3(inBase, out);
550             verifyResultsPownHalf3Int3Half3(inBase, inExponent, out, true);
551         } catch (Exception e) {
552             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf3Int3Half3: " + e.toString());
553         }
554     }
555 
verifyResultsPownHalf3Int3Half3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)556     private void verifyResultsPownHalf3Int3Half3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
557         short[] arrayInBase = new short[INPUTSIZE * 4];
558         Arrays.fill(arrayInBase, (short) 42);
559         inBase.copyTo(arrayInBase);
560         int[] arrayInExponent = new int[INPUTSIZE * 4];
561         Arrays.fill(arrayInExponent, (int) 42);
562         inExponent.copyTo(arrayInExponent);
563         short[] arrayOut = new short[INPUTSIZE * 4];
564         Arrays.fill(arrayOut, (short) 42);
565         out.copyTo(arrayOut);
566         StringBuilder message = new StringBuilder();
567         boolean errorFound = false;
568         for (int i = 0; i < INPUTSIZE; i++) {
569             for (int j = 0; j < 3 ; j++) {
570                 // Extract the inputs.
571                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
572                 args.inBase = arrayInBase[i * 4 + j];
573                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
574                 args.inExponent = arrayInExponent[i * 4 + j];
575                 // Figure out what the outputs should have been.
576                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
577                 CoreMathVerifier.computePown(args, target);
578                 // Validate the outputs.
579                 boolean valid = true;
580                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
581                     valid = false;
582                 }
583                 if (!valid) {
584                     if (!errorFound) {
585                         errorFound = true;
586                         message.append("Input inBase: ");
587                         appendVariableToMessage(message, args.inBase);
588                         message.append("\n");
589                         message.append("Input inExponent: ");
590                         appendVariableToMessage(message, args.inExponent);
591                         message.append("\n");
592                         message.append("Expected output out: ");
593                         appendVariableToMessage(message, args.out);
594                         message.append("\n");
595                         message.append("Actual   output out: ");
596                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
597                         message.append("\n");
598                         message.append("Actual   output out (in double): ");
599                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
600                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
601                             message.append(" FAIL");
602                         }
603                         message.append("\n");
604                         message.append("Errors at");
605                     }
606                     message.append(" [");
607                     message.append(Integer.toString(i));
608                     message.append(", ");
609                     message.append(Integer.toString(j));
610                     message.append("]");
611                 }
612             }
613         }
614         assertFalse("Incorrect output for checkPownHalf3Int3Half3" +
615                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
616     }
617 
checkPownHalf4Int4Half4()618     private void checkPownHalf4Int4Half4() {
619         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0x353d9493l, false);
620         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 4, 0xb70cdd49l, false);
621         try {
622             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
623             script.set_gAllocInExponent(inExponent);
624             script.forEach_testPownHalf4Int4Half4(inBase, out);
625             verifyResultsPownHalf4Int4Half4(inBase, inExponent, out, false);
626         } catch (Exception e) {
627             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf4Int4Half4: " + e.toString());
628         }
629         try {
630             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
631             scriptRelaxed.set_gAllocInExponent(inExponent);
632             scriptRelaxed.forEach_testPownHalf4Int4Half4(inBase, out);
633             verifyResultsPownHalf4Int4Half4(inBase, inExponent, out, true);
634         } catch (Exception e) {
635             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPownHalf4Int4Half4: " + e.toString());
636         }
637     }
638 
verifyResultsPownHalf4Int4Half4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)639     private void verifyResultsPownHalf4Int4Half4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
640         short[] arrayInBase = new short[INPUTSIZE * 4];
641         Arrays.fill(arrayInBase, (short) 42);
642         inBase.copyTo(arrayInBase);
643         int[] arrayInExponent = new int[INPUTSIZE * 4];
644         Arrays.fill(arrayInExponent, (int) 42);
645         inExponent.copyTo(arrayInExponent);
646         short[] arrayOut = new short[INPUTSIZE * 4];
647         Arrays.fill(arrayOut, (short) 42);
648         out.copyTo(arrayOut);
649         StringBuilder message = new StringBuilder();
650         boolean errorFound = false;
651         for (int i = 0; i < INPUTSIZE; i++) {
652             for (int j = 0; j < 4 ; j++) {
653                 // Extract the inputs.
654                 ArgumentsHalfIntHalf args = new ArgumentsHalfIntHalf();
655                 args.inBase = arrayInBase[i * 4 + j];
656                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
657                 args.inExponent = arrayInExponent[i * 4 + j];
658                 // Figure out what the outputs should have been.
659                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
660                 CoreMathVerifier.computePown(args, target);
661                 // Validate the outputs.
662                 boolean valid = true;
663                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
664                     valid = false;
665                 }
666                 if (!valid) {
667                     if (!errorFound) {
668                         errorFound = true;
669                         message.append("Input inBase: ");
670                         appendVariableToMessage(message, args.inBase);
671                         message.append("\n");
672                         message.append("Input inExponent: ");
673                         appendVariableToMessage(message, args.inExponent);
674                         message.append("\n");
675                         message.append("Expected output out: ");
676                         appendVariableToMessage(message, args.out);
677                         message.append("\n");
678                         message.append("Actual   output out: ");
679                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
680                         message.append("\n");
681                         message.append("Actual   output out (in double): ");
682                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
683                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
684                             message.append(" FAIL");
685                         }
686                         message.append("\n");
687                         message.append("Errors at");
688                     }
689                     message.append(" [");
690                     message.append(Integer.toString(i));
691                     message.append(", ");
692                     message.append(Integer.toString(j));
693                     message.append("]");
694                 }
695             }
696         }
697         assertFalse("Incorrect output for checkPownHalf4Int4Half4" +
698                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
699     }
700 
testPown()701     public void testPown() {
702         checkPownFloatIntFloat();
703         checkPownFloat2Int2Float2();
704         checkPownFloat3Int3Float3();
705         checkPownFloat4Int4Float4();
706         checkPownHalfIntHalf();
707         checkPownHalf2Int2Half2();
708         checkPownHalf3Int3Half3();
709         checkPownHalf4Int4Half4();
710     }
711 }
712