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