summaryrefslogtreecommitdiff
path: root/api/gen_runtime.cpp
blob: bfc3d6d8eda9db294d2bc55be65594d60a2dc3f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* This program processes Renderscript function definitions described in spec files.
 * For each spec file provided on the command line, it generates a corresponding
 * Renderscript header (*.rsh) which is meant for inclusion in client scripts.
 *
 * This program also generates Junit test files to automatically test each of the
 * functions using randomly generated data.  We create two files for each function:
 * - a Renderscript file named Test{Function}.rs,
 * - a Junit file named Test{function}.java, which calls the above RS file.
 *
 * This program takes an optional -v parameter, the RS version to target the
 * test files for.  The header file will always contain all the functions.
 *
 * This program contains five main classes:
 * - SpecFile: Represents on spec file.
 * - Function: Each instance represents a function, like clamp.  Even though the
 *      spec file contains many entries for clamp, we'll only have one clamp instance.
 * - Specification: Defines one of the many variations of the function.  There's
 *      a one to one correspondance between Specification objects and entries in the
 *      spec file.  Strings that are parts of a Specification can include placeholders,
 *      which are "#1", "#2", "#3", and "#4".  We'll replace these by values before
 *      generating the files.
 * - Permutation: A concrete version of a specification, where all placeholders have
 *      been replaced by actual values.
 * - ParameterDefinition: A definition of a parameter of a concrete function.
 */

#include <math.h>
#include <stdio.h>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

namespace {

const char* AUTO_GENERATED_WARNING =
            "// Don't edit this file!  It is auto-generated by "
            "frameworks/rs/api/gen_runtime.\n\n";
const char* LEGAL_NOTICE =
            "/*\n"
            " * Copyright (C) 2014 The Android Open Source Project\n"
            " *\n"
            " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
            " * you may not use this file except in compliance with the License.\n"
            " * You may obtain a copy of the License at\n"
            " *\n"
            " *      http://www.apache.org/licenses/LICENSE-2.0\n"
            " *\n"
            " * Unless required by applicable law or agreed to in writing, software\n"
            " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
            " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
            " * See the License for the specific language governing permissions and\n"
            " * limitations under the License.\n"
            " */\n\n";

class Function;
class Specification;
class Permutation;
struct Type;

/* Information about a parameter to a function.  The values of all the fields should only be set by
 * parseParameterDefinition.
 */
struct ParameterDefinition {
    string rsType;        // The Renderscript type, e.g. "uint3"
    string rsBaseType;    // As above but without the number, e.g. "uint"
    string javaBaseType;  // The type we need to declare in Java, e.g. "unsigned int"
    string specType;      // The type found in the spec, e.g. "f16"
    bool isFloatType;     // True if it's a floating point value

    /* The number of entries in the vector.  It should be either "1", "2", "3", or "4".  It's also
     * "1" for scalars.
     */
    string mVectorSize;
    /* The space the vector takes in an array.  It's the same as the vector size, except for size
     * "3", where the width is "4".
     */
    string vectorWidth;

    string specName;       // e.g. x, as found in the spec file
    string variableName;   // e.g. inX, used both in .rs and .java
    string rsAllocName;    // e.g. gAllocInX
    string javaAllocName;  // e.g. inX
    string javaArrayName;  // e.g. arrayInX

    // If non empty, the mininum and maximum values to be used when generating the test data.
    string minValue;
    string maxValue;
    /* If non empty, contains the name of another parameter that should be smaller or equal to this
     * parameter, i.e.  value(smallerParameter) <= value(this).  This is used when testing clamp.
     */
    string smallerParameter;

    bool isOutParameter;       // True if this parameter returns data from the script.
    bool undefinedIfOutIsNan;  // If true, we don't validate if 'out' is NaN.

    int typeIndex;            // Index in the TYPES array.
    int compatibleTypeIndex;  // Index in TYPES for which the test data must also fit.

    /* Parse the parameter definition found in the spec file.  It will generate a name if none
     * are present in the file.  One of the two counts will be incremented, and potentially
     * used to generate unique names.  isReturn is true if we're processing the "return:"
     * definition.
     */
    void parseParameterDefinition(string s, bool isReturn, int* inputCount, int* outputCount);
};

// An entire spec file and the methods to process it.
class SpecFile {
public:
    explicit SpecFile(const string& specFileName) : mSpecFileName(specFileName) {}
    bool process(int versionOfTestFiles);

private:
    const string mSpecFileName;
    // The largest version number that we have found in all the specifications.
    int mLargestVersionNumber;

    map<string, Function*> mFunctionsMap;  // All the known functions.
    typedef map<string, Function*>::iterator FunctionsIterator;

    bool readSpecFile();
    Function* getFunction(const string& name);
    bool generateFiles(int versionOfTestFiles);
    bool writeAllFunctions(ofstream& headerFile, int versionOfTestFiles);
};

/* Represents a function, like "clamp".  Even though the spec file contains many entries for clamp,
 * we'll only have one clamp instance.
 */
class Function {
private:
    string mName;             // The lower case name, e.g. native_log
    string mCapitalizedName;  // The capitalized name, e.g. NativeLog
    string mTestName;         // e.g. TestNativeLog
    string mRelaxedTestName;  // e.g. TestNativeLogRelaxed

    vector<Specification*> mSpecifications;
    typedef vector<Specification*>::iterator SpecificationIterator;

    /* We keep track of the allocations generated in the .rs file and the argument classes defined
     * in the Java file, as we share these between the functions created for each specification.
     */
    set<string> mRsAllocationsGenerated;
    set<string> mJavaGeneratedArgumentClasses;

    string mJavaCallAllCheckMethods;  // Lines of Java code to invoke the check methods.

    ofstream mRsFile;    // The Renderscript test file we're generating.
    ofstream mJavaFile;  // The Jave test file we're generating.

    bool startRsFile();         // Open the mRsFile and writes its header.
    bool writeRelaxedRsFile();  // Write the entire relaxed rs test file (an include essentially)
    bool startJavaFile();       // Open the mJavaFile and writes the header.
    void finishJavaFile();      // Write the test method and closes the file.

public:
    explicit Function(const string& name);
    void addSpecification(Specification* spec) { mSpecifications.push_back(spec); }
    /* Write the .java and the two .rs test files.  versionOfTestFiles is used to restrict which API
     * to test.  Also writes the section of the header file.
     */
    bool writeFiles(ofstream& headerFile, int versionOfTestFiles);
    // Write an allocation and keep track of having it written, so it can be shared.
    void writeRsAllocationDefinition(const ParameterDefinition& param);
    // Write an argument class definiton and keep track of having it written, so it can be shared.
    void writeJavaArgumentClassDefinition(const string& className, const string& definition);
    // Add a call to mJavaCallAllCheckMethods to be used at the end of the file generation.
    void addJavaCheckCall(const string& call);
};

/* Defines one of the many variations of the function.  There's a one to one correspondance between
 * Specification objects and entries in the spec file.  Some of the strings that are parts of a
 * Specification can include placeholders, which are "#1", "#2", "#3", and "#4".  We'll replace
 * these by values before generating the files.
 */
class Specification {
private:
    /* The range of versions this specification applies to. 0 if there's no restriction, so an API
     * that became available at 9 and is still valid would have min:9 max:0.
     */
    int mMinVersion;
    int mMaxVersion;

    /* The name of the function without #n, e.g. convert.  As of this writing, it only differs for
     * convert.
     */
    string mCleanName;
    /* How to test.  One of:
     * "scalar": Generate test code that checks entries of each vector indepently.  E.g. for
     *           sin(float3), the test code will call the CoreMathVerfier.computeSin 3 times.
     * "vector": Generate test code that calls the CoreMathVerifier only once for each vector.
     *           This is useful for APIs like dot() or length().
     * "noverify": Generate test code that calls the API but don't verify the returned value.
     * "limited": Like "scalar" but tests a limited range of input values.
     * "custom": Like "scalar" but instead of calling CoreMathVerifier.computeXXX() to compute
     *           the expected value, we call instead CoreMathVerifier.verifyXXX().  This method
     *           returns a string that contains the error message, null if there's no error.
     */
    string mTest;
    string mPrecisionLimit;  // Maximum precision required when checking output of this function.

    vector<vector<string> > mReplaceables;

    // The following fields may contain placeholders that will be replaced using the mReplaceables.

    // The name of this function, can include #, e.g. convert_#1_#2
    string mName;

