Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 17 | /* This program processes Renderscript function definitions described in spec files. |
| 18 | * For each spec file provided on the command line, it generates a corresponding |
| 19 | * Renderscript header (*.rsh) which is meant for inclusion in client scripts. |
| 20 | * |
| 21 | * This program also generates Junit test files to automatically test each of the |
| 22 | * functions using randomly generated data. We create two files for each function: |
| 23 | * - a Renderscript file named Test{Function}.rs, |
| 24 | * - a Junit file named Test{function}.java, which calls the above RS file. |
| 25 | * |
| 26 | * This program takes an optional -v parameter, the RS version to target the |
| 27 | * test files for. The header file will always contain all the functions. |
| 28 | * |
| 29 | * This program contains five main classes: |
| 30 | * - SpecFile: Represents on spec file. |
| 31 | * - Function: Each instance represents a function, like clamp. Even though the |
| 32 | * spec file contains many entries for clamp, we'll only have one clamp instance. |
| 33 | * - Specification: Defines one of the many variations of the function. There's |
| 34 | * a one to one correspondance between Specification objects and entries in the |
| 35 | * spec file. Strings that are parts of a Specification can include placeholders, |
| 36 | * which are "#1", "#2", "#3", and "#4". We'll replace these by values before |
| 37 | * generating the files. |
| 38 | * - Permutation: A concrete version of a specification, where all placeholders have |
| 39 | * been replaced by actual values. |
| 40 | * - ParameterDefinition: A definition of a parameter of a concrete function. |
| 41 | */ |
| 42 | |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 43 | #include <math.h> |
| 44 | #include <stdio.h> |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 45 | #include <cctype> |
| 46 | #include <cstdlib> |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 47 | #include <fstream> |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 48 | #include <functional> |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 49 | #include <iomanip> |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 50 | #include <list> |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 51 | #include <map> |
| 52 | #include <set> |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 53 | #include <sstream> |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 54 | #include <string> |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 55 | #include <vector> |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 56 | |
| 57 | using namespace std; |
| 58 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 59 | namespace { |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 60 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 61 | const char* AUTO_GENERATED_WARNING = |
| 62 | "// Don't edit this file! It is auto-generated by " |
| 63 | "frameworks/rs/api/gen_runtime.\n\n"; |
| 64 | const char* LEGAL_NOTICE = |
| 65 | "/*\n" |
| 66 | " * Copyright (C) 2014 The Android Open Source Project\n" |
| 67 | " *\n" |
| 68 | " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" |
| 69 | " * you may not use this file except in compliance with the License.\n" |
| 70 | " * You may obtain a copy of the License at\n" |
| 71 | " *\n" |
| 72 | " * http://www.apache.org/licenses/LICENSE-2.0\n" |
| 73 | " *\n" |
| 74 | " * Unless required by applicable law or agreed to in writing, software\n" |
| 75 | " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" |
| 76 | " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" |
| 77 | " * See the License for the specific language governing permissions and\n" |
| 78 | " * limitations under the License.\n" |
| 79 | " */\n\n"; |
| 80 | |
| 81 | class Function; |
| 82 | class Specification; |
| 83 | class Permutation; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 84 | struct Type; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 85 | |
| 86 | /* Information about a parameter to a function. The values of all the fields should only be set by |
| 87 | * parseParameterDefinition. |
| 88 | */ |
| 89 | struct ParameterDefinition { |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 90 | string rsType; // The Renderscript type, e.g. "uint3" |
| 91 | string rsBaseType; // As above but without the number, e.g. "uint" |
| 92 | string javaBaseType; // The type we need to declare in Java, e.g. "unsigned int" |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 93 | string specType; // The type found in the spec, e.g. "f16" |
| 94 | bool isFloatType; // True if it's a floating point value |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 95 | |
| 96 | /* The number of entries in the vector. It should be either "1", "2", "3", or "4". It's also |
| 97 | * "1" for scalars. |
| 98 | */ |
| 99 | string mVectorSize; |
| 100 | /* The space the vector takes in an array. It's the same as the vector size, except for size |
| 101 | * "3", where the width is "4". |
| 102 | */ |
| 103 | string vectorWidth; |
| 104 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 105 | string specName; // e.g. x, as found in the spec file |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 106 | string variableName; // e.g. inX, used both in .rs and .java |
| 107 | string rsAllocName; // e.g. gAllocInX |
| 108 | string javaAllocName; // e.g. inX |
| 109 | string javaArrayName; // e.g. arrayInX |
| 110 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 111 | // If non empty, the mininum and maximum values to be used when generating the test data. |
| 112 | string minValue; |
| 113 | string maxValue; |
| 114 | /* If non empty, contains the name of another parameter that should be smaller or equal to this |
| 115 | * parameter, i.e. value(smallerParameter) <= value(this). This is used when testing clamp. |
| 116 | */ |
| 117 | string smallerParameter; |
| 118 | |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 119 | bool isOutParameter; // True if this parameter returns data from the script. |
| 120 | bool undefinedIfOutIsNan; // If true, we don't validate if 'out' is NaN. |
| 121 | |
| 122 | int typeIndex; // Index in the TYPES array. |
| 123 | int compatibleTypeIndex; // Index in TYPES for which the test data must also fit. |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 124 | |
| 125 | /* Parse the parameter definition found in the spec file. It will generate a name if none |
| 126 | * are present in the file. One of the two counts will be incremented, and potentially |
| 127 | * used to generate unique names. isReturn is true if we're processing the "return:" |
| 128 | * definition. |
| 129 | */ |
| 130 | void parseParameterDefinition(string s, bool isReturn, int* inputCount, int* outputCount); |
| 131 | }; |
| 132 | |
| 133 | // An entire spec file and the methods to process it. |
| 134 | class SpecFile { |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 135 | public: |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 136 | explicit SpecFile(const string& specFileName) : mSpecFileName(specFileName) {} |
| 137 | bool process(int versionOfTestFiles); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 138 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 139 | private: |
| 140 | const string mSpecFileName; |
| 141 | // The largest version number that we have found in all the specifications. |
| 142 | int mLargestVersionNumber; |
| 143 | |
| 144 | map<string, Function*> mFunctionsMap; // All the known functions. |
| 145 | typedef map<string, Function*>::iterator FunctionsIterator; |
| 146 | |
| 147 | bool readSpecFile(); |
| 148 | Function* getFunction(const string& name); |
| 149 | bool generateFiles(int versionOfTestFiles); |
| 150 | bool writeAllFunctions(ofstream& headerFile, int versionOfTestFiles); |
| 151 | }; |
| 152 | |
| 153 | /* Represents a function, like "clamp". Even though the spec file contains many entries for clamp, |
| 154 | * we'll only have one clamp instance. |
| 155 | */ |
| 156 | class Function { |
| 157 | private: |
| 158 | string mName; // The lower case name, e.g. native_log |
| 159 | string mCapitalizedName; // The capitalized name, e.g. NativeLog |
| 160 | string mTestName; // e.g. TestNativeLog |
| 161 | string mRelaxedTestName; // e.g. TestNativeLogRelaxed |
| 162 | |
| 163 | vector<Specification*> mSpecifications; |
| 164 | typedef vector<Specification*>::iterator SpecificationIterator; |
| 165 | |
| 166 | /* We keep track of the allocations generated in the .rs file and the argument classes defined |
| 167 | * in the Java file, as we share these between the functions created for each specification. |
| 168 | */ |
| 169 | set<string> mRsAllocationsGenerated; |
| 170 | set<string> mJavaGeneratedArgumentClasses; |
| 171 | |
| 172 | string mJavaCallAllCheckMethods; // Lines of Java code to invoke the check methods. |
| 173 | |
| 174 | ofstream mRsFile; // The Renderscript test file we're generating. |
| 175 | ofstream mJavaFile; // The Jave test file we're generating. |
| 176 | |
| 177 | bool startRsFile(); // Open the mRsFile and writes its header. |
| 178 | bool writeRelaxedRsFile(); // Write the entire relaxed rs test file (an include essentially) |
| 179 | bool startJavaFile(); // Open the mJavaFile and writes the header. |
| 180 | void finishJavaFile(); // Write the test method and closes the file. |
| 181 | |
| 182 | public: |
| 183 | explicit Function(const string& name); |
| 184 | void addSpecification(Specification* spec) { mSpecifications.push_back(spec); } |
| 185 | /* Write the .java and the two .rs test files. versionOfTestFiles is used to restrict which API |
| 186 | * to test. Also writes the section of the header file. |
| 187 | */ |
| 188 | bool writeFiles(ofstream& headerFile, int versionOfTestFiles); |
| 189 | // Write an allocation and keep track of having it written, so it can be shared. |
| 190 | void writeRsAllocationDefinition(const ParameterDefinition& param); |
| 191 | // Write an argument class definiton and keep track of having it written, so it can be shared. |
| 192 | void writeJavaArgumentClassDefinition(const string& className, const string& definition); |
| 193 | // Add a call to mJavaCallAllCheckMethods to be used at the end of the file generation. |
| 194 | void addJavaCheckCall(const string& call); |
| 195 | }; |
| 196 | |
| 197 | /* Defines one of the many variations of the function. There's a one to one correspondance between |
| 198 | * Specification objects and entries in the spec file. Some of the strings that are parts of a |
| 199 | * Specification can include placeholders, which are "#1", "#2", "#3", and "#4". We'll replace |
| 200 | * these by values before generating the files. |
| 201 | */ |
| 202 | class Specification { |
| 203 | private: |
| 204 | /* The range of versions this specification applies to. 0 if there's no restriction, so an API |
| 205 | * that became available at 9 and is still valid would have min:9 max:0. |
| 206 | */ |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 207 | int mMinVersion; |
| 208 | int mMaxVersion; |
| 209 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 210 | /* The name of the function without #n, e.g. convert. As of this writing, it only differs for |
| 211 | * convert. |
| 212 | */ |
| 213 | string mCleanName; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 214 | string mTest; // How to test. One of "scalar", "vector", "noverify", "limited", and "none". |
| 215 | string mPrecisionLimit; // Maximum precision required when checking output of this function. |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 216 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 217 | vector<vector<string> > mReplaceables; |
| 218 | |
| 219 | // The following fields may contain placeholders that will be replaced using the mReplaceables. |
| 220 | |
| 221 | // The name of this function, can include #, e.g. convert_#1_#2 |
| 222 | string mName; |
| 223 | |
| 224 | string mReturn; // The return type |
| 225 | vector<string> mComment; // The comments to be included in the header |
| 226 | vector<string> mInline; // The inline code to be included in the header |
| 227 | vector<string> mParam; // One entry per parameter defined |
| 228 | |
| 229 | // Substitute the placeholders in the strings by the corresponding entries in mReplaceables. |
| 230 | string expandString(string s, int i1, int i2, int i3, int i4) const; |
| 231 | void expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4, |
| 232 | vector<string>* out) const; |
| 233 | |
| 234 | public: |
| 235 | Specification() { |
| 236 | mMinVersion = 0; |
| 237 | mMaxVersion = 0; |
| 238 | } |
| 239 | int getMinVersion() const { return mMinVersion; } |
| 240 | int getMaxVersion() const { return mMaxVersion; } |
| 241 | |
| 242 | string getName(int i1, int i2, int i3, int i4) const { |
| 243 | return expandString(mName, i1, i2, i3, i4); |
| 244 | } |
| 245 | string getReturn(int i1, int i2, int i3, int i4) const { |
| 246 | return expandString(mReturn, i1, i2, i3, i4); |
| 247 | } |
| 248 | void getComments(int i1, int i2, int i3, int i4, vector<string>* comments) const { |
| 249 | return expandStringVector(mComment, i1, i2, i3, i4, comments); |
| 250 | } |
| 251 | void getInlines(int i1, int i2, int i3, int i4, vector<string>* inlines) const { |
| 252 | return expandStringVector(mInline, i1, i2, i3, i4, inlines); |
| 253 | } |
| 254 | void getParams(int i1, int i2, int i3, int i4, vector<string>* params) const { |
| 255 | return expandStringVector(mParam, i1, i2, i3, i4, params); |
| 256 | } |
| 257 | string getTest() const { return mTest; } |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 258 | string getPrecisionLimit() const { return mPrecisionLimit; } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 259 | string getCleanName() const { return mCleanName; } |
| 260 | |
| 261 | void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile, Function* function, |
| 262 | int versionOfTestFiles); |
| 263 | bool writeRelaxedRsFile() const; |
| 264 | // Return true if this specification should be generated for this version. |
| 265 | bool relevantForVersion(int versionOfTestFiles) const; |
| 266 | |
| 267 | static Specification* scanSpecification(FILE* in); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 268 | }; |
| 269 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 270 | // A concrete version of a specification, where all placeholders have been replaced by actual |
| 271 | // values. |
| 272 | class Permutation { |
| 273 | private: |
| 274 | Function* mFunction; |
| 275 | Specification* mSpecification; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 276 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 277 | // These are the expanded version of those found on Specification |
| 278 | string mName; |
| 279 | string mCleanName; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 280 | string mTest; // How to test. One of "scalar", "vector", "noverify", "limited", and "none". |
| 281 | string mPrecisionLimit; // Maximum precision required when checking output of this function. |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 282 | vector<string> mInline; |
| 283 | vector<string> mComment; |
| 284 | |
| 285 | // The inputs and outputs of the function. This include the return type, if present. |
| 286 | vector<ParameterDefinition*> mParams; |
| 287 | // The index of the return value in mParams, -1 if the function is void. |
| 288 | int mReturnIndex; |
| 289 | // The index of the first input value in mParams, -1 if there's no input. |
| 290 | int mFirstInputIndex; |
| 291 | // The number of input and output parameters. |
| 292 | int mInputCount; |
| 293 | int mOutputCount; |
| 294 | |
| 295 | string mRsKernelName; |
| 296 | string mJavaArgumentsClassName; |
| 297 | string mJavaArgumentsNClassName; |
| 298 | string mJavaVerifierComputeMethodName; |
| 299 | string mJavaCheckMethodName; |
| 300 | string mJavaVerifyMethodName; |
| 301 | |
| 302 | void writeHeaderSection(ofstream& file) const; |
| 303 | |
| 304 | void writeRsSection(ofstream& rs) const; |
| 305 | |
| 306 | void writeJavaSection(ofstream& file) const; |
| 307 | void writeJavaArgumentClass(ofstream& file, bool scalar) const; |
| 308 | void writeJavaCheckMethod(ofstream& file, bool generateCallToVerify) const; |
| 309 | void writeJavaVerifyScalarMethod(ofstream& file) const; |
| 310 | void writeJavaVerifyVectorMethod(ofstream& file) const; |
| 311 | void writeJavaVerifyFunctionHeader(ofstream& file) const; |
| 312 | void writeJavaInputAllocationDefinition(ofstream& file, const string& indent, |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 313 | const ParameterDefinition& param) const; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 314 | void writeJavaOutputAllocationDefinition(ofstream& file, const string& indent, |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 315 | const ParameterDefinition& param) const; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 316 | // Write code to create a random allocation for which the data must be compatible for two types. |
| 317 | void writeJavaRandomCompatibleFloatAllocation(ofstream& file, const string& dataType, |
| 318 | const string& seed, char vectorSize, |
| 319 | const Type& compatibleType, |
| 320 | const Type& generatedType) const; |
| 321 | void writeJavaRandomCompatibleIntegerAllocation(ofstream& file, const string& dataType, |
| 322 | const string& seed, char vectorSize, |
| 323 | const Type& compatibleType, |
| 324 | const Type& generatedType) const; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 325 | void writeJavaCallToRs(ofstream& file, bool relaxed, bool generateCallToVerify) const; |
| 326 | |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 327 | void writeJavaTestAndSetValid(ofstream& file, int indent, const ParameterDefinition& p, |
| 328 | const string& argsIndex, const string& actualIndex) const; |
| 329 | void writeJavaTestOneValue(ofstream& file, int indent, const ParameterDefinition& p, |
| 330 | const string& argsIndex, const string& actualIndex) const; |
| 331 | void writeJavaAppendOutputToMessage(ofstream& file, int indent, const ParameterDefinition& p, |
| 332 | const string& argsIndex, const string& actualIndex) const; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 333 | void writeJavaAppendInputToMessage(ofstream& file, int indent, const ParameterDefinition& p, |
| 334 | const string& actual) const; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 335 | void writeJavaAppendNewLineToMessage(ofstream& file, int indent) const; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 336 | void writeJavaAppendVariableToMessage(ofstream& file, int indent, const ParameterDefinition& p, |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 337 | const string& value) const; |
| 338 | void writeJavaAppendFloatyVariableToMessage(ofstream& file, int indent, |
| 339 | const string& value) const; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 340 | void writeJavaVectorComparison(ofstream& file, int indent, const ParameterDefinition& p) const; |
| 341 | void writeJavaAppendVectorInputToMessage(ofstream& file, int indent, |
| 342 | const ParameterDefinition& p) const; |
| 343 | void writeJavaAppendVectorOutputToMessage(ofstream& file, int indent, |
| 344 | const ParameterDefinition& p) const; |
| 345 | bool passByAddressToSet(const string& name) const; |
| 346 | void convertToRsType(const string& name, string* dataType, char* vectorSize) const; |
| 347 | |
| 348 | public: |
| 349 | Permutation(Function* function, Specification* specification, int i1, int i2, int i3, int i4); |
| 350 | void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile, |
| 351 | int versionOfTestFiles); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 352 | }; |
| 353 | |
| 354 | // Table of type equivalences |
| 355 | // TODO: We should just be pulling this from a shared header. Slang does exactly the same thing. |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 356 | |
| 357 | enum NumberKind { SIGNED_INTEGER, UNSIGNED_INTEGER, FLOATING_POINT }; |
| 358 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 359 | struct Type { |
| 360 | const char* specType; // Name found in the .spec file |
| 361 | string rsDataType; // RS data type |
| 362 | string cType; // Type in a C file |
| 363 | const char* javaType; // Type in a Java file |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 364 | NumberKind kind; |
| 365 | /* For integers, number of bits of the number, excluding the sign bit. |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 366 | * For floats, number of implied bits of the mantissa. |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 367 | */ |
| 368 | int significantBits; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 369 | // For floats, number of bits of the exponent. 0 for integer types. |
| 370 | int exponentBits; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 371 | }; |
| 372 | |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 373 | const Type TYPES[] = {{"f16", "FLOAT_16", "half", "half", FLOATING_POINT, 11, 5}, |
| 374 | {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 24, 8}, |
| 375 | {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 53, 11}, |
| 376 | {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7, 0}, |
| 377 | {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8, 0}, |
| 378 | {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15, 0}, |
| 379 | {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16, 0}, |
| 380 | {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31, 0}, |
| 381 | {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32, 0}, |
| 382 | {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63, 0}, |
| 383 | {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64, 0}}; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 384 | |
| 385 | const int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]); |
| 386 | |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 387 | // Returns the index in TYPES for the provided cType |
| 388 | int FindCType(const string& cType) { |
| 389 | for (int i = 0; i < NUM_TYPES; i++) { |
| 390 | if (cType == TYPES[i].cType) { |
| 391 | return i; |
| 392 | } |
| 393 | } |
| 394 | return -1; |
| 395 | } |
| 396 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 397 | // Capitalizes and removes underscores. E.g. converts "native_log" to NativeLog. |
| 398 | string capitalize(const string& source) { |
| 399 | int length = source.length(); |
| 400 | string result; |
| 401 | bool capitalize = true; |
| 402 | for (int s = 0; s < length; s++) { |
| 403 | if (source[s] == '_') { |
| 404 | capitalize = true; |
| 405 | } else if (capitalize) { |
| 406 | result += toupper(source[s]); |
| 407 | capitalize = false; |
| 408 | } else { |
| 409 | result += source[s]; |
| 410 | } |
| 411 | } |
| 412 | return result; |
| 413 | } |
| 414 | |
| 415 | string tab(int n) { return string(n * 4, ' '); } |
| 416 | |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 417 | // Returns a string that's an hexadecimal constant fo the hash of the string. |
| 418 | string hashString(const string& s) { |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 419 | long hash = 0; |
| 420 | for (size_t i = 0; i < s.length(); i++) { |
| 421 | hash = hash * 43 + s[i]; |
| 422 | } |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 423 | stringstream stream; |
| 424 | stream << "0x" << std::hex << hash << "l"; |
| 425 | return stream.str(); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 428 | // Removes the character from present. Returns true if the string contained the character. |
| 429 | static bool charRemoved(char c, string* s) { |
| 430 | size_t p = s->find(c); |
| 431 | if (p != string::npos) { |
| 432 | s->erase(p, 1); |
| 433 | return true; |
| 434 | } |
| 435 | return false; |
| 436 | } |
| 437 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 438 | // Return true if the string is already in the set. Inserts it if not. |
| 439 | bool testAndSet(const string& flag, set<string>* set) { |
| 440 | if (set->find(flag) == set->end()) { |
| 441 | set->insert(flag); |
| 442 | return false; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 447 | // Convert an int into a string. |
| 448 | string toString(int n) { |
| 449 | char buf[100]; |
| 450 | snprintf(buf, sizeof(buf), "%d", n); |
| 451 | return string(buf); |
| 452 | } |
| 453 | |
| 454 | void trim(string* s, size_t start) { |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 455 | if (start > 0) { |
| 456 | s->erase(0, start); |
| 457 | } |
| 458 | |
| 459 | while (s->size() && (s->at(0) == ' ')) { |
| 460 | s->erase(0, 1); |
| 461 | } |
| 462 | |
| 463 | size_t p = s->find_first_of("\n\r"); |
| 464 | if (p != string::npos) { |
| 465 | s->erase(p); |
| 466 | } |
| 467 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 468 | while ((s->size() > 0) && (s->at(s->size() - 1) == ' ')) { |
| 469 | s->erase(s->size() - 1); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 473 | string stringReplace(string s, string match, string rep) { |
| 474 | while (1) { |
| 475 | size_t p = s.find(match); |
| 476 | if (p == string::npos) break; |
| 477 | |
| 478 | s.erase(p, match.size()); |
| 479 | s.insert(p, rep); |
| 480 | } |
| 481 | return s; |
| 482 | } |
| 483 | |
| 484 | // Return the next line from the input file. |
| 485 | bool getNextLine(FILE* in, string* s) { |
| 486 | s->clear(); |
| 487 | while (1) { |
| 488 | int c = fgetc(in); |
| 489 | if (c == EOF) return s->size() != 0; |
| 490 | if (c == '\n') break; |
| 491 | s->push_back((char)c); |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | void writeIfdef(ofstream& file, string filename, bool isStart) { |
| 497 | string t = "__"; |
| 498 | t += filename; |
| 499 | t += "__"; |
| 500 | |
| 501 | for (size_t i = 2; i < t.size(); i++) { |
| 502 | if (t[i] == '.') { |
| 503 | t[i] = '_'; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (isStart) { |
| 508 | file << "#ifndef " << t << "\n"; |
| 509 | file << "#define " << t << "\n"; |
| 510 | } else { |
| 511 | file << "#endif // " << t << "\n"; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void writeJavaArrayInitialization(ofstream& file, const ParameterDefinition& p) { |
| 516 | file << tab(2) << p.javaBaseType << "[] " << p.javaArrayName << " = new " << p.javaBaseType |
| 517 | << "[INPUTSIZE * " << p.vectorWidth << "];\n"; |
| 518 | file << tab(2) << p.javaAllocName << ".copyTo(" << p.javaArrayName << ");\n"; |
| 519 | } |
| 520 | |
| 521 | bool parseCommandLine(int argc, char* argv[], int* versionOfTestFiles, |
| 522 | vector<string>* specFileNames) { |
| 523 | for (int i = 1; i < argc; i++) { |
| 524 | if (argv[i][0] == '-') { |
| 525 | if (argv[i][1] == 'v') { |
| 526 | i++; |
| 527 | if (i < argc) { |
| 528 | char* end; |
| 529 | *versionOfTestFiles = strtol(argv[i], &end, 10); |
| 530 | if (*end != '\0') { |
| 531 | printf("Can't parse the version number %s\n", argv[i]); |
| 532 | return false; |
| 533 | } |
| 534 | } else { |
| 535 | printf("Missing version number after -v\n"); |
| 536 | return false; |
| 537 | } |
| 538 | } else { |
| 539 | printf("Unrecognized flag %s\n", argv[i]); |
| 540 | return false; |
| 541 | } |
| 542 | } else { |
| 543 | specFileNames->push_back(argv[i]); |
| 544 | } |
| 545 | } |
| 546 | if (specFileNames->size() == 0) { |
| 547 | printf("No spec file specified\n"); |
| 548 | return false; |
| 549 | } |
| 550 | return true; |
| 551 | } |
| 552 | |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 553 | /* Returns a double that should be able to be converted to an integer of size |
| 554 | * numberOfIntegerBits. |
| 555 | */ |
| 556 | static double MaxDoubleForInteger(int numberOfIntegerBits, int mantissaSize) { |
| 557 | /* Double has only 52 bits of precision (53 implied). So for longs, we want |
| 558 | * to create smaller values to avoid a round up. Same for floats and halfs. |
| 559 | */ |
| 560 | int lowZeroBits = max(0, numberOfIntegerBits - mantissaSize); |
| 561 | unsigned long l = (0xffffffffffffffff >> (64 - numberOfIntegerBits + lowZeroBits)) |
| 562 | << lowZeroBits; |
| 563 | return (double)l; |
| 564 | } |
| 565 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 566 | /* Parse a parameter definition. It's of the form "type [*][name]". The type |
| 567 | * is required. The name is optional. The * indicates it's an output |
| 568 | * parameter. We also pass the indexed of this parameter in the definition, so |
| 569 | * we can create names like in2, in3, etc. */ |
| 570 | void ParameterDefinition::parseParameterDefinition(string s, bool isReturn, int* inputCount, |
| 571 | int* outputCount) { |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 572 | istringstream stream(s); |
| 573 | string name, type, option; |
| 574 | stream >> rsType; |
| 575 | stream >> specName; |
| 576 | stream >> option; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 577 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 578 | // Determine if this is an output. |
| 579 | isOutParameter = charRemoved('*', &rsType) || charRemoved('*', &specName) || isReturn; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 580 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 581 | // Extract the vector size out of the type. |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 582 | int last = rsType.size() - 1; |
| 583 | char lastChar = rsType[last]; |
| 584 | if (lastChar >= '0' && lastChar <= '9') { |
| 585 | rsBaseType = rsType.substr(0, last); |
| 586 | mVectorSize = lastChar; |
| 587 | } else { |
| 588 | rsBaseType = rsType; |
| 589 | mVectorSize = "1"; |
| 590 | } |
| 591 | if (mVectorSize == "3") { |
| 592 | vectorWidth = "4"; |
| 593 | } else { |
| 594 | vectorWidth = mVectorSize; |
| 595 | } |
| 596 | |
| 597 | /* Create variable names to be used in the java and .rs files. Because x and |
| 598 | * y are reserved in .rs files, we prefix variable names with "in" or "out". |
| 599 | */ |
| 600 | if (isOutParameter) { |
| 601 | variableName = "out"; |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 602 | if (!specName.empty()) { |
| 603 | variableName += capitalize(specName); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 604 | } else if (!isReturn) { |
| 605 | variableName += toString(*outputCount); |
| 606 | } |
| 607 | (*outputCount)++; |
| 608 | } else { |
| 609 | variableName = "in"; |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 610 | if (!specName.empty()) { |
| 611 | variableName += capitalize(specName); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 612 | } else if (*inputCount > 0) { |
| 613 | variableName += toString(*inputCount); |
| 614 | } |
| 615 | (*inputCount)++; |
| 616 | } |
| 617 | rsAllocName = "gAlloc" + capitalize(variableName); |
| 618 | javaAllocName = variableName; |
| 619 | javaArrayName = "array" + capitalize(javaAllocName); |
| 620 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 621 | // Process the option. |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 622 | undefinedIfOutIsNan = false; |
| 623 | compatibleTypeIndex = -1; |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 624 | if (!option.empty()) { |
| 625 | if (option.compare(0, 6, "range(") == 0) { |
| 626 | size_t pComma = option.find(','); |
| 627 | size_t pParen = option.find(')'); |
| 628 | if (pComma == string::npos || pParen == string::npos) { |
| 629 | printf("Incorrect range %s\n", option.c_str()); |
| 630 | } else { |
| 631 | minValue = option.substr(6, pComma - 6); |
| 632 | maxValue = option.substr(pComma + 1, pParen - pComma - 1); |
| 633 | } |
| 634 | } else if (option.compare(0, 6, "above(") == 0) { |
| 635 | size_t pParen = option.find(')'); |
| 636 | if (pParen == string::npos) { |
| 637 | printf("Incorrect option %s\n", option.c_str()); |
| 638 | } else { |
| 639 | smallerParameter = option.substr(6, pParen - 6); |
| 640 | } |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 641 | } else if (option.compare(0, 11, "compatible(") == 0) { |
| 642 | size_t pParen = option.find(')'); |
| 643 | if (pParen == string::npos) { |
| 644 | printf("Incorrect option %s\n", option.c_str()); |
| 645 | } else { |
| 646 | compatibleTypeIndex = FindCType(option.substr(11, pParen - 11)); |
| 647 | } |
| 648 | } else if (option.compare(0, 11, "conditional") == 0) { |
| 649 | undefinedIfOutIsNan = true; |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 650 | } else { |
| 651 | printf("Unrecognized option %s\n", option.c_str()); |
| 652 | } |
| 653 | } |
| 654 | |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 655 | typeIndex = FindCType(rsBaseType); |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 656 | isFloatType = false; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 657 | if (typeIndex < 0) { |
| 658 | // TODO set a global flag when we encounter an error & abort |
| 659 | printf("Error, could not find %s\n", rsBaseType.c_str()); |
| 660 | } else { |
| 661 | javaBaseType = TYPES[typeIndex].javaType; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 662 | specType = TYPES[typeIndex].specType; |
| 663 | isFloatType = TYPES[typeIndex].exponentBits > 0; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | bool SpecFile::process(int versionOfTestFiles) { |
| 668 | if (!readSpecFile()) { |
| 669 | return false; |
| 670 | } |
| 671 | if (versionOfTestFiles == 0) { |
| 672 | versionOfTestFiles = mLargestVersionNumber; |
| 673 | } |
| 674 | if (!generateFiles(versionOfTestFiles)) { |
| 675 | return false; |
| 676 | } |
| 677 | printf("%s: %ld functions processed.\n", mSpecFileName.c_str(), mFunctionsMap.size()); |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | // Read the specification, adding the definitions to the global functions map. |
| 682 | bool SpecFile::readSpecFile() { |
| 683 | FILE* specFile = fopen(mSpecFileName.c_str(), "rt"); |
| 684 | if (!specFile) { |
| 685 | printf("Error opening input file: %s\n", mSpecFileName.c_str()); |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | mLargestVersionNumber = 0; |
| 690 | while (1) { |
| 691 | Specification* spec = Specification::scanSpecification(specFile); |
| 692 | if (spec == NULL) { |
| 693 | break; |
| 694 | } |
| 695 | getFunction(spec->getCleanName())->addSpecification(spec); |
| 696 | int specMin = spec->getMinVersion(); |
| 697 | int specMax = spec->getMaxVersion(); |
| 698 | if (specMin && specMin > mLargestVersionNumber) { |
| 699 | mLargestVersionNumber = specMin; |
| 700 | } |
| 701 | if (specMax && specMax > mLargestVersionNumber) { |
| 702 | mLargestVersionNumber = specMax; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | fclose(specFile); |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | bool SpecFile::generateFiles(int versionOfTestFiles) { |
| 711 | printf("%s: Generating test files for version %d\n", mSpecFileName.c_str(), versionOfTestFiles); |
| 712 | |
| 713 | // The header file name should have the same base but with a ".rsh" extension. |
| 714 | string headerFileName = mSpecFileName; |
| 715 | size_t l = headerFileName.length(); |
| 716 | const char SPEC[] = ".spec"; |
| 717 | const int SPEC_SIZE = sizeof(SPEC) - 1; |
| 718 | const int start = l - SPEC_SIZE; |
| 719 | if (start >= 0 && headerFileName.compare(start, SPEC_SIZE, SPEC) == 0) { |
| 720 | headerFileName.erase(start); |
| 721 | } |
| 722 | headerFileName += ".rsh"; |
| 723 | |
| 724 | // Write the start of the header file. |
| 725 | ofstream headerFile; |
| 726 | headerFile.open(headerFileName.c_str(), ios::out | ios::trunc); |
| 727 | if (!headerFile.is_open()) { |
| 728 | printf("Error opening output file: %s\n", headerFileName.c_str()); |
| 729 | return false; |
| 730 | } |
| 731 | headerFile << LEGAL_NOTICE; |
| 732 | headerFile << AUTO_GENERATED_WARNING; |
| 733 | writeIfdef(headerFile, headerFileName, true); |
| 734 | |
| 735 | // Write the functions to the header and test files. |
| 736 | bool success = writeAllFunctions(headerFile, versionOfTestFiles); |
| 737 | |
| 738 | // Finish the header file. |
| 739 | writeIfdef(headerFile, headerFileName, false); |
| 740 | headerFile.close(); |
| 741 | |
| 742 | return success; |
| 743 | } |
| 744 | |
| 745 | // Return the named function from the map. Creates it if it's not there. |
| 746 | Function* SpecFile::getFunction(const string& name) { |
| 747 | FunctionsIterator iter = mFunctionsMap.find(name); |
| 748 | if (iter != mFunctionsMap.end()) { |
| 749 | return iter->second; |
| 750 | } |
| 751 | Function* f = new Function(name); |
| 752 | mFunctionsMap[name] = f; |
| 753 | return f; |
| 754 | } |
| 755 | |
| 756 | bool SpecFile::writeAllFunctions(ofstream& headerFile, int versionOfTestFiles) { |
| 757 | bool success = true; |
| 758 | for (FunctionsIterator iter = mFunctionsMap.begin(); iter != mFunctionsMap.end(); iter++) { |
| 759 | Function* func = iter->second; |
| 760 | if (!func->writeFiles(headerFile, versionOfTestFiles)) { |
| 761 | success = false; |
| 762 | } |
| 763 | } |
| 764 | return success; |
| 765 | } |
| 766 | |
| 767 | Function::Function(const string& name) { |
| 768 | mName = name; |
| 769 | mCapitalizedName = capitalize(mName); |
| 770 | mTestName = "Test" + mCapitalizedName; |
| 771 | mRelaxedTestName = mTestName + "Relaxed"; |
| 772 | } |
| 773 | |
| 774 | bool Function::writeFiles(ofstream& headerFile, int versionOfTestFiles) { |
| 775 | if (!startRsFile() || !startJavaFile() || !writeRelaxedRsFile()) { |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | for (SpecificationIterator i = mSpecifications.begin(); i < mSpecifications.end(); i++) { |
| 780 | (*i)->writeFiles(headerFile, mRsFile, mJavaFile, this, versionOfTestFiles); |
| 781 | } |
| 782 | |
| 783 | finishJavaFile(); |
| 784 | // There's no work to wrap-up in the .rs file. |
| 785 | |
| 786 | mRsFile.close(); |
| 787 | mJavaFile.close(); |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | bool Function::startRsFile() { |
| 792 | string fileName = mTestName + ".rs"; |
| 793 | mRsFile.open(fileName.c_str(), ios::out | ios::trunc); |
| 794 | if (!mRsFile.is_open()) { |
| 795 | printf("Error opening file: %s\n", fileName.c_str()); |
| 796 | return false; |
| 797 | } |
| 798 | mRsFile << LEGAL_NOTICE; |
| 799 | mRsFile << "#pragma version(1)\n"; |
| 800 | mRsFile << "#pragma rs java_package_name(android.renderscript.cts)\n\n"; |
| 801 | mRsFile << AUTO_GENERATED_WARNING; |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | // Write an allocation definition if not already emitted in the .rs file. |
| 806 | void Function::writeRsAllocationDefinition(const ParameterDefinition& param) { |
| 807 | if (!testAndSet(param.rsAllocName, &mRsAllocationsGenerated)) { |
| 808 | mRsFile << "rs_allocation " << param.rsAllocName << ";\n"; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | // Write the entire *Relaxed.rs test file, as it only depends on the name. |
| 813 | bool Function::writeRelaxedRsFile() { |
| 814 | string name = mRelaxedTestName + ".rs"; |
| 815 | FILE* file = fopen(name.c_str(), "wt"); |
| 816 | if (!file) { |
| 817 | printf("Error opening file: %s\n", name.c_str()); |
| 818 | return false; |
| 819 | } |
| 820 | fputs(LEGAL_NOTICE, file); |
| 821 | string s; |
| 822 | s += "#include \"" + mTestName + ".rs\"\n"; |
| 823 | s += "#pragma rs_fp_relaxed\n"; |
| 824 | s += AUTO_GENERATED_WARNING; |
| 825 | fputs(s.c_str(), file); |
| 826 | fclose(file); |
| 827 | return true; |
| 828 | } |
| 829 | |
| 830 | bool Function::startJavaFile() { |
| 831 | string fileName = mTestName + ".java"; |
| 832 | mJavaFile.open(fileName.c_str(), ios::out | ios::trunc); |
| 833 | if (!mJavaFile.is_open()) { |
| 834 | printf("Error opening file: %s\n", fileName.c_str()); |
| 835 | return false; |
| 836 | } |
| 837 | mJavaFile << LEGAL_NOTICE; |
| 838 | mJavaFile << AUTO_GENERATED_WARNING; |
| 839 | mJavaFile << "package android.renderscript.cts;\n\n"; |
| 840 | |
| 841 | mJavaFile << "import android.renderscript.Allocation;\n"; |
| 842 | mJavaFile << "import android.renderscript.RSRuntimeException;\n"; |
| 843 | mJavaFile << "import android.renderscript.Element;\n\n"; |
| 844 | |
| 845 | mJavaFile << "public class " << mTestName << " extends RSBaseCompute {\n\n"; |
| 846 | |
| 847 | mJavaFile << tab(1) << "private ScriptC_" << mTestName << " script;\n"; |
| 848 | mJavaFile << tab(1) << "private ScriptC_" << mRelaxedTestName << " scriptRelaxed;\n\n"; |
| 849 | |
| 850 | mJavaFile << tab(1) << "@Override\n"; |
| 851 | mJavaFile << tab(1) << "protected void setUp() throws Exception {\n"; |
| 852 | mJavaFile << tab(2) << "super.setUp();\n"; |
| 853 | mJavaFile << tab(2) << "script = new ScriptC_" << mTestName << "(mRS);\n"; |
| 854 | mJavaFile << tab(2) << "scriptRelaxed = new ScriptC_" << mRelaxedTestName << "(mRS);\n"; |
| 855 | mJavaFile << tab(1) << "}\n\n"; |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | void Function::writeJavaArgumentClassDefinition(const string& className, const string& definition) { |
| 860 | if (!testAndSet(className, &mJavaGeneratedArgumentClasses)) { |
| 861 | mJavaFile << definition; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | void Function::addJavaCheckCall(const string& call) { |
| 866 | mJavaCallAllCheckMethods += tab(2) + call + "\n"; |
| 867 | } |
| 868 | |
| 869 | void Function::finishJavaFile() { |
| 870 | mJavaFile << tab(1) << "public void test" << mCapitalizedName << "() {\n"; |
| 871 | mJavaFile << mJavaCallAllCheckMethods; |
| 872 | mJavaFile << tab(1) << "}\n"; |
| 873 | mJavaFile << "}\n"; |
| 874 | } |
| 875 | |
| 876 | void Specification::expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4, |
| 877 | vector<string>* out) const { |
| 878 | out->clear(); |
| 879 | for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) { |
| 880 | out->push_back(expandString(*iter, i1, i2, i3, i4)); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | Specification* Specification::scanSpecification(FILE* in) { |
| 885 | Specification* spec = new Specification(); |
| 886 | spec->mTest = "scalar"; // default |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 887 | bool modeComment = false; |
| 888 | bool modeInline = false; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 889 | bool success = true; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 890 | |
| 891 | while (1) { |
| 892 | string s; |
| 893 | bool ret = getNextLine(in, &s); |
| 894 | if (!ret) break; |
| 895 | |
| 896 | if (modeComment) { |
| 897 | if (!s.size() || (s[0] == ' ')) { |
| 898 | trim(&s, 0); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 899 | spec->mComment.push_back(s); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 900 | continue; |
| 901 | } else { |
| 902 | modeComment = false; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | if (modeInline) { |
| 907 | if (!s.size() || (s[0] == ' ')) { |
| 908 | trim(&s, 0); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 909 | spec->mInline.push_back(s); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 910 | continue; |
| 911 | } else { |
| 912 | modeInline = false; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (s[0] == '#') { |
| 917 | continue; |
| 918 | } |
| 919 | |
| 920 | if (s.compare(0, 5, "name:") == 0) { |
| 921 | trim(&s, 5); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 922 | spec->mName = s; |
| 923 | // Some functions like convert have # part of the name. Truncate at that point. |
| 924 | size_t p = s.find('#'); |
| 925 | if (p != string::npos) { |
| 926 | if (p > 0 && s[p - 1] == '_') { |
| 927 | p--; |
| 928 | } |
| 929 | s.erase(p); |
| 930 | } |
| 931 | spec->mCleanName = s; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 932 | continue; |
| 933 | } |
| 934 | |
| 935 | if (s.compare(0, 4, "arg:") == 0) { |
| 936 | trim(&s, 4); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 937 | spec->mParam.push_back(s); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 938 | continue; |
| 939 | } |
| 940 | |
| 941 | if (s.compare(0, 4, "ret:") == 0) { |
| 942 | trim(&s, 4); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 943 | spec->mReturn = s; |
| 944 | continue; |
| 945 | } |
| 946 | |
| 947 | if (s.compare(0, 5, "test:") == 0) { |
| 948 | trim(&s, 5); |
| 949 | if (s == "scalar" || s == "vector" || s == "noverify" || s == "none") { |
| 950 | spec->mTest = s; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 951 | } else if (s.compare(0, 7, "limited") == 0) { |
| 952 | spec->mTest = "limited"; |
| 953 | if (s.compare(7, 1, "(") == 0) { |
| 954 | size_t pParen = s.find(')'); |
| 955 | if (pParen == string::npos) { |
| 956 | printf("Incorrect test %s\n", s.c_str()); |
| 957 | } else { |
| 958 | spec->mPrecisionLimit = s.substr(8, pParen - 8); |
| 959 | } |
| 960 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 961 | } else { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 962 | printf("Error: Unrecognized test option: %s\n", s.c_str()); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 963 | success = false; |
| 964 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 965 | continue; |
| 966 | } |
| 967 | |
| 968 | if (s.compare(0, 4, "end:") == 0) { |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 969 | if (success) { |
| 970 | return spec; |
| 971 | } else { |
| 972 | delete spec; |
| 973 | return NULL; |
| 974 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | if (s.compare(0, 8, "comment:") == 0) { |
| 978 | modeComment = true; |
| 979 | continue; |
| 980 | } |
| 981 | |
| 982 | if (s.compare(0, 7, "inline:") == 0) { |
| 983 | modeInline = true; |
| 984 | continue; |
| 985 | } |
| 986 | |
| 987 | if (s.compare(0, 8, "version:") == 0) { |
| 988 | trim(&s, 8); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 989 | sscanf(s.c_str(), "%i %i", &spec->mMinVersion, &spec->mMaxVersion); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 990 | continue; |
| 991 | } |
| 992 | |
| 993 | if (s.compare(0, 8, "start:") == 0) { |
| 994 | continue; |
| 995 | } |
| 996 | |
| 997 | if (s.compare(0, 2, "w:") == 0) { |
| 998 | vector<string> t; |
| 999 | if (s.find("1") != string::npos) { |
| 1000 | t.push_back(""); |
| 1001 | } |
| 1002 | if (s.find("2") != string::npos) { |
| 1003 | t.push_back("2"); |
| 1004 | } |
| 1005 | if (s.find("3") != string::npos) { |
| 1006 | t.push_back("3"); |
| 1007 | } |
| 1008 | if (s.find("4") != string::npos) { |
| 1009 | t.push_back("4"); |
| 1010 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1011 | spec->mReplaceables.push_back(t); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1012 | continue; |
| 1013 | } |
| 1014 | |
| 1015 | if (s.compare(0, 2, "t:") == 0) { |
| 1016 | vector<string> t; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1017 | for (int i = 0; i < NUM_TYPES; i++) { |
| 1018 | if (s.find(TYPES[i].specType) != string::npos) { |
| 1019 | t.push_back(TYPES[i].cType); |
| 1020 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1021 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1022 | spec->mReplaceables.push_back(t); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1023 | continue; |
| 1024 | } |
| 1025 | |
| 1026 | if (s.size() == 0) { |
| 1027 | // eat empty line |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | printf("Error, line:\n"); |
| 1032 | printf(" %s\n", s.c_str()); |
| 1033 | } |
| 1034 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1035 | delete spec; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1036 | return NULL; |
| 1037 | } |
| 1038 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1039 | void Specification::writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile, |
| 1040 | Function* function, int versionOfTestFiles) { |
| 1041 | int start[4]; |
| 1042 | int end[4]; |
| 1043 | for (int i = 0; i < 4; i++) { |
| 1044 | if (i < (int)mReplaceables.size()) { |
| 1045 | start[i] = 0; |
| 1046 | end[i] = mReplaceables[i].size(); |
| 1047 | } else { |
| 1048 | start[i] = -1; |
| 1049 | end[i] = 0; |
| 1050 | } |
| 1051 | } |
| 1052 | for (int i4 = start[3]; i4 < end[3]; i4++) { |
| 1053 | for (int i3 = start[2]; i3 < end[2]; i3++) { |
| 1054 | for (int i2 = start[1]; i2 < end[1]; i2++) { |
| 1055 | for (int i1 = start[0]; i1 < end[0]; i1++) { |
| 1056 | Permutation p(function, this, i1, i2, i3, i4); |
| 1057 | p.writeFiles(headerFile, rsFile, javaFile, versionOfTestFiles); |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1063 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1064 | bool Specification::relevantForVersion(int versionOfTestFiles) const { |
| 1065 | if (mMinVersion != 0 && mMinVersion > versionOfTestFiles) { |
| 1066 | return false; |
| 1067 | } |
| 1068 | if (mMaxVersion != 0 && mMaxVersion < versionOfTestFiles) { |
| 1069 | return false; |
| 1070 | } |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
| 1074 | string Specification::expandString(string s, int i1, int i2, int i3, int i4) const { |
| 1075 | if (mReplaceables.size() > 0) { |
| 1076 | s = stringReplace(s, "#1", mReplaceables[0][i1]); |
| 1077 | } |
| 1078 | if (mReplaceables.size() > 1) { |
| 1079 | s = stringReplace(s, "#2", mReplaceables[1][i2]); |
| 1080 | } |
| 1081 | if (mReplaceables.size() > 2) { |
| 1082 | s = stringReplace(s, "#3", mReplaceables[2][i3]); |
| 1083 | } |
| 1084 | if (mReplaceables.size() > 3) { |
| 1085 | s = stringReplace(s, "#4", mReplaceables[3][i4]); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1086 | } |
| 1087 | return s; |
| 1088 | } |
| 1089 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1090 | Permutation::Permutation(Function* func, Specification* spec, int i1, int i2, int i3, int i4) |
| 1091 | : mFunction(func), |
| 1092 | mSpecification(spec), |
| 1093 | mReturnIndex(-1), |
| 1094 | mFirstInputIndex(-1), |
| 1095 | mInputCount(0), |
| 1096 | mOutputCount(0) { |
| 1097 | // We expand the strings now to make capitalization easier. The previous code preserved the #n |
| 1098 | // markers just before emitting, which made capitalization difficult. |
| 1099 | mName = spec->getName(i1, i2, i3, i4); |
| 1100 | mCleanName = spec->getCleanName(); |
| 1101 | mTest = spec->getTest(); |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1102 | mPrecisionLimit = spec->getPrecisionLimit(); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1103 | spec->getInlines(i1, i2, i3, i4, &mInline); |
| 1104 | spec->getComments(i1, i2, i3, i4, &mComment); |
| 1105 | |
| 1106 | vector<string> paramDefinitions; |
| 1107 | spec->getParams(i1, i2, i3, i4, ¶mDefinitions); |
| 1108 | for (size_t i = 0; i < paramDefinitions.size(); i++) { |
| 1109 | ParameterDefinition* def = new ParameterDefinition(); |
| 1110 | def->parseParameterDefinition(paramDefinitions[i], false, &mInputCount, &mOutputCount); |
| 1111 | if (!def->isOutParameter && mFirstInputIndex < 0) { |
| 1112 | mFirstInputIndex = mParams.size(); |
| 1113 | } |
| 1114 | mParams.push_back(def); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1115 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1116 | |
| 1117 | const string s = spec->getReturn(i1, i2, i3, i4); |
| 1118 | if (!s.empty() && s != "void") { |
| 1119 | ParameterDefinition* def = new ParameterDefinition(); |
| 1120 | // Adding "*" tells the parse method it's an output. |
| 1121 | def->parseParameterDefinition(s, true, &mInputCount, &mOutputCount); |
| 1122 | mReturnIndex = mParams.size(); |
| 1123 | mParams.push_back(def); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1124 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1125 | |
| 1126 | mRsKernelName = "test" + capitalize(mName); |
| 1127 | mJavaArgumentsClassName = "Arguments"; |
| 1128 | mJavaArgumentsNClassName = "Arguments"; |
| 1129 | mJavaCheckMethodName = "check" + capitalize(mCleanName); |
| 1130 | mJavaVerifyMethodName = "verifyResults" + capitalize(mCleanName); |
| 1131 | for (int i = 0; i < (int)mParams.size(); i++) { |
| 1132 | const ParameterDefinition& p = *mParams[i]; |
| 1133 | mRsKernelName += capitalize(p.rsType); |
| 1134 | mJavaArgumentsClassName += capitalize(p.rsBaseType); |
| 1135 | mJavaArgumentsNClassName += capitalize(p.rsBaseType); |
| 1136 | if (p.mVectorSize != "1") { |
| 1137 | mJavaArgumentsNClassName += "N"; |
| 1138 | } |
| 1139 | mJavaCheckMethodName += capitalize(p.rsType); |
| 1140 | mJavaVerifyMethodName += capitalize(p.rsType); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1141 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1142 | mJavaVerifierComputeMethodName = "compute" + capitalize(mCleanName); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1145 | void Permutation::writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile, |
| 1146 | int versionOfTestFiles) { |
| 1147 | writeHeaderSection(headerFile); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1148 | if (mSpecification->relevantForVersion(versionOfTestFiles) && mTest != "none") { |
| 1149 | writeRsSection(rsFile); |
| 1150 | writeJavaSection(javaFile); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | void Permutation::writeHeaderSection(ofstream& file) const { |
| 1155 | int minVersion = mSpecification->getMinVersion(); |
| 1156 | int maxVersion = mSpecification->getMaxVersion(); |
| 1157 | bool hasVersion = minVersion || maxVersion; |
| 1158 | |
| 1159 | if (hasVersion) { |
| 1160 | if (maxVersion) { |
| 1161 | file << "#if (defined(RS_VERSION) && (RS_VERSION >= " << minVersion |
| 1162 | << ") && (RS_VERSION <= " << maxVersion << "))\n"; |
| 1163 | } else { |
| 1164 | file << "#if (defined(RS_VERSION) && (RS_VERSION >= " << minVersion << "))\n"; |
Jason Sams | ea877ed | 2014-01-09 15:48:32 -0800 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1168 | file << "/*\n"; |
| 1169 | for (size_t ct = 0; ct < mComment.size(); ct++) { |
| 1170 | if (!mComment[ct].empty()) { |
| 1171 | file << " * " << mComment[ct] << "\n"; |
| 1172 | } else { |
| 1173 | file << " *\n"; |
| 1174 | } |
Jason Sams | ea877ed | 2014-01-09 15:48:32 -0800 | [diff] [blame] | 1175 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1176 | file << " *\n"; |
| 1177 | if (minVersion || maxVersion) { |
| 1178 | if (maxVersion) { |
| 1179 | file << " * Suppored by API versions " << minVersion << " - " << maxVersion << "\n"; |
| 1180 | } else { |
| 1181 | file << " * Supported by API versions " << minVersion << " and newer.\n"; |
| 1182 | } |
| 1183 | } |
| 1184 | file << " */\n"; |
| 1185 | if (mInline.size() > 0) { |
| 1186 | file << "static "; |
| 1187 | } else { |
| 1188 | file << "extern "; |
| 1189 | } |
| 1190 | if (mReturnIndex >= 0) { |
| 1191 | file << mParams[mReturnIndex]->rsType; |
| 1192 | } else { |
| 1193 | file << "void"; |
| 1194 | } |
| 1195 | file << " __attribute__(("; |
| 1196 | if (mOutputCount <= 1) { |
| 1197 | file << "const, "; |
| 1198 | } |
| 1199 | file << "overloadable))"; |
| 1200 | file << mName; |
| 1201 | file << "("; |
| 1202 | bool needComma = false; |
| 1203 | for (int i = 0; i < (int)mParams.size(); i++) { |
| 1204 | if (i != mReturnIndex) { |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1205 | const ParameterDefinition& p = *mParams[i]; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1206 | if (needComma) { |
| 1207 | file << ", "; |
| 1208 | } |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1209 | file << p.rsType; |
| 1210 | if (p.isOutParameter) { |
| 1211 | file << "*"; |
| 1212 | } |
| 1213 | if (!p.specName.empty()) { |
| 1214 | file << " " << p.specName; |
| 1215 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1216 | needComma = true; |
| 1217 | } |
| 1218 | } |
| 1219 | if (mInline.size() > 0) { |
| 1220 | file << ") {\n"; |
| 1221 | for (size_t ct = 0; ct < mInline.size(); ct++) { |
| 1222 | file << " " << mInline[ct].c_str() << "\n"; |
| 1223 | } |
| 1224 | file << "}\n"; |
| 1225 | } else { |
| 1226 | file << ");\n"; |
| 1227 | } |
| 1228 | if (hasVersion) { |
| 1229 | file << "#endif\n"; |
| 1230 | } |
| 1231 | file << "\n"; |
Jason Sams | ea877ed | 2014-01-09 15:48:32 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1234 | /* Write the section of the .rs file for this permutation. |
| 1235 | * |
| 1236 | * We communicate the extra input and output parameters via global allocations. |
| 1237 | * For example, if we have a function that takes three arguments, two for input |
| 1238 | * and one for output: |
| 1239 | * |
| 1240 | * start: |
| 1241 | * name: gamn |
| 1242 | * ret: float3 |
| 1243 | * arg: float3 a |
| 1244 | * arg: int b |
| 1245 | * arg: float3 *c |
| 1246 | * end: |
| 1247 | * |
| 1248 | * We'll produce: |
| 1249 | * |
| 1250 | * rs_allocation gAllocInB; |
| 1251 | * rs_allocation gAllocOutC; |
| 1252 | * |
| 1253 | * float3 __attribute__((kernel)) test_gamn_float3_int_float3(float3 inA, unsigned int x) { |
| 1254 | * int inB; |
| 1255 | * float3 outC; |
| 1256 | * float2 out; |
| 1257 | * inB = rsGetElementAt_int(gAllocInB, x); |
| 1258 | * out = gamn(a, in_b, &outC); |
| 1259 | * rsSetElementAt_float4(gAllocOutC, &outC, x); |
| 1260 | * return out; |
| 1261 | * } |
| 1262 | * |
| 1263 | * We avoid re-using x and y from the definition because these have reserved |
| 1264 | * meanings in a .rs file. |
| 1265 | */ |
| 1266 | void Permutation::writeRsSection(ofstream& rs) const { |
| 1267 | // Write the allocation declarations we'll need. |
| 1268 | for (int i = 0; i < (int)mParams.size(); i++) { |
| 1269 | const ParameterDefinition& p = *mParams[i]; |
| 1270 | // Don't need allocation for one input and one return value. |
| 1271 | if (i != mReturnIndex && i != mFirstInputIndex) { |
| 1272 | mFunction->writeRsAllocationDefinition(p); |
| 1273 | } |
| 1274 | } |
| 1275 | rs << "\n"; |
Jason Sams | ea877ed | 2014-01-09 15:48:32 -0800 | [diff] [blame] | 1276 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1277 | // Write the function header. |
| 1278 | if (mReturnIndex >= 0) { |
| 1279 | rs << mParams[mReturnIndex]->rsType; |
| 1280 | } else { |
| 1281 | rs << "void"; |
| 1282 | } |
| 1283 | rs << " __attribute__((kernel)) " << mRsKernelName; |
| 1284 | rs << "("; |
| 1285 | bool needComma = false; |
| 1286 | if (mFirstInputIndex >= 0) { |
| 1287 | rs << mParams[mFirstInputIndex]->rsType << " " << mParams[mFirstInputIndex]->variableName; |
| 1288 | needComma = true; |
| 1289 | } |
| 1290 | if (mOutputCount > 1 || mInputCount > 1) { |
| 1291 | if (needComma) { |
| 1292 | rs << ", "; |
| 1293 | } |
| 1294 | rs << "unsigned int x"; |
| 1295 | } |
| 1296 | rs << ") {\n"; |
Jason Sams | ea877ed | 2014-01-09 15:48:32 -0800 | [diff] [blame] | 1297 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1298 | // Write the local variable declarations and initializations. |
| 1299 | for (int i = 0; i < (int)mParams.size(); i++) { |
| 1300 | if (i == mFirstInputIndex || i == mReturnIndex) { |
| 1301 | continue; |
| 1302 | } |
| 1303 | const ParameterDefinition& p = *mParams[i]; |
| 1304 | rs << tab(1) << p.rsType << " " << p.variableName; |
| 1305 | if (p.isOutParameter) { |
| 1306 | rs << " = 0;\n"; |
| 1307 | } else { |
| 1308 | rs << " = rsGetElementAt_" << p.rsType << "(" << p.rsAllocName << ", x);\n"; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | // Write the function call. |
| 1313 | if (mReturnIndex >= 0) { |
| 1314 | if (mOutputCount > 1) { |
| 1315 | rs << tab(1) << mParams[mReturnIndex]->rsType << " " |
| 1316 | << mParams[mReturnIndex]->variableName << " = "; |
| 1317 | } else { |
| 1318 | rs << tab(1) << "return "; |
| 1319 | } |
| 1320 | } |
| 1321 | rs << mName << "("; |
| 1322 | needComma = false; |
| 1323 | for (int i = 0; i < (int)mParams.size(); i++) { |
| 1324 | const ParameterDefinition& p = *mParams[i]; |
| 1325 | if (i == mReturnIndex) { |
| 1326 | continue; |
| 1327 | } |
| 1328 | if (needComma) { |
| 1329 | rs << ", "; |
| 1330 | } |
| 1331 | if (p.isOutParameter) { |
| 1332 | rs << "&"; |
| 1333 | } |
| 1334 | rs << p.variableName; |
| 1335 | needComma = true; |
| 1336 | } |
| 1337 | rs << ");\n"; |
| 1338 | |
| 1339 | if (mOutputCount > 1) { |
| 1340 | // Write setting the extra out parameters into the allocations. |
| 1341 | for (int i = 0; i < (int)mParams.size(); i++) { |
| 1342 | const ParameterDefinition& p = *mParams[i]; |
| 1343 | if (p.isOutParameter && i != mReturnIndex) { |
| 1344 | rs << tab(1) << "rsSetElementAt_" << p.rsType << "(" << p.rsAllocName << ", "; |
| 1345 | if (passByAddressToSet(p.variableName)) { |
| 1346 | rs << "&"; |
| 1347 | } |
| 1348 | rs << p.variableName << ", x);\n"; |
| 1349 | } |
| 1350 | } |
| 1351 | if (mReturnIndex >= 0) { |
| 1352 | rs << tab(1) << "return " << mParams[mReturnIndex]->variableName << ";\n"; |
| 1353 | } |
| 1354 | } |
| 1355 | rs << "}\n"; |
| 1356 | } |
| 1357 | |
| 1358 | bool Permutation::passByAddressToSet(const string& name) const { |
| 1359 | string s = name; |
| 1360 | int last = s.size() - 1; |
| 1361 | char lastChar = s[last]; |
| 1362 | return lastChar >= '0' && lastChar <= '9'; |
| 1363 | } |
| 1364 | |
| 1365 | void Permutation::writeJavaSection(ofstream& file) const { |
| 1366 | // By default, we test the results using item by item comparison. |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1367 | if (mTest == "scalar" || mTest == "limited") { |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1368 | writeJavaArgumentClass(file, true); |
| 1369 | writeJavaCheckMethod(file, true); |
| 1370 | writeJavaVerifyScalarMethod(file); |
| 1371 | } else if (mTest == "vector") { |
| 1372 | writeJavaArgumentClass(file, false); |
| 1373 | writeJavaCheckMethod(file, true); |
| 1374 | writeJavaVerifyVectorMethod(file); |
| 1375 | } else if (mTest == "noverify") { |
| 1376 | writeJavaCheckMethod(file, false); |
| 1377 | } |
| 1378 | |
| 1379 | // Register the check method to be called. This code will be written at the end. |
| 1380 | mFunction->addJavaCheckCall(mJavaCheckMethodName + "();"); |
| 1381 | } |
| 1382 | |
| 1383 | void Permutation::writeJavaArgumentClass(ofstream& file, bool scalar) const { |
| 1384 | string name; |
| 1385 | if (scalar) { |
| 1386 | name = mJavaArgumentsClassName; |
| 1387 | } else { |
| 1388 | name = mJavaArgumentsNClassName; |
| 1389 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1390 | string s; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1391 | s += tab(1) + "public class " + name + " {\n"; |
| 1392 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1393 | const ParameterDefinition& p = *mParams[i]; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1394 | s += tab(2) + "public "; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1395 | if (p.isOutParameter && p.isFloatType) { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1396 | s += "Floaty"; |
| 1397 | } else { |
| 1398 | s += p.javaBaseType; |
| 1399 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1400 | if (!scalar && p.mVectorSize != "1") { |
| 1401 | s += "[]"; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1402 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1403 | s += " " + p.variableName + ";\n"; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1404 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1405 | s += tab(1) + "}\n\n"; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1406 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1407 | mFunction->writeJavaArgumentClassDefinition(name, s); |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1410 | void Permutation::writeJavaCheckMethod(ofstream& file, bool generateCallToVerify) const { |
| 1411 | file << tab(1) << "private void " << mJavaCheckMethodName << "() {\n"; |
| 1412 | // Generate the input allocations and initialization. |
| 1413 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1414 | const ParameterDefinition& p = *mParams[i]; |
| 1415 | if (!p.isOutParameter) { |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1416 | writeJavaInputAllocationDefinition(file, tab(2), p); |
| 1417 | } |
| 1418 | } |
| 1419 | // Enforce ordering if needed. |
| 1420 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1421 | const ParameterDefinition& p = *mParams[i]; |
| 1422 | if (!p.isOutParameter && !p.smallerParameter.empty()) { |
| 1423 | string smallerAlloc = "in" + capitalize(p.smallerParameter); |
| 1424 | file << tab(2) << "enforceOrdering(" << smallerAlloc << ", " << p.javaAllocName |
| 1425 | << ");\n"; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1426 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1427 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1428 | writeJavaCallToRs(file, false, generateCallToVerify); |
| 1429 | writeJavaCallToRs(file, true, generateCallToVerify); |
| 1430 | file << tab(1) << "}\n\n"; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1433 | void Permutation::writeJavaInputAllocationDefinition(ofstream& file, const string& indent, |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1434 | const ParameterDefinition& param) const { |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1435 | string dataType; |
| 1436 | char vectorSize; |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1437 | convertToRsType(param.rsType, &dataType, &vectorSize); |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1438 | |
| 1439 | string seed = hashString(mJavaCheckMethodName + param.javaAllocName); |
| 1440 | file << indent << "Allocation " << param.javaAllocName << " = "; |
| 1441 | if (param.compatibleTypeIndex >= 0) { |
| 1442 | if (TYPES[param.typeIndex].kind == FLOATING_POINT) { |
| 1443 | writeJavaRandomCompatibleFloatAllocation(file, dataType, seed, vectorSize, |
| 1444 | TYPES[param.compatibleTypeIndex], |
| 1445 | TYPES[param.typeIndex]); |
| 1446 | } else { |
| 1447 | writeJavaRandomCompatibleIntegerAllocation(file, dataType, seed, vectorSize, |
| 1448 | TYPES[param.compatibleTypeIndex], |
| 1449 | TYPES[param.typeIndex]); |
| 1450 | } |
| 1451 | } else if (!param.minValue.empty()) { |
| 1452 | if (TYPES[param.typeIndex].kind != FLOATING_POINT) { |
| 1453 | printf("range(,) is only supported for floating point\n"); |
| 1454 | } else { |
| 1455 | file << "createRandomFloatAllocation(mRS, Element.DataType." << dataType << ", " |
| 1456 | << vectorSize << ", " << seed << ", " << param.minValue << ", " << param.maxValue |
| 1457 | << ")"; |
| 1458 | } |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1459 | } else { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1460 | file << "createRandomAllocation(mRS, Element.DataType." << dataType << ", " << vectorSize |
| 1461 | << ", " << seed << ", false)"; // TODO set to false only for native |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1462 | } |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1463 | file << ";\n"; |
| 1464 | } |
| 1465 | |
| 1466 | void Permutation::writeJavaRandomCompatibleFloatAllocation(ofstream& file, const string& dataType, |
| 1467 | const string& seed, char vectorSize, |
| 1468 | const Type& compatibleType, |
| 1469 | const Type& generatedType) const { |
| 1470 | file << "createRandomFloatAllocation" |
| 1471 | << "(mRS, Element.DataType." << dataType << ", " << vectorSize << ", " << seed << ", "; |
Jean-Luc Brouillet | 2f281cf | 2014-03-12 18:25:33 -0700 | [diff] [blame] | 1472 | double minValue = 0.0; |
| 1473 | double maxValue = 0.0; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1474 | switch (compatibleType.kind) { |
| 1475 | case FLOATING_POINT: { |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1476 | // We're generating floating point values. We just worry about the exponent. |
| 1477 | // Subtract 1 for the exponent sign. |
| 1478 | int bits = min(compatibleType.exponentBits, generatedType.exponentBits) - 1; |
Jean-Luc Brouillet | 2f281cf | 2014-03-12 18:25:33 -0700 | [diff] [blame] | 1479 | maxValue = ldexp(0.95, (1 << bits) - 1); |
| 1480 | minValue = -maxValue; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1481 | break; |
| 1482 | } |
| 1483 | case UNSIGNED_INTEGER: |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1484 | maxValue = MaxDoubleForInteger(compatibleType.significantBits, |
| 1485 | generatedType.significantBits); |
Jean-Luc Brouillet | 2f281cf | 2014-03-12 18:25:33 -0700 | [diff] [blame] | 1486 | minValue = 0.0; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1487 | break; |
Jean-Luc Brouillet | 2f281cf | 2014-03-12 18:25:33 -0700 | [diff] [blame] | 1488 | case SIGNED_INTEGER: |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1489 | maxValue = MaxDoubleForInteger(compatibleType.significantBits, |
| 1490 | generatedType.significantBits); |
| 1491 | minValue = -maxValue - 1.0; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1492 | break; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1493 | } |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1494 | file << scientific << std::setprecision(19); |
Jean-Luc Brouillet | 2f281cf | 2014-03-12 18:25:33 -0700 | [diff] [blame] | 1495 | file << minValue << ", " << maxValue << ")"; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1496 | file.unsetf(ios_base::floatfield); |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | void Permutation::writeJavaRandomCompatibleIntegerAllocation(ofstream& file, const string& dataType, |
| 1500 | const string& seed, char vectorSize, |
| 1501 | const Type& compatibleType, |
| 1502 | const Type& generatedType) const { |
| 1503 | file << "createRandomIntegerAllocation" |
| 1504 | << "(mRS, Element.DataType." << dataType << ", " << vectorSize << ", " << seed << ", "; |
| 1505 | |
| 1506 | if (compatibleType.kind == FLOATING_POINT) { |
| 1507 | // Currently, all floating points can take any number we generate. |
| 1508 | bool isSigned = generatedType.kind == SIGNED_INTEGER; |
| 1509 | file << (isSigned ? "true" : "false") << ", " << generatedType.significantBits; |
| 1510 | } else { |
| 1511 | bool isSigned = |
| 1512 | compatibleType.kind == SIGNED_INTEGER && generatedType.kind == SIGNED_INTEGER; |
| 1513 | file << (isSigned ? "true" : "false") << ", " |
| 1514 | << min(compatibleType.significantBits, generatedType.significantBits); |
| 1515 | } |
| 1516 | file << ")"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1517 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1518 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1519 | void Permutation::writeJavaOutputAllocationDefinition(ofstream& file, const string& indent, |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1520 | const ParameterDefinition& param) const { |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1521 | string dataType; |
| 1522 | char vectorSize; |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1523 | convertToRsType(param.rsType, &dataType, &vectorSize); |
| 1524 | file << indent << "Allocation " << param.javaAllocName << " = Allocation.createSized(mRS, " |
| 1525 | << "getElement(mRS, Element.DataType." << dataType << ", " << vectorSize |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1526 | << "), INPUTSIZE);\n"; |
| 1527 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1528 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1529 | // Converts float2 to FLOAT_32 and 2, etc. |
| 1530 | void Permutation::convertToRsType(const string& name, string* dataType, char* vectorSize) const { |
| 1531 | string s = name; |
| 1532 | int last = s.size() - 1; |
| 1533 | char lastChar = s[last]; |
| 1534 | if (lastChar >= '1' && lastChar <= '4') { |
| 1535 | s.erase(last); |
| 1536 | *vectorSize = lastChar; |
| 1537 | } else { |
| 1538 | *vectorSize = '1'; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1539 | } |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1540 | dataType->clear(); |
| 1541 | for (int i = 0; i < NUM_TYPES; i++) { |
| 1542 | if (s == TYPES[i].cType) { |
| 1543 | *dataType = TYPES[i].rsDataType; |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1544 | break; |
| 1545 | } |
| 1546 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1549 | void Permutation::writeJavaVerifyScalarMethod(ofstream& file) const { |
| 1550 | writeJavaVerifyFunctionHeader(file); |
| 1551 | string vectorSize = "1"; |
| 1552 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1553 | const ParameterDefinition& p = *mParams[i]; |
| 1554 | writeJavaArrayInitialization(file, p); |
| 1555 | if (p.mVectorSize != "1" && p.mVectorSize != vectorSize) { |
| 1556 | if (vectorSize == "1") { |
| 1557 | vectorSize = p.mVectorSize; |
| 1558 | } else { |
| 1559 | printf("Yikes, had vector %s and %s\n", vectorSize.c_str(), p.mVectorSize.c_str()); |
| 1560 | } |
| 1561 | } |
| 1562 | } |
Jason Sams | 135c4b7 | 2013-12-11 18:24:45 -0800 | [diff] [blame] | 1563 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1564 | file << tab(2) << "for (int i = 0; i < INPUTSIZE; i++) {\n"; |
| 1565 | file << tab(3) << "for (int j = 0; j < " << vectorSize << " ; j++) {\n"; |
| 1566 | |
| 1567 | file << tab(4) << "// Extract the inputs.\n"; |
| 1568 | file << tab(4) << mJavaArgumentsClassName << " args = new " << mJavaArgumentsClassName |
| 1569 | << "();\n"; |
| 1570 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1571 | const ParameterDefinition& p = *mParams[i]; |
| 1572 | if (!p.isOutParameter) { |
| 1573 | file << tab(4) << "args." << p.variableName << " = " << p.javaArrayName << "[i"; |
| 1574 | if (p.vectorWidth != "1") { |
| 1575 | file << " * " << p.vectorWidth << " + j"; |
| 1576 | } |
| 1577 | file << "];\n"; |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | file << tab(4) << "// Figure out what the outputs should have been.\n"; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1582 | file << tab(4) << "Floaty.setRelaxed(relaxed);\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1583 | file << tab(4) << "CoreMathVerifier." << mJavaVerifierComputeMethodName << "(args);\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1584 | |
| 1585 | file << tab(4) << "// Figure out what the outputs should have been.\n"; |
| 1586 | file << tab(4) << "boolean valid = true;\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1587 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1588 | const ParameterDefinition& p = *mParams[i]; |
| 1589 | if (p.isOutParameter) { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1590 | writeJavaTestAndSetValid(file, 4, p, "", "[i * " + p.vectorWidth + " + j]"); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | file << tab(4) << "if (!valid) {\n"; |
| 1595 | file << tab(5) << "StringBuilder message = new StringBuilder();\n"; |
| 1596 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1597 | const ParameterDefinition& p = *mParams[i]; |
| 1598 | if (p.isOutParameter) { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1599 | writeJavaAppendOutputToMessage(file, 5, p, "", "[i * " + p.vectorWidth + " + j]"); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1600 | } else { |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1601 | writeJavaAppendInputToMessage(file, 5, p, "args." + p.variableName); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1602 | } |
| 1603 | } |
| 1604 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1605 | file << tab(5) << "assertTrue(\"Incorrect output for " << mJavaCheckMethodName << "\" +\n"; |
| 1606 | file << tab(7) << "(relaxed ? \"_relaxed\" : \"\") + \":\\n\" + message.toString(), valid);\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1607 | file << tab(4) << "}\n"; |
| 1608 | file << tab(3) << "}\n"; |
| 1609 | file << tab(2) << "}\n"; |
| 1610 | file << tab(1) << "}\n\n"; |
| 1611 | } |
| 1612 | |
| 1613 | void Permutation::writeJavaVerifyFunctionHeader(ofstream& file) const { |
| 1614 | file << tab(1) << "private void " << mJavaVerifyMethodName << "("; |
| 1615 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1616 | const ParameterDefinition& p = *mParams[i]; |
| 1617 | file << "Allocation " << p.javaAllocName << ", "; |
| 1618 | } |
| 1619 | file << "boolean relaxed) {\n"; |
| 1620 | } |
| 1621 | |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1622 | void Permutation::writeJavaTestAndSetValid(ofstream& file, int indent, const ParameterDefinition& p, |
| 1623 | const string& argsIndex, |
| 1624 | const string& actualIndex) const { |
| 1625 | writeJavaTestOneValue(file, indent, p, argsIndex, actualIndex); |
| 1626 | file << tab(indent + 1) << "valid = false;\n"; |
| 1627 | file << tab(indent) << "}\n"; |
| 1628 | } |
| 1629 | |
| 1630 | void Permutation::writeJavaTestOneValue(ofstream& file, int indent, const ParameterDefinition& p, |
| 1631 | const string& argsIndex, const string& actualIndex) const { |
| 1632 | file << tab(indent) << "if ("; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1633 | if (p.isFloatType) { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1634 | file << "!args." << p.variableName << argsIndex << ".couldBe(" << p.javaArrayName |
| 1635 | << actualIndex; |
| 1636 | if (!mPrecisionLimit.empty()) { |
| 1637 | file << ", " << mPrecisionLimit; |
| 1638 | } |
| 1639 | file << ")"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1640 | } else { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1641 | file << "args." << p.variableName << argsIndex << " != " << p.javaArrayName << actualIndex; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1642 | } |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1643 | if (p.undefinedIfOutIsNan && mReturnIndex >= 0) { |
| 1644 | file << " && args." << mParams[mReturnIndex]->variableName << argsIndex << ".isNaN()"; |
| 1645 | } |
| 1646 | file << ") {\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | void Permutation::writeJavaAppendOutputToMessage(ofstream& file, int indent, |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1650 | const ParameterDefinition& p, |
| 1651 | const string& argsIndex, |
| 1652 | const string& actualIndex) const { |
| 1653 | const string expected = "args." + p.variableName + argsIndex; |
| 1654 | const string actual = p.javaArrayName + actualIndex; |
| 1655 | file << tab(indent) << "message.append(\"Expected output " + p.variableName + ": \");\n"; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1656 | if (p.isFloatType) { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1657 | writeJavaAppendFloatyVariableToMessage(file, indent, expected); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1658 | } else { |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1659 | writeJavaAppendVariableToMessage(file, indent, p, expected); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1660 | } |
| 1661 | writeJavaAppendNewLineToMessage(file, indent); |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1662 | file << tab(indent) << "message.append(\"Actual output " + p.variableName + ": \");\n"; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1663 | writeJavaAppendVariableToMessage(file, indent, p, actual); |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1664 | |
| 1665 | writeJavaTestOneValue(file, indent, p, argsIndex, actualIndex); |
| 1666 | file << tab(indent + 1) << "message.append(\" FAIL\");\n"; |
| 1667 | file << tab(indent) << "}\n"; |
| 1668 | writeJavaAppendNewLineToMessage(file, indent); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | void Permutation::writeJavaAppendInputToMessage(ofstream& file, int indent, |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1672 | const ParameterDefinition& p, |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1673 | const string& actual) const { |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1674 | file << tab(indent) << "message.append(\"Input " + p.variableName + ": \");\n"; |
| 1675 | writeJavaAppendVariableToMessage(file, indent, p, actual); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1676 | writeJavaAppendNewLineToMessage(file, indent); |
| 1677 | } |
| 1678 | |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1679 | void Permutation::writeJavaAppendNewLineToMessage(ofstream& file, int indent) const { |
| 1680 | file << tab(indent) << "message.append(\"\\n\");\n"; |
| 1681 | } |
| 1682 | |
| 1683 | void Permutation::writeJavaAppendVariableToMessage(ofstream& file, int indent, |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1684 | const ParameterDefinition& p, |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1685 | const string& value) const { |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1686 | if (p.specType == "f16" || p.specType == "f32") { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1687 | file << tab(indent) << "message.append(String.format(\"%14.8g %8x %15a\",\n"; |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1688 | file << tab(indent + 2) << value << ", " |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1689 | << "Float.floatToRawIntBits(" << value << "), " << value << "));\n"; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1690 | } else if (p.specType == "f64") { |
| 1691 | file << tab(indent) << "message.append(String.format(\"%24.8g %16x %31a\",\n"; |
| 1692 | file << tab(indent + 2) << value << ", " |
| 1693 | << "Double.doubleToRawLongBits(" << value << "), " << value << "));\n"; |
| 1694 | } else if (p.specType[0] == 'u') { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1695 | file << tab(indent) << "message.append(String.format(\"0x%x\", " << value << "));\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1696 | } else { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1697 | file << tab(indent) << "message.append(String.format(\"%d\", " << value << "));\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1698 | } |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | void Permutation::writeJavaAppendFloatyVariableToMessage(ofstream& file, int indent, |
| 1702 | const string& value) const { |
| 1703 | file << tab(indent) << "message.append(" << value << ".toString());\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | void Permutation::writeJavaVectorComparison(ofstream& file, int indent, |
| 1707 | const ParameterDefinition& p) const { |
| 1708 | if (p.mVectorSize == "1") { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1709 | writeJavaTestAndSetValid(file, indent, p, "", "[i]"); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1710 | |
| 1711 | } else { |
| 1712 | file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n"; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1713 | writeJavaTestAndSetValid(file, indent + 1, p, "[j]", "[i * " + p.vectorWidth + " + j]"); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1714 | file << tab(indent) << "}\n"; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | void Permutation::writeJavaAppendVectorInputToMessage(ofstream& file, int indent, |
| 1719 | const ParameterDefinition& p) const { |
| 1720 | if (p.mVectorSize == "1") { |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1721 | writeJavaAppendInputToMessage(file, indent, p, p.javaArrayName + "[i]"); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1722 | } else { |
| 1723 | file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n"; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1724 | writeJavaAppendInputToMessage(file, indent + 1, p, |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1725 | p.javaArrayName + "[i * " + p.vectorWidth + " + j]"); |
| 1726 | file << tab(indent) << "}\n"; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | void Permutation::writeJavaAppendVectorOutputToMessage(ofstream& file, int indent, |
| 1731 | const ParameterDefinition& p) const { |
| 1732 | if (p.mVectorSize == "1") { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1733 | writeJavaAppendOutputToMessage(file, indent, p, "", "[i]"); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1734 | |
| 1735 | } else { |
| 1736 | file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n"; |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1737 | writeJavaAppendOutputToMessage(file, indent + 1, p, "[j]", |
| 1738 | "[i * " + p.vectorWidth + " + j]"); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1739 | file << tab(indent) << "}\n"; |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | void Permutation::writeJavaVerifyVectorMethod(ofstream& file) const { |
| 1744 | writeJavaVerifyFunctionHeader(file); |
| 1745 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1746 | const ParameterDefinition& p = *mParams[i]; |
| 1747 | writeJavaArrayInitialization(file, p); |
| 1748 | } |
| 1749 | file << tab(2) + "for (int i = 0; i < INPUTSIZE; i++) {\n"; |
| 1750 | file << tab(3) << mJavaArgumentsNClassName << " args = new " << mJavaArgumentsNClassName |
| 1751 | << "();\n"; |
| 1752 | |
| 1753 | file << tab(3) << "// Create the appropriate sized arrays in args\n"; |
| 1754 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1755 | const ParameterDefinition& p = *mParams[i]; |
| 1756 | if (p.mVectorSize != "1") { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1757 | string type = p.javaBaseType; |
Jean-Luc Brouillet | 46ebc97 | 2014-05-05 20:33:25 -0700 | [diff] [blame] | 1758 | if (p.isOutParameter && p.isFloatType) { |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1759 | type = "Floaty"; |
| 1760 | } |
| 1761 | file << tab(3) << "args." << p.variableName << " = new " << type << "[" << p.mVectorSize |
| 1762 | << "];\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | file << tab(3) << "// Fill args with the input values\n"; |
| 1767 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1768 | const ParameterDefinition& p = *mParams[i]; |
| 1769 | if (!p.isOutParameter) { |
| 1770 | if (p.mVectorSize == "1") { |
| 1771 | file << tab(3) << "args." << p.variableName << " = " << p.javaArrayName + "[i]" |
| 1772 | << ";\n"; |
| 1773 | } else { |
| 1774 | file << tab(3) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n"; |
| 1775 | file << tab(4) << "args." << p.variableName + "[j] = " |
| 1776 | << p.javaArrayName + "[i * " + p.vectorWidth + " + j]" |
| 1777 | << ";\n"; |
| 1778 | file << tab(3) << "}\n"; |
| 1779 | } |
| 1780 | } |
| 1781 | } |
Jean-Luc Brouillet | bcd5b9a | 2014-03-07 18:00:57 -0800 | [diff] [blame] | 1782 | file << tab(3) << "Floaty.setRelaxed(relaxed);\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1783 | file << tab(3) << "CoreMathVerifier." << mJavaVerifierComputeMethodName << "(args);\n\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1784 | |
| 1785 | file << tab(3) << "// Compare the expected outputs to the actual values returned by RS.\n"; |
| 1786 | file << tab(3) << "boolean valid = true;\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1787 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1788 | const ParameterDefinition& p = *mParams[i]; |
| 1789 | if (p.isOutParameter) { |
| 1790 | writeJavaVectorComparison(file, 3, p); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | file << tab(3) << "if (!valid) {\n"; |
| 1795 | file << tab(4) << "StringBuilder message = new StringBuilder();\n"; |
| 1796 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1797 | const ParameterDefinition& p = *mParams[i]; |
| 1798 | if (p.isOutParameter) { |
| 1799 | writeJavaAppendVectorOutputToMessage(file, 4, p); |
| 1800 | } else { |
| 1801 | writeJavaAppendVectorInputToMessage(file, 4, p); |
| 1802 | } |
| 1803 | } |
| 1804 | |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1805 | file << tab(4) << "assertTrue(\"Incorrect output for " << mJavaCheckMethodName << "\" +\n"; |
| 1806 | file << tab(6) << "(relaxed ? \"_relaxed\" : \"\") + \":\\n\" + message.toString(), valid);\n"; |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1807 | file << tab(3) << "}\n"; |
| 1808 | file << tab(2) << "}\n"; |
| 1809 | file << tab(1) << "}\n\n"; |
| 1810 | } |
| 1811 | |
| 1812 | void Permutation::writeJavaCallToRs(ofstream& file, bool relaxed, bool generateCallToVerify) const { |
| 1813 | string script = "script"; |
| 1814 | if (relaxed) { |
| 1815 | script += "Relaxed"; |
| 1816 | } |
| 1817 | |
| 1818 | file << tab(2) << "try {\n"; |
| 1819 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1820 | const ParameterDefinition& p = *mParams[i]; |
| 1821 | if (p.isOutParameter) { |
Jean-Luc Brouillet | 4634143 | 2014-02-21 22:49:22 -0800 | [diff] [blame] | 1822 | writeJavaOutputAllocationDefinition(file, tab(3), p); |
Jean-Luc Brouillet | 963c367 | 2014-02-12 20:58:47 -0800 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | for (int i = 0; i < (int)mParams.size(); i++) { |
| 1827 | const ParameterDefinition& p = *mParams[i]; |
| 1828 | if (i != mReturnIndex && i != mFirstInputIndex) { |
| 1829 | file << tab(3) << script << ".set_" << p.rsAllocName << "(" << p.javaAllocName |
| 1830 | << ");\n"; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | file << tab(3) << script << ".forEach_" << mRsKernelName << "("; |
| 1835 | bool needComma = false; |
| 1836 | if (mFirstInputIndex >= 0) { |
| 1837 | file << mParams[mFirstInputIndex]->javaAllocName; |
| 1838 | needComma = true; |
| 1839 | } |
| 1840 | if (mReturnIndex >= 0) { |
| 1841 | if (needComma) { |
| 1842 | file << ", "; |
| 1843 | } |
| 1844 | file << mParams[mReturnIndex]->variableName << ");\n"; |
| 1845 | } |
| 1846 | |
| 1847 | if (generateCallToVerify) { |
| 1848 | file << tab(3) << mJavaVerifyMethodName << "("; |
| 1849 | for (size_t i = 0; i < mParams.size(); i++) { |
| 1850 | const ParameterDefinition& p = *mParams[i]; |
| 1851 | file << p.variableName << ", "; |
| 1852 | } |
| 1853 | |
| 1854 | if (relaxed) { |
| 1855 | file << "true"; |
| 1856 | } else { |
| 1857 | file << "false"; |
| 1858 | } |
| 1859 | file << ");\n"; |
| 1860 | } |
| 1861 | file << tab(2) << "} catch (Exception e) {\n"; |
| 1862 | file << tab(3) << "throw new RSRuntimeException(\"RenderScript. Can't invoke forEach_" |
| 1863 | << mRsKernelName << ": \" + e.toString());\n"; |
| 1864 | file << tab(2) << "}\n"; |
| 1865 | } |
| 1866 | |
| 1867 | } // namespace |
| 1868 | |
| 1869 | int main(int argc, char* argv[]) { |
| 1870 | int versionOfTestFiles = 0; |
| 1871 | vector<string> specFileNames; |
| 1872 | if (!parseCommandLine(argc, argv, &versionOfTestFiles, &specFileNames)) { |
| 1873 | printf("Usage: gen_runtime spec_file [spec_file...] [-v version_of_test_files]\n"); |
| 1874 | return -1; |
| 1875 | } |
| 1876 | int result = 0; |
| 1877 | for (size_t i = 0; i < specFileNames.size(); i++) { |
| 1878 | SpecFile specFile(specFileNames[i]); |
| 1879 | if (!specFile.process(versionOfTestFiles)) { |
| 1880 | result = -1; |
| 1881 | } |
| 1882 | } |
| 1883 | return result; |
| 1884 | } |