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