    string mReturn;           // The return type
    vector<string> mComment;  // The comments to be included in the header
    vector<string> mInline;   // The inline code to be included in the header
    vector<string> mParam;    // One entry per parameter defined

    // Substitute the placeholders in the strings by the corresponding entries in mReplaceables.
    string expandString(string s, int i1, int i2, int i3, int i4) const;
    void expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4,
                            vector<string>* out) const;

public:
    Specification() {
        mMinVersion = 0;
        mMaxVersion = 0;
    }
    int getMinVersion() const { return mMinVersion; }
    int getMaxVersion() const { return mMaxVersion; }

    string getName(int i1, int i2, int i3, int i4) const {
        return expandString(mName, i1, i2, i3, i4);
    }
    string getReturn(int i1, int i2, int i3, int i4) const {
        return expandString(mReturn, i1, i2, i3, i4);
    }
    void getComments(int i1, int i2, int i3, int i4, vector<string>* comments) const {
        return expandStringVector(mComment, i1, i2, i3, i4, comments);
    }
    void getInlines(int i1, int i2, int i3, int i4, vector<string>* inlines) const {
        return expandStringVector(mInline, i1, i2, i3, i4, inlines);
    }
    void getParams(int i1, int i2, int i3, int i4, vector<string>* params) const {
        return expandStringVector(mParam, i1, i2, i3, i4, params);
    }
    string getTest() const { return mTest; }
    string getPrecisionLimit() const { return mPrecisionLimit; }
    string getCleanName() const { return mCleanName; }

    void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile, Function* function,
                    int versionOfTestFiles);
    bool writeRelaxedRsFile() const;
    // Return true if this specification should be generated for this version.
    bool relevantForVersion(int versionOfTestFiles) const;

    static Specification* scanSpecification(FILE* in);
};

// A concrete version of a specification, where all placeholders have been replaced by actual
// values.
class Permutation {
private:
    Function* mFunction;
    Specification* mSpecification;

    // These are the expanded version of those found on Specification
    string mName;
    string mCleanName;
    string mTest;  // How to test.  One of "scalar", "vector", "noverify", "limited", and "none".
    string mPrecisionLimit;  // Maximum precision required when checking output of this function.
    vector<string> mInline;
    vector<string> mComment;

    // The inputs and outputs of the function.  This include the return type, if present.
    vector<ParameterDefinition*> mParams;
    // The index of the return value in mParams, -1 if the function is void.
    int mReturnIndex;
    // The index of the first input value in mParams, -1 if there's no input.
    int mFirstInputIndex;
    // The number of input and output parameters.
    int mInputCount;
    int mOutputCount;

    string mRsKernelName;
    string mJavaArgumentsClassName;
    string mJavaArgumentsNClassName;
    string mJavaVerifierComputeMethodName;
    string mJavaVerifierVerifyMethodName;
    string mJavaCheckMethodName;
    string mJavaVerifyMethodName;

    void writeHeaderSection(ofstream& file) const;

    void writeRsSection(ofstream& rs) const;

    void writeJavaSection(ofstream& file) const;
    void writeJavaArgumentClass(ofstream& file, bool scalar) const;
    void writeJavaCheckMethod(ofstream& file, bool generateCallToVerifier) const;
    void writeJavaVerifyScalarMethod(ofstream& file, bool verifierValidates) const;
    void writeJavaVerifyVectorMethod(ofstream& file) const;
    void writeJavaVerifyFunctionHeader(ofstream& file) const;
    void writeJavaInputAllocationDefinition(ofstream& file, const string& indent,
                                            const ParameterDefinition& param) const;
    void writeJavaOutputAllocationDefinition(ofstream& file, const string& indent,
                                             const ParameterDefinition& param) const;
    // Write code to create a random allocation for which the data must be compatible for two types.
    void writeJavaRandomCompatibleFloatAllocation(ofstream& file, const string& dataType,
                                                  const string& seed, char vectorSize,
                                                  const Type& compatibleType,
                                                  const Type& generatedType) const;
    void writeJavaRandomCompatibleIntegerAllocation(ofstream& file, const string& dataType,
                                                    const string& seed, char vectorSize,
                                                    const Type& compatibleType,
                                                    const Type& generatedType) const;
    void writeJavaCallToRs(ofstream& file, bool relaxed, bool generateCallToVerifier) const;

    void writeJavaTestAndSetValid(ofstream& file, int indent, const ParameterDefinition& p,
                                  const string& argsIndex, const string& actualIndex) const;
    void writeJavaTestOneValue(ofstream& file, int indent, const ParameterDefinition& p,
                               const string& argsIndex, const string& actualIndex) const;
    void writeJavaAppendOutputToMessage(ofstream& file, int indent, const ParameterDefinition& p,
                                        const string& argsIndex, const string& actualIndex,
                                        bool verifierValidates) const;
    void writeJavaAppendInputToMessage(ofstream& file, int indent, const ParameterDefinition& p,
                                       const string& actual) const;
    void writeJavaAppendNewLineToMessage(ofstream& file, int indent) const;
    void writeJavaAppendVariableToMessage(ofstream& file, int indent, const ParameterDefinition& p,
                                          const string& value) const;
    void writeJavaAppendFloatyVariableToMessage(ofstream& file, int indent,
                                                const string& value) const;
    void writeJavaVectorComparison(ofstream& file, int indent, const ParameterDefinition& p) const;
    void writeJavaAppendVectorInputToMessage(ofstream& file, int indent,
                                             const ParameterDefinition& p) const;
    void writeJavaAppendVectorOutputToMessage(ofstream& file, int indent,
                                              const ParameterDefinition& p) const;
    bool passByAddressToSet(const string& name) const;
    void convertToRsType(const string& name, string* dataType, char* vectorSize) const;

public:
    Permutation(Function* function, Specification* specification, int i1, int i2, int i3, int i4);
    void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile,
                    int versionOfTestFiles);
};

// Table of type equivalences
// TODO: We should just be pulling this from a shared header. Slang does exactly the same thing.

enum NumberKind { SIGNED_INTEGER, UNSIGNED_INTEGER, FLOATING_POINT };

struct Type {
    const char* specType;  // Name found in the .spec file
    string rsDataType;     // RS data type
    string cType;          // Type in a C file
    const char* javaType;  // Type in a Java file
    NumberKind kind;
    /* For integers, number of bits of the number, excluding the sign bit.
     * For floats, number of implied bits of the mantissa.
     */
    int significantBits;
    // For floats, number of bits of the exponent.  0 for integer types.
    int exponentBits;
};

const Type TYPES[] = {{"f16", "FLOAT_16", "half", "half", FLOATING_POINT, 11, 5},
                      {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 24, 8},
                      {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 53, 11},
                      {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7, 0},
                      {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8, 0},
                      {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15, 0},
                      {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16, 0},
                      {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31, 0},
                      {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32, 0},
                      {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63, 0},
                      {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64, 0}};

const int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]);

// Returns the index in TYPES for the provided cType
int FindCType(const string& cType) {
    for (int i = 0; i < NUM_TYPES; i++) {
        if (cType == TYPES[i].cType) {
            return i;
        }
    }
    return -1;
}

// Capitalizes and removes underscores.  E.g. converts "native_log" to NativeLog.
string capitalize(const string& source) {
    int length = source.length();
    string result;
    bool capitalize = true;
    for (int s = 0; s < length; s++) {
        if (source[s] == '_') {
            capitalize = true;
        } else if (capitalize) {
            result += toupper(source[s]);
            capitalize = false;
        } else {
            result += source[s];
        }
    }
    return result;
}

string tab(int n) { return string(n * 4, ' '); }

// Returns a string that's an hexadecimal constant fo the hash of the string.
string hashString(const string& s) {
    long hash = 0;
    for (size_t i = 0; i < s.length(); i++) {
        hash = hash * 43 + s[i];
    }
    stringstream stream;
    stream << "0x" << std::hex << hash << "l";
    return stream.str();
}

// Removes the character from present. Returns true if the string contained the character.
static bool charRemoved(char c, string* s) {
    size_t p = s->find(c);
    if (p != string::npos) {
        s->erase(p, 1);
        return true;
    }
    return false;
}

// Return true if the string is already in the set.  Inserts it if not.
bool testAndSet(const string& flag, set<string>* set) {
    if (set->find(flag) == set->end()) {
        set->insert(flag);
        return false;
    }
    return true;
}

