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