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 TestStep extends RSBaseCompute {
29 
30     private ScriptC_TestStep script;
31     private ScriptC_TestStepRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestStep(mRS);
37         scriptRelaxed = new ScriptC_TestStepRelaxed(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 ArgumentsFloatFloatFloat {
48         public float inEdge;
49         public float inV;
50         public Target.Floaty out;
51     }
52 
checkStepFloatFloatFloat()53     private void checkStepFloatFloatFloat() {
54         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x598900c49184fbfel, false);
55         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x9aefccaa832f44e9l, false);
56         try {
57             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
58             script.set_gAllocInV(inV);
59             script.forEach_testStepFloatFloatFloat(inEdge, out);
60             verifyResultsStepFloatFloatFloat(inEdge, inV, out, false);
61             out.destroy();
62         } catch (Exception e) {
63             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloatFloat: " + e.toString());
64         }
65         try {
66             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
67             scriptRelaxed.set_gAllocInV(inV);
68             scriptRelaxed.forEach_testStepFloatFloatFloat(inEdge, out);
69             verifyResultsStepFloatFloatFloat(inEdge, inV, out, true);
70             out.destroy();
71         } catch (Exception e) {
72             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloatFloat: " + e.toString());
73         }
74         inEdge.destroy();
75         inV.destroy();
76     }
77 
verifyResultsStepFloatFloatFloat(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)78     private void verifyResultsStepFloatFloatFloat(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
79         float[] arrayInEdge = new float[INPUTSIZE * 1];
80         Arrays.fill(arrayInEdge, (float) 42);
81         inEdge.copyTo(arrayInEdge);
82         float[] arrayInV = new float[INPUTSIZE * 1];
83         Arrays.fill(arrayInV, (float) 42);
84         inV.copyTo(arrayInV);
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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
94                 args.inEdge = arrayInEdge[i];
95                 args.inV = arrayInV[i];
96                 // Figure out what the outputs should have been.
97                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
98                 CoreMathVerifier.computeStep(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 inEdge: ");
108                         appendVariableToMessage(message, args.inEdge);
109                         message.append("\n");
110                         message.append("Input inV: ");
111                         appendVariableToMessage(message, args.inV);
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 checkStepFloatFloatFloat" +
133                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
134     }
135 
checkStepFloat2Float2Float2()136     private void checkStepFloat2Float2Float2() {
137         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x6efefa297df69504l, false);
138         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x12eb000b8567f58bl, false);
139         try {
140             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
141             script.set_gAllocInV(inV);
142             script.forEach_testStepFloat2Float2Float2(inEdge, out);
143             verifyResultsStepFloat2Float2Float2(inEdge, inV, out, false);
144             out.destroy();
145         } catch (Exception e) {
146             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat2Float2Float2: " + e.toString());
147         }
148         try {
149             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
150             scriptRelaxed.set_gAllocInV(inV);
151             scriptRelaxed.forEach_testStepFloat2Float2Float2(inEdge, out);
152             verifyResultsStepFloat2Float2Float2(inEdge, inV, out, true);
153             out.destroy();
154         } catch (Exception e) {
155             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat2Float2Float2: " + e.toString());
156         }
157         inEdge.destroy();
158         inV.destroy();
159     }
160 
verifyResultsStepFloat2Float2Float2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)161     private void verifyResultsStepFloat2Float2Float2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
162         float[] arrayInEdge = new float[INPUTSIZE * 2];
163         Arrays.fill(arrayInEdge, (float) 42);
164         inEdge.copyTo(arrayInEdge);
165         float[] arrayInV = new float[INPUTSIZE * 2];
166         Arrays.fill(arrayInV, (float) 42);
167         inV.copyTo(arrayInV);
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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
177                 args.inEdge = arrayInEdge[i * 2 + j];
178                 args.inV = arrayInV[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.computeStep(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 inEdge: ");
191                         appendVariableToMessage(message, args.inEdge);
192                         message.append("\n");
193                         message.append("Input inV: ");
194                         appendVariableToMessage(message, args.inV);
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 checkStepFloat2Float2Float2" +
216                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
217     }
218 
checkStepFloat3Float3Float3()219     private void checkStepFloat3Float3Float3() {
220         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x9e548cd666a7a77l, false);
221         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x674fde2b8745f72cl, false);
222         try {
223             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
224             script.set_gAllocInV(inV);
225             script.forEach_testStepFloat3Float3Float3(inEdge, out);
226             verifyResultsStepFloat3Float3Float3(inEdge, inV, out, false);
227             out.destroy();
228         } catch (Exception e) {
229             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat3Float3Float3: " + e.toString());
230         }
231         try {
232             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
233             scriptRelaxed.set_gAllocInV(inV);
234             scriptRelaxed.forEach_testStepFloat3Float3Float3(inEdge, out);
235             verifyResultsStepFloat3Float3Float3(inEdge, inV, out, true);
236             out.destroy();
237         } catch (Exception e) {
238             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat3Float3Float3: " + e.toString());
239         }
240         inEdge.destroy();
241         inV.destroy();
242     }
243 
verifyResultsStepFloat3Float3Float3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)244     private void verifyResultsStepFloat3Float3Float3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
245         float[] arrayInEdge = new float[INPUTSIZE * 4];
246         Arrays.fill(arrayInEdge, (float) 42);
247         inEdge.copyTo(arrayInEdge);
248         float[] arrayInV = new float[INPUTSIZE * 4];
249         Arrays.fill(arrayInV, (float) 42);
250         inV.copyTo(arrayInV);
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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
260                 args.inEdge = arrayInEdge[i * 4 + j];
261                 args.inV = arrayInV[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.computeStep(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 inEdge: ");
274                         appendVariableToMessage(message, args.inEdge);
275                         message.append("\n");
276                         message.append("Input inV: ");
277                         appendVariableToMessage(message, args.inV);
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 checkStepFloat3Float3Float3" +
299                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
300     }
301 
checkStepFloat4Float4Float4()302     private void checkStepFloat4Float4Float4() {
303         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xa4cb97714ede5feal, false);
304         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xbbb4bc4b8923f8cdl, false);
305         try {
306             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
307             script.set_gAllocInV(inV);
308             script.forEach_testStepFloat4Float4Float4(inEdge, out);
309             verifyResultsStepFloat4Float4Float4(inEdge, inV, out, false);
310             out.destroy();
311         } catch (Exception e) {
312             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat4Float4Float4: " + e.toString());
313         }
314         try {
315             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
316             scriptRelaxed.set_gAllocInV(inV);
317             scriptRelaxed.forEach_testStepFloat4Float4Float4(inEdge, out);
318             verifyResultsStepFloat4Float4Float4(inEdge, inV, out, true);
319             out.destroy();
320         } catch (Exception e) {
321             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat4Float4Float4: " + e.toString());
322         }
323         inEdge.destroy();
324         inV.destroy();
325     }
326 
verifyResultsStepFloat4Float4Float4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)327     private void verifyResultsStepFloat4Float4Float4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
328         float[] arrayInEdge = new float[INPUTSIZE * 4];
329         Arrays.fill(arrayInEdge, (float) 42);
330         inEdge.copyTo(arrayInEdge);
331         float[] arrayInV = new float[INPUTSIZE * 4];
332         Arrays.fill(arrayInV, (float) 42);
333         inV.copyTo(arrayInV);
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                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
343                 args.inEdge = arrayInEdge[i * 4 + j];
344                 args.inV = arrayInV[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.computeStep(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 inEdge: ");
357                         appendVariableToMessage(message, args.inEdge);
358                         message.append("\n");
359                         message.append("Input inV: ");
360                         appendVariableToMessage(message, args.inV);
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 checkStepFloat4Float4Float4" +
382                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
383     }
384 
385     public class ArgumentsHalfHalfHalf {
386         public short inEdge;
387         public double inEdgeDouble;
388         public short inV;
389         public double inVDouble;
390         public Target.Floaty out;
391     }
392 
checkStepHalfHalfHalf()393     private void checkStepHalfHalfHalf() {
394         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0x5b86b69ee2c3f577l, false);
395         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0xba03c0b83240702cl, false);
396         try {
397             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
398             script.set_gAllocInV(inV);
399             script.forEach_testStepHalfHalfHalf(inEdge, out);
400             verifyResultsStepHalfHalfHalf(inEdge, inV, out, false);
401             out.destroy();
402         } catch (Exception e) {
403             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalfHalf: " + e.toString());
404         }
405         try {
406             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
407             scriptRelaxed.set_gAllocInV(inV);
408             scriptRelaxed.forEach_testStepHalfHalfHalf(inEdge, out);
409             verifyResultsStepHalfHalfHalf(inEdge, inV, out, true);
410             out.destroy();
411         } catch (Exception e) {
412             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalfHalf: " + e.toString());
413         }
414         inEdge.destroy();
415         inV.destroy();
416     }
417 
verifyResultsStepHalfHalfHalf(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)418     private void verifyResultsStepHalfHalfHalf(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
419         short[] arrayInEdge = new short[INPUTSIZE * 1];
420         Arrays.fill(arrayInEdge, (short) 42);
421         inEdge.copyTo(arrayInEdge);
422         short[] arrayInV = new short[INPUTSIZE * 1];
423         Arrays.fill(arrayInV, (short) 42);
424         inV.copyTo(arrayInV);
425         short[] arrayOut = new short[INPUTSIZE * 1];
426         Arrays.fill(arrayOut, (short) 42);
427         out.copyTo(arrayOut);
428         StringBuilder message = new StringBuilder();
429         boolean errorFound = false;
430         for (int i = 0; i < INPUTSIZE; i++) {
431             for (int j = 0; j < 1 ; j++) {
432                 // Extract the inputs.
433                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
434                 args.inEdge = arrayInEdge[i];
435                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
436                 args.inV = arrayInV[i];
437                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
438                 // Figure out what the outputs should have been.
439                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
440                 CoreMathVerifier.computeStep(args, target);
441                 // Validate the outputs.
442                 boolean valid = true;
443                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
444                     valid = false;
445                 }
446                 if (!valid) {
447                     if (!errorFound) {
448                         errorFound = true;
449                         message.append("Input inEdge: ");
450                         appendVariableToMessage(message, args.inEdge);
451                         message.append("\n");
452                         message.append("Input inV: ");
453                         appendVariableToMessage(message, args.inV);
454                         message.append("\n");
455                         message.append("Expected output out: ");
456                         appendVariableToMessage(message, args.out);
457                         message.append("\n");
458                         message.append("Actual   output out: ");
459                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
460                         message.append("\n");
461                         message.append("Actual   output out (in double): ");
462                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]));
463                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
464                             message.append(" FAIL");
465                         }
466                         message.append("\n");
467                         message.append("Errors at");
468                     }
469                     message.append(" [");
470                     message.append(Integer.toString(i));
471                     message.append(", ");
472                     message.append(Integer.toString(j));
473                     message.append("]");
474                 }
475             }
476         }
477         assertFalse("Incorrect output for checkStepHalfHalfHalf" +
478                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
479     }
480 
checkStepHalf2Half2Half2()481     private void checkStepHalf2Half2Half2() {
482         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x670e5805ee08afadl, false);
483         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x6a600ec7ba1588del, false);
484         try {
485             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
486             script.set_gAllocInV(inV);
487             script.forEach_testStepHalf2Half2Half2(inEdge, out);
488             verifyResultsStepHalf2Half2Half2(inEdge, inV, out, false);
489             out.destroy();
490         } catch (Exception e) {
491             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf2Half2Half2: " + e.toString());
492         }
493         try {
494             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
495             scriptRelaxed.set_gAllocInV(inV);
496             scriptRelaxed.forEach_testStepHalf2Half2Half2(inEdge, out);
497             verifyResultsStepHalf2Half2Half2(inEdge, inV, out, true);
498             out.destroy();
499         } catch (Exception e) {
500             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf2Half2Half2: " + e.toString());
501         }
502         inEdge.destroy();
503         inV.destroy();
504     }
505 
verifyResultsStepHalf2Half2Half2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)506     private void verifyResultsStepHalf2Half2Half2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
507         short[] arrayInEdge = new short[INPUTSIZE * 2];
508         Arrays.fill(arrayInEdge, (short) 42);
509         inEdge.copyTo(arrayInEdge);
510         short[] arrayInV = new short[INPUTSIZE * 2];
511         Arrays.fill(arrayInV, (short) 42);
512         inV.copyTo(arrayInV);
513         short[] arrayOut = new short[INPUTSIZE * 2];
514         Arrays.fill(arrayOut, (short) 42);
515         out.copyTo(arrayOut);
516         StringBuilder message = new StringBuilder();
517         boolean errorFound = false;
518         for (int i = 0; i < INPUTSIZE; i++) {
519             for (int j = 0; j < 2 ; j++) {
520                 // Extract the inputs.
521                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
522                 args.inEdge = arrayInEdge[i * 2 + j];
523                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
524                 args.inV = arrayInV[i * 2 + j];
525                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
526                 // Figure out what the outputs should have been.
527                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
528                 CoreMathVerifier.computeStep(args, target);
529                 // Validate the outputs.
530                 boolean valid = true;
531                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
532                     valid = false;
533                 }
534                 if (!valid) {
535                     if (!errorFound) {
536                         errorFound = true;
537                         message.append("Input inEdge: ");
538                         appendVariableToMessage(message, args.inEdge);
539                         message.append("\n");
540                         message.append("Input inV: ");
541                         appendVariableToMessage(message, args.inV);
542                         message.append("\n");
543                         message.append("Expected output out: ");
544                         appendVariableToMessage(message, args.out);
545                         message.append("\n");
546                         message.append("Actual   output out: ");
547                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
548                         message.append("\n");
549                         message.append("Actual   output out (in double): ");
550                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]));
551                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
552                             message.append(" FAIL");
553                         }
554                         message.append("\n");
555                         message.append("Errors at");
556                     }
557                     message.append(" [");
558                     message.append(Integer.toString(i));
559                     message.append(", ");
560                     message.append(Integer.toString(j));
561                     message.append("]");
562                 }
563             }
564         }
565         assertFalse("Incorrect output for checkStepHalf2Half2Half2" +
566                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
567     }
568 
checkStepHalf3Half3Half3()569     private void checkStepHalf3Half3Half3() {
570         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0xa098639be1ba008al, false);
571         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0x90148eb118b499adl, false);
572         try {
573             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
574             script.set_gAllocInV(inV);
575             script.forEach_testStepHalf3Half3Half3(inEdge, out);
576             verifyResultsStepHalf3Half3Half3(inEdge, inV, out, false);
577             out.destroy();
578         } catch (Exception e) {
579             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf3Half3Half3: " + e.toString());
580         }
581         try {
582             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
583             scriptRelaxed.set_gAllocInV(inV);
584             scriptRelaxed.forEach_testStepHalf3Half3Half3(inEdge, out);
585             verifyResultsStepHalf3Half3Half3(inEdge, inV, out, true);
586             out.destroy();
587         } catch (Exception e) {
588             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf3Half3Half3: " + e.toString());
589         }
590         inEdge.destroy();
591         inV.destroy();
592     }
593 
verifyResultsStepHalf3Half3Half3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)594     private void verifyResultsStepHalf3Half3Half3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
595         short[] arrayInEdge = new short[INPUTSIZE * 4];
596         Arrays.fill(arrayInEdge, (short) 42);
597         inEdge.copyTo(arrayInEdge);
598         short[] arrayInV = new short[INPUTSIZE * 4];
599         Arrays.fill(arrayInV, (short) 42);
600         inV.copyTo(arrayInV);
601         short[] arrayOut = new short[INPUTSIZE * 4];
602         Arrays.fill(arrayOut, (short) 42);
603         out.copyTo(arrayOut);
604         StringBuilder message = new StringBuilder();
605         boolean errorFound = false;
606         for (int i = 0; i < INPUTSIZE; i++) {
607             for (int j = 0; j < 3 ; j++) {
608                 // Extract the inputs.
609                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
610                 args.inEdge = arrayInEdge[i * 4 + j];
611                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
612                 args.inV = arrayInV[i * 4 + j];
613                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
614                 // Figure out what the outputs should have been.
615                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
616                 CoreMathVerifier.computeStep(args, target);
617                 // Validate the outputs.
618                 boolean valid = true;
619                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
620                     valid = false;
621                 }
622                 if (!valid) {
623                     if (!errorFound) {
624                         errorFound = true;
625                         message.append("Input inEdge: ");
626                         appendVariableToMessage(message, args.inEdge);
627                         message.append("\n");
628                         message.append("Input inV: ");
629                         appendVariableToMessage(message, args.inV);
630                         message.append("\n");
631                         message.append("Expected output out: ");
632                         appendVariableToMessage(message, args.out);
633                         message.append("\n");
634                         message.append("Actual   output out: ");
635                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
636                         message.append("\n");
637                         message.append("Actual   output out (in double): ");
638                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
639                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
640                             message.append(" FAIL");
641                         }
642                         message.append("\n");
643                         message.append("Errors at");
644                     }
645                     message.append(" [");
646                     message.append(Integer.toString(i));
647                     message.append(", ");
648                     message.append(Integer.toString(j));
649                     message.append("]");
650                 }
651             }
652         }
653         assertFalse("Incorrect output for checkStepHalf3Half3Half3" +
654                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
655     }
656 
checkStepHalf4Half4Half4()657     private void checkStepHalf4Half4Half4() {
658         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0xda226f31d56b5167l, false);
659         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0xb5c90e9a7753aa7cl, false);
660         try {
661             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
662             script.set_gAllocInV(inV);
663             script.forEach_testStepHalf4Half4Half4(inEdge, out);
664             verifyResultsStepHalf4Half4Half4(inEdge, inV, out, false);
665             out.destroy();
666         } catch (Exception e) {
667             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf4Half4Half4: " + e.toString());
668         }
669         try {
670             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
671             scriptRelaxed.set_gAllocInV(inV);
672             scriptRelaxed.forEach_testStepHalf4Half4Half4(inEdge, out);
673             verifyResultsStepHalf4Half4Half4(inEdge, inV, out, true);
674             out.destroy();
675         } catch (Exception e) {
676             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf4Half4Half4: " + e.toString());
677         }
678         inEdge.destroy();
679         inV.destroy();
680     }
681 
verifyResultsStepHalf4Half4Half4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)682     private void verifyResultsStepHalf4Half4Half4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
683         short[] arrayInEdge = new short[INPUTSIZE * 4];
684         Arrays.fill(arrayInEdge, (short) 42);
685         inEdge.copyTo(arrayInEdge);
686         short[] arrayInV = new short[INPUTSIZE * 4];
687         Arrays.fill(arrayInV, (short) 42);
688         inV.copyTo(arrayInV);
689         short[] arrayOut = new short[INPUTSIZE * 4];
690         Arrays.fill(arrayOut, (short) 42);
691         out.copyTo(arrayOut);
692         StringBuilder message = new StringBuilder();
693         boolean errorFound = false;
694         for (int i = 0; i < INPUTSIZE; i++) {
695             for (int j = 0; j < 4 ; j++) {
696                 // Extract the inputs.
697                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
698                 args.inEdge = arrayInEdge[i * 4 + j];
699                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
700                 args.inV = arrayInV[i * 4 + j];
701                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
702                 // Figure out what the outputs should have been.
703                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
704                 CoreMathVerifier.computeStep(args, target);
705                 // Validate the outputs.
706                 boolean valid = true;
707                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
708                     valid = false;
709                 }
710                 if (!valid) {
711                     if (!errorFound) {
712                         errorFound = true;
713                         message.append("Input inEdge: ");
714                         appendVariableToMessage(message, args.inEdge);
715                         message.append("\n");
716                         message.append("Input inV: ");
717                         appendVariableToMessage(message, args.inV);
718                         message.append("\n");
719                         message.append("Expected output out: ");
720                         appendVariableToMessage(message, args.out);
721                         message.append("\n");
722                         message.append("Actual   output out: ");
723                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
724                         message.append("\n");
725                         message.append("Actual   output out (in double): ");
726                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
727                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
728                             message.append(" FAIL");
729                         }
730                         message.append("\n");
731                         message.append("Errors at");
732                     }
733                     message.append(" [");
734                     message.append(Integer.toString(i));
735                     message.append(", ");
736                     message.append(Integer.toString(j));
737                     message.append("]");
738                 }
739             }
740         }
741         assertFalse("Incorrect output for checkStepHalf4Half4Half4" +
742                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
743     }
744 
checkStepFloat2FloatFloat2()745     private void checkStepFloat2FloatFloat2() {
746         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0xb0ac06c45b3d8b26l, false);
747         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xfa7d66f2b6d48a21l, false);
748         try {
749             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
750             script.set_gAllocInV(inV);
751             script.forEach_testStepFloat2FloatFloat2(inEdge, out);
752             verifyResultsStepFloat2FloatFloat2(inEdge, inV, out, false);
753             out.destroy();
754         } catch (Exception e) {
755             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat2FloatFloat2: " + e.toString());
756         }
757         try {
758             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
759             scriptRelaxed.set_gAllocInV(inV);
760             scriptRelaxed.forEach_testStepFloat2FloatFloat2(inEdge, out);
761             verifyResultsStepFloat2FloatFloat2(inEdge, inV, out, true);
762             out.destroy();
763         } catch (Exception e) {
764             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat2FloatFloat2: " + e.toString());
765         }
766         inEdge.destroy();
767         inV.destroy();
768     }
769 
verifyResultsStepFloat2FloatFloat2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)770     private void verifyResultsStepFloat2FloatFloat2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
771         float[] arrayInEdge = new float[INPUTSIZE * 2];
772         Arrays.fill(arrayInEdge, (float) 42);
773         inEdge.copyTo(arrayInEdge);
774         float[] arrayInV = new float[INPUTSIZE * 1];
775         Arrays.fill(arrayInV, (float) 42);
776         inV.copyTo(arrayInV);
777         float[] arrayOut = new float[INPUTSIZE * 2];
778         Arrays.fill(arrayOut, (float) 42);
779         out.copyTo(arrayOut);
780         StringBuilder message = new StringBuilder();
781         boolean errorFound = false;
782         for (int i = 0; i < INPUTSIZE; i++) {
783             for (int j = 0; j < 2 ; j++) {
784                 // Extract the inputs.
785                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
786                 args.inEdge = arrayInEdge[i * 2 + j];
787                 args.inV = arrayInV[i];
788                 // Figure out what the outputs should have been.
789                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
790                 CoreMathVerifier.computeStep(args, target);
791                 // Validate the outputs.
792                 boolean valid = true;
793                 if (!args.out.couldBe(arrayOut[i * 2 + j])) {
794                     valid = false;
795                 }
796                 if (!valid) {
797                     if (!errorFound) {
798                         errorFound = true;
799                         message.append("Input inEdge: ");
800                         appendVariableToMessage(message, args.inEdge);
801                         message.append("\n");
802                         message.append("Input inV: ");
803                         appendVariableToMessage(message, args.inV);
804                         message.append("\n");
805                         message.append("Expected output out: ");
806                         appendVariableToMessage(message, args.out);
807                         message.append("\n");
808                         message.append("Actual   output out: ");
809                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
810                         if (!args.out.couldBe(arrayOut[i * 2 + j])) {
811                             message.append(" FAIL");
812                         }
813                         message.append("\n");
814                         message.append("Errors at");
815                     }
816                     message.append(" [");
817                     message.append(Integer.toString(i));
818                     message.append(", ");
819                     message.append(Integer.toString(j));
820                     message.append("]");
821                 }
822             }
823         }
824         assertFalse("Incorrect output for checkStepFloat2FloatFloat2" +
825                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
826     }
827 
checkStepFloat3FloatFloat3()828     private void checkStepFloat3FloatFloat3() {
829         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x304ed837c68f43fal, false);
830         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x4fcd1a0aa53f7e7dl, false);
831         try {
832             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
833             script.set_gAllocInV(inV);
834             script.forEach_testStepFloat3FloatFloat3(inEdge, out);
835             verifyResultsStepFloat3FloatFloat3(inEdge, inV, out, false);
836             out.destroy();
837         } catch (Exception e) {
838             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat3FloatFloat3: " + e.toString());
839         }
840         try {
841             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
842             scriptRelaxed.set_gAllocInV(inV);
843             scriptRelaxed.forEach_testStepFloat3FloatFloat3(inEdge, out);
844             verifyResultsStepFloat3FloatFloat3(inEdge, inV, out, true);
845             out.destroy();
846         } catch (Exception e) {
847             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat3FloatFloat3: " + e.toString());
848         }
849         inEdge.destroy();
850         inV.destroy();
851     }
852 
verifyResultsStepFloat3FloatFloat3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)853     private void verifyResultsStepFloat3FloatFloat3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
854         float[] arrayInEdge = new float[INPUTSIZE * 4];
855         Arrays.fill(arrayInEdge, (float) 42);
856         inEdge.copyTo(arrayInEdge);
857         float[] arrayInV = new float[INPUTSIZE * 1];
858         Arrays.fill(arrayInV, (float) 42);
859         inV.copyTo(arrayInV);
860         float[] arrayOut = new float[INPUTSIZE * 4];
861         Arrays.fill(arrayOut, (float) 42);
862         out.copyTo(arrayOut);
863         StringBuilder message = new StringBuilder();
864         boolean errorFound = false;
865         for (int i = 0; i < INPUTSIZE; i++) {
866             for (int j = 0; j < 3 ; j++) {
867                 // Extract the inputs.
868                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
869                 args.inEdge = arrayInEdge[i * 4 + j];
870                 args.inV = arrayInV[i];
871                 // Figure out what the outputs should have been.
872                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
873                 CoreMathVerifier.computeStep(args, target);
874                 // Validate the outputs.
875                 boolean valid = true;
876                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
877                     valid = false;
878                 }
879                 if (!valid) {
880                     if (!errorFound) {
881                         errorFound = true;
882                         message.append("Input inEdge: ");
883                         appendVariableToMessage(message, args.inEdge);
884                         message.append("\n");
885                         message.append("Input inV: ");
886                         appendVariableToMessage(message, args.inV);
887                         message.append("\n");
888                         message.append("Expected output out: ");
889                         appendVariableToMessage(message, args.out);
890                         message.append("\n");
891                         message.append("Actual   output out: ");
892                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
893                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
894                             message.append(" FAIL");
895                         }
896                         message.append("\n");
897                         message.append("Errors at");
898                     }
899                     message.append(" [");
900                     message.append(Integer.toString(i));
901                     message.append(", ");
902                     message.append(Integer.toString(j));
903                     message.append("]");
904                 }
905             }
906         }
907         assertFalse("Incorrect output for checkStepFloat3FloatFloat3" +
908                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
909     }
910 
checkStepFloat4FloatFloat4()911     private void checkStepFloat4FloatFloat4() {
912         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xaff1a9ab31e0fccel, false);
913         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xa51ccd2293aa72d9l, false);
914         try {
915             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
916             script.set_gAllocInV(inV);
917             script.forEach_testStepFloat4FloatFloat4(inEdge, out);
918             verifyResultsStepFloat4FloatFloat4(inEdge, inV, out, false);
919             out.destroy();
920         } catch (Exception e) {
921             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat4FloatFloat4: " + e.toString());
922         }
923         try {
924             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
925             scriptRelaxed.set_gAllocInV(inV);
926             scriptRelaxed.forEach_testStepFloat4FloatFloat4(inEdge, out);
927             verifyResultsStepFloat4FloatFloat4(inEdge, inV, out, true);
928             out.destroy();
929         } catch (Exception e) {
930             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloat4FloatFloat4: " + e.toString());
931         }
932         inEdge.destroy();
933         inV.destroy();
934     }
935 
verifyResultsStepFloat4FloatFloat4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)936     private void verifyResultsStepFloat4FloatFloat4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
937         float[] arrayInEdge = new float[INPUTSIZE * 4];
938         Arrays.fill(arrayInEdge, (float) 42);
939         inEdge.copyTo(arrayInEdge);
940         float[] arrayInV = new float[INPUTSIZE * 1];
941         Arrays.fill(arrayInV, (float) 42);
942         inV.copyTo(arrayInV);
943         float[] arrayOut = new float[INPUTSIZE * 4];
944         Arrays.fill(arrayOut, (float) 42);
945         out.copyTo(arrayOut);
946         StringBuilder message = new StringBuilder();
947         boolean errorFound = false;
948         for (int i = 0; i < INPUTSIZE; i++) {
949             for (int j = 0; j < 4 ; j++) {
950                 // Extract the inputs.
951                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
952                 args.inEdge = arrayInEdge[i * 4 + j];
953                 args.inV = arrayInV[i];
954                 // Figure out what the outputs should have been.
955                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
956                 CoreMathVerifier.computeStep(args, target);
957                 // Validate the outputs.
958                 boolean valid = true;
959                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
960                     valid = false;
961                 }
962                 if (!valid) {
963                     if (!errorFound) {
964                         errorFound = true;
965                         message.append("Input inEdge: ");
966                         appendVariableToMessage(message, args.inEdge);
967                         message.append("\n");
968                         message.append("Input inV: ");
969                         appendVariableToMessage(message, args.inV);
970                         message.append("\n");
971                         message.append("Expected output out: ");
972                         appendVariableToMessage(message, args.out);
973                         message.append("\n");
974                         message.append("Actual   output out: ");
975                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
976                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
977                             message.append(" FAIL");
978                         }
979                         message.append("\n");
980                         message.append("Errors at");
981                     }
982                     message.append(" [");
983                     message.append(Integer.toString(i));
984                     message.append(", ");
985                     message.append(Integer.toString(j));
986                     message.append("]");
987                 }
988             }
989         }
990         assertFalse("Incorrect output for checkStepFloat4FloatFloat4" +
991                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
992     }
993 
checkStepHalf2HalfHalf2()994     private void checkStepHalf2HalfHalf2() {
995         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x5690fcfb2f4438d3l, false);
996         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0x974fa6ac002d7ce0l, false);
997         try {
998             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
999             script.set_gAllocInV(inV);
1000             script.forEach_testStepHalf2HalfHalf2(inEdge, out);
1001             verifyResultsStepHalf2HalfHalf2(inEdge, inV, out, false);
1002             out.destroy();
1003         } catch (Exception e) {
1004             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf2HalfHalf2: " + e.toString());
1005         }
1006         try {
1007             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
1008             scriptRelaxed.set_gAllocInV(inV);
1009             scriptRelaxed.forEach_testStepHalf2HalfHalf2(inEdge, out);
1010             verifyResultsStepHalf2HalfHalf2(inEdge, inV, out, true);
1011             out.destroy();
1012         } catch (Exception e) {
1013             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf2HalfHalf2: " + e.toString());
1014         }
1015         inEdge.destroy();
1016         inV.destroy();
1017     }
1018 
verifyResultsStepHalf2HalfHalf2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1019     private void verifyResultsStepHalf2HalfHalf2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1020         short[] arrayInEdge = new short[INPUTSIZE * 2];
1021         Arrays.fill(arrayInEdge, (short) 42);
1022         inEdge.copyTo(arrayInEdge);
1023         short[] arrayInV = new short[INPUTSIZE * 1];
1024         Arrays.fill(arrayInV, (short) 42);
1025         inV.copyTo(arrayInV);
1026         short[] arrayOut = new short[INPUTSIZE * 2];
1027         Arrays.fill(arrayOut, (short) 42);
1028         out.copyTo(arrayOut);
1029         StringBuilder message = new StringBuilder();
1030         boolean errorFound = false;
1031         for (int i = 0; i < INPUTSIZE; i++) {
1032             for (int j = 0; j < 2 ; j++) {
1033                 // Extract the inputs.
1034                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
1035                 args.inEdge = arrayInEdge[i * 2 + j];
1036                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
1037                 args.inV = arrayInV[i];
1038                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
1039                 // Figure out what the outputs should have been.
1040                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
1041                 CoreMathVerifier.computeStep(args, target);
1042                 // Validate the outputs.
1043                 boolean valid = true;
1044                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
1045                     valid = false;
1046                 }
1047                 if (!valid) {
1048                     if (!errorFound) {
1049                         errorFound = true;
1050                         message.append("Input inEdge: ");
1051                         appendVariableToMessage(message, args.inEdge);
1052                         message.append("\n");
1053                         message.append("Input inV: ");
1054                         appendVariableToMessage(message, args.inV);
1055                         message.append("\n");
1056                         message.append("Expected output out: ");
1057                         appendVariableToMessage(message, args.out);
1058                         message.append("\n");
1059                         message.append("Actual   output out: ");
1060                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
1061                         message.append("\n");
1062                         message.append("Actual   output out (in double): ");
1063                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]));
1064                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
1065                             message.append(" FAIL");
1066                         }
1067                         message.append("\n");
1068                         message.append("Errors at");
1069                     }
1070                     message.append(" [");
1071                     message.append(Integer.toString(i));
1072                     message.append(", ");
1073                     message.append(Integer.toString(j));
1074                     message.append("]");
1075                 }
1076             }
1077         }
1078         assertFalse("Incorrect output for checkStepHalf2HalfHalf2" +
1079                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1080     }
1081 
checkStepHalf3HalfHalf3()1082     private void checkStepHalf3HalfHalf3() {
1083         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0xaaf41201b3cf71ffl, false);
1084         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0xc1dcc72ab2c70e84l, false);
1085         try {
1086             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
1087             script.set_gAllocInV(inV);
1088             script.forEach_testStepHalf3HalfHalf3(inEdge, out);
1089             verifyResultsStepHalf3HalfHalf3(inEdge, inV, out, false);
1090             out.destroy();
1091         } catch (Exception e) {
1092             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf3HalfHalf3: " + e.toString());
1093         }
1094         try {
1095             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
1096             scriptRelaxed.set_gAllocInV(inV);
1097             scriptRelaxed.forEach_testStepHalf3HalfHalf3(inEdge, out);
1098             verifyResultsStepHalf3HalfHalf3(inEdge, inV, out, true);
1099             out.destroy();
1100         } catch (Exception e) {
1101             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf3HalfHalf3: " + e.toString());
1102         }
1103         inEdge.destroy();
1104         inV.destroy();
1105     }
1106 
verifyResultsStepHalf3HalfHalf3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1107     private void verifyResultsStepHalf3HalfHalf3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1108         short[] arrayInEdge = new short[INPUTSIZE * 4];
1109         Arrays.fill(arrayInEdge, (short) 42);
1110         inEdge.copyTo(arrayInEdge);
1111         short[] arrayInV = new short[INPUTSIZE * 1];
1112         Arrays.fill(arrayInV, (short) 42);
1113         inV.copyTo(arrayInV);
1114         short[] arrayOut = new short[INPUTSIZE * 4];
1115         Arrays.fill(arrayOut, (short) 42);
1116         out.copyTo(arrayOut);
1117         StringBuilder message = new StringBuilder();
1118         boolean errorFound = false;
1119         for (int i = 0; i < INPUTSIZE; i++) {
1120             for (int j = 0; j < 3 ; j++) {
1121                 // Extract the inputs.
1122                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
1123                 args.inEdge = arrayInEdge[i * 4 + j];
1124                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
1125                 args.inV = arrayInV[i];
1126                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
1127                 // Figure out what the outputs should have been.
1128                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
1129                 CoreMathVerifier.computeStep(args, target);
1130                 // Validate the outputs.
1131                 boolean valid = true;
1132                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1133                     valid = false;
1134                 }
1135                 if (!valid) {
1136                     if (!errorFound) {
1137                         errorFound = true;
1138                         message.append("Input inEdge: ");
1139                         appendVariableToMessage(message, args.inEdge);
1140                         message.append("\n");
1141                         message.append("Input inV: ");
1142                         appendVariableToMessage(message, args.inV);
1143                         message.append("\n");
1144                         message.append("Expected output out: ");
1145                         appendVariableToMessage(message, args.out);
1146                         message.append("\n");
1147                         message.append("Actual   output out: ");
1148                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
1149                         message.append("\n");
1150                         message.append("Actual   output out (in double): ");
1151                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
1152                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1153                             message.append(" FAIL");
1154                         }
1155                         message.append("\n");
1156                         message.append("Errors at");
1157                     }
1158                     message.append(" [");
1159                     message.append(Integer.toString(i));
1160                     message.append(", ");
1161                     message.append(Integer.toString(j));
1162                     message.append("]");
1163                 }
1164             }
1165         }
1166         assertFalse("Incorrect output for checkStepHalf3HalfHalf3" +
1167                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1168     }
1169 
checkStepHalf4HalfHalf4()1170     private void checkStepHalf4HalfHalf4() {
1171         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0xff572708385aab2bl, false);
1172         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0xec69e7a96560a028l, false);
1173         try {
1174             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
1175             script.set_gAllocInV(inV);
1176             script.forEach_testStepHalf4HalfHalf4(inEdge, out);
1177             verifyResultsStepHalf4HalfHalf4(inEdge, inV, out, false);
1178             out.destroy();
1179         } catch (Exception e) {
1180             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf4HalfHalf4: " + e.toString());
1181         }
1182         try {
1183             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
1184             scriptRelaxed.set_gAllocInV(inV);
1185             scriptRelaxed.forEach_testStepHalf4HalfHalf4(inEdge, out);
1186             verifyResultsStepHalf4HalfHalf4(inEdge, inV, out, true);
1187             out.destroy();
1188         } catch (Exception e) {
1189             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalf4HalfHalf4: " + e.toString());
1190         }
1191         inEdge.destroy();
1192         inV.destroy();
1193     }
1194 
verifyResultsStepHalf4HalfHalf4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1195     private void verifyResultsStepHalf4HalfHalf4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1196         short[] arrayInEdge = new short[INPUTSIZE * 4];
1197         Arrays.fill(arrayInEdge, (short) 42);
1198         inEdge.copyTo(arrayInEdge);
1199         short[] arrayInV = new short[INPUTSIZE * 1];
1200         Arrays.fill(arrayInV, (short) 42);
1201         inV.copyTo(arrayInV);
1202         short[] arrayOut = new short[INPUTSIZE * 4];
1203         Arrays.fill(arrayOut, (short) 42);
1204         out.copyTo(arrayOut);
1205         StringBuilder message = new StringBuilder();
1206         boolean errorFound = false;
1207         for (int i = 0; i < INPUTSIZE; i++) {
1208             for (int j = 0; j < 4 ; j++) {
1209                 // Extract the inputs.
1210                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
1211                 args.inEdge = arrayInEdge[i * 4 + j];
1212                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
1213                 args.inV = arrayInV[i];
1214                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
1215                 // Figure out what the outputs should have been.
1216                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
1217                 CoreMathVerifier.computeStep(args, target);
1218                 // Validate the outputs.
1219                 boolean valid = true;
1220                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1221                     valid = false;
1222                 }
1223                 if (!valid) {
1224                     if (!errorFound) {
1225                         errorFound = true;
1226                         message.append("Input inEdge: ");
1227                         appendVariableToMessage(message, args.inEdge);
1228                         message.append("\n");
1229                         message.append("Input inV: ");
1230                         appendVariableToMessage(message, args.inV);
1231                         message.append("\n");
1232                         message.append("Expected output out: ");
1233                         appendVariableToMessage(message, args.out);
1234                         message.append("\n");
1235                         message.append("Actual   output out: ");
1236                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
1237                         message.append("\n");
1238                         message.append("Actual   output out (in double): ");
1239                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
1240                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1241                             message.append(" FAIL");
1242                         }
1243                         message.append("\n");
1244                         message.append("Errors at");
1245                     }
1246                     message.append(" [");
1247                     message.append(Integer.toString(i));
1248                     message.append(", ");
1249                     message.append(Integer.toString(j));
1250                     message.append("]");
1251                 }
1252             }
1253         }
1254         assertFalse("Incorrect output for checkStepHalf4HalfHalf4" +
1255                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1256     }
1257 
checkStepFloatFloat2Float2()1258     private void checkStepFloatFloat2Float2() {
1259         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x70a0554e664b1852l, false);
1260         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0xdd7f0d444e2f7c5l, false);
1261         try {
1262             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
1263             script.set_gAllocInV(inV);
1264             script.forEach_testStepFloatFloat2Float2(inEdge, out);
1265             verifyResultsStepFloatFloat2Float2(inEdge, inV, out, false);
1266             out.destroy();
1267         } catch (Exception e) {
1268             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloat2Float2: " + e.toString());
1269         }
1270         try {
1271             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
1272             scriptRelaxed.set_gAllocInV(inV);
1273             scriptRelaxed.forEach_testStepFloatFloat2Float2(inEdge, out);
1274             verifyResultsStepFloatFloat2Float2(inEdge, inV, out, true);
1275             out.destroy();
1276         } catch (Exception e) {
1277             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloat2Float2: " + e.toString());
1278         }
1279         inEdge.destroy();
1280         inV.destroy();
1281     }
1282 
verifyResultsStepFloatFloat2Float2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1283     private void verifyResultsStepFloatFloat2Float2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1284         float[] arrayInEdge = new float[INPUTSIZE * 1];
1285         Arrays.fill(arrayInEdge, (float) 42);
1286         inEdge.copyTo(arrayInEdge);
1287         float[] arrayInV = new float[INPUTSIZE * 2];
1288         Arrays.fill(arrayInV, (float) 42);
1289         inV.copyTo(arrayInV);
1290         float[] arrayOut = new float[INPUTSIZE * 2];
1291         Arrays.fill(arrayOut, (float) 42);
1292         out.copyTo(arrayOut);
1293         StringBuilder message = new StringBuilder();
1294         boolean errorFound = false;
1295         for (int i = 0; i < INPUTSIZE; i++) {
1296             for (int j = 0; j < 2 ; j++) {
1297                 // Extract the inputs.
1298                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
1299                 args.inEdge = arrayInEdge[i];
1300                 args.inV = arrayInV[i * 2 + j];
1301                 // Figure out what the outputs should have been.
1302                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
1303                 CoreMathVerifier.computeStep(args, target);
1304                 // Validate the outputs.
1305                 boolean valid = true;
1306                 if (!args.out.couldBe(arrayOut[i * 2 + j])) {
1307                     valid = false;
1308                 }
1309                 if (!valid) {
1310                     if (!errorFound) {
1311                         errorFound = true;
1312                         message.append("Input inEdge: ");
1313                         appendVariableToMessage(message, args.inEdge);
1314                         message.append("\n");
1315                         message.append("Input inV: ");
1316                         appendVariableToMessage(message, args.inV);
1317                         message.append("\n");
1318                         message.append("Expected output out: ");
1319                         appendVariableToMessage(message, args.out);
1320                         message.append("\n");
1321                         message.append("Actual   output out: ");
1322                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
1323                         if (!args.out.couldBe(arrayOut[i * 2 + j])) {
1324                             message.append(" FAIL");
1325                         }
1326                         message.append("\n");
1327                         message.append("Errors at");
1328                     }
1329                     message.append(" [");
1330                     message.append(Integer.toString(i));
1331                     message.append(", ");
1332                     message.append(Integer.toString(j));
1333                     message.append("]");
1334                 }
1335             }
1336         }
1337         assertFalse("Incorrect output for checkStepFloatFloat2Float2" +
1338                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1339     }
1340 
checkStepFloatFloat3Float3()1341     private void checkStepFloatFloat3Float3() {
1342         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x9b2d75ce91abcbccl, false);
1343         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0xdd9b9ef3afe18a3l, false);
1344         try {
1345             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
1346             script.set_gAllocInV(inV);
1347             script.forEach_testStepFloatFloat3Float3(inEdge, out);
1348             verifyResultsStepFloatFloat3Float3(inEdge, inV, out, false);
1349             out.destroy();
1350         } catch (Exception e) {
1351             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloat3Float3: " + e.toString());
1352         }
1353         try {
1354             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
1355             scriptRelaxed.set_gAllocInV(inV);
1356             scriptRelaxed.forEach_testStepFloatFloat3Float3(inEdge, out);
1357             verifyResultsStepFloatFloat3Float3(inEdge, inV, out, true);
1358             out.destroy();
1359         } catch (Exception e) {
1360             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloat3Float3: " + e.toString());
1361         }
1362         inEdge.destroy();
1363         inV.destroy();
1364     }
1365 
verifyResultsStepFloatFloat3Float3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1366     private void verifyResultsStepFloatFloat3Float3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1367         float[] arrayInEdge = new float[INPUTSIZE * 1];
1368         Arrays.fill(arrayInEdge, (float) 42);
1369         inEdge.copyTo(arrayInEdge);
1370         float[] arrayInV = new float[INPUTSIZE * 4];
1371         Arrays.fill(arrayInV, (float) 42);
1372         inV.copyTo(arrayInV);
1373         float[] arrayOut = new float[INPUTSIZE * 4];
1374         Arrays.fill(arrayOut, (float) 42);
1375         out.copyTo(arrayOut);
1376         StringBuilder message = new StringBuilder();
1377         boolean errorFound = false;
1378         for (int i = 0; i < INPUTSIZE; i++) {
1379             for (int j = 0; j < 3 ; j++) {
1380                 // Extract the inputs.
1381                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
1382                 args.inEdge = arrayInEdge[i];
1383                 args.inV = arrayInV[i * 4 + j];
1384                 // Figure out what the outputs should have been.
1385                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
1386                 CoreMathVerifier.computeStep(args, target);
1387                 // Validate the outputs.
1388                 boolean valid = true;
1389                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
1390                     valid = false;
1391                 }
1392                 if (!valid) {
1393                     if (!errorFound) {
1394                         errorFound = true;
1395                         message.append("Input inEdge: ");
1396                         appendVariableToMessage(message, args.inEdge);
1397                         message.append("\n");
1398                         message.append("Input inV: ");
1399                         appendVariableToMessage(message, args.inV);
1400                         message.append("\n");
1401                         message.append("Expected output out: ");
1402                         appendVariableToMessage(message, args.out);
1403                         message.append("\n");
1404                         message.append("Actual   output out: ");
1405                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
1406                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
1407                             message.append(" FAIL");
1408                         }
1409                         message.append("\n");
1410                         message.append("Errors at");
1411                     }
1412                     message.append(" [");
1413                     message.append(Integer.toString(i));
1414                     message.append(", ");
1415                     message.append(Integer.toString(j));
1416                     message.append("]");
1417                 }
1418             }
1419         }
1420         assertFalse("Incorrect output for checkStepFloatFloat3Float3" +
1421                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1422     }
1423 
checkStepFloatFloat4Float4()1424     private void checkStepFloatFloat4Float4() {
1425         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xc5ba964ebd0c7f46l, false);
1426         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xddb830a31193981l, false);
1427         try {
1428             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
1429             script.set_gAllocInV(inV);
1430             script.forEach_testStepFloatFloat4Float4(inEdge, out);
1431             verifyResultsStepFloatFloat4Float4(inEdge, inV, out, false);
1432             out.destroy();
1433         } catch (Exception e) {
1434             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloat4Float4: " + e.toString());
1435         }
1436         try {
1437             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
1438             scriptRelaxed.set_gAllocInV(inV);
1439             scriptRelaxed.forEach_testStepFloatFloat4Float4(inEdge, out);
1440             verifyResultsStepFloatFloat4Float4(inEdge, inV, out, true);
1441             out.destroy();
1442         } catch (Exception e) {
1443             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepFloatFloat4Float4: " + e.toString());
1444         }
1445         inEdge.destroy();
1446         inV.destroy();
1447     }
1448 
verifyResultsStepFloatFloat4Float4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1449     private void verifyResultsStepFloatFloat4Float4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1450         float[] arrayInEdge = new float[INPUTSIZE * 1];
1451         Arrays.fill(arrayInEdge, (float) 42);
1452         inEdge.copyTo(arrayInEdge);
1453         float[] arrayInV = new float[INPUTSIZE * 4];
1454         Arrays.fill(arrayInV, (float) 42);
1455         inV.copyTo(arrayInV);
1456         float[] arrayOut = new float[INPUTSIZE * 4];
1457         Arrays.fill(arrayOut, (float) 42);
1458         out.copyTo(arrayOut);
1459         StringBuilder message = new StringBuilder();
1460         boolean errorFound = false;
1461         for (int i = 0; i < INPUTSIZE; i++) {
1462             for (int j = 0; j < 4 ; j++) {
1463                 // Extract the inputs.
1464                 ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
1465                 args.inEdge = arrayInEdge[i];
1466                 args.inV = arrayInV[i * 4 + j];
1467                 // Figure out what the outputs should have been.
1468                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
1469                 CoreMathVerifier.computeStep(args, target);
1470                 // Validate the outputs.
1471                 boolean valid = true;
1472                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
1473                     valid = false;
1474                 }
1475                 if (!valid) {
1476                     if (!errorFound) {
1477                         errorFound = true;
1478                         message.append("Input inEdge: ");
1479                         appendVariableToMessage(message, args.inEdge);
1480                         message.append("\n");
1481                         message.append("Input inV: ");
1482                         appendVariableToMessage(message, args.inV);
1483                         message.append("\n");
1484                         message.append("Expected output out: ");
1485                         appendVariableToMessage(message, args.out);
1486                         message.append("\n");
1487                         message.append("Actual   output out: ");
1488                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
1489                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
1490                             message.append(" FAIL");
1491                         }
1492                         message.append("\n");
1493                         message.append("Errors at");
1494                     }
1495                     message.append(" [");
1496                     message.append(Integer.toString(i));
1497                     message.append(", ");
1498                     message.append(Integer.toString(j));
1499                     message.append("]");
1500                 }
1501             }
1502         }
1503         assertFalse("Incorrect output for checkStepFloatFloat4Float4" +
1504                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1505     }
1506 
checkStepHalfHalf2Half2()1507     private void checkStepHalfHalf2Half2() {
1508         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0xecb0cdca0915e1ddl, false);
1509         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x851a02469aa13beel, false);
1510         try {
1511             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
1512             script.set_gAllocInV(inV);
1513             script.forEach_testStepHalfHalf2Half2(inEdge, out);
1514             verifyResultsStepHalfHalf2Half2(inEdge, inV, out, false);
1515             out.destroy();
1516         } catch (Exception e) {
1517             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalf2Half2: " + e.toString());
1518         }
1519         try {
1520             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
1521             scriptRelaxed.set_gAllocInV(inV);
1522             scriptRelaxed.forEach_testStepHalfHalf2Half2(inEdge, out);
1523             verifyResultsStepHalfHalf2Half2(inEdge, inV, out, true);
1524             out.destroy();
1525         } catch (Exception e) {
1526             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalf2Half2: " + e.toString());
1527         }
1528         inEdge.destroy();
1529         inV.destroy();
1530     }
1531 
verifyResultsStepHalfHalf2Half2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1532     private void verifyResultsStepHalfHalf2Half2(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1533         short[] arrayInEdge = new short[INPUTSIZE * 1];
1534         Arrays.fill(arrayInEdge, (short) 42);
1535         inEdge.copyTo(arrayInEdge);
1536         short[] arrayInV = new short[INPUTSIZE * 2];
1537         Arrays.fill(arrayInV, (short) 42);
1538         inV.copyTo(arrayInV);
1539         short[] arrayOut = new short[INPUTSIZE * 2];
1540         Arrays.fill(arrayOut, (short) 42);
1541         out.copyTo(arrayOut);
1542         StringBuilder message = new StringBuilder();
1543         boolean errorFound = false;
1544         for (int i = 0; i < INPUTSIZE; i++) {
1545             for (int j = 0; j < 2 ; j++) {
1546                 // Extract the inputs.
1547                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
1548                 args.inEdge = arrayInEdge[i];
1549                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
1550                 args.inV = arrayInV[i * 2 + j];
1551                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
1552                 // Figure out what the outputs should have been.
1553                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
1554                 CoreMathVerifier.computeStep(args, target);
1555                 // Validate the outputs.
1556                 boolean valid = true;
1557                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
1558                     valid = false;
1559                 }
1560                 if (!valid) {
1561                     if (!errorFound) {
1562                         errorFound = true;
1563                         message.append("Input inEdge: ");
1564                         appendVariableToMessage(message, args.inEdge);
1565                         message.append("\n");
1566                         message.append("Input inV: ");
1567                         appendVariableToMessage(message, args.inV);
1568                         message.append("\n");
1569                         message.append("Expected output out: ");
1570                         appendVariableToMessage(message, args.out);
1571                         message.append("\n");
1572                         message.append("Actual   output out: ");
1573                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
1574                         message.append("\n");
1575                         message.append("Actual   output out (in double): ");
1576                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]));
1577                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 2 + j]))) {
1578                             message.append(" FAIL");
1579                         }
1580                         message.append("\n");
1581                         message.append("Errors at");
1582                     }
1583                     message.append(" [");
1584                     message.append(Integer.toString(i));
1585                     message.append(", ");
1586                     message.append(Integer.toString(j));
1587                     message.append("]");
1588                 }
1589             }
1590         }
1591         assertFalse("Incorrect output for checkStepHalfHalf2Half2" +
1592                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1593     }
1594 
checkStepHalfHalf3Half3()1595     private void checkStepHalfHalf3Half3() {
1596         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0xf9965187030b71f9l, false);
1597         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0x851a0ce7f9a900e2l, false);
1598         try {
1599             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
1600             script.set_gAllocInV(inV);
1601             script.forEach_testStepHalfHalf3Half3(inEdge, out);
1602             verifyResultsStepHalfHalf3Half3(inEdge, inV, out, false);
1603             out.destroy();
1604         } catch (Exception e) {
1605             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalf3Half3: " + e.toString());
1606         }
1607         try {
1608             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
1609             scriptRelaxed.set_gAllocInV(inV);
1610             scriptRelaxed.forEach_testStepHalfHalf3Half3(inEdge, out);
1611             verifyResultsStepHalfHalf3Half3(inEdge, inV, out, true);
1612             out.destroy();
1613         } catch (Exception e) {
1614             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalf3Half3: " + e.toString());
1615         }
1616         inEdge.destroy();
1617         inV.destroy();
1618     }
1619 
verifyResultsStepHalfHalf3Half3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1620     private void verifyResultsStepHalfHalf3Half3(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1621         short[] arrayInEdge = new short[INPUTSIZE * 1];
1622         Arrays.fill(arrayInEdge, (short) 42);
1623         inEdge.copyTo(arrayInEdge);
1624         short[] arrayInV = new short[INPUTSIZE * 4];
1625         Arrays.fill(arrayInV, (short) 42);
1626         inV.copyTo(arrayInV);
1627         short[] arrayOut = new short[INPUTSIZE * 4];
1628         Arrays.fill(arrayOut, (short) 42);
1629         out.copyTo(arrayOut);
1630         StringBuilder message = new StringBuilder();
1631         boolean errorFound = false;
1632         for (int i = 0; i < INPUTSIZE; i++) {
1633             for (int j = 0; j < 3 ; j++) {
1634                 // Extract the inputs.
1635                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
1636                 args.inEdge = arrayInEdge[i];
1637                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
1638                 args.inV = arrayInV[i * 4 + j];
1639                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
1640                 // Figure out what the outputs should have been.
1641                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
1642                 CoreMathVerifier.computeStep(args, target);
1643                 // Validate the outputs.
1644                 boolean valid = true;
1645                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1646                     valid = false;
1647                 }
1648                 if (!valid) {
1649                     if (!errorFound) {
1650                         errorFound = true;
1651                         message.append("Input inEdge: ");
1652                         appendVariableToMessage(message, args.inEdge);
1653                         message.append("\n");
1654                         message.append("Input inV: ");
1655                         appendVariableToMessage(message, args.inV);
1656                         message.append("\n");
1657                         message.append("Expected output out: ");
1658                         appendVariableToMessage(message, args.out);
1659                         message.append("\n");
1660                         message.append("Actual   output out: ");
1661                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
1662                         message.append("\n");
1663                         message.append("Actual   output out (in double): ");
1664                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
1665                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1666                             message.append(" FAIL");
1667                         }
1668                         message.append("\n");
1669                         message.append("Errors at");
1670                     }
1671                     message.append(" [");
1672                     message.append(Integer.toString(i));
1673                     message.append(", ");
1674                     message.append(Integer.toString(j));
1675                     message.append("]");
1676                 }
1677             }
1678         }
1679         assertFalse("Incorrect output for checkStepHalfHalf3Half3" +
1680                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1681     }
1682 
checkStepHalfHalf4Half4()1683     private void checkStepHalfHalf4Half4() {
1684         Allocation inEdge = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0x67bd543fd010215l, false);
1685         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0x851a178958b0c5d6l, false);
1686         try {
1687             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
1688             script.set_gAllocInV(inV);
1689             script.forEach_testStepHalfHalf4Half4(inEdge, out);
1690             verifyResultsStepHalfHalf4Half4(inEdge, inV, out, false);
1691             out.destroy();
1692         } catch (Exception e) {
1693             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalf4Half4: " + e.toString());
1694         }
1695         try {
1696             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
1697             scriptRelaxed.set_gAllocInV(inV);
1698             scriptRelaxed.forEach_testStepHalfHalf4Half4(inEdge, out);
1699             verifyResultsStepHalfHalf4Half4(inEdge, inV, out, true);
1700             out.destroy();
1701         } catch (Exception e) {
1702             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testStepHalfHalf4Half4: " + e.toString());
1703         }
1704         inEdge.destroy();
1705         inV.destroy();
1706     }
1707 
verifyResultsStepHalfHalf4Half4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed)1708     private void verifyResultsStepHalfHalf4Half4(Allocation inEdge, Allocation inV, Allocation out, boolean relaxed) {
1709         short[] arrayInEdge = new short[INPUTSIZE * 1];
1710         Arrays.fill(arrayInEdge, (short) 42);
1711         inEdge.copyTo(arrayInEdge);
1712         short[] arrayInV = new short[INPUTSIZE * 4];
1713         Arrays.fill(arrayInV, (short) 42);
1714         inV.copyTo(arrayInV);
1715         short[] arrayOut = new short[INPUTSIZE * 4];
1716         Arrays.fill(arrayOut, (short) 42);
1717         out.copyTo(arrayOut);
1718         StringBuilder message = new StringBuilder();
1719         boolean errorFound = false;
1720         for (int i = 0; i < INPUTSIZE; i++) {
1721             for (int j = 0; j < 4 ; j++) {
1722                 // Extract the inputs.
1723                 ArgumentsHalfHalfHalf args = new ArgumentsHalfHalfHalf();
1724                 args.inEdge = arrayInEdge[i];
1725                 args.inEdgeDouble = Float16Utils.convertFloat16ToDouble(args.inEdge);
1726                 args.inV = arrayInV[i * 4 + j];
1727                 args.inVDouble = Float16Utils.convertFloat16ToDouble(args.inV);
1728                 // Figure out what the outputs should have been.
1729                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
1730                 CoreMathVerifier.computeStep(args, target);
1731                 // Validate the outputs.
1732                 boolean valid = true;
1733                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1734                     valid = false;
1735                 }
1736                 if (!valid) {
1737                     if (!errorFound) {
1738                         errorFound = true;
1739                         message.append("Input inEdge: ");
1740                         appendVariableToMessage(message, args.inEdge);
1741                         message.append("\n");
1742                         message.append("Input inV: ");
1743                         appendVariableToMessage(message, args.inV);
1744                         message.append("\n");
1745                         message.append("Expected output out: ");
1746                         appendVariableToMessage(message, args.out);
1747                         message.append("\n");
1748                         message.append("Actual   output out: ");
1749                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
1750                         message.append("\n");
1751                         message.append("Actual   output out (in double): ");
1752                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]));
1753                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 4 + j]))) {
1754                             message.append(" FAIL");
1755                         }
1756                         message.append("\n");
1757                         message.append("Errors at");
1758                     }
1759                     message.append(" [");
1760                     message.append(Integer.toString(i));
1761                     message.append(", ");
1762                     message.append(Integer.toString(j));
1763                     message.append("]");
1764                 }
1765             }
1766         }
1767         assertFalse("Incorrect output for checkStepHalfHalf4Half4" +
1768                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
1769     }
1770 
testStep()1771     public void testStep() {
1772         checkStepFloatFloatFloat();
1773         checkStepFloat2Float2Float2();
1774         checkStepFloat3Float3Float3();
1775         checkStepFloat4Float4Float4();
1776         checkStepHalfHalfHalf();
1777         checkStepHalf2Half2Half2();
1778         checkStepHalf3Half3Half3();
1779         checkStepHalf4Half4Half4();
1780         checkStepFloat2FloatFloat2();
1781         checkStepFloat3FloatFloat3();
1782         checkStepFloat4FloatFloat4();
1783         checkStepHalf2HalfHalf2();
1784         checkStepHalf3HalfHalf3();
1785         checkStepHalf4HalfHalf4();
1786         checkStepFloatFloat2Float2();
1787         checkStepFloatFloat3Float3();
1788         checkStepFloatFloat4Float4();
1789         checkStepHalfHalf2Half2();
1790         checkStepHalfHalf3Half3();
1791         checkStepHalfHalf4Half4();
1792     }
1793 }
1794