// Convert an int into a string.
string toString(int n) {
    char buf[100];
    snprintf(buf, sizeof(buf), "%d", n);
    return string(buf);
}

void trim(string* s, size_t start) {
    if (start > 0) {
        s->erase(0, start);
    }

    while (s->size() && (s->at(0) == ' ')) {
        s->erase(0, 1);
    }

    size_t p = s->find_first_of("\n\r");
    if (p != string::npos) {
        s->erase(p);
    }

    while ((s->size() > 0) && (s->at(s->size() - 1) == ' ')) {
        s->erase(s->size() - 1);
    }
}

string stringReplace(string s, string match, string rep) {
    while (1) {
        size_t p = s.find(match);
        if (p == string::npos) break;

        s.erase(p, match.size());
        s.insert(p, rep);
    }
    return s;
}

// Return the next line from the input file.
bool getNextLine(FILE* in, string* s) {
    s->clear();
    while (1) {
        int c = fgetc(in);
        if (c == EOF) return s->size() != 0;
        if (c == '\n') break;
        s->push_back((char)c);
    }
    return true;
}

void writeIfdef(ofstream& file, string filename, bool isStart) {
    string t = "__";
    t += filename;
    t += "__";

    for (size_t i = 2; i < t.size(); i++) {
        if (t[i] == '.') {
            t[i] = '_';
        }
    }

    if (isStart) {
        file << "#ifndef " << t << "\n";
        file << "#define " << t << "\n";
    } else {
        file << "#endif // " << t << "\n";
    }
}

void writeJavaArrayInitialization(ofstream& file, const ParameterDefinition& p) {
    file << tab(2) << p.javaBaseType << "[] " << p.javaArrayName << " = new " << p.javaBaseType
         << "[INPUTSIZE * " << p.vectorWidth << "];\n";
    file << tab(2) << p.javaAllocName << ".copyTo(" << p.javaArrayName << ");\n";
}

bool parseCommandLine(int argc, char* argv[], int* versionOfTestFiles,
                      vector<string>* specFileNames) {
    for (int i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            if (argv[i][1] == 'v') {
                i++;
                if (i < argc) {
                    char* end;
                    *versionOfTestFiles = strtol(argv[i], &end, 10);
                    if (*end != '\0') {
                        printf("Can't parse the version number %s\n", argv[i]);
                        return false;
                    }
                } else {
                    printf("Missing version number after -v\n");
                    return false;
                }
            } else {
                printf("Unrecognized flag %s\n", argv[i]);
                return false;
            }
        } else {
            specFileNames->push_back(argv[i]);
        }
    }
    if (specFileNames->size() == 0) {
        printf("No spec file specified\n");
        return false;
    }
    return true;
}

/* Returns a double that should be able to be converted to an integer of size
 * numberOfIntegerBits.
 */
static double MaxDoubleForInteger(int numberOfIntegerBits, int mantissaSize) {
    /* Double has only 52 bits of precision (53 implied). So for longs, we want
     * to create smaller values to avoid a round up.  Same for floats and halfs.
     */
    int lowZeroBits = max(0, numberOfIntegerBits - mantissaSize);
    unsigned long l = (0xffffffffffffffff >> (64 - numberOfIntegerBits + lowZeroBits))
                      << lowZeroBits;
    return (double)l;
}

/* Parse a parameter definition.  It's of the form "type [*][name]".  The type
 * is required.  The name is optional.  The * indicates it's an output
 * parameter.  We also pass the indexed of this parameter in the definition, so
 * we can create names like in2, in3, etc. */
void ParameterDefinition::parseParameterDefinition(string s, bool isReturn, int* inputCount,
                                                   int* outputCount) {
    istringstream stream(s);
    string name, type, option;
    stream >> rsType;
    stream >> specName;
    stream >> option;

    // Determine if this is an output.
    isOutParameter = charRemoved('*', &rsType) || charRemoved('*', &specName) || isReturn;

    // Extract the vector size out of the type.
    int last = rsType.size() - 1;
    char lastChar = rsType[last];
    if (lastChar >= '0' && lastChar <= '9') {
        rsBaseType = rsType.substr(0, last);
        mVectorSize = lastChar;
    } else {
        rsBaseType = rsType;
        mVectorSize = "1";
    }
    if (mVectorSize == "3") {
        vectorWidth = "4";
    } else {
        vectorWidth = mVectorSize;
    }

    /* Create variable names to be used in the java and .rs files.  Because x and
     * y are reserved in .rs files, we prefix variable names with "in" or "out".
     */
    if (isOutParameter) {
        variableName = "out";
        if (!specName.empty()) {
            variableName += capitalize(specName);
        } else if (!isReturn) {
            variableName += toString(*outputCount);
        }
        (*outputCount)++;
    } else {
        variableName = "in";
        if (!specName.empty()) {
            variableName += capitalize(specName);
        } else if (*inputCount > 0) {
            variableName += toString(*inputCount);
        }
        (*inputCount)++;
    }
    rsAllocName = "gAlloc" + capitalize(variableName);
    javaAllocName = variableName;
    javaArrayName = "array" + capitalize(javaAllocName);

    // Process the option.
    undefinedIfOutIsNan = false;
    compatibleTypeIndex = -1;
    if (!option.empty()) {
        if (option.compare(0, 6, "range(") == 0) {
            size_t pComma = option.find(',');
            size_t pParen = option.find(')');
            if (pComma == string::npos || pParen == string::npos) {
                printf("Incorrect range %s\n", option.c_str());
            } else {
                minValue = option.substr(6, pComma - 6);
                maxValue = option.substr(pComma + 1, pParen - pComma - 1);
            }
        } else if (option.compare(0, 6, "above(") == 0) {
            size_t pParen = option.find(')');
            if (pParen == string::npos) {
                printf("Incorrect option %s\n", option.c_str());
            } else {
                smallerParameter = option.substr(6, pParen - 6);
            }
        } else if (option.compare(0, 11, "compatible(") == 0) {
            size_t pParen = option.find(')');
            if (pParen == string::npos) {
                printf("Incorrect option %s\n", option.c_str());
            } else {
                compatibleTypeIndex = FindCType(option.substr(11, pParen - 11));
            }
        } else if (option.compare(0, 11, "conditional") == 0) {
            undefinedIfOutIsNan = true;
        } else {
            printf("Unrecognized option %s\n", option.c_str());
        }
    }

    typeIndex = FindCType(rsBaseType);
    isFloatType = false;
    if (typeIndex < 0) {
        // TODO set a global flag when we encounter an error & abort
        printf("Error, could not find %s\n", rsBaseType.c_str());
    } else {
        javaBaseType = TYPES[typeIndex].javaType;
        specType = TYPES[typeIndex].specType;
        isFloatType = TYPES[typeIndex].exponentBits > 0;
    }
}

bool SpecFile::process(int versionOfTestFiles) {
    if (!readSpecFile()) {
        return false;
    }
    if (versionOfTestFiles == 0) {
        versionOfTestFiles = mLargestVersionNumber;
    }
    if (!generateFiles(versionOfTestFiles)) {
        return false;
    }
    printf("%s: %ld functions processed.\n", mSpecFileName.c_str(), mFunctionsMap.size());
    return true;
}

// Read the specification, adding the definitions to the global functions map.
bool SpecFile::readSpecFile() {
    FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
    if (!specFile) {
        printf("Error opening input file: %s\n", mSpecFileName.c_str());
        return false;
    }

    mLargestVersionNumber = 0;
    while (1) {
        Specification* spec = Specification::scanSpecification(specFile);
        if (spec == nullptr) {
            break;
        }
        getFunction(spec->getCleanName())->addSpecification(spec);
        int specMin = spec->getMinVersion();
        int specMax = spec->getMaxVersion();
        if (specMin && specMin > mLargestVersionNumber) {
            mLargestVersionNumber = specMin;
        }
        if (specMax && specMax > mLargestVersionNumber) {
            mLargestVersionNumber = specMax;
        }
    }

    fclose(specFile);
    return true;
}

