1/* 2 * Copyright (C) 2015 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 17#include "shared.rsh" 18 19double gAllowedFloatMatError = 0.00000001; 20double gAllowedDoubleMatError = 0.00000000001; 21static bool hadError = false; 22static int2 errorLoc = {0,0}; 23 24#define printCell(txt, a, xy) \ 25{ \ 26 rs_element e = rsAllocationGetElement(a); \ 27 rs_data_type dt = rsElementGetDataType(e); \ 28 uint32_t vs = rsElementGetVectorSize(e); \ 29 \ 30 if (dt == RS_TYPE_UNSIGNED_8) { \ 31 switch(vs) { \ 32 case 4: \ 33 rsDebug(txt, rsGetElementAt_uchar4(a, xy.x, xy.y)); \ 34 break; \ 35 case 3: \ 36 rsDebug(txt, rsGetElementAt_uchar3(a, xy.x, xy.y)); \ 37 break; \ 38 case 2: \ 39 rsDebug(txt, rsGetElementAt_uchar2(a, xy.x, xy.y)); \ 40 break; \ 41 case 1: \ 42 rsDebug(txt, rsGetElementAt_uchar(a, xy.x, xy.y)); \ 43 break; \ 44 } \ 45 } else if (dt == RS_TYPE_FLOAT_32) { \ 46 switch(vs) { \ 47 case 4: \ 48 rsDebug(txt, rsGetElementAt_float4(a, xy.x, xy.y)); \ 49 break; \ 50 case 3: \ 51 rsDebug(txt, rsGetElementAt_float3(a, xy.x, xy.y)); \ 52 break; \ 53 case 2: \ 54 rsDebug(txt, rsGetElementAt_float2(a, xy.x, xy.y)); \ 55 break; \ 56 case 1: \ 57 rsDebug(txt, rsGetElementAt_float(a, xy.x, xy.y)); \ 58 break; \ 59 } \ 60 } else if (dt == RS_TYPE_FLOAT_64) { \ 61 switch(vs) { \ 62 case 4: \ 63 rsDebug(txt, rsGetElementAt_double4(a, xy.x, xy.y)); \ 64 break; \ 65 case 3: \ 66 rsDebug(txt, rsGetElementAt_double3(a, xy.x, xy.y)); \ 67 break; \ 68 case 2: \ 69 rsDebug(txt, rsGetElementAt_double2(a, xy.x, xy.y)); \ 70 break; \ 71 case 1: \ 72 rsDebug(txt, rsGetElementAt_double(a, xy.x, xy.y)); \ 73 } \ 74 } \ 75} 76 77static bool verify_CMatrix(rs_allocation in1, rs_allocation in2, double l2Norm, bool isUpperMatrix) { 78 uint32_t w = rsAllocationGetDimX(in1); 79 uint32_t h = rsAllocationGetDimY(in1); 80 for (uint32_t y = 0; y < h; y++) { 81 uint32_t xStart = 0; 82 if (isUpperMatrix) { 83 // Just test the upper matrix for certain BLAS routines 84 xStart = y; 85 } 86 for (uint32_t x = xStart; x < w; x++) { 87 float2 pref = rsGetElementAt_float2(in1, x, y); 88 float2 ptst = rsGetElementAt_float2(in2, x, y); 89 double absErr = (pref.x - ptst.x) * (pref.x - ptst.x) + (pref.y - ptst.y) * (pref.y - ptst.y); 90 if (absErr > l2Norm * gAllowedFloatMatError) { 91 errorLoc.x = x; 92 errorLoc.y = y; 93 hadError = true; 94 return false; 95 } 96 } 97 } 98 return true; 99} 100 101static bool verify_SMatrix(rs_allocation in1, rs_allocation in2, double l2Norm, bool isUpperMatrix) { 102 uint32_t w = rsAllocationGetDimX(in1); 103 uint32_t h = rsAllocationGetDimY(in1); 104 for (uint32_t y = 0; y < h; y++) { 105 uint32_t xStart = 0; 106 if (isUpperMatrix) { 107 // Just test the upper matrix for certain BLAS routines 108 xStart = y; 109 } 110 for (uint32_t x = xStart; x < w; x++) { 111 float pref = rsGetElementAt_float(in1, x, y); 112 float ptst = rsGetElementAt_float(in2, x, y); 113 double absErr = (pref - ptst) * (pref - ptst); 114 if (absErr > l2Norm * gAllowedFloatMatError) { 115 errorLoc.x = x; 116 errorLoc.y = y; 117 hadError = true; 118 return false; 119 } 120 } 121 } 122 return true; 123} 124 125static bool verify_ZMatrix(rs_allocation in1, rs_allocation in2, double l2Norm, bool isUpperMatrix) { 126 uint32_t w = rsAllocationGetDimX(in1); 127 uint32_t h = rsAllocationGetDimY(in1); 128 for (uint32_t y = 0; y < h; y++) { 129 uint32_t xStart = 0; 130 if (isUpperMatrix) { 131 // Just test the upper matrix for certain BLAS routines 132 xStart = y; 133 } 134 for (uint32_t x = xStart; x < w; x++) { 135 double2 pref = rsGetElementAt_double2(in1, x, y); 136 double2 ptst = rsGetElementAt_double2(in2, x, y); 137 double absErr = (pref.x - ptst.x) * (pref.x - ptst.x) + (pref.y - ptst.y) * (pref.y - ptst.y); 138 if (absErr > l2Norm * gAllowedDoubleMatError) { 139 errorLoc.x = x; 140 errorLoc.y = y; 141 hadError = true; 142 return false; 143 } 144 } 145 } 146 return true; 147} 148 149static bool verify_DMatrix(rs_allocation in1, rs_allocation in2, double l2Norm, bool isUpperMatrix) { 150 uint32_t w = rsAllocationGetDimX(in1); 151 uint32_t h = rsAllocationGetDimY(in1); 152 for (uint32_t y = 0; y < h; y++) { 153 uint32_t xStart = 0; 154 if (isUpperMatrix) { 155 // Just test the upper matrix for certain BLAS routines 156 xStart = y; 157 } 158 for (uint32_t x = xStart; x < w; x++) { 159 double pref = rsGetElementAt_double(in1, x, y); 160 double ptst = rsGetElementAt_double(in2, x, y); 161 double absErr = (pref - ptst) * (pref - ptst); 162 if (absErr > l2Norm * gAllowedDoubleMatError) { 163 errorLoc.x = x; 164 errorLoc.y = y; 165 hadError = true; 166 return false; 167 } 168 } 169 } 170 return true; 171} 172 173void verifyMatrix(rs_allocation ref_in, rs_allocation tst_in, double l2Norm, bool isUpperMatrix) { 174 rs_element e = rsAllocationGetElement(ref_in); 175 rs_data_type dt = rsElementGetDataType(e); 176 uint32_t vs = rsElementGetVectorSize(e); 177 bool valid = false; 178 179 if (dt == RS_TYPE_FLOAT_32) { 180 switch(vs) { 181 case 2: 182 valid = verify_CMatrix(ref_in, tst_in, l2Norm, isUpperMatrix); 183 break; 184 case 1: 185 valid = verify_SMatrix(ref_in, tst_in, l2Norm, isUpperMatrix); 186 break; 187 } 188 } else if (dt == RS_TYPE_FLOAT_64) { 189 switch(vs) { 190 case 2: 191 valid = verify_ZMatrix(ref_in, tst_in, l2Norm, isUpperMatrix); 192 break; 193 case 1: 194 valid = verify_DMatrix(ref_in, tst_in, l2Norm, isUpperMatrix); 195 break; 196 } 197 } 198 if (!valid) { 199 rsDebug("verify failure at xy", errorLoc); 200 printCell("Expected value ", ref_in, errorLoc); 201 printCell("Actual value ", tst_in, errorLoc); 202 } 203} 204 205void checkError() 206{ 207 if (hadError) { 208 rsSendToClientBlocking(RS_MSG_TEST_FAILED); 209 } else { 210 rsSendToClientBlocking(RS_MSG_TEST_PASSED); 211 } 212} 213