blob: 68e7a4d8514d530d3cb66f3a84a912b053d93b58 [file] [log] [blame]
Jason Sams135c4b72013-12-11 18:24:45 -08001/*
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 Brouillet963c3672014-02-12 20:58:47 -080017/* 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 Brouilletbcd5b9a2014-03-07 18:00:57 -080043#include <math.h>
44#include <stdio.h>
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080045#include <cctype>
46#include <cstdlib>
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -080047#include <fstream>
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080048#include <functional>
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -080049#include <iomanip>
Jason Sams135c4b72013-12-11 18:24:45 -080050#include <list>
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080051#include <map>
52#include <set>
Jean-Luc Brouillet46341432014-02-21 22:49:22 -080053#include <sstream>
Jason Sams135c4b72013-12-11 18:24:45 -080054#include <string>
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080055#include <vector>
Jason Sams135c4b72013-12-11 18:24:45 -080056
57using namespace std;
58
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080059namespace {
Jason Sams135c4b72013-12-11 18:24:45 -080060
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080061const char* AUTO_GENERATED_WARNING =
62 "// Don't edit this file! It is auto-generated by "
63 "frameworks/rs/api/gen_runtime.\n\n";
64const 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
81class Function;
82class Specification;
83class Permutation;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -080084struct Type;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080085
86/* Information about a parameter to a function. The values of all the fields should only be set by
87 * parseParameterDefinition.
88 */
89struct ParameterDefinition {
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -080090 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"
93
94 /* The number of entries in the vector. It should be either "1", "2", "3", or "4". It's also
95 * "1" for scalars.
96 */
97 string mVectorSize;
98 /* The space the vector takes in an array. It's the same as the vector size, except for size
99 * "3", where the width is "4".
100 */
101 string vectorWidth;
102
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800103 string specName; // e.g. x, as found in the spec file
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800104 string variableName; // e.g. inX, used both in .rs and .java
105 string rsAllocName; // e.g. gAllocInX
106 string javaAllocName; // e.g. inX
107 string javaArrayName; // e.g. arrayInX
108
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800109 // If non empty, the mininum and maximum values to be used when generating the test data.
110 string minValue;
111 string maxValue;
112 /* If non empty, contains the name of another parameter that should be smaller or equal to this
113 * parameter, i.e. value(smallerParameter) <= value(this). This is used when testing clamp.
114 */
115 string smallerParameter;
116
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800117 bool isOutParameter; // True if this parameter returns data from the script.
118 bool undefinedIfOutIsNan; // If true, we don't validate if 'out' is NaN.
119
120 int typeIndex; // Index in the TYPES array.
121 int compatibleTypeIndex; // Index in TYPES for which the test data must also fit.
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800122
123 /* Parse the parameter definition found in the spec file. It will generate a name if none
124 * are present in the file. One of the two counts will be incremented, and potentially
125 * used to generate unique names. isReturn is true if we're processing the "return:"
126 * definition.
127 */
128 void parseParameterDefinition(string s, bool isReturn, int* inputCount, int* outputCount);
129};
130
131// An entire spec file and the methods to process it.
132class SpecFile {
Jason Sams135c4b72013-12-11 18:24:45 -0800133public:
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800134 explicit SpecFile(const string& specFileName) : mSpecFileName(specFileName) {}
135 bool process(int versionOfTestFiles);
Jason Sams135c4b72013-12-11 18:24:45 -0800136
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800137private:
138 const string mSpecFileName;
139 // The largest version number that we have found in all the specifications.
140 int mLargestVersionNumber;
141
142 map<string, Function*> mFunctionsMap; // All the known functions.
143 typedef map<string, Function*>::iterator FunctionsIterator;
144
145 bool readSpecFile();
146 Function* getFunction(const string& name);
147 bool generateFiles(int versionOfTestFiles);
148 bool writeAllFunctions(ofstream& headerFile, int versionOfTestFiles);
149};
150
151/* Represents a function, like "clamp". Even though the spec file contains many entries for clamp,
152 * we'll only have one clamp instance.
153 */
154class Function {
155private:
156 string mName; // The lower case name, e.g. native_log
157 string mCapitalizedName; // The capitalized name, e.g. NativeLog
158 string mTestName; // e.g. TestNativeLog
159 string mRelaxedTestName; // e.g. TestNativeLogRelaxed
160
161 vector<Specification*> mSpecifications;
162 typedef vector<Specification*>::iterator SpecificationIterator;
163
164 /* We keep track of the allocations generated in the .rs file and the argument classes defined
165 * in the Java file, as we share these between the functions created for each specification.
166 */
167 set<string> mRsAllocationsGenerated;
168 set<string> mJavaGeneratedArgumentClasses;
169
170 string mJavaCallAllCheckMethods; // Lines of Java code to invoke the check methods.
171
172 ofstream mRsFile; // The Renderscript test file we're generating.
173 ofstream mJavaFile; // The Jave test file we're generating.
174
175 bool startRsFile(); // Open the mRsFile and writes its header.
176 bool writeRelaxedRsFile(); // Write the entire relaxed rs test file (an include essentially)
177 bool startJavaFile(); // Open the mJavaFile and writes the header.
178 void finishJavaFile(); // Write the test method and closes the file.
179
180public:
181 explicit Function(const string& name);
182 void addSpecification(Specification* spec) { mSpecifications.push_back(spec); }
183 /* Write the .java and the two .rs test files. versionOfTestFiles is used to restrict which API
184 * to test. Also writes the section of the header file.
185 */
186 bool writeFiles(ofstream& headerFile, int versionOfTestFiles);
187 // Write an allocation and keep track of having it written, so it can be shared.
188 void writeRsAllocationDefinition(const ParameterDefinition& param);
189 // Write an argument class definiton and keep track of having it written, so it can be shared.
190 void writeJavaArgumentClassDefinition(const string& className, const string& definition);
191 // Add a call to mJavaCallAllCheckMethods to be used at the end of the file generation.
192 void addJavaCheckCall(const string& call);
193};
194
195/* Defines one of the many variations of the function. There's a one to one correspondance between
196 * Specification objects and entries in the spec file. Some of the strings that are parts of a
197 * Specification can include placeholders, which are "#1", "#2", "#3", and "#4". We'll replace
198 * these by values before generating the files.
199 */
200class Specification {
201private:
202 /* The range of versions this specification applies to. 0 if there's no restriction, so an API
203 * that became available at 9 and is still valid would have min:9 max:0.
204 */
Jason Sams135c4b72013-12-11 18:24:45 -0800205 int mMinVersion;
206 int mMaxVersion;
207
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800208 /* The name of the function without #n, e.g. convert. As of this writing, it only differs for
209 * convert.
210 */
211 string mCleanName;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800212 string mTest; // How to test. One of "scalar", "vector", "noverify", "limited", and "none".
213 string mPrecisionLimit; // Maximum precision required when checking output of this function.
Jason Sams135c4b72013-12-11 18:24:45 -0800214
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800215 vector<vector<string> > mReplaceables;
216
217 // The following fields may contain placeholders that will be replaced using the mReplaceables.
218
219 // The name of this function, can include #, e.g. convert_#1_#2
220 string mName;
221
222 string mReturn; // The return type
223 vector<string> mComment; // The comments to be included in the header
224 vector<string> mInline; // The inline code to be included in the header
225 vector<string> mParam; // One entry per parameter defined
226
227 // Substitute the placeholders in the strings by the corresponding entries in mReplaceables.
228 string expandString(string s, int i1, int i2, int i3, int i4) const;
229 void expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4,
230 vector<string>* out) const;
231
232public:
233 Specification() {
234 mMinVersion = 0;
235 mMaxVersion = 0;
236 }
237 int getMinVersion() const { return mMinVersion; }
238 int getMaxVersion() const { return mMaxVersion; }
239
240 string getName(int i1, int i2, int i3, int i4) const {
241 return expandString(mName, i1, i2, i3, i4);
242 }
243 string getReturn(int i1, int i2, int i3, int i4) const {
244 return expandString(mReturn, i1, i2, i3, i4);
245 }
246 void getComments(int i1, int i2, int i3, int i4, vector<string>* comments) const {
247 return expandStringVector(mComment, i1, i2, i3, i4, comments);
248 }
249 void getInlines(int i1, int i2, int i3, int i4, vector<string>* inlines) const {
250 return expandStringVector(mInline, i1, i2, i3, i4, inlines);
251 }
252 void getParams(int i1, int i2, int i3, int i4, vector<string>* params) const {
253 return expandStringVector(mParam, i1, i2, i3, i4, params);
254 }
255 string getTest() const { return mTest; }
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800256 string getPrecisionLimit() const { return mPrecisionLimit; }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800257 string getCleanName() const { return mCleanName; }
258
259 void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile, Function* function,
260 int versionOfTestFiles);
261 bool writeRelaxedRsFile() const;
262 // Return true if this specification should be generated for this version.
263 bool relevantForVersion(int versionOfTestFiles) const;
264
265 static Specification* scanSpecification(FILE* in);
Jason Sams135c4b72013-12-11 18:24:45 -0800266};
267
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800268// A concrete version of a specification, where all placeholders have been replaced by actual
269// values.
270class Permutation {
271private:
272 Function* mFunction;
273 Specification* mSpecification;
Jason Sams135c4b72013-12-11 18:24:45 -0800274
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800275 // These are the expanded version of those found on Specification
276 string mName;
277 string mCleanName;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800278 string mTest; // How to test. One of "scalar", "vector", "noverify", "limited", and "none".
279 string mPrecisionLimit; // Maximum precision required when checking output of this function.
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800280 vector<string> mInline;
281 vector<string> mComment;
282
283 // The inputs and outputs of the function. This include the return type, if present.
284 vector<ParameterDefinition*> mParams;
285 // The index of the return value in mParams, -1 if the function is void.
286 int mReturnIndex;
287 // The index of the first input value in mParams, -1 if there's no input.
288 int mFirstInputIndex;
289 // The number of input and output parameters.
290 int mInputCount;
291 int mOutputCount;
292
293 string mRsKernelName;
294 string mJavaArgumentsClassName;
295 string mJavaArgumentsNClassName;
296 string mJavaVerifierComputeMethodName;
297 string mJavaCheckMethodName;
298 string mJavaVerifyMethodName;
299
300 void writeHeaderSection(ofstream& file) const;
301
302 void writeRsSection(ofstream& rs) const;
303
304 void writeJavaSection(ofstream& file) const;
305 void writeJavaArgumentClass(ofstream& file, bool scalar) const;
306 void writeJavaCheckMethod(ofstream& file, bool generateCallToVerify) const;
307 void writeJavaVerifyScalarMethod(ofstream& file) const;
308 void writeJavaVerifyVectorMethod(ofstream& file) const;
309 void writeJavaVerifyFunctionHeader(ofstream& file) const;
310 void writeJavaInputAllocationDefinition(ofstream& file, const string& indent,
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800311 const ParameterDefinition& param) const;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800312 void writeJavaOutputAllocationDefinition(ofstream& file, const string& indent,
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800313 const ParameterDefinition& param) const;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800314 // Write code to create a random allocation for which the data must be compatible for two types.
315 void writeJavaRandomCompatibleFloatAllocation(ofstream& file, const string& dataType,
316 const string& seed, char vectorSize,
317 const Type& compatibleType,
318 const Type& generatedType) const;
319 void writeJavaRandomCompatibleIntegerAllocation(ofstream& file, const string& dataType,
320 const string& seed, char vectorSize,
321 const Type& compatibleType,
322 const Type& generatedType) const;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800323 void writeJavaCallToRs(ofstream& file, bool relaxed, bool generateCallToVerify) const;
324
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800325 void writeJavaTestAndSetValid(ofstream& file, int indent, const ParameterDefinition& p,
326 const string& argsIndex, const string& actualIndex) const;
327 void writeJavaTestOneValue(ofstream& file, int indent, const ParameterDefinition& p,
328 const string& argsIndex, const string& actualIndex) const;
329 void writeJavaAppendOutputToMessage(ofstream& file, int indent, const ParameterDefinition& p,
330 const string& argsIndex, const string& actualIndex) const;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800331 void writeJavaAppendInputToMessage(ofstream& file, int indent, const string& rsBaseType,
332 const string& name, const string& actual) const;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800333 void writeJavaAppendNewLineToMessage(ofstream& file, int indent) const;
334 void writeJavaAppendVariableToMessage(ofstream& file, int indent, const string& rsBaseType,
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800335 const string& value) const;
336 void writeJavaAppendFloatyVariableToMessage(ofstream& file, int indent,
337 const string& value) const;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800338 void writeJavaVectorComparison(ofstream& file, int indent, const ParameterDefinition& p) const;
339 void writeJavaAppendVectorInputToMessage(ofstream& file, int indent,
340 const ParameterDefinition& p) const;
341 void writeJavaAppendVectorOutputToMessage(ofstream& file, int indent,
342 const ParameterDefinition& p) const;
343 bool passByAddressToSet(const string& name) const;
344 void convertToRsType(const string& name, string* dataType, char* vectorSize) const;
345
346public:
347 Permutation(Function* function, Specification* specification, int i1, int i2, int i3, int i4);
348 void writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile,
349 int versionOfTestFiles);
350 bool hasLongOrDoubleParameter() const;
351};
352
353// Table of type equivalences
354// TODO: We should just be pulling this from a shared header. Slang does exactly the same thing.
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800355
356enum NumberKind { SIGNED_INTEGER, UNSIGNED_INTEGER, FLOATING_POINT };
357
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800358struct Type {
359 const char* specType; // Name found in the .spec file
360 string rsDataType; // RS data type
361 string cType; // Type in a C file
362 const char* javaType; // Type in a Java file
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800363 NumberKind kind;
364 /* For integers, number of bits of the number, excluding the sign bit.
365 * For floats, number of bits of the exponent.
366 */
367 int significantBits;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800368};
369
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800370const Type TYPES[] = {{"f16", "FLOAT_16", "half", "half", FLOATING_POINT, 5},
371 {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 8},
372 {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 11},
373 {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7},
374 {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8},
375 {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15},
376 {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16},
377 {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31},
378 {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32},
379 {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63},
380 {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64}};
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800381
382const int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]);
383
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800384// Returns the index in TYPES for the provided cType
385int FindCType(const string& cType) {
386 for (int i = 0; i < NUM_TYPES; i++) {
387 if (cType == TYPES[i].cType) {
388 return i;
389 }
390 }
391 return -1;
392}
393
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800394// Capitalizes and removes underscores. E.g. converts "native_log" to NativeLog.
395string capitalize(const string& source) {
396 int length = source.length();
397 string result;
398 bool capitalize = true;
399 for (int s = 0; s < length; s++) {
400 if (source[s] == '_') {
401 capitalize = true;
402 } else if (capitalize) {
403 result += toupper(source[s]);
404 capitalize = false;
405 } else {
406 result += source[s];
407 }
408 }
409 return result;
410}
411
412string tab(int n) { return string(n * 4, ' '); }
413
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800414// Returns a string that's an hexadecimal constant fo the hash of the string.
415string hashString(const string& s) {
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800416 long hash = 0;
417 for (size_t i = 0; i < s.length(); i++) {
418 hash = hash * 43 + s[i];
419 }
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800420 stringstream stream;
421 stream << "0x" << std::hex << hash << "l";
422 return stream.str();
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800423}
424
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800425// Removes the character from present. Returns true if the string contained the character.
426static bool charRemoved(char c, string* s) {
427 size_t p = s->find(c);
428 if (p != string::npos) {
429 s->erase(p, 1);
430 return true;
431 }
432 return false;
433}
434
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800435// Return true if the string is already in the set. Inserts it if not.
436bool testAndSet(const string& flag, set<string>* set) {
437 if (set->find(flag) == set->end()) {
438 set->insert(flag);
439 return false;
Jason Sams135c4b72013-12-11 18:24:45 -0800440 }
441 return true;
442}
443
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800444// Convert an int into a string.
445string toString(int n) {
446 char buf[100];
447 snprintf(buf, sizeof(buf), "%d", n);
448 return string(buf);
449}
450
451void trim(string* s, size_t start) {
Jason Sams135c4b72013-12-11 18:24:45 -0800452 if (start > 0) {
453 s->erase(0, start);
454 }
455
456 while (s->size() && (s->at(0) == ' ')) {
457 s->erase(0, 1);
458 }
459
460 size_t p = s->find_first_of("\n\r");
461 if (p != string::npos) {
462 s->erase(p);
463 }
464
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800465 while ((s->size() > 0) && (s->at(s->size() - 1) == ' ')) {
466 s->erase(s->size() - 1);
Jason Sams135c4b72013-12-11 18:24:45 -0800467 }
468}
469
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800470string stringReplace(string s, string match, string rep) {
471 while (1) {
472 size_t p = s.find(match);
473 if (p == string::npos) break;
474
475 s.erase(p, match.size());
476 s.insert(p, rep);
477 }
478 return s;
479}
480
481// Return the next line from the input file.
482bool getNextLine(FILE* in, string* s) {
483 s->clear();
484 while (1) {
485 int c = fgetc(in);
486 if (c == EOF) return s->size() != 0;
487 if (c == '\n') break;
488 s->push_back((char)c);
489 }
490 return true;
491}
492
493void writeIfdef(ofstream& file, string filename, bool isStart) {
494 string t = "__";
495 t += filename;
496 t += "__";
497
498 for (size_t i = 2; i < t.size(); i++) {
499 if (t[i] == '.') {
500 t[i] = '_';
501 }
502 }
503
504 if (isStart) {
505 file << "#ifndef " << t << "\n";
506 file << "#define " << t << "\n";
507 } else {
508 file << "#endif // " << t << "\n";
509 }
510}
511
512void writeJavaArrayInitialization(ofstream& file, const ParameterDefinition& p) {
513 file << tab(2) << p.javaBaseType << "[] " << p.javaArrayName << " = new " << p.javaBaseType
514 << "[INPUTSIZE * " << p.vectorWidth << "];\n";
515 file << tab(2) << p.javaAllocName << ".copyTo(" << p.javaArrayName << ");\n";
516}
517
518bool parseCommandLine(int argc, char* argv[], int* versionOfTestFiles,
519 vector<string>* specFileNames) {
520 for (int i = 1; i < argc; i++) {
521 if (argv[i][0] == '-') {
522 if (argv[i][1] == 'v') {
523 i++;
524 if (i < argc) {
525 char* end;
526 *versionOfTestFiles = strtol(argv[i], &end, 10);
527 if (*end != '\0') {
528 printf("Can't parse the version number %s\n", argv[i]);
529 return false;
530 }
531 } else {
532 printf("Missing version number after -v\n");
533 return false;
534 }
535 } else {
536 printf("Unrecognized flag %s\n", argv[i]);
537 return false;
538 }
539 } else {
540 specFileNames->push_back(argv[i]);
541 }
542 }
543 if (specFileNames->size() == 0) {
544 printf("No spec file specified\n");
545 return false;
546 }
547 return true;
548}
549
550/* Parse a parameter definition. It's of the form "type [*][name]". The type
551 * is required. The name is optional. The * indicates it's an output
552 * parameter. We also pass the indexed of this parameter in the definition, so
553 * we can create names like in2, in3, etc. */
554void ParameterDefinition::parseParameterDefinition(string s, bool isReturn, int* inputCount,
555 int* outputCount) {
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800556 istringstream stream(s);
557 string name, type, option;
558 stream >> rsType;
559 stream >> specName;
560 stream >> option;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800561
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800562 // Determine if this is an output.
563 isOutParameter = charRemoved('*', &rsType) || charRemoved('*', &specName) || isReturn;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800564
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800565 // Extract the vector size out of the type.
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800566 int last = rsType.size() - 1;
567 char lastChar = rsType[last];
568 if (lastChar >= '0' && lastChar <= '9') {
569 rsBaseType = rsType.substr(0, last);
570 mVectorSize = lastChar;
571 } else {
572 rsBaseType = rsType;
573 mVectorSize = "1";
574 }
575 if (mVectorSize == "3") {
576 vectorWidth = "4";
577 } else {
578 vectorWidth = mVectorSize;
579 }
580
581 /* Create variable names to be used in the java and .rs files. Because x and
582 * y are reserved in .rs files, we prefix variable names with "in" or "out".
583 */
584 if (isOutParameter) {
585 variableName = "out";
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800586 if (!specName.empty()) {
587 variableName += capitalize(specName);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800588 } else if (!isReturn) {
589 variableName += toString(*outputCount);
590 }
591 (*outputCount)++;
592 } else {
593 variableName = "in";
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800594 if (!specName.empty()) {
595 variableName += capitalize(specName);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800596 } else if (*inputCount > 0) {
597 variableName += toString(*inputCount);
598 }
599 (*inputCount)++;
600 }
601 rsAllocName = "gAlloc" + capitalize(variableName);
602 javaAllocName = variableName;
603 javaArrayName = "array" + capitalize(javaAllocName);
604
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800605 // Process the option.
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800606 undefinedIfOutIsNan = false;
607 compatibleTypeIndex = -1;
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800608 if (!option.empty()) {
609 if (option.compare(0, 6, "range(") == 0) {
610 size_t pComma = option.find(',');
611 size_t pParen = option.find(')');
612 if (pComma == string::npos || pParen == string::npos) {
613 printf("Incorrect range %s\n", option.c_str());
614 } else {
615 minValue = option.substr(6, pComma - 6);
616 maxValue = option.substr(pComma + 1, pParen - pComma - 1);
617 }
618 } else if (option.compare(0, 6, "above(") == 0) {
619 size_t pParen = option.find(')');
620 if (pParen == string::npos) {
621 printf("Incorrect option %s\n", option.c_str());
622 } else {
623 smallerParameter = option.substr(6, pParen - 6);
624 }
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800625 } else if (option.compare(0, 11, "compatible(") == 0) {
626 size_t pParen = option.find(')');
627 if (pParen == string::npos) {
628 printf("Incorrect option %s\n", option.c_str());
629 } else {
630 compatibleTypeIndex = FindCType(option.substr(11, pParen - 11));
631 }
632 } else if (option.compare(0, 11, "conditional") == 0) {
633 undefinedIfOutIsNan = true;
Jean-Luc Brouillet46341432014-02-21 22:49:22 -0800634 } else {
635 printf("Unrecognized option %s\n", option.c_str());
636 }
637 }
638
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800639 typeIndex = FindCType(rsBaseType);
640 if (typeIndex < 0) {
641 // TODO set a global flag when we encounter an error & abort
642 printf("Error, could not find %s\n", rsBaseType.c_str());
643 } else {
644 javaBaseType = TYPES[typeIndex].javaType;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800645 }
646}
647
648bool SpecFile::process(int versionOfTestFiles) {
649 if (!readSpecFile()) {
650 return false;
651 }
652 if (versionOfTestFiles == 0) {
653 versionOfTestFiles = mLargestVersionNumber;
654 }
655 if (!generateFiles(versionOfTestFiles)) {
656 return false;
657 }
658 printf("%s: %ld functions processed.\n", mSpecFileName.c_str(), mFunctionsMap.size());
659 return true;
660}
661
662// Read the specification, adding the definitions to the global functions map.
663bool SpecFile::readSpecFile() {
664 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
665 if (!specFile) {
666 printf("Error opening input file: %s\n", mSpecFileName.c_str());
667 return false;
668 }
669
670 mLargestVersionNumber = 0;
671 while (1) {
672 Specification* spec = Specification::scanSpecification(specFile);
673 if (spec == NULL) {
674 break;
675 }
676 getFunction(spec->getCleanName())->addSpecification(spec);
677 int specMin = spec->getMinVersion();
678 int specMax = spec->getMaxVersion();
679 if (specMin && specMin > mLargestVersionNumber) {
680 mLargestVersionNumber = specMin;
681 }
682 if (specMax && specMax > mLargestVersionNumber) {
683 mLargestVersionNumber = specMax;
684 }
685 }
686
687 fclose(specFile);
688 return true;
689}
690
691bool SpecFile::generateFiles(int versionOfTestFiles) {
692 printf("%s: Generating test files for version %d\n", mSpecFileName.c_str(), versionOfTestFiles);
693
694 // The header file name should have the same base but with a ".rsh" extension.
695 string headerFileName = mSpecFileName;
696 size_t l = headerFileName.length();
697 const char SPEC[] = ".spec";
698 const int SPEC_SIZE = sizeof(SPEC) - 1;
699 const int start = l - SPEC_SIZE;
700 if (start >= 0 && headerFileName.compare(start, SPEC_SIZE, SPEC) == 0) {
701 headerFileName.erase(start);
702 }
703 headerFileName += ".rsh";
704
705 // Write the start of the header file.
706 ofstream headerFile;
707 headerFile.open(headerFileName.c_str(), ios::out | ios::trunc);
708 if (!headerFile.is_open()) {
709 printf("Error opening output file: %s\n", headerFileName.c_str());
710 return false;
711 }
712 headerFile << LEGAL_NOTICE;
713 headerFile << AUTO_GENERATED_WARNING;
714 writeIfdef(headerFile, headerFileName, true);
715
716 // Write the functions to the header and test files.
717 bool success = writeAllFunctions(headerFile, versionOfTestFiles);
718
719 // Finish the header file.
720 writeIfdef(headerFile, headerFileName, false);
721 headerFile.close();
722
723 return success;
724}
725
726// Return the named function from the map. Creates it if it's not there.
727Function* SpecFile::getFunction(const string& name) {
728 FunctionsIterator iter = mFunctionsMap.find(name);
729 if (iter != mFunctionsMap.end()) {
730 return iter->second;
731 }
732 Function* f = new Function(name);
733 mFunctionsMap[name] = f;
734 return f;
735}
736
737bool SpecFile::writeAllFunctions(ofstream& headerFile, int versionOfTestFiles) {
738 bool success = true;
739 for (FunctionsIterator iter = mFunctionsMap.begin(); iter != mFunctionsMap.end(); iter++) {
740 Function* func = iter->second;
741 if (!func->writeFiles(headerFile, versionOfTestFiles)) {
742 success = false;
743 }
744 }
745 return success;
746}
747
748Function::Function(const string& name) {
749 mName = name;
750 mCapitalizedName = capitalize(mName);
751 mTestName = "Test" + mCapitalizedName;
752 mRelaxedTestName = mTestName + "Relaxed";
753}
754
755bool Function::writeFiles(ofstream& headerFile, int versionOfTestFiles) {
756 if (!startRsFile() || !startJavaFile() || !writeRelaxedRsFile()) {
757 return false;
758 }
759
760 for (SpecificationIterator i = mSpecifications.begin(); i < mSpecifications.end(); i++) {
761 (*i)->writeFiles(headerFile, mRsFile, mJavaFile, this, versionOfTestFiles);
762 }
763
764 finishJavaFile();
765 // There's no work to wrap-up in the .rs file.
766
767 mRsFile.close();
768 mJavaFile.close();
769 return true;
770}
771
772bool Function::startRsFile() {
773 string fileName = mTestName + ".rs";
774 mRsFile.open(fileName.c_str(), ios::out | ios::trunc);
775 if (!mRsFile.is_open()) {
776 printf("Error opening file: %s\n", fileName.c_str());
777 return false;
778 }
779 mRsFile << LEGAL_NOTICE;
780 mRsFile << "#pragma version(1)\n";
781 mRsFile << "#pragma rs java_package_name(android.renderscript.cts)\n\n";
782 mRsFile << AUTO_GENERATED_WARNING;
783 return true;
784}
785
786// Write an allocation definition if not already emitted in the .rs file.
787void Function::writeRsAllocationDefinition(const ParameterDefinition& param) {
788 if (!testAndSet(param.rsAllocName, &mRsAllocationsGenerated)) {
789 mRsFile << "rs_allocation " << param.rsAllocName << ";\n";
790 }
791}
792
793// Write the entire *Relaxed.rs test file, as it only depends on the name.
794bool Function::writeRelaxedRsFile() {
795 string name = mRelaxedTestName + ".rs";
796 FILE* file = fopen(name.c_str(), "wt");
797 if (!file) {
798 printf("Error opening file: %s\n", name.c_str());
799 return false;
800 }
801 fputs(LEGAL_NOTICE, file);
802 string s;
803 s += "#include \"" + mTestName + ".rs\"\n";
804 s += "#pragma rs_fp_relaxed\n";
805 s += AUTO_GENERATED_WARNING;
806 fputs(s.c_str(), file);
807 fclose(file);
808 return true;
809}
810
811bool Function::startJavaFile() {
812 string fileName = mTestName + ".java";
813 mJavaFile.open(fileName.c_str(), ios::out | ios::trunc);
814 if (!mJavaFile.is_open()) {
815 printf("Error opening file: %s\n", fileName.c_str());
816 return false;
817 }
818 mJavaFile << LEGAL_NOTICE;
819 mJavaFile << AUTO_GENERATED_WARNING;
820 mJavaFile << "package android.renderscript.cts;\n\n";
821
822 mJavaFile << "import android.renderscript.Allocation;\n";
823 mJavaFile << "import android.renderscript.RSRuntimeException;\n";
824 mJavaFile << "import android.renderscript.Element;\n\n";
825
826 mJavaFile << "public class " << mTestName << " extends RSBaseCompute {\n\n";
827
828 mJavaFile << tab(1) << "private ScriptC_" << mTestName << " script;\n";
829 mJavaFile << tab(1) << "private ScriptC_" << mRelaxedTestName << " scriptRelaxed;\n\n";
830
831 mJavaFile << tab(1) << "@Override\n";
832 mJavaFile << tab(1) << "protected void setUp() throws Exception {\n";
833 mJavaFile << tab(2) << "super.setUp();\n";
834 mJavaFile << tab(2) << "script = new ScriptC_" << mTestName << "(mRS);\n";
835 mJavaFile << tab(2) << "scriptRelaxed = new ScriptC_" << mRelaxedTestName << "(mRS);\n";
836 mJavaFile << tab(1) << "}\n\n";
837 return true;
838}
839
840void Function::writeJavaArgumentClassDefinition(const string& className, const string& definition) {
841 if (!testAndSet(className, &mJavaGeneratedArgumentClasses)) {
842 mJavaFile << definition;
843 }
844}
845
846void Function::addJavaCheckCall(const string& call) {
847 mJavaCallAllCheckMethods += tab(2) + call + "\n";
848}
849
850void Function::finishJavaFile() {
851 mJavaFile << tab(1) << "public void test" << mCapitalizedName << "() {\n";
852 mJavaFile << mJavaCallAllCheckMethods;
853 mJavaFile << tab(1) << "}\n";
854 mJavaFile << "}\n";
855}
856
857void Specification::expandStringVector(const vector<string>& in, int i1, int i2, int i3, int i4,
858 vector<string>* out) const {
859 out->clear();
860 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
861 out->push_back(expandString(*iter, i1, i2, i3, i4));
862 }
863}
864
865Specification* Specification::scanSpecification(FILE* in) {
866 Specification* spec = new Specification();
867 spec->mTest = "scalar"; // default
Jason Sams135c4b72013-12-11 18:24:45 -0800868 bool modeComment = false;
869 bool modeInline = false;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800870 bool success = true;
Jason Sams135c4b72013-12-11 18:24:45 -0800871
872 while (1) {
873 string s;
874 bool ret = getNextLine(in, &s);
875 if (!ret) break;
876
877 if (modeComment) {
878 if (!s.size() || (s[0] == ' ')) {
879 trim(&s, 0);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800880 spec->mComment.push_back(s);
Jason Sams135c4b72013-12-11 18:24:45 -0800881 continue;
882 } else {
883 modeComment = false;
884 }
885 }
886
887 if (modeInline) {
888 if (!s.size() || (s[0] == ' ')) {
889 trim(&s, 0);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800890 spec->mInline.push_back(s);
Jason Sams135c4b72013-12-11 18:24:45 -0800891 continue;
892 } else {
893 modeInline = false;
894 }
895 }
896
897 if (s[0] == '#') {
898 continue;
899 }
900
901 if (s.compare(0, 5, "name:") == 0) {
902 trim(&s, 5);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800903 spec->mName = s;
904 // Some functions like convert have # part of the name. Truncate at that point.
905 size_t p = s.find('#');
906 if (p != string::npos) {
907 if (p > 0 && s[p - 1] == '_') {
908 p--;
909 }
910 s.erase(p);
911 }
912 spec->mCleanName = s;
Jason Sams135c4b72013-12-11 18:24:45 -0800913 continue;
914 }
915
916 if (s.compare(0, 4, "arg:") == 0) {
917 trim(&s, 4);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800918 spec->mParam.push_back(s);
Jason Sams135c4b72013-12-11 18:24:45 -0800919 continue;
920 }
921
922 if (s.compare(0, 4, "ret:") == 0) {
923 trim(&s, 4);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800924 spec->mReturn = s;
925 continue;
926 }
927
928 if (s.compare(0, 5, "test:") == 0) {
929 trim(&s, 5);
930 if (s == "scalar" || s == "vector" || s == "noverify" || s == "none") {
931 spec->mTest = s;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800932 } else if (s.compare(0, 7, "limited") == 0) {
933 spec->mTest = "limited";
934 if (s.compare(7, 1, "(") == 0) {
935 size_t pParen = s.find(')');
936 if (pParen == string::npos) {
937 printf("Incorrect test %s\n", s.c_str());
938 } else {
939 spec->mPrecisionLimit = s.substr(8, pParen - 8);
940 }
941 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800942 } else {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -0800943 printf("Error: Unrecognized test option: %s\n", s.c_str());
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800944 success = false;
945 }
Jason Sams135c4b72013-12-11 18:24:45 -0800946 continue;
947 }
948
949 if (s.compare(0, 4, "end:") == 0) {
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800950 if (success) {
951 return spec;
952 } else {
953 delete spec;
954 return NULL;
955 }
Jason Sams135c4b72013-12-11 18:24:45 -0800956 }
957
958 if (s.compare(0, 8, "comment:") == 0) {
959 modeComment = true;
960 continue;
961 }
962
963 if (s.compare(0, 7, "inline:") == 0) {
964 modeInline = true;
965 continue;
966 }
967
968 if (s.compare(0, 8, "version:") == 0) {
969 trim(&s, 8);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800970 sscanf(s.c_str(), "%i %i", &spec->mMinVersion, &spec->mMaxVersion);
Jason Sams135c4b72013-12-11 18:24:45 -0800971 continue;
972 }
973
974 if (s.compare(0, 8, "start:") == 0) {
975 continue;
976 }
977
978 if (s.compare(0, 2, "w:") == 0) {
979 vector<string> t;
980 if (s.find("1") != string::npos) {
981 t.push_back("");
982 }
983 if (s.find("2") != string::npos) {
984 t.push_back("2");
985 }
986 if (s.find("3") != string::npos) {
987 t.push_back("3");
988 }
989 if (s.find("4") != string::npos) {
990 t.push_back("4");
991 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800992 spec->mReplaceables.push_back(t);
Jason Sams135c4b72013-12-11 18:24:45 -0800993 continue;
994 }
995
996 if (s.compare(0, 2, "t:") == 0) {
997 vector<string> t;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -0800998 for (int i = 0; i < NUM_TYPES; i++) {
999 if (s.find(TYPES[i].specType) != string::npos) {
1000 t.push_back(TYPES[i].cType);
1001 }
Jason Sams135c4b72013-12-11 18:24:45 -08001002 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001003 spec->mReplaceables.push_back(t);
Jason Sams135c4b72013-12-11 18:24:45 -08001004 continue;
1005 }
1006
1007 if (s.size() == 0) {
1008 // eat empty line
1009 continue;
1010 }
1011
1012 printf("Error, line:\n");
1013 printf(" %s\n", s.c_str());
1014 }
1015
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001016 delete spec;
Jason Sams135c4b72013-12-11 18:24:45 -08001017 return NULL;
1018}
1019
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001020void Specification::writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile,
1021 Function* function, int versionOfTestFiles) {
1022 int start[4];
1023 int end[4];
1024 for (int i = 0; i < 4; i++) {
1025 if (i < (int)mReplaceables.size()) {
1026 start[i] = 0;
1027 end[i] = mReplaceables[i].size();
1028 } else {
1029 start[i] = -1;
1030 end[i] = 0;
1031 }
1032 }
1033 for (int i4 = start[3]; i4 < end[3]; i4++) {
1034 for (int i3 = start[2]; i3 < end[2]; i3++) {
1035 for (int i2 = start[1]; i2 < end[1]; i2++) {
1036 for (int i1 = start[0]; i1 < end[0]; i1++) {
1037 Permutation p(function, this, i1, i2, i3, i4);
1038 p.writeFiles(headerFile, rsFile, javaFile, versionOfTestFiles);
1039 }
1040 }
1041 }
1042 }
1043}
Jason Sams135c4b72013-12-11 18:24:45 -08001044
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001045bool Specification::relevantForVersion(int versionOfTestFiles) const {
1046 if (mMinVersion != 0 && mMinVersion > versionOfTestFiles) {
1047 return false;
1048 }
1049 if (mMaxVersion != 0 && mMaxVersion < versionOfTestFiles) {
1050 return false;
1051 }
1052 return true;
1053}
1054
1055string Specification::expandString(string s, int i1, int i2, int i3, int i4) const {
1056 if (mReplaceables.size() > 0) {
1057 s = stringReplace(s, "#1", mReplaceables[0][i1]);
1058 }
1059 if (mReplaceables.size() > 1) {
1060 s = stringReplace(s, "#2", mReplaceables[1][i2]);
1061 }
1062 if (mReplaceables.size() > 2) {
1063 s = stringReplace(s, "#3", mReplaceables[2][i3]);
1064 }
1065 if (mReplaceables.size() > 3) {
1066 s = stringReplace(s, "#4", mReplaceables[3][i4]);
Jason Sams135c4b72013-12-11 18:24:45 -08001067 }
1068 return s;
1069}
1070
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001071Permutation::Permutation(Function* func, Specification* spec, int i1, int i2, int i3, int i4)
1072 : mFunction(func),
1073 mSpecification(spec),
1074 mReturnIndex(-1),
1075 mFirstInputIndex(-1),
1076 mInputCount(0),
1077 mOutputCount(0) {
1078 // We expand the strings now to make capitalization easier. The previous code preserved the #n
1079 // markers just before emitting, which made capitalization difficult.
1080 mName = spec->getName(i1, i2, i3, i4);
1081 mCleanName = spec->getCleanName();
1082 mTest = spec->getTest();
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001083 mPrecisionLimit = spec->getPrecisionLimit();
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001084 spec->getInlines(i1, i2, i3, i4, &mInline);
1085 spec->getComments(i1, i2, i3, i4, &mComment);
1086
1087 vector<string> paramDefinitions;
1088 spec->getParams(i1, i2, i3, i4, &paramDefinitions);
1089 for (size_t i = 0; i < paramDefinitions.size(); i++) {
1090 ParameterDefinition* def = new ParameterDefinition();
1091 def->parseParameterDefinition(paramDefinitions[i], false, &mInputCount, &mOutputCount);
1092 if (!def->isOutParameter && mFirstInputIndex < 0) {
1093 mFirstInputIndex = mParams.size();
1094 }
1095 mParams.push_back(def);
Jason Sams135c4b72013-12-11 18:24:45 -08001096 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001097
1098 const string s = spec->getReturn(i1, i2, i3, i4);
1099 if (!s.empty() && s != "void") {
1100 ParameterDefinition* def = new ParameterDefinition();
1101 // Adding "*" tells the parse method it's an output.
1102 def->parseParameterDefinition(s, true, &mInputCount, &mOutputCount);
1103 mReturnIndex = mParams.size();
1104 mParams.push_back(def);
Jason Sams135c4b72013-12-11 18:24:45 -08001105 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001106
1107 mRsKernelName = "test" + capitalize(mName);
1108 mJavaArgumentsClassName = "Arguments";
1109 mJavaArgumentsNClassName = "Arguments";
1110 mJavaCheckMethodName = "check" + capitalize(mCleanName);
1111 mJavaVerifyMethodName = "verifyResults" + capitalize(mCleanName);
1112 for (int i = 0; i < (int)mParams.size(); i++) {
1113 const ParameterDefinition& p = *mParams[i];
1114 mRsKernelName += capitalize(p.rsType);
1115 mJavaArgumentsClassName += capitalize(p.rsBaseType);
1116 mJavaArgumentsNClassName += capitalize(p.rsBaseType);
1117 if (p.mVectorSize != "1") {
1118 mJavaArgumentsNClassName += "N";
1119 }
1120 mJavaCheckMethodName += capitalize(p.rsType);
1121 mJavaVerifyMethodName += capitalize(p.rsType);
Jason Sams135c4b72013-12-11 18:24:45 -08001122 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001123 mJavaVerifierComputeMethodName = "compute" + capitalize(mCleanName);
Jason Sams135c4b72013-12-11 18:24:45 -08001124}
1125
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001126// TODO Remove once we have long/double copyTo/copyFrom
1127bool Permutation::hasLongOrDoubleParameter() const {
1128 for (size_t i = 0; i < mParams.size(); i++) {
1129 const ParameterDefinition& p = *mParams[i];
1130 if (p.javaBaseType == "long" || p.javaBaseType == "double") {
1131 return true;
1132 }
1133 }
1134 return false;
Jason Samsea877ed2014-01-09 15:48:32 -08001135}
1136
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001137void Permutation::writeFiles(ofstream& headerFile, ofstream& rsFile, ofstream& javaFile,
1138 int versionOfTestFiles) {
1139 writeHeaderSection(headerFile);
1140 if (hasLongOrDoubleParameter()) {
1141 printf("Warning: skipping a test for %s as we don't support long or double arguments (due "
1142 "to Allocation not supporting them).\n",
1143 mName.c_str());
1144 return;
1145 }
Jason Samsea877ed2014-01-09 15:48:32 -08001146
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001147 if (mSpecification->relevantForVersion(versionOfTestFiles) && mTest != "none") {
1148 writeRsSection(rsFile);
1149 writeJavaSection(javaFile);
1150 }
1151}
1152
1153void Permutation::writeHeaderSection(ofstream& file) const {
1154 int minVersion = mSpecification->getMinVersion();
1155 int maxVersion = mSpecification->getMaxVersion();
1156 bool hasVersion = minVersion || maxVersion;
1157
1158 if (hasVersion) {
1159 if (maxVersion) {
1160 file << "#if (defined(RS_VERSION) && (RS_VERSION >= " << minVersion
1161 << ") && (RS_VERSION <= " << maxVersion << "))\n";
1162 } else {
1163 file << "#if (defined(RS_VERSION) && (RS_VERSION >= " << minVersion << "))\n";
Jason Samsea877ed2014-01-09 15:48:32 -08001164 }
1165 }
1166
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001167 file << "/*\n";
1168 for (size_t ct = 0; ct < mComment.size(); ct++) {
1169 if (!mComment[ct].empty()) {
1170 file << " * " << mComment[ct] << "\n";
1171 } else {
1172 file << " *\n";
1173 }
Jason Samsea877ed2014-01-09 15:48:32 -08001174 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001175 file << " *\n";
1176 if (minVersion || maxVersion) {
1177 if (maxVersion) {
1178 file << " * Suppored by API versions " << minVersion << " - " << maxVersion << "\n";
1179 } else {
1180 file << " * Supported by API versions " << minVersion << " and newer.\n";
1181 }
1182 }
1183 file << " */\n";
1184 if (mInline.size() > 0) {
1185 file << "static ";
1186 } else {
1187 file << "extern ";
1188 }
1189 if (mReturnIndex >= 0) {
1190 file << mParams[mReturnIndex]->rsType;
1191 } else {
1192 file << "void";
1193 }
1194 file << " __attribute__((";
1195 if (mOutputCount <= 1) {
1196 file << "const, ";
1197 }
1198 file << "overloadable))";
1199 file << mName;
1200 file << "(";
1201 bool needComma = false;
1202 for (int i = 0; i < (int)mParams.size(); i++) {
1203 if (i != mReturnIndex) {
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001204 const ParameterDefinition& p = *mParams[i];
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001205 if (needComma) {
1206 file << ", ";
1207 }
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001208 file << p.rsType;
1209 if (p.isOutParameter) {
1210 file << "*";
1211 }
1212 if (!p.specName.empty()) {
1213 file << " " << p.specName;
1214 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001215 needComma = true;
1216 }
1217 }
1218 if (mInline.size() > 0) {
1219 file << ") {\n";
1220 for (size_t ct = 0; ct < mInline.size(); ct++) {
1221 file << " " << mInline[ct].c_str() << "\n";
1222 }
1223 file << "}\n";
1224 } else {
1225 file << ");\n";
1226 }
1227 if (hasVersion) {
1228 file << "#endif\n";
1229 }
1230 file << "\n";
Jason Samsea877ed2014-01-09 15:48:32 -08001231}
1232
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001233/* Write the section of the .rs file for this permutation.
1234 *
1235 * We communicate the extra input and output parameters via global allocations.
1236 * For example, if we have a function that takes three arguments, two for input
1237 * and one for output:
1238 *
1239 * start:
1240 * name: gamn
1241 * ret: float3
1242 * arg: float3 a
1243 * arg: int b
1244 * arg: float3 *c
1245 * end:
1246 *
1247 * We'll produce:
1248 *
1249 * rs_allocation gAllocInB;
1250 * rs_allocation gAllocOutC;
1251 *
1252 * float3 __attribute__((kernel)) test_gamn_float3_int_float3(float3 inA, unsigned int x) {
1253 * int inB;
1254 * float3 outC;
1255 * float2 out;
1256 * inB = rsGetElementAt_int(gAllocInB, x);
1257 * out = gamn(a, in_b, &outC);
1258 * rsSetElementAt_float4(gAllocOutC, &outC, x);
1259 * return out;
1260 * }
1261 *
1262 * We avoid re-using x and y from the definition because these have reserved
1263 * meanings in a .rs file.
1264 */
1265void Permutation::writeRsSection(ofstream& rs) const {
1266 // Write the allocation declarations we'll need.
1267 for (int i = 0; i < (int)mParams.size(); i++) {
1268 const ParameterDefinition& p = *mParams[i];
1269 // Don't need allocation for one input and one return value.
1270 if (i != mReturnIndex && i != mFirstInputIndex) {
1271 mFunction->writeRsAllocationDefinition(p);
1272 }
1273 }
1274 rs << "\n";
Jason Samsea877ed2014-01-09 15:48:32 -08001275
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001276 // Write the function header.
1277 if (mReturnIndex >= 0) {
1278 rs << mParams[mReturnIndex]->rsType;
1279 } else {
1280 rs << "void";
1281 }
1282 rs << " __attribute__((kernel)) " << mRsKernelName;
1283 rs << "(";
1284 bool needComma = false;
1285 if (mFirstInputIndex >= 0) {
1286 rs << mParams[mFirstInputIndex]->rsType << " " << mParams[mFirstInputIndex]->variableName;
1287 needComma = true;
1288 }
1289 if (mOutputCount > 1 || mInputCount > 1) {
1290 if (needComma) {
1291 rs << ", ";
1292 }
1293 rs << "unsigned int x";
1294 }
1295 rs << ") {\n";
Jason Samsea877ed2014-01-09 15:48:32 -08001296
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001297 // Write the local variable declarations and initializations.
1298 for (int i = 0; i < (int)mParams.size(); i++) {
1299 if (i == mFirstInputIndex || i == mReturnIndex) {
1300 continue;
1301 }
1302 const ParameterDefinition& p = *mParams[i];
1303 rs << tab(1) << p.rsType << " " << p.variableName;
1304 if (p.isOutParameter) {
1305 rs << " = 0;\n";
1306 } else {
1307 rs << " = rsGetElementAt_" << p.rsType << "(" << p.rsAllocName << ", x);\n";
1308 }
1309 }
1310
1311 // Write the function call.
1312 if (mReturnIndex >= 0) {
1313 if (mOutputCount > 1) {
1314 rs << tab(1) << mParams[mReturnIndex]->rsType << " "
1315 << mParams[mReturnIndex]->variableName << " = ";
1316 } else {
1317 rs << tab(1) << "return ";
1318 }
1319 }
1320 rs << mName << "(";
1321 needComma = false;
1322 for (int i = 0; i < (int)mParams.size(); i++) {
1323 const ParameterDefinition& p = *mParams[i];
1324 if (i == mReturnIndex) {
1325 continue;
1326 }
1327 if (needComma) {
1328 rs << ", ";
1329 }
1330 if (p.isOutParameter) {
1331 rs << "&";
1332 }
1333 rs << p.variableName;
1334 needComma = true;
1335 }
1336 rs << ");\n";
1337
1338 if (mOutputCount > 1) {
1339 // Write setting the extra out parameters into the allocations.
1340 for (int i = 0; i < (int)mParams.size(); i++) {
1341 const ParameterDefinition& p = *mParams[i];
1342 if (p.isOutParameter && i != mReturnIndex) {
1343 rs << tab(1) << "rsSetElementAt_" << p.rsType << "(" << p.rsAllocName << ", ";
1344 if (passByAddressToSet(p.variableName)) {
1345 rs << "&";
1346 }
1347 rs << p.variableName << ", x);\n";
1348 }
1349 }
1350 if (mReturnIndex >= 0) {
1351 rs << tab(1) << "return " << mParams[mReturnIndex]->variableName << ";\n";
1352 }
1353 }
1354 rs << "}\n";
1355}
1356
1357bool Permutation::passByAddressToSet(const string& name) const {
1358 string s = name;
1359 int last = s.size() - 1;
1360 char lastChar = s[last];
1361 return lastChar >= '0' && lastChar <= '9';
1362}
1363
1364void Permutation::writeJavaSection(ofstream& file) const {
1365 // By default, we test the results using item by item comparison.
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001366 if (mTest == "scalar" || mTest == "limited") {
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001367 writeJavaArgumentClass(file, true);
1368 writeJavaCheckMethod(file, true);
1369 writeJavaVerifyScalarMethod(file);
1370 } else if (mTest == "vector") {
1371 writeJavaArgumentClass(file, false);
1372 writeJavaCheckMethod(file, true);
1373 writeJavaVerifyVectorMethod(file);
1374 } else if (mTest == "noverify") {
1375 writeJavaCheckMethod(file, false);
1376 }
1377
1378 // Register the check method to be called. This code will be written at the end.
1379 mFunction->addJavaCheckCall(mJavaCheckMethodName + "();");
1380}
1381
1382void Permutation::writeJavaArgumentClass(ofstream& file, bool scalar) const {
1383 string name;
1384 if (scalar) {
1385 name = mJavaArgumentsClassName;
1386 } else {
1387 name = mJavaArgumentsNClassName;
1388 }
Jason Sams135c4b72013-12-11 18:24:45 -08001389 string s;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001390 s += tab(1) + "public class " + name + " {\n";
1391 for (size_t i = 0; i < mParams.size(); i++) {
1392 const ParameterDefinition& p = *mParams[i];
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001393 s += tab(2) + "public ";
1394 if (p.isOutParameter && p.javaBaseType == "float") {
1395 s += "Floaty";
1396 } else {
1397 s += p.javaBaseType;
1398 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001399 if (!scalar && p.mVectorSize != "1") {
1400 s += "[]";
Jason Sams135c4b72013-12-11 18:24:45 -08001401 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001402 s += " " + p.variableName + ";\n";
Jason Sams135c4b72013-12-11 18:24:45 -08001403 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001404 s += tab(1) + "}\n\n";
Jason Sams135c4b72013-12-11 18:24:45 -08001405
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001406 mFunction->writeJavaArgumentClassDefinition(name, s);
Jason Sams135c4b72013-12-11 18:24:45 -08001407}
1408
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001409void Permutation::writeJavaCheckMethod(ofstream& file, bool generateCallToVerify) const {
1410 file << tab(1) << "private void " << mJavaCheckMethodName << "() {\n";
1411 // Generate the input allocations and initialization.
1412 for (size_t i = 0; i < mParams.size(); i++) {
1413 const ParameterDefinition& p = *mParams[i];
1414 if (!p.isOutParameter) {
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001415 writeJavaInputAllocationDefinition(file, tab(2), p);
1416 }
1417 }
1418 // Enforce ordering if needed.
1419 for (size_t i = 0; i < mParams.size(); i++) {
1420 const ParameterDefinition& p = *mParams[i];
1421 if (!p.isOutParameter && !p.smallerParameter.empty()) {
1422 string smallerAlloc = "in" + capitalize(p.smallerParameter);
1423 file << tab(2) << "enforceOrdering(" << smallerAlloc << ", " << p.javaAllocName
1424 << ");\n";
Jason Sams135c4b72013-12-11 18:24:45 -08001425 }
Jason Sams135c4b72013-12-11 18:24:45 -08001426 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001427 writeJavaCallToRs(file, false, generateCallToVerify);
1428 writeJavaCallToRs(file, true, generateCallToVerify);
1429 file << tab(1) << "}\n\n";
Jason Sams135c4b72013-12-11 18:24:45 -08001430}
1431
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001432void Permutation::writeJavaInputAllocationDefinition(ofstream& file, const string& indent,
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001433 const ParameterDefinition& param) const {
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001434 string dataType;
1435 char vectorSize;
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001436 convertToRsType(param.rsType, &dataType, &vectorSize);
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001437
1438 string seed = hashString(mJavaCheckMethodName + param.javaAllocName);
1439 file << indent << "Allocation " << param.javaAllocName << " = ";
1440 if (param.compatibleTypeIndex >= 0) {
1441 if (TYPES[param.typeIndex].kind == FLOATING_POINT) {
1442 writeJavaRandomCompatibleFloatAllocation(file, dataType, seed, vectorSize,
1443 TYPES[param.compatibleTypeIndex],
1444 TYPES[param.typeIndex]);
1445 } else {
1446 writeJavaRandomCompatibleIntegerAllocation(file, dataType, seed, vectorSize,
1447 TYPES[param.compatibleTypeIndex],
1448 TYPES[param.typeIndex]);
1449 }
1450 } else if (!param.minValue.empty()) {
1451 if (TYPES[param.typeIndex].kind != FLOATING_POINT) {
1452 printf("range(,) is only supported for floating point\n");
1453 } else {
1454 file << "createRandomFloatAllocation(mRS, Element.DataType." << dataType << ", "
1455 << vectorSize << ", " << seed << ", " << param.minValue << ", " << param.maxValue
1456 << ")";
1457 }
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001458 } else {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001459 file << "createRandomAllocation(mRS, Element.DataType." << dataType << ", " << vectorSize
1460 << ", " << seed << ", false)"; // TODO set to false only for native
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001461 }
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001462 file << ";\n";
1463}
1464
1465void Permutation::writeJavaRandomCompatibleFloatAllocation(ofstream& file, const string& dataType,
1466 const string& seed, char vectorSize,
1467 const Type& compatibleType,
1468 const Type& generatedType) const {
1469 file << "createRandomFloatAllocation"
1470 << "(mRS, Element.DataType." << dataType << ", " << vectorSize << ", " << seed << ", ";
Jean-Luc Brouillet2f281cf2014-03-12 18:25:33 -07001471 double minValue = 0.0;
1472 double maxValue = 0.0;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001473 switch (compatibleType.kind) {
1474 case FLOATING_POINT: {
1475 // We're generating floating point values. We just have to worry about the
1476 // exponent. Subtract 1 for the sign.
1477 int bits = min(compatibleType.significantBits, generatedType.significantBits) - 1;
Jean-Luc Brouillet2f281cf2014-03-12 18:25:33 -07001478 maxValue = ldexp(0.95, (1 << bits) - 1);
1479 minValue = -maxValue;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001480 break;
1481 }
1482 case UNSIGNED_INTEGER:
Jean-Luc Brouillet2f281cf2014-03-12 18:25:33 -07001483 minValue = 0.0;
1484 maxValue = ldexp(1, compatibleType.significantBits) - 1.0;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001485 break;
Jean-Luc Brouillet2f281cf2014-03-12 18:25:33 -07001486 case SIGNED_INTEGER:
1487 minValue = -ldexp(1, compatibleType.significantBits);
1488 maxValue = ldexp(1, compatibleType.significantBits) - 1;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001489 break;
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001490 }
Jean-Luc Brouillet2f281cf2014-03-12 18:25:33 -07001491 file << scientific << std::setprecision(10);
1492 file << minValue << ", " << maxValue << ")";
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001493 file.unsetf(ios_base::floatfield);
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001494}
1495
1496void Permutation::writeJavaRandomCompatibleIntegerAllocation(ofstream& file, const string& dataType,
1497 const string& seed, char vectorSize,
1498 const Type& compatibleType,
1499 const Type& generatedType) const {
1500 file << "createRandomIntegerAllocation"
1501 << "(mRS, Element.DataType." << dataType << ", " << vectorSize << ", " << seed << ", ";
1502
1503 if (compatibleType.kind == FLOATING_POINT) {
1504 // Currently, all floating points can take any number we generate.
1505 bool isSigned = generatedType.kind == SIGNED_INTEGER;
1506 file << (isSigned ? "true" : "false") << ", " << generatedType.significantBits;
1507 } else {
1508 bool isSigned =
1509 compatibleType.kind == SIGNED_INTEGER && generatedType.kind == SIGNED_INTEGER;
1510 file << (isSigned ? "true" : "false") << ", "
1511 << min(compatibleType.significantBits, generatedType.significantBits);
1512 }
1513 file << ")";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001514}
Jason Sams135c4b72013-12-11 18:24:45 -08001515
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001516void Permutation::writeJavaOutputAllocationDefinition(ofstream& file, const string& indent,
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001517 const ParameterDefinition& param) const {
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001518 string dataType;
1519 char vectorSize;
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001520 convertToRsType(param.rsType, &dataType, &vectorSize);
1521 file << indent << "Allocation " << param.javaAllocName << " = Allocation.createSized(mRS, "
1522 << "getElement(mRS, Element.DataType." << dataType << ", " << vectorSize
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001523 << "), INPUTSIZE);\n";
1524}
Jason Sams135c4b72013-12-11 18:24:45 -08001525
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001526// Converts float2 to FLOAT_32 and 2, etc.
1527void Permutation::convertToRsType(const string& name, string* dataType, char* vectorSize) const {
1528 string s = name;
1529 int last = s.size() - 1;
1530 char lastChar = s[last];
1531 if (lastChar >= '1' && lastChar <= '4') {
1532 s.erase(last);
1533 *vectorSize = lastChar;
1534 } else {
1535 *vectorSize = '1';
Jason Sams135c4b72013-12-11 18:24:45 -08001536 }
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001537 dataType->clear();
1538 for (int i = 0; i < NUM_TYPES; i++) {
1539 if (s == TYPES[i].cType) {
1540 *dataType = TYPES[i].rsDataType;
Jason Sams135c4b72013-12-11 18:24:45 -08001541 break;
1542 }
1543 }
Jason Sams135c4b72013-12-11 18:24:45 -08001544}
1545
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001546void Permutation::writeJavaVerifyScalarMethod(ofstream& file) const {
1547 writeJavaVerifyFunctionHeader(file);
1548 string vectorSize = "1";
1549 for (size_t i = 0; i < mParams.size(); i++) {
1550 const ParameterDefinition& p = *mParams[i];
1551 writeJavaArrayInitialization(file, p);
1552 if (p.mVectorSize != "1" && p.mVectorSize != vectorSize) {
1553 if (vectorSize == "1") {
1554 vectorSize = p.mVectorSize;
1555 } else {
1556 printf("Yikes, had vector %s and %s\n", vectorSize.c_str(), p.mVectorSize.c_str());
1557 }
1558 }
1559 }
Jason Sams135c4b72013-12-11 18:24:45 -08001560
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001561 file << tab(2) << "for (int i = 0; i < INPUTSIZE; i++) {\n";
1562 file << tab(3) << "for (int j = 0; j < " << vectorSize << " ; j++) {\n";
1563
1564 file << tab(4) << "// Extract the inputs.\n";
1565 file << tab(4) << mJavaArgumentsClassName << " args = new " << mJavaArgumentsClassName
1566 << "();\n";
1567 for (size_t i = 0; i < mParams.size(); i++) {
1568 const ParameterDefinition& p = *mParams[i];
1569 if (!p.isOutParameter) {
1570 file << tab(4) << "args." << p.variableName << " = " << p.javaArrayName << "[i";
1571 if (p.vectorWidth != "1") {
1572 file << " * " << p.vectorWidth << " + j";
1573 }
1574 file << "];\n";
1575 }
1576 }
1577
1578 file << tab(4) << "// Figure out what the outputs should have been.\n";
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001579 file << tab(4) << "Floaty.setRelaxed(relaxed);\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001580 file << tab(4) << "CoreMathVerifier." << mJavaVerifierComputeMethodName << "(args);\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001581
1582 file << tab(4) << "// Figure out what the outputs should have been.\n";
1583 file << tab(4) << "boolean valid = true;\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001584 for (size_t i = 0; i < mParams.size(); i++) {
1585 const ParameterDefinition& p = *mParams[i];
1586 if (p.isOutParameter) {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001587 writeJavaTestAndSetValid(file, 4, p, "", "[i * " + p.vectorWidth + " + j]");
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001588 }
1589 }
1590
1591 file << tab(4) << "if (!valid) {\n";
1592 file << tab(5) << "StringBuilder message = new StringBuilder();\n";
1593 for (size_t i = 0; i < mParams.size(); i++) {
1594 const ParameterDefinition& p = *mParams[i];
1595 if (p.isOutParameter) {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001596 writeJavaAppendOutputToMessage(file, 5, p, "", "[i * " + p.vectorWidth + " + j]");
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001597 } else {
1598 writeJavaAppendInputToMessage(file, 5, p.rsBaseType, p.variableName,
1599 "args." + p.variableName);
1600 }
1601 }
1602
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001603 file << tab(5) << "assertTrue(\"Incorrect output for " << mJavaCheckMethodName << "\" +\n";
1604 file << tab(7) << "(relaxed ? \"_relaxed\" : \"\") + \":\\n\" + message.toString(), valid);\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001605 file << tab(4) << "}\n";
1606 file << tab(3) << "}\n";
1607 file << tab(2) << "}\n";
1608 file << tab(1) << "}\n\n";
1609}
1610
1611void Permutation::writeJavaVerifyFunctionHeader(ofstream& file) const {
1612 file << tab(1) << "private void " << mJavaVerifyMethodName << "(";
1613 for (size_t i = 0; i < mParams.size(); i++) {
1614 const ParameterDefinition& p = *mParams[i];
1615 file << "Allocation " << p.javaAllocName << ", ";
1616 }
1617 file << "boolean relaxed) {\n";
1618}
1619
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001620void Permutation::writeJavaTestAndSetValid(ofstream& file, int indent, const ParameterDefinition& p,
1621 const string& argsIndex,
1622 const string& actualIndex) const {
1623 writeJavaTestOneValue(file, indent, p, argsIndex, actualIndex);
1624 file << tab(indent + 1) << "valid = false;\n";
1625 file << tab(indent) << "}\n";
1626}
1627
1628void Permutation::writeJavaTestOneValue(ofstream& file, int indent, const ParameterDefinition& p,
1629 const string& argsIndex, const string& actualIndex) const {
1630 file << tab(indent) << "if (";
1631 if (p.rsBaseType[0] == 'f') {
1632 file << "!args." << p.variableName << argsIndex << ".couldBe(" << p.javaArrayName
1633 << actualIndex;
1634 if (!mPrecisionLimit.empty()) {
1635 file << ", " << mPrecisionLimit;
1636 }
1637 file << ")";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001638 } else {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001639 file << "args." << p.variableName << argsIndex << " != " << p.javaArrayName << actualIndex;
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001640 }
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001641 if (p.undefinedIfOutIsNan && mReturnIndex >= 0) {
1642 file << " && args." << mParams[mReturnIndex]->variableName << argsIndex << ".isNaN()";
1643 }
1644 file << ") {\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001645}
1646
1647void Permutation::writeJavaAppendOutputToMessage(ofstream& file, int indent,
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001648 const ParameterDefinition& p,
1649 const string& argsIndex,
1650 const string& actualIndex) const {
1651 const string expected = "args." + p.variableName + argsIndex;
1652 const string actual = p.javaArrayName + actualIndex;
1653 file << tab(indent) << "message.append(\"Expected output " + p.variableName + ": \");\n";
1654 if (p.rsBaseType[0] == 'f') {
1655 writeJavaAppendFloatyVariableToMessage(file, indent, expected);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001656 } else {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001657 writeJavaAppendVariableToMessage(file, indent, p.rsBaseType, expected);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001658 }
1659 writeJavaAppendNewLineToMessage(file, indent);
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001660 file << tab(indent) << "message.append(\"Actual output " + p.variableName + ": \");\n";
1661 writeJavaAppendVariableToMessage(file, indent, p.rsBaseType, actual);
1662
1663 writeJavaTestOneValue(file, indent, p, argsIndex, actualIndex);
1664 file << tab(indent + 1) << "message.append(\" FAIL\");\n";
1665 file << tab(indent) << "}\n";
1666 writeJavaAppendNewLineToMessage(file, indent);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001667}
1668
1669void Permutation::writeJavaAppendInputToMessage(ofstream& file, int indent,
1670 const string& rsBaseType, const string& name,
1671 const string& actual) const {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001672 file << tab(indent) << "message.append(\"Input " + name + ": \");\n";
1673 writeJavaAppendVariableToMessage(file, indent, rsBaseType, actual);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001674 writeJavaAppendNewLineToMessage(file, indent);
1675}
1676
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001677void Permutation::writeJavaAppendNewLineToMessage(ofstream& file, int indent) const {
1678 file << tab(indent) << "message.append(\"\\n\");\n";
1679}
1680
1681void Permutation::writeJavaAppendVariableToMessage(ofstream& file, int indent,
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001682 const string& rsBaseType,
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001683 const string& value) const {
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001684 if (rsBaseType[0] == 'f') {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001685 file << tab(indent) << "message.append(String.format(\"%14.8g %8x %15a\",\n";
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001686 file << tab(indent + 2) << value << ", "
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001687 << "Float.floatToRawIntBits(" << value << "), " << value << "));\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001688 } else if (rsBaseType[0] == 'u') {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001689 file << tab(indent) << "message.append(String.format(\"0x%x\", " << value << "));\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001690 } else {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001691 file << tab(indent) << "message.append(String.format(\"%d\", " << value << "));\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001692 }
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001693}
1694
1695void Permutation::writeJavaAppendFloatyVariableToMessage(ofstream& file, int indent,
1696 const string& value) const {
1697 file << tab(indent) << "message.append(" << value << ".toString());\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001698}
1699
1700void Permutation::writeJavaVectorComparison(ofstream& file, int indent,
1701 const ParameterDefinition& p) const {
1702 if (p.mVectorSize == "1") {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001703 writeJavaTestAndSetValid(file, indent, p, "", "[i]");
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001704
1705 } else {
1706 file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001707 writeJavaTestAndSetValid(file, indent + 1, p, "[j]", "[i * " + p.vectorWidth + " + j]");
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001708 file << tab(indent) << "}\n";
1709 }
1710}
1711
1712void Permutation::writeJavaAppendVectorInputToMessage(ofstream& file, int indent,
1713 const ParameterDefinition& p) const {
1714 if (p.mVectorSize == "1") {
1715 writeJavaAppendInputToMessage(file, indent, p.rsBaseType, p.variableName,
1716 p.javaArrayName + "[i]");
1717 } else {
1718 file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
1719 writeJavaAppendInputToMessage(file, indent + 1, p.rsBaseType, p.variableName,
1720 p.javaArrayName + "[i * " + p.vectorWidth + " + j]");
1721 file << tab(indent) << "}\n";
1722 }
1723}
1724
1725void Permutation::writeJavaAppendVectorOutputToMessage(ofstream& file, int indent,
1726 const ParameterDefinition& p) const {
1727 if (p.mVectorSize == "1") {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001728 writeJavaAppendOutputToMessage(file, indent, p, "", "[i]");
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001729
1730 } else {
1731 file << tab(indent) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001732 writeJavaAppendOutputToMessage(file, indent + 1, p, "[j]",
1733 "[i * " + p.vectorWidth + " + j]");
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001734 file << tab(indent) << "}\n";
1735 }
1736}
1737
1738void Permutation::writeJavaVerifyVectorMethod(ofstream& file) const {
1739 writeJavaVerifyFunctionHeader(file);
1740 for (size_t i = 0; i < mParams.size(); i++) {
1741 const ParameterDefinition& p = *mParams[i];
1742 writeJavaArrayInitialization(file, p);
1743 }
1744 file << tab(2) + "for (int i = 0; i < INPUTSIZE; i++) {\n";
1745 file << tab(3) << mJavaArgumentsNClassName << " args = new " << mJavaArgumentsNClassName
1746 << "();\n";
1747
1748 file << tab(3) << "// Create the appropriate sized arrays in args\n";
1749 for (size_t i = 0; i < mParams.size(); i++) {
1750 const ParameterDefinition& p = *mParams[i];
1751 if (p.mVectorSize != "1") {
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001752 string type = p.javaBaseType;
1753 if (p.isOutParameter && type == "float") {
1754 type = "Floaty";
1755 }
1756 file << tab(3) << "args." << p.variableName << " = new " << type << "[" << p.mVectorSize
1757 << "];\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001758 }
1759 }
1760
1761 file << tab(3) << "// Fill args with the input values\n";
1762 for (size_t i = 0; i < mParams.size(); i++) {
1763 const ParameterDefinition& p = *mParams[i];
1764 if (!p.isOutParameter) {
1765 if (p.mVectorSize == "1") {
1766 file << tab(3) << "args." << p.variableName << " = " << p.javaArrayName + "[i]"
1767 << ";\n";
1768 } else {
1769 file << tab(3) << "for (int j = 0; j < " << p.mVectorSize << " ; j++) {\n";
1770 file << tab(4) << "args." << p.variableName + "[j] = "
1771 << p.javaArrayName + "[i * " + p.vectorWidth + " + j]"
1772 << ";\n";
1773 file << tab(3) << "}\n";
1774 }
1775 }
1776 }
Jean-Luc Brouilletbcd5b9a2014-03-07 18:00:57 -08001777 file << tab(3) << "Floaty.setRelaxed(relaxed);\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001778 file << tab(3) << "CoreMathVerifier." << mJavaVerifierComputeMethodName << "(args);\n\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001779
1780 file << tab(3) << "// Compare the expected outputs to the actual values returned by RS.\n";
1781 file << tab(3) << "boolean valid = true;\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001782 for (size_t i = 0; i < mParams.size(); i++) {
1783 const ParameterDefinition& p = *mParams[i];
1784 if (p.isOutParameter) {
1785 writeJavaVectorComparison(file, 3, p);
1786 }
1787 }
1788
1789 file << tab(3) << "if (!valid) {\n";
1790 file << tab(4) << "StringBuilder message = new StringBuilder();\n";
1791 for (size_t i = 0; i < mParams.size(); i++) {
1792 const ParameterDefinition& p = *mParams[i];
1793 if (p.isOutParameter) {
1794 writeJavaAppendVectorOutputToMessage(file, 4, p);
1795 } else {
1796 writeJavaAppendVectorInputToMessage(file, 4, p);
1797 }
1798 }
1799
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001800 file << tab(4) << "assertTrue(\"Incorrect output for " << mJavaCheckMethodName << "\" +\n";
1801 file << tab(6) << "(relaxed ? \"_relaxed\" : \"\") + \":\\n\" + message.toString(), valid);\n";
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001802 file << tab(3) << "}\n";
1803 file << tab(2) << "}\n";
1804 file << tab(1) << "}\n\n";
1805}
1806
1807void Permutation::writeJavaCallToRs(ofstream& file, bool relaxed, bool generateCallToVerify) const {
1808 string script = "script";
1809 if (relaxed) {
1810 script += "Relaxed";
1811 }
1812
1813 file << tab(2) << "try {\n";
1814 for (size_t i = 0; i < mParams.size(); i++) {
1815 const ParameterDefinition& p = *mParams[i];
1816 if (p.isOutParameter) {
Jean-Luc Brouillet46341432014-02-21 22:49:22 -08001817 writeJavaOutputAllocationDefinition(file, tab(3), p);
Jean-Luc Brouillet963c3672014-02-12 20:58:47 -08001818 }
1819 }
1820
1821 for (int i = 0; i < (int)mParams.size(); i++) {
1822 const ParameterDefinition& p = *mParams[i];
1823 if (i != mReturnIndex && i != mFirstInputIndex) {
1824 file << tab(3) << script << ".set_" << p.rsAllocName << "(" << p.javaAllocName
1825 << ");\n";
1826 }
1827 }
1828
1829 file << tab(3) << script << ".forEach_" << mRsKernelName << "(";
1830 bool needComma = false;
1831 if (mFirstInputIndex >= 0) {
1832 file << mParams[mFirstInputIndex]->javaAllocName;
1833 needComma = true;
1834 }
1835 if (mReturnIndex >= 0) {
1836 if (needComma) {
1837 file << ", ";
1838 }
1839 file << mParams[mReturnIndex]->variableName << ");\n";
1840 }
1841
1842 if (generateCallToVerify) {
1843 file << tab(3) << mJavaVerifyMethodName << "(";
1844 for (size_t i = 0; i < mParams.size(); i++) {
1845 const ParameterDefinition& p = *mParams[i];
1846 file << p.variableName << ", ";
1847 }
1848
1849 if (relaxed) {
1850 file << "true";
1851 } else {
1852 file << "false";
1853 }
1854 file << ");\n";
1855 }
1856 file << tab(2) << "} catch (Exception e) {\n";
1857 file << tab(3) << "throw new RSRuntimeException(\"RenderScript. Can't invoke forEach_"
1858 << mRsKernelName << ": \" + e.toString());\n";
1859 file << tab(2) << "}\n";
1860}
1861
1862} // namespace
1863
1864int main(int argc, char* argv[]) {
1865 int versionOfTestFiles = 0;
1866 vector<string> specFileNames;
1867 if (!parseCommandLine(argc, argv, &versionOfTestFiles, &specFileNames)) {
1868 printf("Usage: gen_runtime spec_file [spec_file...] [-v version_of_test_files]\n");
1869 return -1;
1870 }
1871 int result = 0;
1872 for (size_t i = 0; i < specFileNames.size(); i++) {
1873 SpecFile specFile(specFileNames[i]);
1874 if (!specFile.process(versionOfTestFiles)) {
1875 result = -1;
1876 }
1877 }
1878 return result;
1879}