1 // 2 // Copyright (c) 2017 The Khronos Group Inc. 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 #ifndef TESTPRINTF_INCLUDED_H 17 #define TESTPRINTF_INCLUDED_H 18 19 #include "harness/compat.h" 20 #include "harness/testHarness.h" 21 #include "harness/rounding_mode.h" 22 23 #include <stdio.h> 24 #include <string.h> 25 #include <vector> 26 27 #ifdef __APPLE__ 28 #include <OpenCL/opencl.h> 29 #include <OpenCL/cl_platform.h> 30 #else 31 #include <CL/opencl.h> 32 #include <CL/cl_platform.h> 33 #endif 34 35 #include <CL/cl_half.h> 36 37 #define ANALYSIS_BUFFER_SIZE 256 38 39 //----------------------------------------- 40 // Definitions and initializations 41 //----------------------------------------- 42 43 //----------------------------------------- 44 // Types 45 //----------------------------------------- 46 enum PrintfTestType 47 { 48 TYPE_INT, 49 TYPE_HALF, 50 TYPE_HALF_LIMITS, 51 TYPE_FLOAT, 52 TYPE_FLOAT_LIMITS, 53 TYPE_OCTAL, 54 TYPE_UNSIGNED, 55 TYPE_HEXADEC, 56 TYPE_CHAR, 57 TYPE_STRING, 58 TYPE_VECTOR, 59 TYPE_ADDRESS_SPACE, 60 TYPE_COUNT 61 }; 62 63 struct printDataGenParameters 64 { 65 const char* genericFormat; 66 const char* dataRepresentation; 67 const char* vectorFormatFlag; 68 const char* vectorFormatSpecifier; 69 const char* dataType; 70 const char* vectorSize; 71 const char* addrSpaceArgumentTypeQualifier; 72 const char* addrSpaceVariableTypeQualifier; 73 const char* addrSpaceParameter; 74 const char* addrSpacePAdd; 75 }; 76 77 // Reference results - filled out at run-time 78 static std::vector<std::string> correctBufferInt; 79 static std::vector<std::string> correctBufferHalf; 80 static std::vector<std::string> correctBufferFloat; 81 static std::vector<std::string> correctBufferOctal; 82 static std::vector<std::string> correctBufferUnsigned; 83 static std::vector<std::string> correctBufferHexadecimal; 84 // Reference results - Compile-time known 85 extern std::vector<std::string> correctBufferChar; 86 extern std::vector<std::string> correctBufferString; 87 extern std::vector<std::string> correctBufferFloatLimits; 88 extern std::vector<std::string> correctBufferVector; 89 extern std::vector<std::string> correctAddrSpace; 90 91 // Helper for generating reference results 92 void generateRef(const cl_device_id device); 93 94 //----------------------------------------- 95 //Test Case 96 //----------------------------------------- 97 98 struct testCase 99 { 100 enum PrintfTestType _type; //(data)type for test 101 std::vector<std::string>& _correctBuffer; //look-up table for correct results for printf 102 std::vector<printDataGenParameters>& _genParameters; //auxiliary data to build the code for kernel source 103 void (*printFN)(printDataGenParameters&, 104 char*, 105 const size_t); //function pointer for generating reference results 106 Type dataType; //the data type that will be printed during reference result generation (used for setting rounding mode) 107 }; 108 109 extern const char* strType[]; 110 extern std::vector<testCase*> allTestCase; 111 extern cl_half_rounding_mode half_rounding_mode; 112 113 //----------------------------------------- 114 115 size_t verifyOutputBuffer(char *analysisBuffer,testCase* pTestCase,size_t testId,cl_ulong pAddr = 0); 116 117 // Helpful macros 118 119 // The next three functions check on different return values. Returns -1 120 // if the check failed 121 #define checkErr(err, msg) \ 122 if (err != CL_SUCCESS) { \ 123 log_error("%s failed errcode:%d\n", msg, err); \ 124 return -1; \ 125 } 126 127 #define checkZero(val, msg) \ 128 if (val == 0) { \ 129 log_error("%s failed errcode:%d\n", msg, err); \ 130 return -1; \ 131 } 132 133 #define checkNull(ptr, msg) \ 134 if (!ptr) { \ 135 log_error("%s failed\n", msg); \ 136 return TEST_FAIL; \ 137 } 138 139 // When a helper returns a negative one, we want to return from main 140 // with negative one. This helper prevents me from having to write 141 // this multiple time 142 #define checkHelperErr(err) \ 143 if (err == -1) { \ 144 return err; \ 145 } 146 147 #endif // TESTSPRINTF_INCLUDED_H 148