bool SpecFile::generateFiles(int versionOfTestFiles) {
    printf("%s: Generating test files for version %d\n", mSpecFileName.c_str(), versionOfTestFiles);

    // The header file name should have the same base but with a ".rsh" extension.
    string headerFileName = mSpecFileName;
    size_t l = headerFileName.length();
    const char SPEC[] = ".spec";
    const int SPEC_SIZE = sizeof(SPEC) - 1;
    const int start = l - SPEC_SIZE;
    if (start >= 0 && headerFileName.compare(start, SPEC_SIZE, SPEC) == 0) {
        headerFileName.erase(start);
    }
    headerFileName += ".rsh";

    // Write the start of the header file.
    ofstream headerFile;
    headerFile.open(headerFileName.c_str(), ios::out | ios::trunc);
    if (!headerFile.is_open()) {
        printf("Error opening output file: %s\n", headerFileName.c_str());
        return false;
    }
    headerFile << LEGAL_NOTICE;
    headerFile << AUTO_GENERATED_WARNING;
    writeIfdef(headerFile, headerFileName, true);

    // Write the functions to the header and test files.
    bool success = writeAllFunctions(headerFile, versionOfTestFiles);

    // Finish the header file.
    writeIfdef(headerFile, headerFileName, false);
    headerFile.close();

    return success;
}

// Return the named function from the map.  Creates it if it's not there.
Function* SpecFile::getFunction(const string& name) {
    FunctionsIterator iter = mFunctionsMap.find(name);
    if (iter != mFunctionsMap.end()) {
        return iter->second;
    }
    Function* f = new Function(name);
    mFunctionsMap[name] = f;
    return f;
}

bool SpecFile::writeAllFunctions(ofstream& headerFile, int versionOfTestFiles) {
    bool success = true;
    for (FunctionsIterator iter = mFunctionsMap.begin(); iter != mFunctionsMap.end(); iter++) {
        Function* func = iter->second;
        if (!func->writeFiles(headerFile, versionOfTestFiles)) {
            success = false;
        }
    }
    return success;
}

Function::Function(const string& name) {
    mName = name;
    mCapitalizedName = capitalize(mName);
    mTestName = "Test" + mCapitalizedName;
    mRelaxedTestName = mTestName + "Relaxed";
}

bool Function::writeFiles(ofstream& headerFile, int versionOfTestFiles) {
    if (!startRsFile() || !startJavaFile() || !writeRelaxedRsFile()) {
        return false;
    }

    for (SpecificationIterator i = mSpecifications.begin(); i < mSpecifications.end(); i++) {
        (*i)->writeFiles(headerFile, mRsFile, mJavaFile, this, versionOfTestFiles);
    }

    finishJavaFile();
    // There's no work to wrap-up in the .rs file.

    mRsFile.close();
    mJavaFile.close();
    return true;
}

bool Function::startRsFile() {
    string fileName = mTestName + ".rs";
    mRsFile.open(fileName.c_str(), ios::out | ios::trunc);
    if (!mRsFile.is_open()) {
        printf("Error opening file: %s\n", fileName.c_str());
        return false;
    }
    mRsFile << LEGAL_NOTICE;
    mRsFile << "#pragma version(1)\n";
    mRsFile << "#pragma rs java_package_name(android.renderscript.cts)\n\n";
    mRsFile << AUTO_GENERATED_WARNING;
    return true;
}

// Write an allocation definition if not already emitted in the .rs file.
void Function::writeRsAllocationDefinition(const ParameterDefinition& param) {
    if (!testAndSet(param.rsAllocName, &mRsAllocationsGenerated)) {
        mRsFile << "rs_allocation " << param.rsAllocName << ";\n";
    }
}

// Write the entire *Relaxed.rs test file, as it only depends on the name.
bool Function::writeRelaxedRsFile() {
    string name = mRelaxedTestName + ".rs";
    FILE* file = fopen(name.c_str(), "wt");
    if (!file) {
        printf("Error opening file: %s\n", name.c_str());
        return false;
    }
    fputs(LEGAL_NOTICE, file);
    string s;
    s += "#include \"" + mTestName + ".rs\"\n";
    s += "#pragma rs_fp_relaxed\n";
    s += AUTO_GENERATED_WARNING;
    fputs(s.c_str(), file);
    fclose(file);
    return true;
}

bool Function::startJavaFile() {
    string fileName = mTestName + ".java";
    mJavaFile.open(fileName.c_str(), ios::out | ios::trunc);
    if (!mJavaFile.is_open()) {
        printf("Error opening file: %s\n", fileName.c_str());
        return false;
    }
    mJavaFile << LEGAL_NOTICE;
    mJavaFile << AUTO_GENERATED_WARNING;
    mJavaFile << "package android.renderscript.cts;\n\n";

    mJavaFile << "import android.renderscript.Allocation;\n";
    mJavaFile << "import android.renderscript.RSRuntimeException;\n";
    mJavaFile << "import android.renderscript.Element;\n\n";

    mJavaFile << "public class " << mTestName << " extends RSBaseCompute {\n\n";

    mJavaFile << tab(1) << "private ScriptC_" << mTestName << " script;\n";
    mJavaFile << tab(1) << "private ScriptC_" << mRelaxedTestName << " scriptRelaxed;\n\n";

    mJavaFile << tab(1) << "@Override\n";
    mJavaFile << tab(1) << "protected void setUp() throws Exception {\n";
    mJavaFile << tab(2) << "super.setUp();\n";
    mJavaFile << tab(2) << "script = new ScriptC_" << mTestName << "(mRS);\n";
    mJavaFile << tab(2) << "scriptRelaxed = new ScriptC_" << mRelaxedTestName << "(mRS);\n";
    mJavaFile << tab(1) << "}\n\n";
    return true;
}

void Function::writeJavaArgumentClassDefinition(const string& className, const string& definition) {
    if (!testAndSet(className, &mJavaGeneratedArgumentClasses)) {
        mJavaFile << definition;
    }
}

void Function::addJavaCheckCall(const string& call) {
    mJavaCallAllCheckMethods += tab(2) + call + "\n";
}

void Function::finishJavaFile() {
    mJavaFile << tab(1) << "public void test" << mCapitalizedName << "() {\n";
    mJavaFile << mJavaCallAllCheckMethods;
    mJavaFile << tab(1) << "}\n";
    mJavaFile << "}\n";
}

void Specification::expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4,
                                       vector<string>* out) const {
    out->clear();
    for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
        out->push_back(expandString(*iter, i1, i2, i3, i4));
    }
}

Specification* Specification::scanSpecification(FILE* in) {
    Specification* spec = new Specification();
    spec->mTest = "scalar";  // default
    bool modeComment = false;
    bool modeInline = false;
    bool success = true;

    while (1) {
        string s;
        bool ret = getNextLine(in, &s);
        if (!ret) break;

        if (modeComment) {
            if (!s.size() || (s[0] == ' ')) {
                trim(&s, 0);
                spec->mComment.push_back(s);
                continue;
            } else {
                modeComment = false;
            }
        }

        if (modeInline) {
            if (!s.size() || (s[0] == ' ')) {
                trim(&s, 0);
                spec->mInline.push_back(s);
                continue;
            } else {
                modeInline = false;
            }
        }

        if (s[0] == '#') {
            continue;
        }

        if (s.compare(0, 5, "name:") == 0) {
            trim(&s, 5);
            spec->mName = s;
            // Some functions like convert have # part of the name.  Truncate at that point.
            size_t p = s.find('#');
            if (p != string::npos) {
                if (p > 0 && s[p - 1] == '_') {
                    p--;
                }
                s.erase(p);
            }
            spec->mCleanName = s;
            continue;
        }

        if (s.compare(0, 4, "arg:") == 0) {
            trim(&s, 4);
            spec->mParam.push_back(s);
            continue;
        }

        if (s.compare(0, 4, "ret:") == 0) {
            trim(&s, 4);
            spec->mReturn = s;
            continue;
        }

        if (s.compare(0, 5, "test:") == 0) {
            trim(&s, 5);
            if (s == "scalar" || s == "vector" || s == "noverify" || s == "custom" || s == "none") {
                spec->mTest = s;
            } else if (s.compare(0, 7, "limited") == 0) {
                spec->mTest = "limited";
                if (s.compare(7, 1, "(") == 0) {
                    size_t pParen = s.find(')');
                    if (pParen == string::npos) {
                        printf("Incorrect test %s\n", s.c_str());
                    } else {
                        spec->mPrecisionLimit = s.substr(8, pParen - 8);
                    }
                }
            } else {
                printf("Error: Unrecognized test option: %s\n", s.c_str());
                success = false;
            }
            continue;
        }

        if (s.compare(0, 4, "end:") == 0) {
            if (success) {
                return spec;
            } else {
                delete spec;
                return nullptr;
            }
        }

        if (s.compare(0, 8, "comment:") == 0) {
            modeComment = true;
            continue;
        }

        if (s.compare(0, 7, "inline:") == 0) {
            modeInline = true;
            continue;
        }

        if (s.compare(0, 8, "version:") == 0) {
            trim(&s, 8);
            sscanf(s.c_str(), "%i %i", &spec->mMinVersion, &spec->mMaxVersion);
            continue;
        }

        if (s.compare(0, 8, "start:") == 0) {
            continue;
        }

        if (s.compare(0, 2, "w:") == 0) {
            vector<string> t;
            if (s.find("1") != string::npos) {
                t.push_back("");
            }
            if (s.find("2") != string::npos) {
                t.push_back("2");
            }
            if (s.find("3") != string::npos) {
                t.push_back("3");
            }
            if (s.find("4") != string::npos) {
                t.push_back("4");
            }
            spec->mReplaceables.push_back(t);
            continue;
        }

        if (s.compare(0, 2, "t:") == 0) {
            vector<string> t;
            for (int i = 0; i < NUM_TYPES; i++) {
                if (s.find(TYPES[i].specType) != string::npos) {
                    t.push_back(TYPES[i].cType);
                }
            }
            spec->mReplaceables.push_back(t);
            continue;
        }

        if (s.size() == 0) {
            // eat empty line
            continue;
        }

        printf("Error, line:\n");
        printf("  %s\n", s.c_str());
    }

    delete spec;
    return nullptr;
}

