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