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