void Specification::writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile,
                               Function* function, int versionOfTestFiles) {
    int start[4];
    int end[4];
    for (int i = 0; i < 4; i++) {
        if (i < (int)mReplaceables.size()) {
            start[i] = 0;
            end[i] = mReplaceables[i].size();
        } else {
            start[i] = -1;
            end[i] = 0;
        }
    }
    for (int i4 = start[3]; i4 < end[3]; i4++) {
        for (int i3 = start[2]; i3 < end[2]; i3++) {
            for (int i2 = start[1]; i2 < end[1]; i2++) {
                for (int i1 = start[0]; i1 < end[0]; i1++) {
                    Permutation p(function, this, i1, i2, i3, i4);
                    p.writeFiles(headerFile, rsFile, javaFile, versionOfTestFiles);
                }
            }
        }
    }
}

bool Specification::relevantForVersion(int versionOfTestFiles) const {
    if (mMinVersion != 0 && mMinVersion > versionOfTestFiles) {
        return false;
    }
    if (mMaxVersion != 0 && mMaxVersion < versionOfTestFiles) {
        return false;
    }
    return true;
}

string Specification::expandString(string s, int i1, int i2, int i3, int i4) const {
    if (mReplaceables.size() > 0) {
        s = stringReplace(s, "#1", mReplaceables[0][i1]);
    }
    if (mReplaceables.size() > 1) {
        s = stringReplace(s, "#2", mReplaceables[1][i2]);
    }
    if (mReplaceables.size() > 2) {
        s = stringReplace(s, "#3", mReplaceables[2][i3]);
    }
    if (mReplaceables.size() > 3) {
        s = stringReplace(s, "#4", mReplaceables[3][i4]);
    }
    return s;
}

Permutation::Permutation(Function* func, Specification* spec, int i1, int i2, int i3, int i4)
    : mFunction(func),
      mSpecification(spec),
      mReturnIndex(-1),
      mFirstInputIndex(-1),
      mInputCount(0),
      mOutputCount(0) {
    // We expand the strings now to make capitalization easier.  The previous code preserved the #n
    // markers just before emitting, which made capitalization difficult.
    mName = spec->getName(i1, i2, i3, i4);
    mCleanName = spec->getCleanName();
    mTest = spec->getTest();
    mPrecisionLimit = spec->getPrecisionLimit();
    spec->getInlines(i1, i2, i3, i4, &mInline);
    spec->getComments(i1, i2, i3, i4, &mComment);

    vector<string> paramDefinitions;
    spec->getParams(i1, i2, i3, i4, &paramDefinitions);
    for (size_t i = 0; i < paramDefinitions.size(); i++) {
        ParameterDefinition* def = new ParameterDefinition();
        def->parseParameterDefinition(paramDefinitions[i], false, &mInputCount, &mOutputCount);
        if (!def->isOutParameter && mFirstInputIndex < 0) {
            mFirstInputIndex = mParams.size();
        }
        mParams.push_back(def);
    }

    const string s = spec->getReturn(i1, i2, i3, i4);
    if (!s.empty() && s != "void") {
        ParameterDefinition* def = new ParameterDefinition();
        // Adding "*" tells the parse method it's an output.
        def->parseParameterDefinition(s, true, &mInputCount, &mOutputCount);
        mReturnIndex = mParams.size();
        mParams.push_back(def);
    }

    mRsKernelName = "test" + capitalize(mName);
    mJavaArgumentsClassName = "Arguments";
    mJavaArgumentsNClassName = "Arguments";
    mJavaCheckMethodName = "check" + capitalize(mCleanName);
    mJavaVerifyMethodName = "verifyResults" + capitalize(mCleanName);
    for (int i = 0; i < (int)mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        mRsKernelName += capitalize(p.rsType);
        mJavaArgumentsClassName += capitalize(p.rsBaseType);
        mJavaArgumentsNClassName += capitalize(p.rsBaseType);
        if (p.mVectorSize != "1") {
            mJavaArgumentsNClassName += "N";
        }
        mJavaCheckMethodName += capitalize(p.rsType);
        mJavaVerifyMethodName += capitalize(p.rsType);
    }
    mJavaVerifierComputeMethodName = "compute" + capitalize(mCleanName);
    mJavaVerifierVerifyMethodName = "verify" + capitalize(mCleanName);
}

void Permutation::writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile,
                             int versionOfTestFiles) {
    writeHeaderSection(headerFile);
    if (mSpecification->relevantForVersion(versionOfTestFiles) && mTest != "none") {
        writeRsSection(rsFile);
        writeJavaSection(javaFile);
    }
}

void Permutation::writeHeaderSection(ofstream& file) const {
    int minVersion = mSpecification->getMinVersion();
    int maxVersion = mSpecification->getMaxVersion();
    bool hasVersion = minVersion || maxVersion;

    if (hasVersion) {
        if (maxVersion) {
            file << "#if (defined(RS_VERSION) && (RS_VERSION >= " << minVersion
                 << ") && (RS_VERSION <= " << maxVersion << "))\n";
        } else {
            file << "#if (defined(RS_VERSION) && (RS_VERSION >= " << minVersion << "))\n";
        }
    }

    file << "/*\n";
    for (size_t ct = 0; ct < mComment.size(); ct++) {
        if (!mComment[ct].empty()) {
            file << " * " << mComment[ct] << "\n";
        } else {
            file << " *\n";
        }
    }
    file << " *\n";
    if (minVersion || maxVersion) {
        if (maxVersion) {
            file << " * Suppored by API versions " << minVersion << " - " << maxVersion << "\n";
        } else {
            file << " * Supported by API versions " << minVersion << " and newer.\n";
        }
    }
    file << " */\n";
    if (mInline.size() > 0) {
        file << "static ";
    } else {
        file << "extern ";
    }
    if (mReturnIndex >= 0) {
        file << mParams[mReturnIndex]->rsType;
    } else {
        file << "void";
    }
    file << " __attribute__((";
    if (mOutputCount <= 1) {
        file << "const, ";
    }
    file << "overloadable))";
    file << mName;
    file << "(";
    bool needComma = false;
    for (int i = 0; i < (int)mParams.size(); i++) {
        if (i != mReturnIndex) {
            const ParameterDefinition& p = *mParams[i];
            if (needComma) {
                file << ", ";
            }
            file << p.rsType;
            if (p.isOutParameter) {
                file << "*";
            }
            if (!p.specName.empty()) {
                file << " " << p.specName;
            }
            needComma = true;
        }
    }
    if (mInline.size() > 0) {
        file << ") {\n";
        for (size_t ct = 0; ct < mInline.size(); ct++) {
            file << " " << mInline[ct].c_str() << "\n";
        }
        file << "}\n";
    } else {
        file << ");\n";
    }
    if (hasVersion) {
        file << "#endif\n";
    }
    file << "\n";
}

