1135c4b747bce5898d98de964d7c453d215254d0dJason Sams/*
2135c4b747bce5898d98de964d7c453d215254d0dJason Sams * Copyright (C) 2013 The Android Open Source Project
3135c4b747bce5898d98de964d7c453d215254d0dJason Sams *
4135c4b747bce5898d98de964d7c453d215254d0dJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5135c4b747bce5898d98de964d7c453d215254d0dJason Sams * you may not use this file except in compliance with the License.
6135c4b747bce5898d98de964d7c453d215254d0dJason Sams * You may obtain a copy of the License at
7135c4b747bce5898d98de964d7c453d215254d0dJason Sams *
8135c4b747bce5898d98de964d7c453d215254d0dJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9135c4b747bce5898d98de964d7c453d215254d0dJason Sams *
10135c4b747bce5898d98de964d7c453d215254d0dJason Sams * Unless required by applicable law or agreed to in writing, software
11135c4b747bce5898d98de964d7c453d215254d0dJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12135c4b747bce5898d98de964d7c453d215254d0dJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13135c4b747bce5898d98de964d7c453d215254d0dJason Sams * See the License for the specific language governing permissions and
14135c4b747bce5898d98de964d7c453d215254d0dJason Sams * limitations under the License.
15135c4b747bce5898d98de964d7c453d215254d0dJason Sams */
16135c4b747bce5898d98de964d7c453d215254d0dJason Sams
17963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet/* This program processes Renderscript function definitions described in spec files.
18963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * For each spec file provided on the command line, it generates a corresponding
19963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * Renderscript header (*.rsh) which is meant for inclusion in client scripts.
20963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *
21963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * This program also generates Junit test files to automatically test each of the
22963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * functions using randomly generated data.  We create two files for each function:
23963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * - a Renderscript file named Test{Function}.rs,
24963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * - a Junit file named Test{function}.java, which calls the above RS file.
25963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *
26963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * This program takes an optional -v parameter, the RS version to target the
27963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * test files for.  The header file will always contain all the functions.
28963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *
29963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * This program contains five main classes:
30963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * - SpecFile: Represents on spec file.
31963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * - Function: Each instance represents a function, like clamp.  Even though the
32963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *      spec file contains many entries for clamp, we'll only have one clamp instance.
33963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * - Specification: Defines one of the many variations of the function.  There's
34963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *      a one to one correspondance between Specification objects and entries in the
35963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *      spec file.  Strings that are parts of a Specification can include placeholders,
36963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *      which are "#1", "#2", "#3", and "#4".  We'll replace these by values before
37963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *      generating the files.
38963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * - Permutation: A concrete version of a specification, where all placeholders have
39963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet *      been replaced by actual values.
40963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * - ParameterDefinition: A definition of a parameter of a concrete function.
41963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet */
42963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
43bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet#include <math.h>
44bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet#include <stdio.h>
45963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet#include <cctype>
46963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet#include <cstdlib>
47bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet#include <fstream>
48963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet#include <functional>
49bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet#include <iomanip>
50135c4b747bce5898d98de964d7c453d215254d0dJason Sams#include <list>
51963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet#include <map>
52963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet#include <set>
5346341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet#include <sstream>
54135c4b747bce5898d98de964d7c453d215254d0dJason Sams#include <string>
55963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet#include <vector>
56135c4b747bce5898d98de964d7c453d215254d0dJason Sams
57135c4b747bce5898d98de964d7c453d215254d0dJason Samsusing namespace std;
58135c4b747bce5898d98de964d7c453d215254d0dJason Sams
59963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletnamespace {
60963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
61963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletconst char* AUTO_GENERATED_WARNING =
62963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            "// Don't edit this file!  It is auto-generated by "
63963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            "frameworks/rs/api/gen_runtime.\n\n";
64963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletconst char* LEGAL_NOTICE =
65963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            "/*\n"
66963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * Copyright (C) 2014 The Android Open Source Project\n"
67963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " *\n"
68963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
69963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * you may not use this file except in compliance with the License.\n"
70963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * You may obtain a copy of the License at\n"
71963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " *\n"
72963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " *      http://www.apache.org/licenses/LICENSE-2.0\n"
73963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " *\n"
74963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * Unless required by applicable law or agreed to in writing, software\n"
75963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
76963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
77963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * See the License for the specific language governing permissions and\n"
78963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " * limitations under the License.\n"
79963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            " */\n\n";
80963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
81963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletclass Function;
82963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletclass Specification;
83963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletclass Permutation;
84bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouilletstruct Type;
85963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
86963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet/* Information about a parameter to a function.  The values of all the fields should only be set by
87963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * parseParameterDefinition.
88963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet */
89963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletstruct ParameterDefinition {
90963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string rsType;        // The Renderscript type, e.g. "uint3"
91963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string rsBaseType;    // As above but without the number, e.g. "uint"
92963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string javaBaseType;  // The type we need to declare in Java, e.g. "unsigned int"
9346ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    string specType;      // The type found in the spec, e.g. "f16"
9446ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    bool isFloatType;     // True if it's a floating point value
95963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
96963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* The number of entries in the vector.  It should be either "1", "2", "3", or "4".  It's also
97963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * "1" for scalars.
98963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
99963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mVectorSize;
100963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* The space the vector takes in an array.  It's the same as the vector size, except for size
101963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * "3", where the width is "4".
102963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
103963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string vectorWidth;
104963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
10546341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    string specName;       // e.g. x, as found in the spec file
106963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string variableName;   // e.g. inX, used both in .rs and .java
107963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string rsAllocName;    // e.g. gAllocInX
108963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string javaAllocName;  // e.g. inX
109963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string javaArrayName;  // e.g. arrayInX
110135c4b747bce5898d98de964d7c453d215254d0dJason Sams
11146341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    // If non empty, the mininum and maximum values to be used when generating the test data.
11246341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    string minValue;
11346341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    string maxValue;
11446341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    /* If non empty, contains the name of another parameter that should be smaller or equal to this
11546341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet     * parameter, i.e.  value(smallerParameter) <= value(this).  This is used when testing clamp.
11646341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet     */
11746341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    string smallerParameter;
11846341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet
119bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    bool isOutParameter;       // True if this parameter returns data from the script.
120bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    bool undefinedIfOutIsNan;  // If true, we don't validate if 'out' is NaN.
121bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet
122bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    int typeIndex;            // Index in the TYPES array.
123bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    int compatibleTypeIndex;  // Index in TYPES for which the test data must also fit.
124963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
125963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* Parse the parameter definition found in the spec file.  It will generate a name if none
126963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * are present in the file.  One of the two counts will be incremented, and potentially
127963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * used to generate unique names.  isReturn is true if we're processing the "return:"
128963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * definition.
129963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
130963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void parseParameterDefinition(string s, bool isReturn, int* inputCount, int* outputCount);
131963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet};
132963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
133963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// An entire spec file and the methods to process it.
134963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletclass SpecFile {
135135c4b747bce5898d98de964d7c453d215254d0dJason Samspublic:
136963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    explicit SpecFile(const string& specFileName) : mSpecFileName(specFileName) {}
137963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool process(int versionOfTestFiles);
138135c4b747bce5898d98de964d7c453d215254d0dJason Sams
139963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletprivate:
140963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    const string mSpecFileName;
141963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The largest version number that we have found in all the specifications.
142963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int mLargestVersionNumber;
143963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
144963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    map<string, Function*> mFunctionsMap;  // All the known functions.
145963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    typedef map<string, Function*>::iterator FunctionsIterator;
146963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
147963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool readSpecFile();
148963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    Function* getFunction(const string& name);
149963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool generateFiles(int versionOfTestFiles);
150963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool writeAllFunctions(ofstream& headerFile, int versionOfTestFiles);
151963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet};
152963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
153963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet/* Represents a function, like "clamp".  Even though the spec file contains many entries for clamp,
154963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * we'll only have one clamp instance.
155963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet */
156963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletclass Function {
157963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletprivate:
158963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mName;             // The lower case name, e.g. native_log
159963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mCapitalizedName;  // The capitalized name, e.g. NativeLog
160963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mTestName;         // e.g. TestNativeLog
161963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mRelaxedTestName;  // e.g. TestNativeLogRelaxed
162963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
163963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    vector<Specification*> mSpecifications;
164963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    typedef vector<Specification*>::iterator SpecificationIterator;
165963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
166963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* We keep track of the allocations generated in the .rs file and the argument classes defined
167963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * in the Java file, as we share these between the functions created for each specification.
168963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
169963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    set<string> mRsAllocationsGenerated;
170963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    set<string> mJavaGeneratedArgumentClasses;
171963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
172963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mJavaCallAllCheckMethods;  // Lines of Java code to invoke the check methods.
173963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
174963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    ofstream mRsFile;    // The Renderscript test file we're generating.
175963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    ofstream mJavaFile;  // The Jave test file we're generating.
176963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
177963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool startRsFile();         // Open the mRsFile and writes its header.
178963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool writeRelaxedRsFile();  // Write the entire relaxed rs test file (an include essentially)
179963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool startJavaFile();       // Open the mJavaFile and writes the header.
180963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void finishJavaFile();      // Write the test method and closes the file.
181963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
182963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletpublic:
183963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    explicit Function(const string& name);
184963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void addSpecification(Specification* spec) { mSpecifications.push_back(spec); }
185963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* Write the .java and the two .rs test files.  versionOfTestFiles is used to restrict which API
186963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * to test.  Also writes the section of the header file.
187963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
188963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool writeFiles(ofstream& headerFile, int versionOfTestFiles);
189963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Write an allocation and keep track of having it written, so it can be shared.
190963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeRsAllocationDefinition(const ParameterDefinition& param);
191963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Write an argument class definiton and keep track of having it written, so it can be shared.
192963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaArgumentClassDefinition(const string& className, const string& definition);
193963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Add a call to mJavaCallAllCheckMethods to be used at the end of the file generation.
194963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void addJavaCheckCall(const string& call);
195963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet};
196963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
197963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet/* Defines one of the many variations of the function.  There's a one to one correspondance between
198963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * Specification objects and entries in the spec file.  Some of the strings that are parts of a
199963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * Specification can include placeholders, which are "#1", "#2", "#3", and "#4".  We'll replace
200963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * these by values before generating the files.
201963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet */
202963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletclass Specification {
203963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletprivate:
204963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* The range of versions this specification applies to. 0 if there's no restriction, so an API
205963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * that became available at 9 and is still valid would have min:9 max:0.
206963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
207135c4b747bce5898d98de964d7c453d215254d0dJason Sams    int mMinVersion;
208135c4b747bce5898d98de964d7c453d215254d0dJason Sams    int mMaxVersion;
209135c4b747bce5898d98de964d7c453d215254d0dJason Sams
210963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* The name of the function without #n, e.g. convert.  As of this writing, it only differs for
211963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * convert.
212963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
213963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mCleanName;
21493906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet    /* How to test.  One of:
21593906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     * "scalar": Generate test code that checks entries of each vector indepently.  E.g. for
21693906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     *           sin(float3), the test code will call the CoreMathVerfier.computeSin 3 times.
21793906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     * "vector": Generate test code that calls the CoreMathVerifier only once for each vector.
21893906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     *           This is useful for APIs like dot() or length().
21993906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     * "noverify": Generate test code that calls the API but don't verify the returned value.
22093906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     * "limited": Like "scalar" but tests a limited range of input values.
22193906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     * "custom": Like "scalar" but instead of calling CoreMathVerifier.computeXXX() to compute
22293906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     *           the expected value, we call instead CoreMathVerifier.verifyXXX().  This method
22393906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     *           returns a string that contains the error message, null if there's no error.
22493906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet     */
22593906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet    string mTest;
226bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    string mPrecisionLimit;  // Maximum precision required when checking output of this function.
227963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
228135c4b747bce5898d98de964d7c453d215254d0dJason Sams    vector<vector<string> > mReplaceables;
229963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
230963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The following fields may contain placeholders that will be replaced using the mReplaceables.
231963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
232963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The name of this function, can include #, e.g. convert_#1_#2
233963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mName;
234963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
235963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mReturn;           // The return type
236963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    vector<string> mComment;  // The comments to be included in the header
237963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    vector<string> mInline;   // The inline code to be included in the header
238963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    vector<string> mParam;    // One entry per parameter defined
239963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
240963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Substitute the placeholders in the strings by the corresponding entries in mReplaceables.
241963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string expandString(string s, int i1, int i2, int i3, int i4) const;
242963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4,
243963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                            vector<string>* out) const;
244963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
245963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletpublic:
246963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    Specification() {
247963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        mMinVersion = 0;
248963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        mMaxVersion = 0;
249963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
250963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int getMinVersion() const { return mMinVersion; }
251963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int getMaxVersion() const { return mMaxVersion; }
252963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
253963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string getName(int i1, int i2, int i3, int i4) const {
254963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return expandString(mName, i1, i2, i3, i4);
255963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
256963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string getReturn(int i1, int i2, int i3, int i4) const {
257963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return expandString(mReturn, i1, i2, i3, i4);
258963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
259963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void getComments(int i1, int i2, int i3, int i4, vector<string>* comments) const {
260963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return expandStringVector(mComment, i1, i2, i3, i4, comments);
261963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
262963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void getInlines(int i1, int i2, int i3, int i4, vector<string>* inlines) const {
263963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return expandStringVector(mInline, i1, i2, i3, i4, inlines);
264963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
265963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void getParams(int i1, int i2, int i3, int i4, vector<string>* params) const {
266963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return expandStringVector(mParam, i1, i2, i3, i4, params);
267963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
268963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string getTest() const { return mTest; }
269bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    string getPrecisionLimit() const { return mPrecisionLimit; }
270963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string getCleanName() const { return mCleanName; }
271963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
272963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile, Function* function,
273963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    int versionOfTestFiles);
274963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool writeRelaxedRsFile() const;
275963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Return true if this specification should be generated for this version.
276963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool relevantForVersion(int versionOfTestFiles) const;
277963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
278963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    static Specification* scanSpecification(FILE* in);
279963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet};
280963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
281963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// A concrete version of a specification, where all placeholders have been replaced by actual
282963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// values.
283963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletclass Permutation {
284963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletprivate:
285963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    Function* mFunction;
286963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    Specification* mSpecification;
287963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
288963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // These are the expanded version of those found on Specification
289963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mName;
290963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mCleanName;
291bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    string mTest;  // How to test.  One of "scalar", "vector", "noverify", "limited", and "none".
292bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    string mPrecisionLimit;  // Maximum precision required when checking output of this function.
293135c4b747bce5898d98de964d7c453d215254d0dJason Sams    vector<string> mInline;
294963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    vector<string> mComment;
295963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
296963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The inputs and outputs of the function.  This include the return type, if present.
297963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    vector<ParameterDefinition*> mParams;
298963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The index of the return value in mParams, -1 if the function is void.
299963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int mReturnIndex;
300963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The index of the first input value in mParams, -1 if there's no input.
301963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int mFirstInputIndex;
302963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The number of input and output parameters.
303963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int mInputCount;
304963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int mOutputCount;
30532c18186ad37029056f09b4041e62b09270ea0e6Jean-Luc Brouillet    // Whether one of the output parameters is a float.
30632c18186ad37029056f09b4041e62b09270ea0e6Jean-Luc Brouillet    bool mHasFloatAnswers;
307135c4b747bce5898d98de964d7c453d215254d0dJason Sams
308963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mRsKernelName;
309963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mJavaArgumentsClassName;
310963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mJavaArgumentsNClassName;
311963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mJavaVerifierComputeMethodName;
31293906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet    string mJavaVerifierVerifyMethodName;
313963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mJavaCheckMethodName;
314963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string mJavaVerifyMethodName;
315963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
316963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeHeaderSection(ofstream& file) const;
317963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
318963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeRsSection(ofstream& rs) const;
319963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
320963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaSection(ofstream& file) const;
321963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaArgumentClass(ofstream& file, bool scalar) const;
32293906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet    void writeJavaCheckMethod(ofstream& file, bool generateCallToVerifier) const;
32393906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet    void writeJavaVerifyScalarMethod(ofstream& file, bool verifierValidates) const;
324963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaVerifyVectorMethod(ofstream& file) const;
325963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaVerifyFunctionHeader(ofstream& file) const;
326963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaInputAllocationDefinition(ofstream& file, const string& indent,
32746341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet                                            const ParameterDefinition& param) const;
328963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaOutputAllocationDefinition(ofstream& file, const string& indent,
32946341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet                                             const ParameterDefinition& param) const;
330bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    // Write code to create a random allocation for which the data must be compatible for two types.
331bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    void writeJavaRandomCompatibleFloatAllocation(ofstream& file, const string& dataType,
332bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                                  const string& seed, char vectorSize,
333bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                                  const Type& compatibleType,
334bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                                  const Type& generatedType) const;
335bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    void writeJavaRandomCompatibleIntegerAllocation(ofstream& file, const string& dataType,
336bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                                    const string& seed, char vectorSize,
337bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                                    const Type& compatibleType,
338bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                                    const Type& generatedType) const;
33993906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet    void writeJavaCallToRs(ofstream& file, bool relaxed, bool generateCallToVerifier) const;
340963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
341bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    void writeJavaTestAndSetValid(ofstream& file, int indent, const ParameterDefinition& p,
342bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                  const string& argsIndex, const string& actualIndex) const;
343bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    void writeJavaTestOneValue(ofstream& file, int indent, const ParameterDefinition& p,
344bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                               const string& argsIndex, const string& actualIndex) const;
345bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    void writeJavaAppendOutputToMessage(ofstream& file, int indent, const ParameterDefinition& p,
34693906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet                                        const string& argsIndex, const string& actualIndex,
34793906648166535b090ac9a16c5365f4ddfd360feJean-Luc Brouillet                                        bool verifierValidates) const;
34846ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    void writeJavaAppendInputToMessage(ofstream& file, int indent, const ParameterDefinition& p,
34946ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                                       const string& actual) const;
350963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaAppendNewLineToMessage(ofstream& file, int indent) const;
35146ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    void writeJavaAppendVariableToMessage(ofstream& file, int indent, const ParameterDefinition& p,
352bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                                          const string& value) const;
35332c18186ad37029056f09b4041e62b09270ea0e6Jean-Luc Brouillet    void writeJavaAppendFloatVariableToMessage(ofstream& file, int indent, const string& value,
35432c18186ad37029056f09b4041e62b09270ea0e6Jean-Luc Brouillet                                               bool regularFloat) const;
355963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaVectorComparison(ofstream& file, int indent, const ParameterDefinition& p) const;
356963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaAppendVectorInputToMessage(ofstream& file, int indent,
357963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                                             const ParameterDefinition& p) const;
358963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeJavaAppendVectorOutputToMessage(ofstream& file, int indent,
359963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                                              const ParameterDefinition& p) const;
360963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool passByAddressToSet(const string& name) const;
361963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void convertToRsType(const string& name, string* dataType, char* vectorSize) const;
362963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
363963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletpublic:
364963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    Permutation(Function* function, Specification* specification, int i1, int i2, int i3, int i4);
365963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile,
366963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    int versionOfTestFiles);
367135c4b747bce5898d98de964d7c453d215254d0dJason Sams};
368135c4b747bce5898d98de964d7c453d215254d0dJason Sams
369963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Table of type equivalences
370963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// TODO: We should just be pulling this from a shared header. Slang does exactly the same thing.
371bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet
372bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouilletenum NumberKind { SIGNED_INTEGER, UNSIGNED_INTEGER, FLOATING_POINT };
373bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet
374963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletstruct Type {
375963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    const char* specType;  // Name found in the .spec file
376963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string rsDataType;     // RS data type
377963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string cType;          // Type in a C file
378963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    const char* javaType;  // Type in a Java file
379bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    NumberKind kind;
380bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    /* For integers, number of bits of the number, excluding the sign bit.
38146ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet     * For floats, number of implied bits of the mantissa.
382bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet     */
383bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    int significantBits;
38446ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    // For floats, number of bits of the exponent.  0 for integer types.
38546ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    int exponentBits;
386963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet};
387135c4b747bce5898d98de964d7c453d215254d0dJason Sams
38846ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouilletconst Type TYPES[] = {{"f16", "FLOAT_16", "half", "half", FLOATING_POINT, 11, 5},
38946ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 24, 8},
39046ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 53, 11},
39146ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7, 0},
39246ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8, 0},
39346ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15, 0},
39446ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16, 0},
39546ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31, 0},
39646ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32, 0},
39746ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63, 0},
39846ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64, 0}};
399963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
400963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletconst int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]);
401963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
402bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet// Returns the index in TYPES for the provided cType
403bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouilletint FindCType(const string& cType) {
404bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    for (int i = 0; i < NUM_TYPES; i++) {
405bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet        if (cType == TYPES[i].cType) {
406bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet            return i;
407bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet        }
408bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    }
409bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    return -1;
410bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet}
411bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet
412963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Capitalizes and removes underscores.  E.g. converts "native_log" to NativeLog.
413963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletstring capitalize(const string& source) {
414963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int length = source.length();
415963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string result;
416963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool capitalize = true;
417963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    for (int s = 0; s < length; s++) {
418963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (source[s] == '_') {
419963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            capitalize = true;
420963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        } else if (capitalize) {
421963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            result += toupper(source[s]);
422963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            capitalize = false;
423963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        } else {
424963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            result += source[s];
425963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
426963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
427963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return result;
428963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
429963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
430963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletstring tab(int n) { return string(n * 4, ' '); }
431963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
432bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet// Returns a string that's an hexadecimal constant fo the hash of the string.
433bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouilletstring hashString(const string& s) {
434963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    long hash = 0;
435963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    for (size_t i = 0; i < s.length(); i++) {
436963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        hash = hash * 43 + s[i];
437963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
438bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    stringstream stream;
439bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    stream << "0x" << std::hex << hash << "l";
440bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    return stream.str();
441963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
442963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
44346341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet// Removes the character from present. Returns true if the string contained the character.
44446341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouilletstatic bool charRemoved(char c, string* s) {
44546341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    size_t p = s->find(c);
44646341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    if (p != string::npos) {
44746341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        s->erase(p, 1);
44846341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        return true;
44946341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    }
45046341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    return false;
45146341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet}
45246341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet
453963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Return true if the string is already in the set.  Inserts it if not.
454963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool testAndSet(const string& flag, set<string>* set) {
455963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (set->find(flag) == set->end()) {
456963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        set->insert(flag);
457963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
458135c4b747bce5898d98de964d7c453d215254d0dJason Sams    }
459135c4b747bce5898d98de964d7c453d215254d0dJason Sams    return true;
460135c4b747bce5898d98de964d7c453d215254d0dJason Sams}
461135c4b747bce5898d98de964d7c453d215254d0dJason Sams
462963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Convert an int into a string.
463963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletstring toString(int n) {
464963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    char buf[100];
465963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    snprintf(buf, sizeof(buf), "%d", n);
466963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return string(buf);
467963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
468963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
469963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid trim(string* s, size_t start) {
470135c4b747bce5898d98de964d7c453d215254d0dJason Sams    if (start > 0) {
471135c4b747bce5898d98de964d7c453d215254d0dJason Sams        s->erase(0, start);
472135c4b747bce5898d98de964d7c453d215254d0dJason Sams    }
473135c4b747bce5898d98de964d7c453d215254d0dJason Sams
474135c4b747bce5898d98de964d7c453d215254d0dJason Sams    while (s->size() && (s->at(0) == ' ')) {
475135c4b747bce5898d98de964d7c453d215254d0dJason Sams        s->erase(0, 1);
476135c4b747bce5898d98de964d7c453d215254d0dJason Sams    }
477135c4b747bce5898d98de964d7c453d215254d0dJason Sams
478135c4b747bce5898d98de964d7c453d215254d0dJason Sams    size_t p = s->find_first_of("\n\r");
479135c4b747bce5898d98de964d7c453d215254d0dJason Sams    if (p != string::npos) {
480135c4b747bce5898d98de964d7c453d215254d0dJason Sams        s->erase(p);
481135c4b747bce5898d98de964d7c453d215254d0dJason Sams    }
482135c4b747bce5898d98de964d7c453d215254d0dJason Sams
483963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    while ((s->size() > 0) && (s->at(s->size() - 1) == ' ')) {
484963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        s->erase(s->size() - 1);
485963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
486963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
487963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
488963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletstring stringReplace(string s, string match, string rep) {
489963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    while (1) {
490963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        size_t p = s.find(match);
491963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (p == string::npos) break;
492963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
493963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        s.erase(p, match.size());
494963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        s.insert(p, rep);
495963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
496963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return s;
497963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
498963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
499963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Return the next line from the input file.
500963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool getNextLine(FILE* in, string* s) {
501963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    s->clear();
502963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    while (1) {
503963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        int c = fgetc(in);
504963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (c == EOF) return s->size() != 0;
505963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (c == '\n') break;
506963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        s->push_back((char)c);
507963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
508963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
509963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
510963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
511963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid writeIfdef(ofstream& file, string filename, bool isStart) {
512963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string t = "__";
513963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    t += filename;
514963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    t += "__";
515963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
516963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    for (size_t i = 2; i < t.size(); i++) {
517963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (t[i] == '.') {
518963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            t[i] = '_';
519963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
520963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
521963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
522963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (isStart) {
523963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        file << "#ifndef " << t << "\n";
524963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        file << "#define " << t << "\n";
525963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    } else {
526963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        file << "#endif // " << t << "\n";
527963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
528963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
529963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
530963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid writeJavaArrayInitialization(ofstream& file, const ParameterDefinition& p) {
531963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    file << tab(2) << p.javaBaseType << "[] " << p.javaArrayName << " = new " << p.javaBaseType
532963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet         << "[INPUTSIZE * " << p.vectorWidth << "];\n";
533963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    file << tab(2) << p.javaAllocName << ".copyTo(" << p.javaArrayName << ");\n";
534963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
535963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
536963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool parseCommandLine(int argc, char* argv[], int* versionOfTestFiles,
537963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                      vector<string>* specFileNames) {
538963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    for (int i = 1; i < argc; i++) {
539963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (argv[i][0] == '-') {
540963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            if (argv[i][1] == 'v') {
541963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                i++;
542963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                if (i < argc) {
543963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    char* end;
544963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    *versionOfTestFiles = strtol(argv[i], &end, 10);
545963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    if (*end != '\0') {
546963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                        printf("Can't parse the version number %s\n", argv[i]);
547963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                        return false;
548963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    }
549963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                } else {
550963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    printf("Missing version number after -v\n");
551963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                    return false;
552963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                }
553963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            } else {
554963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                printf("Unrecognized flag %s\n", argv[i]);
555963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                return false;
556963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            }
557963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        } else {
558963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            specFileNames->push_back(argv[i]);
559963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
560963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
561963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (specFileNames->size() == 0) {
562963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        printf("No spec file specified\n");
563963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
564963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
565963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
566963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
567963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
56846ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet/* Returns a double that should be able to be converted to an integer of size
56946ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet * numberOfIntegerBits.
57046ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet */
57146ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouilletstatic double MaxDoubleForInteger(int numberOfIntegerBits, int mantissaSize) {
57246ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    /* Double has only 52 bits of precision (53 implied). So for longs, we want
57346ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet     * to create smaller values to avoid a round up.  Same for floats and halfs.
57446ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet     */
57546ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    int lowZeroBits = max(0, numberOfIntegerBits - mantissaSize);
57646ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    unsigned long l = (0xffffffffffffffff >> (64 - numberOfIntegerBits + lowZeroBits))
57746ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet                      << lowZeroBits;
57846ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    return (double)l;
57946ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet}
58046ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet
581963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet/* Parse a parameter definition.  It's of the form "type [*][name]".  The type
582963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * is required.  The name is optional.  The * indicates it's an output
583963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * parameter.  We also pass the indexed of this parameter in the definition, so
584963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet * we can create names like in2, in3, etc. */
585963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid ParameterDefinition::parseParameterDefinition(string s, bool isReturn, int* inputCount,
586963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                                                   int* outputCount) {
58746341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    istringstream stream(s);
58846341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    string name, type, option;
58946341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    stream >> rsType;
59046341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    stream >> specName;
59146341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    stream >> option;
592963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
59346341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    // Determine if this is an output.
59446341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    isOutParameter = charRemoved('*', &rsType) || charRemoved('*', &specName) || isReturn;
595963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
59646341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    // Extract the vector size out of the type.
597963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    int last = rsType.size() - 1;
598963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    char lastChar = rsType[last];
599963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (lastChar >= '0' && lastChar <= '9') {
600963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        rsBaseType = rsType.substr(0, last);
601963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        mVectorSize = lastChar;
602963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    } else {
603963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        rsBaseType = rsType;
604963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        mVectorSize = "1";
605963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
606963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (mVectorSize == "3") {
607963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        vectorWidth = "4";
608963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    } else {
609963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        vectorWidth = mVectorSize;
610963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
611963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
612963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    /* Create variable names to be used in the java and .rs files.  Because x and
613963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     * y are reserved in .rs files, we prefix variable names with "in" or "out".
614963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet     */
615963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (isOutParameter) {
616963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        variableName = "out";
61746341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        if (!specName.empty()) {
61846341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            variableName += capitalize(specName);
619963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        } else if (!isReturn) {
620963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            variableName += toString(*outputCount);
621963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
622963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        (*outputCount)++;
623963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    } else {
624963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        variableName = "in";
62546341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        if (!specName.empty()) {
62646341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            variableName += capitalize(specName);
627963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        } else if (*inputCount > 0) {
628963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            variableName += toString(*inputCount);
629963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
630963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        (*inputCount)++;
631963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
632963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    rsAllocName = "gAlloc" + capitalize(variableName);
633963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    javaAllocName = variableName;
634963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    javaArrayName = "array" + capitalize(javaAllocName);
635963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
63646341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    // Process the option.
637bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    undefinedIfOutIsNan = false;
638bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    compatibleTypeIndex = -1;
63946341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    if (!option.empty()) {
64046341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        if (option.compare(0, 6, "range(") == 0) {
64146341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            size_t pComma = option.find(',');
64246341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            size_t pParen = option.find(')');
64346341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            if (pComma == string::npos || pParen == string::npos) {
64446341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet                printf("Incorrect range %s\n", option.c_str());
64546341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            } else {
64646341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet                minValue = option.substr(6, pComma - 6);
64746341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet                maxValue = option.substr(pComma + 1, pParen - pComma - 1);
64846341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            }
64946341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        } else if (option.compare(0, 6, "above(") == 0) {
65046341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            size_t pParen = option.find(')');
65146341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            if (pParen == string::npos) {
65246341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet                printf("Incorrect option %s\n", option.c_str());
65346341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            } else {
65446341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet                smallerParameter = option.substr(6, pParen - 6);
65546341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            }
656bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet        } else if (option.compare(0, 11, "compatible(") == 0) {
657bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet            size_t pParen = option.find(')');
658bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet            if (pParen == string::npos) {
659bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                printf("Incorrect option %s\n", option.c_str());
660bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet            } else {
661bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet                compatibleTypeIndex = FindCType(option.substr(11, pParen - 11));
662bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet            }
663bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet        } else if (option.compare(0, 11, "conditional") == 0) {
664bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet            undefinedIfOutIsNan = true;
66546341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        } else {
66646341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet            printf("Unrecognized option %s\n", option.c_str());
66746341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet        }
66846341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet    }
66946341434b4a766d9dee33a93469564a1de8b0793Jean-Luc Brouillet
670bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    typeIndex = FindCType(rsBaseType);
67146ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet    isFloatType = false;
672bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    if (typeIndex < 0) {
673bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet        // TODO set a global flag when we encounter an error & abort
674bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet        printf("Error, could not find %s\n", rsBaseType.c_str());
675bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet    } else {
676bcd5b9af756d10317faf54fa3742f89dfacef152Jean-Luc Brouillet        javaBaseType = TYPES[typeIndex].javaType;
67746ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet        specType = TYPES[typeIndex].specType;
67846ebc97e1ba04766f23296574ebde52102fbd4beJean-Luc Brouillet        isFloatType = TYPES[typeIndex].exponentBits > 0;
679963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
680963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
681963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
682963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool SpecFile::process(int versionOfTestFiles) {
683963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!readSpecFile()) {
684963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
685963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
686963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (versionOfTestFiles == 0) {
687963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        versionOfTestFiles = mLargestVersionNumber;
688963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
689963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!generateFiles(versionOfTestFiles)) {
690963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
691963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
692963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    printf("%s: %ld functions processed.\n", mSpecFileName.c_str(), mFunctionsMap.size());
693963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
694963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
695963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
696963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Read the specification, adding the definitions to the global functions map.
697963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool SpecFile::readSpecFile() {
698963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
699963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!specFile) {
700963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        printf("Error opening input file: %s\n", mSpecFileName.c_str());
701963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
702963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
703963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
704963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mLargestVersionNumber = 0;
705963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    while (1) {
706963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        Specification* spec = Specification::scanSpecification(specFile);
707963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (spec == NULL) {
708963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            break;
709963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
710963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        getFunction(spec->getCleanName())->addSpecification(spec);
711963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        int specMin = spec->getMinVersion();
712963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        int specMax = spec->getMaxVersion();
713963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (specMin && specMin > mLargestVersionNumber) {
714963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            mLargestVersionNumber = specMin;
715963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
716963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (specMax && specMax > mLargestVersionNumber) {
717963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            mLargestVersionNumber = specMax;
718963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
719963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
720963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
721963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    fclose(specFile);
722963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
723963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
724963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
725963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool SpecFile::generateFiles(int versionOfTestFiles) {
726963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    printf("%s: Generating test files for version %d\n", mSpecFileName.c_str(), versionOfTestFiles);
727963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
728963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // The header file name should have the same base but with a ".rsh" extension.
729963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string headerFileName = mSpecFileName;
730963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    size_t l = headerFileName.length();
731963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    const char SPEC[] = ".spec";
732963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    const int SPEC_SIZE = sizeof(SPEC) - 1;
733963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    const int start = l - SPEC_SIZE;
734963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (start >= 0 && headerFileName.compare(start, SPEC_SIZE, SPEC) == 0) {
735963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        headerFileName.erase(start);
736963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
737963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    headerFileName += ".rsh";
738963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
739963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Write the start of the header file.
740963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    ofstream headerFile;
741963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    headerFile.open(headerFileName.c_str(), ios::out | ios::trunc);
742963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!headerFile.is_open()) {
743963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        printf("Error opening output file: %s\n", headerFileName.c_str());
744963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
745963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
746963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    headerFile << LEGAL_NOTICE;
747963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    headerFile << AUTO_GENERATED_WARNING;
748963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    writeIfdef(headerFile, headerFileName, true);
749963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
750963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Write the functions to the header and test files.
751963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool success = writeAllFunctions(headerFile, versionOfTestFiles);
752963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
753963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // Finish the header file.
754963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    writeIfdef(headerFile, headerFileName, false);
755963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    headerFile.close();
756963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
757963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return success;
758963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
759963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
760963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Return the named function from the map.  Creates it if it's not there.
761963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc BrouilletFunction* SpecFile::getFunction(const string& name) {
762963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    FunctionsIterator iter = mFunctionsMap.find(name);
763963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (iter != mFunctionsMap.end()) {
764963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return iter->second;
765963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
766963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    Function* f = new Function(name);
767963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mFunctionsMap[name] = f;
768963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return f;
769963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
770963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
771963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool SpecFile::writeAllFunctions(ofstream& headerFile, int versionOfTestFiles) {
772963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool success = true;
773963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    for (FunctionsIterator iter = mFunctionsMap.begin(); iter != mFunctionsMap.end(); iter++) {
774963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        Function* func = iter->second;
775963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        if (!func->writeFiles(headerFile, versionOfTestFiles)) {
776963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet            success = false;
777963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        }
778963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
779963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return success;
780963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
781963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
782963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc BrouilletFunction::Function(const string& name) {
783963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mName = name;
784963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mCapitalizedName = capitalize(mName);
785963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mTestName = "Test" + mCapitalizedName;
786963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mRelaxedTestName = mTestName + "Relaxed";
787963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
788963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
789963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool Function::writeFiles(ofstream& headerFile, int versionOfTestFiles) {
790963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!startRsFile() || !startJavaFile() || !writeRelaxedRsFile()) {
791963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
792963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
793963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
794963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    for (SpecificationIterator i = mSpecifications.begin(); i < mSpecifications.end(); i++) {
795963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        (*i)->writeFiles(headerFile, mRsFile, mJavaFile, this, versionOfTestFiles);
796963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
797963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
798963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    finishJavaFile();
799963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    // There's no work to wrap-up in the .rs file.
800963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
801963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mRsFile.close();
802963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile.close();
803963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
804963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
805963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
806963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool Function::startRsFile() {
807963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string fileName = mTestName + ".rs";
808963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mRsFile.open(fileName.c_str(), ios::out | ios::trunc);
809963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!mRsFile.is_open()) {
810963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        printf("Error opening file: %s\n", fileName.c_str());
811963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
812963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
813963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mRsFile << LEGAL_NOTICE;
814963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mRsFile << "#pragma version(1)\n";
815963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mRsFile << "#pragma rs java_package_name(android.renderscript.cts)\n\n";
816963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mRsFile << AUTO_GENERATED_WARNING;
817963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
818963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
819963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
820963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Write an allocation definition if not already emitted in the .rs file.
821963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid Function::writeRsAllocationDefinition(const ParameterDefinition& param) {
822963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!testAndSet(param.rsAllocName, &mRsAllocationsGenerated)) {
823963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        mRsFile << "rs_allocation " << param.rsAllocName << ";\n";
824963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
825963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
826963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
827963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet// Write the entire *Relaxed.rs test file, as it only depends on the name.
828963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool Function::writeRelaxedRsFile() {
829963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string name = mRelaxedTestName + ".rs";
830963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    FILE* file = fopen(name.c_str(), "wt");
831963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!file) {
832963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        printf("Error opening file: %s\n", name.c_str());
833963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
834963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
835963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    fputs(LEGAL_NOTICE, file);
836963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string s;
837963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    s += "#include \"" + mTestName + ".rs\"\n";
838963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    s += "#pragma rs_fp_relaxed\n";
839963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    s += AUTO_GENERATED_WARNING;
840963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    fputs(s.c_str(), file);
841963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    fclose(file);
842963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
843963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
844963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
845963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletbool Function::startJavaFile() {
846963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    string fileName = mTestName + ".java";
847963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile.open(fileName.c_str(), ios::out | ios::trunc);
848963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!mJavaFile.is_open()) {
849963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        printf("Error opening file: %s\n", fileName.c_str());
850963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        return false;
851963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
852963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << LEGAL_NOTICE;
853963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << AUTO_GENERATED_WARNING;
854963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << "package android.renderscript.cts;\n\n";
855963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
856963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << "import android.renderscript.Allocation;\n";
857963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << "import android.renderscript.RSRuntimeException;\n";
858963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << "import android.renderscript.Element;\n\n";
859963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
860963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << "public class " << mTestName << " extends RSBaseCompute {\n\n";
861963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
862963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(1) << "private ScriptC_" << mTestName << " script;\n";
863963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(1) << "private ScriptC_" << mRelaxedTestName << " scriptRelaxed;\n\n";
864963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
865963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(1) << "@Override\n";
866963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(1) << "protected void setUp() throws Exception {\n";
867963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(2) << "super.setUp();\n";
868963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(2) << "script = new ScriptC_" << mTestName << "(mRS);\n";
869963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(2) << "scriptRelaxed = new ScriptC_" << mRelaxedTestName << "(mRS);\n";
870963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(1) << "}\n\n";
871963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    return true;
872963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
873963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
874963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid Function::writeJavaArgumentClassDefinition(const string& className, const string& definition) {
875963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    if (!testAndSet(className, &mJavaGeneratedArgumentClasses)) {
876963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        mJavaFile << definition;
877963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    }
878963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
879963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
880963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid Function::addJavaCheckCall(const string& call) {
881963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaCallAllCheckMethods += tab(2) + call + "\n";
882963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
883963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
884963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid Function::finishJavaFile() {
885963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(1) << "public void test" << mCapitalizedName << "() {\n";
886963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << mJavaCallAllCheckMethods;
887963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << tab(1) << "}\n";
888963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    mJavaFile << "}\n";
889963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet}
890963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet
891963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouilletvoid Specification::expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4,
892963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                                       vector<string>* out) const {
893963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    out->clear();
894963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
895963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet        out->push_back(expandString(*iter, i1, i2, i3, i4));
896135c4b747bce5898d98de964d7c453d215254d0dJason Sams    }
897135c4b747bce5898d98de964d7c453d215254d0dJason Sams}
898135c4b747bce5898d98de964d7c453d215254d0dJason Sams
899963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc BrouilletSpecification* Specification::scanSpecification(FILE* in) {
900963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    Specification* spec = new Specification();
901963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    spec->mTest = "scalar";  // default
902135c4b747bce5898d98de964d7c453d215254d0dJason Sams    bool modeComment = false;
903135c4b747bce5898d98de964d7c453d215254d0dJason Sams    bool modeInline = false;
904963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet    bool success = true;
905135c4b747bce5898d98de964d7c453d215254d0dJason Sams
906135c4b747bce5898d98de964d7c453d215254d0dJason Sams    while (1) {
907135c4b747bce5898d98de964d7c453d215254d0dJason Sams        string s;
908135c4b747bce5898d98de964d7c453d215254d0dJason Sams        bool ret = getNextLine(in, &s);
909135c4b747bce5898d98de964d7c453d215254d0dJason Sams        if (!ret) break;
910135c4b747bce5898d98de964d7c453d215254d0dJason Sams
911135c4b747bce5898d98de964d7c453d215254d0dJason Sams        if (modeComment) {
912135c4b747bce5898d98de964d7c453d215254d0dJason Sams            if (!s.size() || (s[0] == ' ')) {
913135c4b747bce5898d98de964d7c453d215254d0dJason Sams                trim(&s, 0);
914963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                spec->mComment.push_back(s);
915135c4b747bce5898d98de964d7c453d215254d0dJason Sams                continue;
916135c4b747bce5898d98de964d7c453d215254d0dJason Sams            } else {
917135c4b747bce5898d98de964d7c453d215254d0dJason Sams                modeComment = false;
918135c4b747bce5898d98de964d7c453d215254d0dJason Sams            }
919135c4b747bce5898d98de964d7c453d215254d0dJason Sams        }
920135c4b747bce5898d98de964d7c453d215254d0dJason Sams
921135c4b747bce5898d98de964d7c453d215254d0dJason Sams        if (modeInline) {
922135c4b747bce5898d98de964d7c453d215254d0dJason Sams            if (!s.size() || (s[0] == ' ')) {
923135c4b747bce5898d98de964d7c453d215254d0dJason Sams                trim(&s, 0);
924963c367450cd54403f4e54064da63c35aa7e0178Jean-Luc Brouillet                spec->mInline.push_back(s);
925135c4b747bce5898d98de964d7c453d215254d0dJason Sams                continue;
926135c4b747bce5898d98de964d7c453d215254d0dJason Sams            } else {
927135c4b747bce5898d98de964d7c453d215254d0dJason Sams                modeInline = false;
928135c4b747bce5898d98de964d7c453d215254d0dJason Sams            }
929135c4b747bce5898d98de964d7c453d215254d0dJason Sams        }
930135c4b747bce5898d98de964d7c453d215254d0dJason Sams
931135c4b747bce5898d98de964d7c453d215254d0dJason Sams        if (s[0] == '#') {
932135c4b747bce5898d98de964d7c453d215254d0dJason Sams            continue;
933135c4b747bce5898d98de964d7c453d215254d0dJason Sams        }
934135c4b747bce5898d98de964d7c453d215254d0dJason Sams
935135c4b747bce5898d98de964d7c453d215254d0dJason Sams        if (s.compare(0, 5, "name:") == 0) {
936135c4b747bce5898d98de964d7c453d215254d0dJason Sams            trim(&s, 5);
937