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