/* Write the section of the .rs file for this permutation.
 *
 * We communicate the extra input and output parameters via global allocations.
 * For example, if we have a function that takes three arguments, two for input
 * and one for output:
 *
 * start:
 * name: gamn
 * ret: float3
 * arg: float3 a
 * arg: int b
 * arg: float3 *c
 * end:
 *
 * We'll produce:
 *
 * rs_allocation gAllocInB;
 * rs_allocation gAllocOutC;
 *
 * float3 __attribute__((kernel)) test_gamn_float3_int_float3(float3 inA, unsigned int x) {
 *    int inB;
 *    float3 outC;
 *    float2 out;
 *    inB = rsGetElementAt_int(gAllocInB, x);
 *    out = gamn(a, in_b, &outC);
 *    rsSetElementAt_float4(gAllocOutC, &outC, x);
 *    return out;
 * }
 *
 * We avoid re-using x and y from the definition because these have reserved
 * meanings in a .rs file.
 */
void Permutation::writeRsSection(ofstream& rs) const {
    // Write the allocation declarations we'll need.
    for (int i = 0; i < (int)mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        // Don't need allocation for one input and one return value.
        if (i != mReturnIndex && i != mFirstInputIndex) {
            mFunction->writeRsAllocationDefinition(p);
        }
    }
    rs << "\n";

    // Write the function header.
    if (mReturnIndex >= 0) {
        rs << mParams[mReturnIndex]->rsType;
    } else {
        rs << "void";
    }
    rs << " __attribute__((kernel)) " << mRsKernelName;
    rs << "(";
    bool needComma = false;
    if (mFirstInputIndex >= 0) {
        rs << mParams[mFirstInputIndex]->rsType << " " << mParams[mFirstInputIndex]->variableName;
        needComma = true;
    }
    if (mOutputCount > 1 || mInputCount > 1) {
        if (needComma) {
            rs << ", ";
        }
        rs << "unsigned int x";
    }
    rs << ") {\n";

    // Write the local variable declarations and initializations.
    for (int i = 0; i < (int)mParams.size(); i++) {
        if (i == mFirstInputIndex || i == mReturnIndex) {
            continue;
        }
        const ParameterDefinition& p = *mParams[i];
        rs << tab(1) << p.rsType << " " << p.variableName;
        if (p.isOutParameter) {
            rs << " = 0;\n";
        } else {
            rs << " = rsGetElementAt_" << p.rsType << "(" << p.rsAllocName << ", x);\n";
        }
    }

    // Write the function call.
    if (mReturnIndex >= 0) {
        if (mOutputCount > 1) {
            rs << tab(1) << mParams[mReturnIndex]->rsType << " "
               << mParams[mReturnIndex]->variableName << " = ";
        } else {
            rs << tab(1) << "return ";
        }
    }
    rs << mName << "(";
    needComma = false;
    for (int i = 0; i < (int)mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (i == mReturnIndex) {
            continue;
        }
        if (needComma) {
            rs << ", ";
        }
        if (p.isOutParameter) {
            rs << "&";
        }
        rs << p.variableName;
        needComma = true;
    }
    rs << ");\n";

    if (mOutputCount > 1) {
        // Write setting the extra out parameters into the allocations.
        for (int i = 0; i < (int)mParams.size(); i++) {
            const ParameterDefinition& p = *mParams[i];
            if (p.isOutParameter && i != mReturnIndex) {
                rs << tab(1) << "rsSetElementAt_" << p.rsType << "(" << p.rsAllocName << ", ";
                if (passByAddressToSet(p.variableName)) {
                    rs << "&";
                }
                rs << p.variableName << ", x);\n";
            }
        }
        if (mReturnIndex >= 0) {
            rs << tab(1) << "return " << mParams[mReturnIndex]->variableName << ";\n";
        }
    }
    rs << "}\n";
}

bool Permutation::passByAddressToSet(const string& name) const {
    string s = name;
    int last = s.size() - 1;
    char lastChar = s[last];
    return lastChar >= '0' && lastChar <= '9';
}

void Permutation::writeJavaSection(ofstream& file) const {
    // By default, we test the results using item by item comparison.
    if (mTest == "scalar" || mTest == "limited") {
        writeJavaArgumentClass(file, true);
        writeJavaCheckMethod(file, true);
        writeJavaVerifyScalarMethod(file, false);
    } else if (mTest == "custom") {
        writeJavaArgumentClass(file, true);
        writeJavaCheckMethod(file, true);
        writeJavaVerifyScalarMethod(file, true);
    } else if (mTest == "vector") {
        writeJavaArgumentClass(file, false);
        writeJavaCheckMethod(file, true);
        writeJavaVerifyVectorMethod(file);
    } else if (mTest == "noverify") {
        writeJavaCheckMethod(file, false);
    }

    // Register the check method to be called.  This code will be written at the end.
    mFunction->addJavaCheckCall(mJavaCheckMethodName + "();");
}

void Permutation::writeJavaArgumentClass(ofstream& file, bool scalar) const {
    string name;
    if (scalar) {
        name = mJavaArgumentsClassName;
    } else {
        name = mJavaArgumentsNClassName;
    }
    string s;
    s += tab(1) + "public class " + name + " {\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        s += tab(2) + "public ";
        if (p.isOutParameter && p.isFloatType) {
            s += "Floaty";
        } else {
            s += p.javaBaseType;
        }
        if (!scalar && p.mVectorSize != "1") {
            s += "[]";
        }
        s += " " + p.variableName + ";\n";
    }
    s += tab(1) + "}\n\n";

    mFunction->writeJavaArgumentClassDefinition(name, s);
}

void Permutation::writeJavaCheckMethod(ofstream& file, bool generateCallToVerifier) const {
    file << tab(1) << "private void " << mJavaCheckMethodName << "() {\n";
    // Generate the input allocations and initialization.
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (!p.isOutParameter) {
            writeJavaInputAllocationDefinition(file, tab(2), p);
        }
    }
    // Enforce ordering if needed.
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (!p.isOutParameter && !p.smallerParameter.empty()) {
            string smallerAlloc = "in" + capitalize(p.smallerParameter);
            file << tab(2) << "enforceOrdering(" << smallerAlloc << ", " << p.javaAllocName
                 << ");\n";
        }
    }
    writeJavaCallToRs(file, false, generateCallToVerifier);
    writeJavaCallToRs(file, true, generateCallToVerifier);
    file << tab(1) << "}\n\n";
}

void Permutation::writeJavaInputAllocationDefinition(ofstream& file, const string& indent,
                                                     const ParameterDefinition& param) const {
    string dataType;
    char vectorSize;
    convertToRsType(param.rsType, &dataType, &vectorSize);

    string seed = hashString(mJavaCheckMethodName + param.javaAllocName);
    file << indent << "Allocation " << param.javaAllocName << " = ";
    if (param.compatibleTypeIndex >= 0) {
        if (TYPES[param.typeIndex].kind == FLOATING_POINT) {
            writeJavaRandomCompatibleFloatAllocation(file, dataType, seed, vectorSize,
                                                     TYPES[param.compatibleTypeIndex],
                                                     TYPES[param.typeIndex]);
        } else {
            writeJavaRandomCompatibleIntegerAllocation(file, dataType, seed, vectorSize,
                                                       TYPES[param.compatibleTypeIndex],
                                                       TYPES[param.typeIndex]);
        }
    } else if (!param.minValue.empty()) {
        if (TYPES[param.typeIndex].kind != FLOATING_POINT) {
            printf("range(,) is only supported for floating point\n");
        } else {
            file << "createRandomFloatAllocation(mRS, Element.DataType." << dataType << ", "
                 << vectorSize << ", " << seed << ", " << param.minValue << ", " << param.maxValue
                 << ")";
        }
    } else {
        file << "createRandomAllocation(mRS, Element.DataType." << dataType << ", " << vectorSize
             << ", " << seed << ", false)";  // TODO set to false only for native
    }
    file << ";\n";
}

void Permutation::writeJavaRandomCompatibleFloatAllocation(ofstream& file, const string& dataType,
                                                           const string& seed, char vectorSize,
                                                           const Type& compatibleType,
                                                           const Type& generatedType) const {
    file << "createRandomFloatAllocation"
         << "(mRS, Element.DataType." << dataType << ", " << vectorSize << ", " << seed << ", ";
    double minValue = 0.0;
    double maxValue = 0.0;
    switch (compatibleType.kind) {
        case FLOATING_POINT: {
            // We're generating floating point values.  We just worry about the exponent.
            // Subtract 1 for the exponent sign.
            int bits = min(compatibleType.exponentBits, generatedType.exponentBits) - 1;
            maxValue = ldexp(0.95, (1 << bits) - 1);
            minValue = -maxValue;
            break;
        }
        case UNSIGNED_INTEGER:
            maxValue = MaxDoubleForInteger(compatibleType.significantBits,
                                           generatedType.significantBits);
            minValue = 0.0;
            break;
        case SIGNED_INTEGER:
            maxValue = MaxDoubleForInteger(compatibleType.significantBits,
                                           generatedType.significantBits);
            minValue = -maxValue - 1.0;
            break;
    }
    file << scientific << std::setprecision(19);
    file << minValue << ", " << maxValue << ")";
    file.unsetf(ios_base::floatfield);
}

