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 TestPow extends RSBaseCompute {
29 
30     private ScriptC_TestPow script;
31     private ScriptC_TestPowRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestPow(mRS);
37         scriptRelaxed = new ScriptC_TestPowRelaxed(mRS);
38     }
39 
40     public class ArgumentsFloatFloatFloat {
41         public float inBase;
42         public float inExponent;
43         public Target.Floaty out;
44     }
45 
checkPowFloatFloatFloat()46     private void checkPowFloatFloatFloat() {
47         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x622b91eel, false);
48         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x49a5f734l, 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_testPowFloatFloatFloat(inBase, out);
53             verifyResultsPowFloatFloatFloat(inBase, inExponent, out, false);
54         } catch (Exception e) {
55             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloatFloatFloat: " + 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_testPowFloatFloatFloat(inBase, out);
61             verifyResultsPowFloatFloatFloat(inBase, inExponent, out, true);
62         } catch (Exception e) {
63             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloatFloatFloat: " + e.toString());
64         }
65     }
66 
verifyResultsPowFloatFloatFloat(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)67     private void verifyResultsPowFloatFloatFloat(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         float[] arrayInExponent = new float[INPUTSIZE * 1];
72         Arrays.fill(arrayInExponent, (float) 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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
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.computePow(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 checkPowFloatFloatFloat" +
122                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
123     }
124 
checkPowFloat2Float2Float2()125     private void checkPowFloat2Float2Float2() {
126         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x895c2294l, false);
127         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0xbbad35fal, 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_testPowFloat2Float2Float2(inBase, out);
132             verifyResultsPowFloat2Float2Float2(inBase, inExponent, out, false);
133         } catch (Exception e) {
134             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloat2Float2Float2: " + 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_testPowFloat2Float2Float2(inBase, out);
140             verifyResultsPowFloat2Float2Float2(inBase, inExponent, out, true);
141         } catch (Exception e) {
142             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloat2Float2Float2: " + e.toString());
143         }
144     }
145 
verifyResultsPowFloat2Float2Float2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)146     private void verifyResultsPowFloat2Float2Float2(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         float[] arrayInExponent = new float[INPUTSIZE * 2];
151         Arrays.fill(arrayInExponent, (float) 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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
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.computePow(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 checkPowFloat2Float2Float2" +
201                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
202     }
203 
checkPowFloat3Float3Float3()204     private void checkPowFloat3Float3Float3() {
205         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x71d00807l, false);
206         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x12cfb87dl, 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_testPowFloat3Float3Float3(inBase, out);
211             verifyResultsPowFloat3Float3Float3(inBase, inExponent, out, false);
212         } catch (Exception e) {
213             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloat3Float3Float3: " + 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_testPowFloat3Float3Float3(inBase, out);
219             verifyResultsPowFloat3Float3Float3(inBase, inExponent, out, true);
220         } catch (Exception e) {
221             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloat3Float3Float3: " + e.toString());
222         }
223     }
224 
verifyResultsPowFloat3Float3Float3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)225     private void verifyResultsPowFloat3Float3Float3(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         float[] arrayInExponent = new float[INPUTSIZE * 4];
230         Arrays.fill(arrayInExponent, (float) 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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
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.computePow(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 checkPowFloat3Float3Float3" +
280                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
281     }
282 
checkPowFloat4Float4Float4()283     private void checkPowFloat4Float4Float4() {
284         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x5a43ed7al, false);
285         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x69f23b00l, 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_testPowFloat4Float4Float4(inBase, out);
290             verifyResultsPowFloat4Float4Float4(inBase, inExponent, out, false);
291         } catch (Exception e) {
292             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloat4Float4Float4: " + 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_testPowFloat4Float4Float4(inBase, out);
298             verifyResultsPowFloat4Float4Float4(inBase, inExponent, out, true);
299         } catch (Exception e) {
300             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowFloat4Float4Float4: " + e.toString());
301         }
302     }
303 
verifyResultsPowFloat4Float4Float4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)304     private void verifyResultsPowFloat4Float4Float4(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         float[] arrayInExponent = new float[INPUTSIZE * 4];
309         Arrays.fill(arrayInExponent, (float) 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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
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.computePow(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 checkPowFloat4Float4Float4" +
359                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
360     }
361 
362     public class ArgumentsHalfHalfHalf {
363         public short inBase;
364         public double inBaseDouble;
365         public short inExponent;
366         public double inExponentDouble;
367         public Target.Floaty out;
368     }
369 
checkPowHalfHalfHalf()370     private void checkPowHalfHalfHalf() {
371         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0xe2a33d87l, false);
372         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0x999eb5fdl, false);
373         try {
374             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
375             script.set_gAllocInExponent(inExponent);
376             script.forEach_testPowHalfHalfHalf(inBase, out);
377             verifyResultsPowHalfHalfHalf(inBase, inExponent, out, false);
378         } catch (Exception e) {
379             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalfHalfHalf: " + e.toString());
380         }
381         try {
382             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
383             scriptRelaxed.set_gAllocInExponent(inExponent);
384             scriptRelaxed.forEach_testPowHalfHalfHalf(inBase, out);
385             verifyResultsPowHalfHalfHalf(inBase, inExponent, out, true);
386         } catch (Exception e) {
387             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalfHalfHalf: " + e.toString());
388         }
389     }
390 
verifyResultsPowHalfHalfHalf(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)391     private void verifyResultsPowHalfHalfHalf(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
392         short[] arrayInBase = new short[INPUTSIZE * 1];
393         Arrays.fill(arrayInBase, (short) 42);
394         inBase.copyTo(arrayInBase);
395         short[] arrayInExponent = new short[INPUTSIZE * 1];
396         Arrays.fill(arrayInExponent, (short) 42);
397         inExponent.copyTo(arrayInExponent);
398         short[] arrayOut = new short[INPUTSIZE * 1];
399         Arrays.fill(arrayOut, (short) 42);
400         out.copyTo(arrayOut);
401         StringBuilder message = new StringBuilder();
402         boolean errorFound = false;
403         for (int i = 0; i < INPUTSIZE; i++) {
404             for (int j = 0; j < 1 ; j++) {
405                 // Extract the inputs.
406                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
407                 args.inBase = arrayInBase[i];
408                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
409                 args.inExponent = arrayInExponent[i];
410                 args.inExponentDouble = Float16Utils.convertFloat16ToDouble(args.inExponent);
411                 // Figure out what the outputs should have been.
412                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
413                 CoreMathVerifier.computePow(args, target);
414                 // Validate the outputs.
415                 boolean valid = true;
416                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
417                     valid = false;
418                 }
419                 if (!valid) {
420                     if (!errorFound) {
421                         errorFound = true;
422                         message.append("Input inBase: ");
423                         appendVariableToMessage(message, args.inBase);
424                         message.append("\n");
425                         message.append("Input inExponent: ");
426                         appendVariableToMessage(message, args.inExponent);
427                         message.append("\n");
428                         message.append("Expected output out: ");
429                         appendVariableToMessage(message, args.out);
430                         message.append("\n");
431                         message.append("Actual   output out: ");
432                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
433                         message.append("\n");
434                         message.append("Actual   output out (in double): ");
435                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]));
436                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
437                             message.append(" FAIL");
438                         }
439                         message.append("\n");
440                         message.append("Errors at");
441                     }
442                     message.append(" [");
443                     message.append(Integer.toString(i));
444                     message.append(", ");
445                     message.append(Integer.toString(j));
446                     message.append("]");
447                 }
448             }
449         }
450         assertFalse("Incorrect output for checkPowHalfHalfHalf" +
451                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
452     }
453 
checkPowHalf2Half2Half2()454     private void checkPowHalf2Half2Half2() {
455         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0xbeaf459dl, false);
456         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x5bbbe933l, false);
457         try {
458             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
459             script.set_gAllocInExponent(inExponent);
460             script.forEach_testPowHalf2Half2Half2(inBase, out);
461             verifyResultsPowHalf2Half2Half2(inBase, inExponent, out, false);
462         } catch (Exception e) {
463             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalf2Half2Half2: " + e.toString());
464         }
465         try {
466             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
467             scriptRelaxed.set_gAllocInExponent(inExponent);
468             scriptRelaxed.forEach_testPowHalf2Half2Half2(inBase, out);
469             verifyResultsPowHalf2Half2Half2(inBase, inExponent, out, true);
470         } catch (Exception e) {
471             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalf2Half2Half2: " + e.toString());
472         }
473     }
474 
verifyResultsPowHalf2Half2Half2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)475     private void verifyResultsPowHalf2Half2Half2(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
476         short[] arrayInBase = new short[INPUTSIZE * 2];
477         Arrays.fill(arrayInBase, (short) 42);
478         inBase.copyTo(arrayInBase);
479         short[] arrayInExponent = new short[INPUTSIZE * 2];
480         Arrays.fill(arrayInExponent, (short) 42);
481         inExponent.copyTo(arrayInExponent);
482         short[] arrayOut = new short[INPUTSIZE * 2];
483         Arrays.fill(arrayOut, (short) 42);
484         out.copyTo(arrayOut);
485         StringBuilder message = new StringBuilder();
486         boolean errorFound = false;
487         for (int i = 0; i < INPUTSIZE; i++) {
488             for (int j = 0; j < 2 ; j++) {
489                 // Extract the inputs.
490                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
491                 args.inBase = arrayInBase[i * 2 + j];
492                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
493                 args.inExponent = arrayInExponent[i * 2 + j];
494                 args.inExponentDouble = Float16Utils.convertFloat16ToDouble(args.inExponent);
495                 // Figure out what the outputs should have been.
496                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
497                 CoreMathVerifier.computePow(args, target);
498                 // Validate the outputs.
499                 boolean valid = true;
500                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
501                     valid = false;
502                 }
503                 if (!valid) {
504                     if (!errorFound) {
505                         errorFound = true;
506                         message.append("Input inBase: ");
507                         appendVariableToMessage(message, args.inBase);
508                         message.append("\n");
509                         message.append("Input inExponent: ");
510                         appendVariableToMessage(message, args.inExponent);
511                         message.append("\n");
512                         message.append("Expected output out: ");
513                         appendVariableToMessage(message, args.out);
514                         message.append("\n");
515                         message.append("Actual   output out: ");
516                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
517                         message.append("\n");
518                         message.append("Actual   output out (in double): ");
519                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]));
520                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
521                             message.append(" FAIL");
522                         }
523                         message.append("\n");
524                         message.append("Errors at");
525                     }
526                     message.append(" [");
527                     message.append(Integer.toString(i));
528                     message.append(", ");
529                     message.append(Integer.toString(j));
530                     message.append("]");
531                 }
532             }
533         }
534         assertFalse("Incorrect output for checkPowHalf2Half2Half2" +
535                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
536     }
537 
checkPowHalf3Half3Half3()538     private void checkPowHalf3Half3Half3() {
539         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0xb260967al, false);
540         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0x5d7d1400l, false);
541         try {
542             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
543             script.set_gAllocInExponent(inExponent);
544             script.forEach_testPowHalf3Half3Half3(inBase, out);
545             verifyResultsPowHalf3Half3Half3(inBase, inExponent, out, false);
546         } catch (Exception e) {
547             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalf3Half3Half3: " + e.toString());
548         }
549         try {
550             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
551             scriptRelaxed.set_gAllocInExponent(inExponent);
552             scriptRelaxed.forEach_testPowHalf3Half3Half3(inBase, out);
553             verifyResultsPowHalf3Half3Half3(inBase, inExponent, out, true);
554         } catch (Exception e) {
555             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalf3Half3Half3: " + e.toString());
556         }
557     }
558 
verifyResultsPowHalf3Half3Half3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)559     private void verifyResultsPowHalf3Half3Half3(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
560         short[] arrayInBase = new short[INPUTSIZE * 4];
561         Arrays.fill(arrayInBase, (short) 42);
562         inBase.copyTo(arrayInBase);
563         short[] arrayInExponent = new short[INPUTSIZE * 4];
564         Arrays.fill(arrayInExponent, (short) 42);
565         inExponent.copyTo(arrayInExponent);
566         short[] arrayOut = new short[INPUTSIZE * 4];
567         Arrays.fill(arrayOut, (short) 42);
568         out.copyTo(arrayOut);
569         StringBuilder message = new StringBuilder();
570         boolean errorFound = false;
571         for (int i = 0; i < INPUTSIZE; i++) {
572             for (int j = 0; j < 3 ; j++) {
573                 // Extract the inputs.
574                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
575                 args.inBase = arrayInBase[i * 4 + j];
576                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
577                 args.inExponent = arrayInExponent[i * 4 + j];
578                 args.inExponentDouble = Float16Utils.convertFloat16ToDouble(args.inExponent);
579                 // Figure out what the outputs should have been.
580                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
581                 CoreMathVerifier.computePow(args, target);
582                 // Validate the outputs.
583                 boolean valid = true;
584                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
585                     valid = false;
586                 }
587                 if (!valid) {
588                     if (!errorFound) {
589                         errorFound = true;
590                         message.append("Input inBase: ");
591                         appendVariableToMessage(message, args.inBase);
592                         message.append("\n");
593                         message.append("Input inExponent: ");
594                         appendVariableToMessage(message, args.inExponent);
595                         message.append("\n");
596                         message.append("Expected output out: ");
597                         appendVariableToMessage(message, args.out);
598                         message.append("\n");
599                         message.append("Actual   output out: ");
600                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
601                         message.append("\n");
602                         message.append("Actual   output out (in double): ");
603                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
604                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
605                             message.append(" FAIL");
606                         }
607                         message.append("\n");
608                         message.append("Errors at");
609                     }
610                     message.append(" [");
611                     message.append(Integer.toString(i));
612                     message.append(", ");
613                     message.append(Integer.toString(j));
614                     message.append("]");
615                 }
616             }
617         }
618         assertFalse("Incorrect output for checkPowHalf3Half3Half3" +
619                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
620     }
621 
checkPowHalf4Half4Half4()622     private void checkPowHalf4Half4Half4() {
623         Allocation inBase = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0xa611e757l, false);
624         Allocation inExponent = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0x5f3e3ecdl, false);
625         try {
626             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
627             script.set_gAllocInExponent(inExponent);
628             script.forEach_testPowHalf4Half4Half4(inBase, out);
629             verifyResultsPowHalf4Half4Half4(inBase, inExponent, out, false);
630         } catch (Exception e) {
631             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalf4Half4Half4: " + e.toString());
632         }
633         try {
634             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
635             scriptRelaxed.set_gAllocInExponent(inExponent);
636             scriptRelaxed.forEach_testPowHalf4Half4Half4(inBase, out);
637             verifyResultsPowHalf4Half4Half4(inBase, inExponent, out, true);
638         } catch (Exception e) {
639             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testPowHalf4Half4Half4: " + e.toString());
640         }
641     }
642 
verifyResultsPowHalf4Half4Half4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed)643     private void verifyResultsPowHalf4Half4Half4(Allocation inBase, Allocation inExponent, Allocation out, boolean relaxed) {
644         short[] arrayInBase = new short[INPUTSIZE * 4];
645         Arrays.fill(arrayInBase, (short) 42);
646         inBase.copyTo(arrayInBase);
647         short[] arrayInExponent = new short[INPUTSIZE * 4];
648         Arrays.fill(arrayInExponent, (short) 42);
649         inExponent.copyTo(arrayInExponent);
650         short[] arrayOut = new short[INPUTSIZE * 4];
651         Arrays.fill(arrayOut, (short) 42);
652         out.copyTo(arrayOut);
653         StringBuilder message = new StringBuilder();
654         boolean errorFound = false;
655         for (int i = 0; i < INPUTSIZE; i++) {
656             for (int j = 0; j < 4 ; j++) {
657                 // Extract the inputs.
658                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
659                 args.inBase = arrayInBase[i * 4 + j];
660                 args.inBaseDouble = Float16Utils.convertFloat16ToDouble(args.inBase);
661                 args.inExponent = arrayInExponent[i * 4 + j];
662                 args.inExponentDouble = Float16Utils.convertFloat16ToDouble(args.inExponent);
663                 // Figure out what the outputs should have been.
664                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
665                 CoreMathVerifier.computePow(args, target);
666                 // Validate the outputs.
667                 boolean valid = true;
668                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
669                     valid = false;
670                 }
671                 if (!valid) {
672                     if (!errorFound) {
673                         errorFound = true;
674                         message.append("Input inBase: ");
675                         appendVariableToMessage(message, args.inBase);
676                         message.append("\n");
677                         message.append("Input inExponent: ");
678                         appendVariableToMessage(message, args.inExponent);
679                         message.append("\n");
680                         message.append("Expected output out: ");
681                         appendVariableToMessage(message, args.out);
682                         message.append("\n");
683                         message.append("Actual   output out: ");
684                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
685                         message.append("\n");
686                         message.append("Actual   output out (in double): ");
687                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
688                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
689                             message.append(" FAIL");
690                         }
691                         message.append("\n");
692                         message.append("Errors at");
693                     }
694                     message.append(" [");
695                     message.append(Integer.toString(i));
696                     message.append(", ");
697                     message.append(Integer.toString(j));
698                     message.append("]");
699                 }
700             }
701         }
702         assertFalse("Incorrect output for checkPowHalf4Half4Half4" +
703                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
704     }
705 
testPow()706     public void testPow() {
707         checkPowFloatFloatFloat();
708         checkPowFloat2Float2Float2();
709         checkPowFloat3Float3Float3();
710         checkPowFloat4Float4Float4();
711         checkPowHalfHalfHalf();
712         checkPowHalf2Half2Half2();
713         checkPowHalf3Half3Half3();
714         checkPowHalf4Half4Half4();
715     }
716 }
717