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 TestNormalize extends RSBaseCompute {
29 
30     private ScriptC_TestNormalize script;
31     private ScriptC_TestNormalizeRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestNormalize(mRS);
37         scriptRelaxed = new ScriptC_TestNormalizeRelaxed(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 
checkNormalizeFloatFloat()52     private void checkNormalizeFloatFloat() {
53         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x6db01d449460061cl, false);
54         try {
55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
56             script.forEach_testNormalizeFloatFloat(inV, out);
57             verifyResultsNormalizeFloatFloat(inV, out, false);
58             out.destroy();
59         } catch (Exception e) {
60             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloatFloat: " + e.toString());
61         }
62         try {
63             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
64             scriptRelaxed.forEach_testNormalizeFloatFloat(inV, out);
65             verifyResultsNormalizeFloatFloat(inV, out, true);
66             out.destroy();
67         } catch (Exception e) {
68             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloatFloat: " + e.toString());
69         }
70         inV.destroy();
71     }
72 
verifyResultsNormalizeFloatFloat(Allocation inV, Allocation out, boolean relaxed)73     private void verifyResultsNormalizeFloatFloat(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.NORMAL, Target.ReturnType.FLOAT, relaxed);
88             CoreMathVerifier.computeNormalize(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 checkNormalizeFloatFloat" +
118                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
119     }
120 
121     public class ArgumentsFloatNFloatN {
122         public float[] inV;
123         public Target.Floaty[] out;
124     }
125 
checkNormalizeFloat2Float2()126     private void checkNormalizeFloat2Float2() {
127         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x3cde199a6e066120l, false);
128         try {
129             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
130             script.forEach_testNormalizeFloat2Float2(inV, out);
131             verifyResultsNormalizeFloat2Float2(inV, out, false);
132             out.destroy();
133         } catch (Exception e) {
134             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloat2Float2: " + e.toString());
135         }
136         try {
137             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
138             scriptRelaxed.forEach_testNormalizeFloat2Float2(inV, out);
139             verifyResultsNormalizeFloat2Float2(inV, out, true);
140             out.destroy();
141         } catch (Exception e) {
142             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloat2Float2: " + e.toString());
143         }
144         inV.destroy();
145     }
146 
verifyResultsNormalizeFloat2Float2(Allocation inV, Allocation out, boolean relaxed)147     private void verifyResultsNormalizeFloat2Float2(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.NORMAL, Target.ReturnType.FLOAT, relaxed);
166             CoreMathVerifier.computeNormalize(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 checkNormalizeFloat2Float2" +
202                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
203     }
204 
checkNormalizeFloat3Float3()205     private void checkNormalizeFloat3Float3() {
206         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x3cdfe2b5642181fel, false);
207         try {
208             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
209             script.forEach_testNormalizeFloat3Float3(inV, out);
210             verifyResultsNormalizeFloat3Float3(inV, out, false);
211             out.destroy();
212         } catch (Exception e) {
213             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloat3Float3: " + e.toString());
214         }
215         try {
216             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
217             scriptRelaxed.forEach_testNormalizeFloat3Float3(inV, out);
218             verifyResultsNormalizeFloat3Float3(inV, out, true);
219             out.destroy();
220         } catch (Exception e) {
221             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloat3Float3: " + e.toString());
222         }
223         inV.destroy();
224     }
225 
verifyResultsNormalizeFloat3Float3(Allocation inV, Allocation out, boolean relaxed)226     private void verifyResultsNormalizeFloat3Float3(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.NORMAL, Target.ReturnType.FLOAT, relaxed);
245             CoreMathVerifier.computeNormalize(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 checkNormalizeFloat3Float3" +
281                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
282     }
283 
checkNormalizeFloat4Float4()284     private void checkNormalizeFloat4Float4() {
285         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x3ce1abd05a3ca2dcl, false);
286         try {
287             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
288             script.forEach_testNormalizeFloat4Float4(inV, out);
289             verifyResultsNormalizeFloat4Float4(inV, out, false);
290             out.destroy();
291         } catch (Exception e) {
292             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloat4Float4: " + e.toString());
293         }
294         try {
295             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
296             scriptRelaxed.forEach_testNormalizeFloat4Float4(inV, out);
297             verifyResultsNormalizeFloat4Float4(inV, out, true);
298             out.destroy();
299         } catch (Exception e) {
300             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeFloat4Float4: " + e.toString());
301         }
302         inV.destroy();
303     }
304 
verifyResultsNormalizeFloat4Float4(Allocation inV, Allocation out, boolean relaxed)305     private void verifyResultsNormalizeFloat4Float4(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.NORMAL, Target.ReturnType.FLOAT, relaxed);
324             CoreMathVerifier.computeNormalize(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 checkNormalizeFloat4Float4" +
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 
checkNormalizeHalfHalf()369     private void checkNormalizeHalfHalf() {
370         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 1, 0xdfc4bff702f4d2e6l, false);
371         try {
372             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
373             script.forEach_testNormalizeHalfHalf(inV, out);
374             verifyResultsNormalizeHalfHalf(inV, out, false);
375             out.destroy();
376         } catch (Exception e) {
377             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalfHalf: " + e.toString());
378         }
379         try {
380             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
381             scriptRelaxed.forEach_testNormalizeHalfHalf(inV, out);
382             verifyResultsNormalizeHalfHalf(inV, out, true);
383             out.destroy();
384         } catch (Exception e) {
385             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalfHalf: " + e.toString());
386         }
387         inV.destroy();
388     }
389 
verifyResultsNormalizeHalfHalf(Allocation inV, Allocation out, boolean relaxed)390     private void verifyResultsNormalizeHalfHalf(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.NORMAL, Target.ReturnType.HALF, relaxed);
406             CoreMathVerifier.computeNormalize(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 checkNormalizeHalfHalf" +
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 
checkNormalizeHalf2Half2()448     private void checkNormalizeHalf2Half2() {
449         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 2, 0x340d8ef8017e4d58l, false);
450         try {
451             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
452             script.forEach_testNormalizeHalf2Half2(inV, out);
453             verifyResultsNormalizeHalf2Half2(inV, out, false);
454             out.destroy();
455         } catch (Exception e) {
456             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalf2Half2: " + e.toString());
457         }
458         try {
459             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 2), INPUTSIZE);
460             scriptRelaxed.forEach_testNormalizeHalf2Half2(inV, out);
461             verifyResultsNormalizeHalf2Half2(inV, out, true);
462             out.destroy();
463         } catch (Exception e) {
464             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalf2Half2: " + e.toString());
465         }
466         inV.destroy();
467     }
468 
verifyResultsNormalizeHalf2Half2(Allocation inV, Allocation out, boolean relaxed)469     private void verifyResultsNormalizeHalf2Half2(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.NORMAL, Target.ReturnType.HALF, relaxed);
490             CoreMathVerifier.computeNormalize(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 checkNormalizeHalf2Half2" +
529                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
530     }
531 
checkNormalizeHalf3Half3()532     private void checkNormalizeHalf3Half3() {
533         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 3, 0x340d99996086124cl, false);
534         try {
535             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
536             script.forEach_testNormalizeHalf3Half3(inV, out);
537             verifyResultsNormalizeHalf3Half3(inV, out, false);
538             out.destroy();
539         } catch (Exception e) {
540             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalf3Half3: " + e.toString());
541         }
542         try {
543             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 3), INPUTSIZE);
544             scriptRelaxed.forEach_testNormalizeHalf3Half3(inV, out);
545             verifyResultsNormalizeHalf3Half3(inV, out, true);
546             out.destroy();
547         } catch (Exception e) {
548             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalf3Half3: " + e.toString());
549         }
550         inV.destroy();
551     }
552 
verifyResultsNormalizeHalf3Half3(Allocation inV, Allocation out, boolean relaxed)553     private void verifyResultsNormalizeHalf3Half3(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.NORMAL, Target.ReturnType.HALF, relaxed);
574             CoreMathVerifier.computeNormalize(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 checkNormalizeHalf3Half3" +
613                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
614     }
615 
checkNormalizeHalf4Half4()616     private void checkNormalizeHalf4Half4() {
617         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_16, 4, 0x340da43abf8dd740l, false);
618         try {
619             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
620             script.forEach_testNormalizeHalf4Half4(inV, out);
621             verifyResultsNormalizeHalf4Half4(inV, out, false);
622             out.destroy();
623         } catch (Exception e) {
624             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalf4Half4: " + e.toString());
625         }
626         try {
627             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 4), INPUTSIZE);
628             scriptRelaxed.forEach_testNormalizeHalf4Half4(inV, out);
629             verifyResultsNormalizeHalf4Half4(inV, out, true);
630             out.destroy();
631         } catch (Exception e) {
632             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNormalizeHalf4Half4: " + e.toString());
633         }
634         inV.destroy();
635     }
636 
verifyResultsNormalizeHalf4Half4(Allocation inV, Allocation out, boolean relaxed)637     private void verifyResultsNormalizeHalf4Half4(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.NORMAL, Target.ReturnType.HALF, relaxed);
658             CoreMathVerifier.computeNormalize(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 checkNormalizeHalf4Half4" +
697                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
698     }
699 
testNormalize()700     public void testNormalize() {
701         checkNormalizeFloatFloat();
702         checkNormalizeFloat2Float2();
703         checkNormalizeFloat3Float3();
704         checkNormalizeFloat4Float4();
705         checkNormalizeHalfHalf();
706         checkNormalizeHalf2Half2();
707         checkNormalizeHalf3Half3();
708         checkNormalizeHalf4Half4();
709     }
710 }
711