void Permutation::writeJavaRandomCompatibleIntegerAllocation(ofstream& file, const string& dataType,
                                                             const string& seed, char vectorSize,
                                                             const Type& compatibleType,
                                                             const Type& generatedType) const {
    file << "createRandomIntegerAllocation"
         << "(mRS, Element.DataType." << dataType << ", " << vectorSize << ", " << seed << ", ";

    if (compatibleType.kind == FLOATING_POINT) {
        // Currently, all floating points can take any number we generate.
        bool isSigned = generatedType.kind == SIGNED_INTEGER;
        file << (isSigned ? "true" : "false") << ", " << generatedType.significantBits;
    } else {
        bool isSigned =
                    compatibleType.kind == SIGNED_INTEGER && generatedType.kind == SIGNED_INTEGER;
        file << (isSigned ? "true" : "false") << ", "
             << min(compatibleType.significantBits, generatedType.significantBits);
    }
    file << ")";
}

void Permutation::writeJavaOutputAllocationDefinition(ofstream& file, const string& indent,
                                                      const ParameterDefinition& param) const {
    string dataType;
    char vectorSize;
    convertToRsType(param.rsType, &dataType, &vectorSize);
    file << indent << "Allocation " << param.javaAllocName << " = Allocation.createSized(mRS, "
         << "getElement(mRS, Element.DataType." << dataType << ", " << vectorSize
         << "), INPUTSIZE);\n";
}

// Converts float2 to FLOAT_32 and 2, etc.
void Permutation::convertToRsType(const string& name, string* dataType, char* vectorSize) const {
    string s = name;
    int last = s.size() - 1;
    char lastChar = s[last];
    if (lastChar >= '1' && lastChar <= '4') {
        s.erase(last);
        *vectorSize = lastChar;
    } else {
        *vectorSize = '1';
    }
    dataType->clear();
    for (int i = 0; i < NUM_TYPES; i++) {
        if (s == TYPES[i].cType) {
            *dataType = TYPES[i].rsDataType;
            break;
        }
    }
}

void Permutation::writeJavaVerifyScalarMethod(ofstream& file, bool verifierValidates) const {
    writeJavaVerifyFunctionHeader(file);
    string vectorSize = "1";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        writeJavaArrayInitialization(file, p);
        if (p.mVectorSize != "1" && p.mVectorSize != vectorSize) {
            if (vectorSize == "1") {
                vectorSize = p.mVectorSize;
            } else {
                printf("Yikes, had vector %s and %s\n", vectorSize.c_str(), p.mVectorSize.c_str());
            }
        }
    }

    file << tab(2) << "for (int i = 0; i < INPUTSIZE; i++) {\n";
    file << tab(3) << "for (int j = 0; j < " << vectorSize << " ; j++) {\n";

    file << tab(4) << "// Extract the inputs.\n";
    file << tab(4) << mJavaArgumentsClassName << " args = new " << mJavaArgumentsClassName
         << "();\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (!p.isOutParameter) {
            file << tab(4) << "args." << p.variableName << " = " << p.javaArrayName << "[i";
            if (p.vectorWidth != "1") {
                file << " * " << p.vectorWidth << " + j";
            }
            file << "];\n";
        }
    }

    if (verifierValidates) {
        file << tab(4) << "// Extract the outputs.\n";
        for (size_t i = 0; i < mParams.size(); i++) {
            const ParameterDefinition& p = *mParams[i];
            if (p.isOutParameter) {
                file << tab(4) << "args." << p.variableName << " = " << p.javaArrayName
                     << "[i * " + p.vectorWidth + " + j];\n";
            }
        }
        file << tab(4) << "// Ask the CoreMathVerifier to validate.\n";
        file << tab(4) << "Floaty.setRelaxed(relaxed);\n";
        file << tab(4) << "String errorMessage = CoreMathVerifier." << mJavaVerifierVerifyMethodName
             << "(args, relaxed);\n";
        file << tab(4) << "boolean valid = errorMessage == null;\n";
    } else {
        file << tab(4) << "// Figure out what the outputs should have been.\n";
        file << tab(4) << "Floaty.setRelaxed(relaxed);\n";
        file << tab(4) << "CoreMathVerifier." << mJavaVerifierComputeMethodName << "(args);\n";
        file << tab(4) << "// Validate the outputs.\n";
        file << tab(4) << "boolean valid = true;\n";
        for (size_t i = 0; i < mParams.size(); i++) {
            const ParameterDefinition& p = *mParams[i];
            if (p.isOutParameter) {
                writeJavaTestAndSetValid(file, 4, p, "", "[i * " + p.vectorWidth + " + j]");
            }
        }
    }

    file << tab(4) << "if (!valid) {\n";
    file << tab(5) << "StringBuilder message = new StringBuilder();\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (p.isOutParameter) {
            writeJavaAppendOutputToMessage(file, 5, p, "", "[i * " + p.vectorWidth + " + j]",
                                           verifierValidates);
        } else {
            writeJavaAppendInputToMessage(file, 5, p, "args." + p.variableName);
        }
    }
    if (verifierValidates) {
        file << tab(5) << "message.append(errorMessage);\n";
    }

    file << tab(5) << "assertTrue(\"Incorrect output for " << mJavaCheckMethodName << "\" +\n";
    file << tab(7) << "(relaxed ? \"_relaxed\" : \"\") + \":\\n\" + message.toString(), valid);\n";
    file << tab(4) << "}\n";
    file << tab(3) << "}\n";
    file << tab(2) << "}\n";
    file << tab(1) << "}\n\n";
}

void Permutation::writeJavaVerifyFunctionHeader(ofstream& file) const {
    file << tab(1) << "private void " << mJavaVerifyMethodName << "(";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        file << "Allocation " << p.javaAllocName << ", ";
    }
    file << "boolean relaxed) {\n";
}

void Permutation::writeJavaTestAndSetValid(ofstream& file, int indent, const ParameterDefinition& p,
                                           const string& argsIndex,
                                           const string& actualIndex) const {
    writeJavaTestOneValue(file, indent, p, argsIndex, actualIndex);
    file << tab(indent + 1) << "valid = false;\n";
    file << tab(indent) << "}\n";
}

void Permutation::writeJavaTestOneValue(ofstream& file, int indent, const ParameterDefinition& p,
                                        const string& argsIndex, const string& actualIndex) const {
    file << tab(indent) << "if (";
    if (p.isFloatType) {
        file << "!args." << p.variableName << argsIndex << ".couldBe(" << p.javaArrayName
             << actualIndex;
        if (!mPrecisionLimit.empty()) {
            file << ", " << mPrecisionLimit;
        }
        file << ")";
    } else {
        file << "args." << p.variableName << argsIndex << " != " << p.javaArrayName << actualIndex;
    }
    if (p.undefinedIfOutIsNan && mReturnIndex >= 0) {
        file << " && args." << mParams[mReturnIndex]->variableName << argsIndex << ".isNaN()";
    }
    file << ") {\n";
}

void Permutation::writeJavaAppendOutputToMessage(ofstream& file, int indent,
                                                 const ParameterDefinition& p,
                                                 const string& argsIndex, const string& actualIndex,
                                                 bool verifierValidates) const {
    if (verifierValidates) {
        const string actual = "args." + p.variableName + argsIndex;
        file << tab(indent) << "message.append(\"Output " + p.variableName + ": \");\n";
        if (p.isFloatType) {
            writeJavaAppendFloatyVariableToMessage(file, indent, actual);
        } else {
            writeJavaAppendVariableToMessage(file, indent, p, actual);
        }
        writeJavaAppendNewLineToMessage(file, indent);
    } else {
        const string expected = "args." + p.variableName + argsIndex;
        const string actual = p.javaArrayName + actualIndex;
        file << tab(indent) << "message.append(\"Expected output " + p.variableName + ": \");\n";
        if (p.isFloatType) {
            writeJavaAppendFloatyVariableToMessage(file, indent, expected);
        } else {
            writeJavaAppendVariableToMessage(file, indent, p, expected);
        }
        writeJavaAppendNewLineToMessage(file, indent);
        file << tab(indent) << "message.append(\"Actual   output " + p.variableName + ": \");\n";
        writeJavaAppendVariableToMessage(file, indent, p, actual);

        writeJavaTestOneValue(file, indent, p, argsIndex, actualIndex);
        file << tab(indent + 1) << "message.append(\" FAIL\");\n";
        file << tab(indent) << "}\n";
        writeJavaAppendNewLineToMessage(file, indent);
    }
}

