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