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 TestHalfSqrt extends RSBaseCompute {
29 
30     private ScriptC_TestHalfSqrt script;
31     private ScriptC_TestHalfSqrtRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestHalfSqrt(mRS);
37         scriptRelaxed = new ScriptC_TestHalfSqrtRelaxed(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 
checkHalfSqrtFloatFloat()52     private void checkHalfSqrtFloatFloat() {
53         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x8be766c7a15db5fal, false);
54         try {
55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
56             script.forEach_testHalfSqrtFloatFloat(inV, out);
57             verifyResultsHalfSqrtFloatFloat(inV, out, false);
58             out.destroy();
59         } catch (Exception e) {
60             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloatFloat: " + e.toString());
61         }
62         try {
63             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
64             scriptRelaxed.forEach_testHalfSqrtFloatFloat(inV, out);
65             verifyResultsHalfSqrtFloatFloat(inV, out, true);
66             out.destroy();
67         } catch (Exception e) {
68             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloatFloat: " + e.toString());
69         }
70         inV.destroy();
71     }
72 
verifyResultsHalfSqrtFloatFloat(Allocation inV, Allocation out, boolean relaxed)73     private void verifyResultsHalfSqrtFloatFloat(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.HALF, Target.ReturnType.FLOAT, relaxed);
89                 CoreMathVerifier.computeHalfSqrt(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 checkHalfSqrtFloatFloat" +
121                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
122     }
123 
checkHalfSqrtFloat2Float2()124     private void checkHalfSqrtFloat2Float2() {
125         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x7a300d2342519b8el, false);
126         try {
127             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
128             script.forEach_testHalfSqrtFloat2Float2(inV, out);
129             verifyResultsHalfSqrtFloat2Float2(inV, out, false);
130             out.destroy();
131         } catch (Exception e) {
132             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloat2Float2: " + e.toString());
133         }
134         try {
135             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
136             scriptRelaxed.forEach_testHalfSqrtFloat2Float2(inV, out);
137             verifyResultsHalfSqrtFloat2Float2(inV, out, true);
138             out.destroy();
139         } catch (Exception e) {
140             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloat2Float2: " + e.toString());
141         }
142         inV.destroy();
143     }
144 
verifyResultsHalfSqrtFloat2Float2(Allocation inV, Allocation out, boolean relaxed)145     private void verifyResultsHalfSqrtFloat2Float2(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.HALF, Target.ReturnType.FLOAT, relaxed);
161                 CoreMathVerifier.computeHalfSqrt(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 checkHalfSqrtFloat2Float2" +
193                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
194     }
195 
checkHalfSqrtFloat3Float3()196     private void checkHalfSqrtFloat3Float3() {
197         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x7a31d63e386cbc6cl, false);
198         try {
199             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
200             script.forEach_testHalfSqrtFloat3Float3(inV, out);
201             verifyResultsHalfSqrtFloat3Float3(inV, out, false);
202             out.destroy();
203         } catch (Exception e) {
204             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloat3Float3: " + e.toString());
205         }
206         try {
207             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
208             scriptRelaxed.forEach_testHalfSqrtFloat3Float3(inV, out);
209             verifyResultsHalfSqrtFloat3Float3(inV, out, true);
210             out.destroy();
211         } catch (Exception e) {
212             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloat3Float3: " + e.toString());
213         }
214         inV.destroy();
215     }
216 
verifyResultsHalfSqrtFloat3Float3(Allocation inV, Allocation out, boolean relaxed)217     private void verifyResultsHalfSqrtFloat3Float3(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.HALF, Target.ReturnType.FLOAT, relaxed);
233                 CoreMathVerifier.computeHalfSqrt(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 checkHalfSqrtFloat3Float3" +
265                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
266     }
267 
checkHalfSqrtFloat4Float4()268     private void checkHalfSqrtFloat4Float4() {
269         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x7a339f592e87dd4al, false);
270         try {
271             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
272             script.forEach_testHalfSqrtFloat4Float4(inV, out);
273             verifyResultsHalfSqrtFloat4Float4(inV, out, false);
274             out.destroy();
275         } catch (Exception e) {
276             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloat4Float4: " + e.toString());
277         }
278         try {
279             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
280             scriptRelaxed.forEach_testHalfSqrtFloat4Float4(inV, out);
281             verifyResultsHalfSqrtFloat4Float4(inV, out, true);
282             out.destroy();
283         } catch (Exception e) {
284             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testHalfSqrtFloat4Float4: " + e.toString());
285         }
286         inV.destroy();
287     }
288 
verifyResultsHalfSqrtFloat4Float4(Allocation inV, Allocation out, boolean relaxed)289     private void verifyResultsHalfSqrtFloat4Float4(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.HALF, Target.ReturnType.FLOAT, relaxed);
305                 CoreMathVerifier.computeHalfSqrt(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 checkHalfSqrtFloat4Float4" +
337                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
338     }
339 
testHalfSqrt()340     public void testHalfSqrt() {
341         checkHalfSqrtFloatFloat();
342         checkHalfSqrtFloat2Float2();
343         checkHalfSqrtFloat3Float3();
344         checkHalfSqrtFloat4Float4();
345     }
346 }
347