void Permutation::writeJavaAppendInputToMessage(ofstream& file, int indent,
                                                const ParameterDefinition& p,
                                                const string& actual) const {
    file << tab(indent) << "message.append(\"Input " + p.variableName + ": \");\n";
    writeJavaAppendVariableToMessage(file, indent, p, actual);
    writeJavaAppendNewLineToMessage(file, indent);
}

void Permutation::writeJavaAppendNewLineToMessage(ofstream& file, int indent) const {
    file << tab(indent) << "message.append(\"\\n\");\n";
}

void Permutation::writeJavaAppendVariableToMessage(ofstream& file, int indent,
                                                   const ParameterDefinition& p,
                                                   const string& value) const {
    if (p.specType == "f16" || p.specType == "f32") {
        file << tab(indent) << "message.append(String.format(\"%14.8g %8x %15a\",\n";
        file << tab(indent + 2) << value << ", "
             << "Float.floatToRawIntBits(" << value << "), " << value << "));\n";
    } else if (p.specType == "f64") {
        file << tab(indent) << "message.append(String.format(\"%24.8g %16x %31a\",\n";
        file << tab(indent + 2) << value << ", "
             << "Double.doubleToRawLongBits(" << value << "), " << value << "));\n";
    } else if (p.specType[0] == 'u') {
        file << tab(indent) << "message.append(String.format(\"0x%x\", " << value << "));\n";
    } else {
        file << tab(indent) << "message.append(String.format(\"%d\", " << value << "));\n";
    }
}

void Permutation::writeJavaAppendFloatyVariableToMessage(ofstream& file, int indent,
                                                         const string& value) const {
    file << tab(indent) << "message.append(" << value << ".toString());\n";
}

void Permutation::writeJavaVectorComparison(ofstream& file, int indent,
                                            const ParameterDefinition& p) const {
    if (p.mVectorSize == "1") {
        writeJavaTestAndSetValid(file, indent, p, "", "[i]");

    } else {
        file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
        writeJavaTestAndSetValid(file, indent + 1, p, "[j]", "[i * " + p.vectorWidth + " + j]");
        file << tab(indent) << "}\n";
    }
}

void Permutation::writeJavaAppendVectorInputToMessage(ofstream& file, int indent,
                                                      const ParameterDefinition& p) const {
    if (p.mVectorSize == "1") {
        writeJavaAppendInputToMessage(file, indent, p, p.javaArrayName + "[i]");
    } else {
        file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
        writeJavaAppendInputToMessage(file, indent + 1, p,
                                      p.javaArrayName + "[i * " + p.vectorWidth + " + j]");
        file << tab(indent) << "}\n";
    }
}

void Permutation::writeJavaAppendVectorOutputToMessage(ofstream& file, int indent,
                                                       const ParameterDefinition& p) const {
    if (p.mVectorSize == "1") {
        writeJavaAppendOutputToMessage(file, indent, p, "", "[i]", false);

    } else {
        file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
        writeJavaAppendOutputToMessage(file, indent + 1, p, "[j]",
                                       "[i * " + p.vectorWidth + " + j]", false);
        file << tab(indent) << "}\n";
    }
}

void Permutation::writeJavaVerifyVectorMethod(ofstream& file) const {
    writeJavaVerifyFunctionHeader(file);
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        writeJavaArrayInitialization(file, p);
    }
    file << tab(2) + "for (int i = 0; i < INPUTSIZE; i++) {\n";
    file << tab(3) << mJavaArgumentsNClassName << " args = new " << mJavaArgumentsNClassName
         << "();\n";

    file << tab(3) << "// Create the appropriate sized arrays in args\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (p.mVectorSize != "1") {
            string type = p.javaBaseType;
            if (p.isOutParameter && p.isFloatType) {
                type = "Floaty";
            }
            file << tab(3) << "args." << p.variableName << " = new " << type << "[" << p.mVectorSize
                 << "];\n";
        }
    }

    file << tab(3) << "// Fill args with the input values\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (!p.isOutParameter) {
            if (p.mVectorSize == "1") {
                file << tab(3) << "args." << p.variableName << " = " << p.javaArrayName + "[i]"
                     << ";\n";
            } else {
                file << tab(3) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
                file << tab(4) << "args." << p.variableName + "[j] = "
                     << p.javaArrayName + "[i * " + p.vectorWidth + " + j]"
                     << ";\n";
                file << tab(3) << "}\n";
            }
        }
    }
    file << tab(3) << "Floaty.setRelaxed(relaxed);\n";
    file << tab(3) << "CoreMathVerifier." << mJavaVerifierComputeMethodName << "(args);\n\n";

    file << tab(3) << "// Compare the expected outputs to the actual values returned by RS.\n";
    file << tab(3) << "boolean valid = true;\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (p.isOutParameter) {
            writeJavaVectorComparison(file, 3, p);
        }
    }

    file << tab(3) << "if (!valid) {\n";
    file << tab(4) << "StringBuilder message = new StringBuilder();\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (p.isOutParameter) {
            writeJavaAppendVectorOutputToMessage(file, 4, p);
        } else {
            writeJavaAppendVectorInputToMessage(file, 4, p);
        }
    }

    file << tab(4) << "assertTrue(\"Incorrect output for " << mJavaCheckMethodName << "\" +\n";
    file << tab(6) << "(relaxed ? \"_relaxed\" : \"\") + \":\\n\" + message.toString(), valid);\n";
    file << tab(3) << "}\n";
    file << tab(2) << "}\n";
    file << tab(1) << "}\n\n";
}

void Permutation::writeJavaCallToRs(ofstream& file, bool relaxed, bool generateCallToVerifier) const {
    string script = "script";
    if (relaxed) {
        script += "Relaxed";
    }

    file << tab(2) << "try {\n";
    for (size_t i = 0; i < mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (p.isOutParameter) {
            writeJavaOutputAllocationDefinition(file, tab(3), p);
        }
    }

    for (int i = 0; i < (int)mParams.size(); i++) {
        const ParameterDefinition& p = *mParams[i];
        if (i != mReturnIndex && i != mFirstInputIndex) {
            file << tab(3) << script << ".set_" << p.rsAllocName << "(" << p.javaAllocName
                 << ");\n";
        }
    }

    file << tab(3) << script << ".forEach_" << mRsKernelName << "(";
    bool needComma = false;
    if (mFirstInputIndex >= 0) {
        file << mParams[mFirstInputIndex]->javaAllocName;
        needComma = true;
    }
    if (mReturnIndex >= 0) {
        if (needComma) {
            file << ", ";
        }
        file << mParams[mReturnIndex]->variableName << ");\n";
    }

    if (generateCallToVerifier) {
        file << tab(3) << mJavaVerifyMethodName << "(";
        for (size_t i = 0; i < mParams.size(); i++) {
            const ParameterDefinition& p = *mParams[i];
            file << p.variableName << ", ";
        }

        if (relaxed) {
            file << "true";
        } else {
            file << "false";
        }
        file << ");\n";
    }
    file << tab(2) << "} catch (Exception e) {\n";
    file << tab(3) << "throw new RSRuntimeException(\"RenderScript. Can't invoke forEach_"
         << mRsKernelName << ": \" + e.toString());\n";
    file << tab(2) << "}\n";
}

}  // namespace

int main(int argc, char* argv[]) {
    int versionOfTestFiles = 0;
    vector<string> specFileNames;
    if (!parseCommandLine(argc, argv, &versionOfTestFiles, &specFileNames)) {
        printf("Usage: gen_runtime spec_file [spec_file...] [-v version_of_test_files]\n");
        return -1;
    }
    int result = 0;
    for (size_t i = 0; i < specFileNames.size(); i++) {
        SpecFile specFile(specFileNames[i]);
        if (!specFile.process(versionOfTestFiles)) {
            result = -1;
        }
    }
    return result;
}