match_template.cl 20.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors as is and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.

#define DATA_SIZE ((int)sizeof(type))
#define ELEM_TYPE elem_type
#define ELEM_SIZE ((int)sizeof(elem_type))

36 37
#define SQSUMS_PTR(ox, oy) mad24(y + oy, src_sqsums_step, mad24(x + ox, cn, src_sqsums_offset))
#define SUMS_PTR(ox, oy) mad24(y + oy, src_sums_step, mad24(x + ox, cn, src_sums_offset))
E
Elena Gvozdeva 已提交
38 39
#define SUMS(ox, oy)    mad24(y+oy, src_sums_step, mad24(x+ox, (int)sizeof(T1)*cn, src_sums_offset))
#define SQ_SUMS(ox, oy) mad24(y+oy, src_sqsums_step, mad24(x+ox, (int)sizeof(T1)*cn, src_sqsums_offset))
40 41 42

inline float normAcc(float num, float denum)
{
43
    if (fabs(num) < denum)
44
        return num / denum;
45
    if (fabs(num) < denum * 1.125f)
46 47 48 49 50 51
        return num > 0 ? 1 : -1;
    return 0;
}

inline float normAcc_SQDIFF(float num, float denum)
{
52
    if (fabs(num) < denum)
53
        return num / denum;
54
    if (fabs(num) < denum * 1.125f)
55 56 57 58
        return num > 0 ? 1 : -1;
    return 1;
}

59
#define noconvert
60

61 62 63 64
#if cn == 1
#define convertToDT(value) (float)(value)
#elif cn == 2
#define convertToDT(value) (float)(value.x + value.y)
E
Elena Gvozdeva 已提交
65 66
#elif cn == 3
#define convertToDT(value) (float)(value.x + value.y + value.z)
67 68 69
#elif cn == 4
#define convertToDT(value) (float)(value.x + value.y + value.z + value.w)
#else
E
Elena Gvozdeva 已提交
70 71 72 73 74 75 76 77 78
#error "cn should be 1-4"
#endif

#if cn != 3
#define loadpix(addr) *(__global const T *)(addr)
#define TSIZE (int)sizeof(T)
#else
#define loadpix(addr) vload3(0, (__global const T1 *)(addr))
#define TSIZE ((int)sizeof(T1)*3)
79 80 81 82
#endif

#ifdef CALC_SUM

I
Ilya Lavrenov 已提交
83 84
__kernel void calcSum(__global const uchar * srcptr, int src_step, int src_offset,
                      int cols, int total, __global float * dst)
85
{
I
Ilya Lavrenov 已提交
86
    int lid = get_local_id(0), id = get_global_id(0);
87

I
Ilya Lavrenov 已提交
88 89
    __local WT localmem[WGS2_ALIGNED];
    WT accumulator = (WT)(0), tmp;
90

I
Ilya Lavrenov 已提交
91
    for ( ; id < total; id += WGS)
92
    {
E
Elena Gvozdeva 已提交
93 94
        int src_index = mad24(id / cols, src_step, mad24(id % cols, TSIZE, src_offset));
        T src = loadpix(srcptr + src_index);
I
Ilya Lavrenov 已提交
95

E
Elena Gvozdeva 已提交
96
        tmp = convertToWT(src);
97
#if wdepth == 4
I
Ilya Lavrenov 已提交
98
        accumulator = mad24(tmp, tmp, accumulator);
99
#else
I
Ilya Lavrenov 已提交
100
        accumulator = mad(tmp, tmp, accumulator);
101
#endif
I
Ilya Lavrenov 已提交
102
    }
103

I
Ilya Lavrenov 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    if (lid < WGS2_ALIGNED)
        localmem[lid] = accumulator;
    barrier(CLK_LOCAL_MEM_FENCE);

    if (lid >= WGS2_ALIGNED && total >= WGS2_ALIGNED)
        localmem[lid - WGS2_ALIGNED] += accumulator;
    barrier(CLK_LOCAL_MEM_FENCE);

    for (int lsize = WGS2_ALIGNED >> 1; lsize > 0; lsize >>= 1)
    {
        if (lid < lsize)
        {
            int lid2 = lsize + lid;
            localmem[lid] += localmem[lid2];
        }
        barrier(CLK_LOCAL_MEM_FENCE);
120
    }
121

I
Ilya Lavrenov 已提交
122 123
    if (lid == 0)
        dst[0] = convertToDT(localmem[0]);
124
}
125

126
#elif defined CCORR
127

E
Elena Gvozdeva 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#if cn==3

__kernel void matchTemplate_Naive_CCORR(__global const uchar * srcptr, int src_step, int src_offset,
                                        __global const uchar * templateptr, int template_step, int template_offset, int template_rows, int template_cols,
                                        __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols)
{
    int x = get_global_id(0);
    int y = get_global_id(1);

    if (x < dst_cols && y < dst_rows)
    {
        WT sum = (WT)(0);

        for (int i = 0; i < template_rows; ++i)
        {
            for (int j = 0; j < template_cols; ++j)
            {
                T src      = vload3(0, (__global const T1 *)(srcptr      + mad24(y+i, src_step,    mad24(x+j, (int)sizeof(T1)*cn, src_offset))));
                T template = vload3(0, (__global const T1 *)(templateptr + mad24(i, template_step, mad24(j, (int)sizeof(T1)*cn, template_offset))));
#if wdepth == 4
                sum = mad24(convertToWT(src), convertToWT(template), sum);
#else
                sum = mad(convertToWT(src), convertToWT(template), sum);
#endif
            }
        }

        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        *(__global float *)(dst + dst_idx) = convertToDT(sum);
    }
}

#else

162 163 164 165 166 167
__kernel void matchTemplate_Naive_CCORR(__global const uchar * srcptr, int src_step, int src_offset,
                                        __global const uchar * templateptr, int template_step, int template_offset, int template_rows, int template_cols,
                                        __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols)
{
    int x = get_global_id(0);
    int y = get_global_id(1);
E
Elena Gvozdeva 已提交
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    if (x < dst_cols && y < dst_rows)
    {
        WT sum = (WT)(0);

        __global const T * src = (__global const T *)(srcptr + mad24(y, src_step, mad24(x, (int)sizeof(T), src_offset)));
        __global const T * template = (__global const T *)(templateptr + template_offset);

        for (int i = 0; i < template_rows; ++i)
        {
            for (int j = 0; j < template_cols; ++j)
#if wdepth == 4
                sum = mad24(convertToWT(src[j]), convertToWT(template[j]), sum);
#else
                sum = mad(convertToWT(src[j]), convertToWT(template[j]), sum);
#endif

            src = (__global const T *)((__global const uchar *)src + src_step);
            template = (__global const T *)((__global const uchar *)template + template_step);
187
        }
188 189 190

        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        *(__global float *)(dst + dst_idx) = convertToDT(sum);
191 192
    }
}
E
Elena Gvozdeva 已提交
193
#endif
194

195
#elif defined CCORR_NORMED
196

197 198 199 200 201 202
__kernel void matchTemplate_CCORR_NORMED(__global const uchar * src_sqsums, int src_sqsums_step, int src_sqsums_offset,
                                         __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
                                         int template_rows, int template_cols, __global const float * template_sqsum)
{
    int x = get_global_id(0);
    int y = get_global_id(1);
203

204
    if (x < dst_cols && y < dst_rows)
205
    {
206
        __global const float * sqsum = (__global const float *)(src_sqsums);
207

208 209 210 211 212 213 214 215
        src_sqsums_step /= sizeof(float);
        src_sqsums_offset /= sizeof(float);
        float image_sqsum_ = (float)(sqsum[SQSUMS_PTR(template_cols, template_rows)] - sqsum[SQSUMS_PTR(template_cols, 0)] -
                                     sqsum[SQSUMS_PTR(0, template_rows)] + sqsum[SQSUMS_PTR(0, 0)]);

        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        __global float * dstult = (__global float *)(dst + dst_idx);
        *dstult = normAcc(*dstult, sqrt(image_sqsum_ * template_sqsum[0]));
216 217 218
    }
}

219
#elif defined SQDIFF
220

E
Elena Gvozdeva 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
#if cn==3

__kernel void matchTemplate_Naive_SQDIFF(__global const uchar * srcptr, int src_step, int src_offset,
                                         __global const uchar * templateptr, int template_step, int template_offset, int template_rows, int template_cols,
                                         __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols)
{
    int x = get_global_id(0);
    int y = get_global_id(1);

    if (x < dst_cols && y < dst_rows)
    {
        WT sum = (WT)(0), value;

        for (int i = 0; i < template_rows; ++i)
        {
            for (int j = 0; j < template_cols; ++j)
            {
                T src      = vload3(0, (__global const T1 *)(srcptr      + mad24(y+i, src_step,    mad24(x+j, (int)sizeof(T1)*cn, src_offset))));
                T template = vload3(0, (__global const T1 *)(templateptr + mad24(i, template_step, mad24(j, (int)sizeof(T1)*cn, template_offset))));

                value = convertToWT(src) - convertToWT(template);
#if wdepth == 4
                sum = mad24(value, value, sum);
#else
                sum = mad(value, value, sum);
#endif
            }
        }

        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        *(__global float *)(dst + dst_idx) = convertToDT(sum);
    }
}

#else

257 258 259
__kernel void matchTemplate_Naive_SQDIFF(__global const uchar * srcptr, int src_step, int src_offset,
                                         __global const uchar * templateptr, int template_step, int template_offset, int template_rows, int template_cols,
                                         __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols)
260
{
261 262
    int x = get_global_id(0);
    int y = get_global_id(1);
263

264
    if (x < dst_cols && y < dst_rows)
265
    {
266 267
        __global const T * src = (__global const T *)(srcptr + mad24(y, src_step, mad24(x, (int)sizeof(T), src_offset)));
        __global const T * template = (__global const T *)(templateptr + template_offset);
268

269
        WT sum = (WT)(0), value;
270

271 272 273 274 275 276 277 278 279 280 281 282 283 284
        for (int i = 0; i < template_rows; ++i)
        {
            for (int j = 0; j < template_cols; ++j)
            {
                value = convertToWT(src[j]) - convertToWT(template[j]);
#if wdepth == 4
                sum = mad24(value, value, sum);
#else
                sum = mad(value, value, sum);
#endif
            }

            src = (__global const T *)((__global const uchar *)src + src_step);
            template = (__global const T *)((__global const uchar *)template + template_step);
285
        }
286 287 288

        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        *(__global float *)(dst + dst_idx) = convertToDT(sum);
289 290 291
    }
}

E
Elena Gvozdeva 已提交
292 293
#endif

294
#elif defined SQDIFF_NORMED
295

296 297 298 299 300 301
__kernel void matchTemplate_SQDIFF_NORMED(__global const uchar * src_sqsums, int src_sqsums_step, int src_sqsums_offset,
                                          __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
                                          int template_rows, int template_cols, __global const float * template_sqsum)
{
    int x = get_global_id(0);
    int y = get_global_id(1);
302

303
    if (x < dst_cols && y < dst_rows)
304
    {
305 306
        src_sqsums_step /= sizeof(float);
        src_sqsums_offset /= sizeof(float);
307

308 309 310 311 312
        __global const float * sqsum = (__global const float *)(src_sqsums);
        float image_sqsum_ = (float)(
                                 (sqsum[SQSUMS_PTR(template_cols, template_rows)] - sqsum[SQSUMS_PTR(template_cols, 0)]) -
                                 (sqsum[SQSUMS_PTR(0, template_rows)] - sqsum[SQSUMS_PTR(0, 0)]));
        float template_sqsum_value = template_sqsum[0];
313

314 315 316
        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        __global float * dstult = (__global float *)(dst + dst_idx);
        *dstult = normAcc_SQDIFF(image_sqsum_ - 2.0f * dstult[0] + template_sqsum_value, sqrt(image_sqsum_ * template_sqsum_value));
317 318 319
    }
}

320
#elif defined CCOEFF
321

322
#if cn == 1
323

324 325 326 327 328 329
__kernel void matchTemplate_Prepared_CCOEFF(__global const uchar * src_sums, int src_sums_step, int src_sums_offset,
                                            __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
                                            int template_rows, int template_cols, float template_sum)
{
    int x = get_global_id(0);
    int y = get_global_id(1);
330

331
    if (x < dst_cols && y < dst_rows)
332
    {
333
        T image_sum = (T)(0), value;
334

E
Elena Gvozdeva 已提交
335 336 337 338
        value  = *(__global const T1 *)(src_sums + SUMS(template_cols, template_rows));
        value -= *(__global const T1 *)(src_sums + SUMS(0, template_rows));
        value -= *(__global const T1 *)(src_sums + SUMS(template_cols, 0));
        value += *(__global const T1 *)(src_sums + SUMS(0, 0));
339

E
Elena Gvozdeva 已提交
340
        image_sum = mad(value, template_sum, 0);
341

342
        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
343
        *(__global float *)(dst + dst_idx) -= convertToDT(image_sum);
344 345 346
    }
}

E
Elena Gvozdeva 已提交
347 348 349 350
#elif cn==3

__kernel void matchTemplate_Prepared_CCOEFF(__global const uchar * src_sums, int src_sums_step, int src_sums_offset,
                                            __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
351
                                            int template_rows, int template_cols, float4 template_sum)
E
Elena Gvozdeva 已提交
352 353 354 355 356 357
{
    int x = get_global_id(0);
    int y = get_global_id(1);

    if (x < dst_cols && y < dst_rows)
    {
358
        T image_sum = (T)(0), value, temp_sum;
E
Elena Gvozdeva 已提交
359

360 361 362
        temp_sum.x = template_sum.x;
        temp_sum.y = template_sum.y;
        temp_sum.z = template_sum.z;
E
Elena Gvozdeva 已提交
363

364 365 366 367
        value  = vload3(0, (__global const T1 *)(src_sums + SUMS(template_cols, template_rows)));
        value -= vload3(0, (__global const T1 *)(src_sums + SUMS(0, template_rows)));
        value -= vload3(0, (__global const T1 *)(src_sums + SUMS(template_cols, 0)));
        value += vload3(0, (__global const T1 *)(src_sums + SUMS(0, 0)));
E
Elena Gvozdeva 已提交
368

369
        image_sum = mad(value, temp_sum , 0);
E
Elena Gvozdeva 已提交
370 371

        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
372
        *(__global float *)(dst + dst_idx) -= convertToDT(image_sum);
E
Elena Gvozdeva 已提交
373 374 375
    }
}

376
#elif (cn==2 || cn==4)
377

378 379
__kernel void matchTemplate_Prepared_CCOEFF(__global const uchar * src_sums, int src_sums_step, int src_sums_offset,
                                            __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
380
                                            int template_rows, int template_cols, float4 template_sum)
381 382 383
{
    int x = get_global_id(0);
    int y = get_global_id(1);
384

385
    if (x < dst_cols && y < dst_rows)
386
    {
387
        __global const T* sum = (__global const T*)(src_sums + mad24(y, src_sums_step, mad24(x, (int)sizeof(T), src_sums_offset)));
388

389 390 391 392 393 394 395 396 397 398
        int step = src_sums_step/(int)sizeof(T);

        T image_sum = (T)(0), value, temp_sum;

#if cn==2
        temp_sum.x = template_sum.x;
        temp_sum.y = template_sum.y;
#else
        temp_sum = template_sum;
#endif
E
fixed  
Elena Gvozdeva 已提交
399

400
        value = (sum[mad24(template_rows, step, template_cols)] - sum[mad24(template_rows, step, 0)] - sum[template_cols] + sum[0]);
401

402
        image_sum = mad(value, temp_sum , image_sum);
403

404
        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
405
        *(__global float *)(dst + dst_idx) -= convertToDT(image_sum);
406 407 408
    }
}

409
#else
E
Elena Gvozdeva 已提交
410
#error "cn should be 1-4"
411 412 413
#endif

#elif defined CCOEFF_NORMED
414

415
#if cn == 1
416

417 418 419 420 421 422 423
__kernel void matchTemplate_CCOEFF_NORMED(__global const uchar * src_sums, int src_sums_step, int src_sums_offset,
                                          __global const uchar * src_sqsums, int src_sqsums_step, int src_sqsums_offset,
                                          __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
                                          int t_rows, int t_cols, float weight, float template_sum, float template_sqsum)
{
    int x = get_global_id(0);
    int y = get_global_id(1);
424 425 426 427

    float sum_[2];
    float sqsum_[2];

428

429
    if (x < dst_cols && y < dst_rows)
430
    {
431
        int step = src_sums_step/(int)sizeof(T);
432

433 434
        __global const T* sum   = (__global const T*)(src_sums + mad24(y, src_sums_step,     mad24(x, (int)sizeof(T), src_sums_offset)));
        __global const T* sqsum = (__global const T*)(src_sqsums + mad24(y, src_sqsums_step, mad24(x, (int)sizeof(T), src_sqsums_offset)));
435

436 437
        T value_sum   = sum[mad24(t_rows, step, t_cols)] - sum[mad24(t_rows, step, 0)] - sum[t_cols] + sum[0];
        T value_sqsum = sqsum[mad24(t_rows, step, t_cols)] - sqsum[mad24(t_rows, step, 0)] - sqsum[t_cols] + sqsum[0];
438

439
        float num = convertToDT(mad(value_sum, template_sum, 0));
440

441 442
        value_sqsum -= weight * value_sum * value_sum;
        float denum = sqrt(mad(template_sqsum, convertToDT(value_sqsum), 0));
443

444 445 446
        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        __global float * dstult = (__global float *)(dst+dst_idx);
        *dstult = normAcc((*dstult) - num, denum);
447 448 449
    }
}

E
Elena Gvozdeva 已提交
450 451 452 453 454
#elif cn==3

__kernel void matchTemplate_CCOEFF_NORMED(__global const uchar * src_sums, int src_sums_step, int src_sums_offset,
                                          __global const uchar * src_sqsums, int src_sqsums_step, int src_sqsums_offset,
                                          __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
455
                                          int t_rows, int t_cols, float weight, float4 template_sum, float template_sqsum)
E
Elena Gvozdeva 已提交
456 457 458 459 460 461
{
    int x = get_global_id(0);
    int y = get_global_id(1);

    if (x < dst_cols && y < dst_rows)
    {
462
        int step = src_sums_step/(int)sizeof(T);
E
Elena Gvozdeva 已提交
463

464
        T temp_sum, value_sum, value_sqsum;
E
Elena Gvozdeva 已提交
465

466 467 468
        temp_sum.x = template_sum.x;
        temp_sum.y = template_sum.y;
        temp_sum.z = template_sum.z;
E
Elena Gvozdeva 已提交
469

470 471 472 473
        value_sum  = vload3(0, (__global const T1 *)(src_sums + SUMS(t_cols, t_rows)));
        value_sum -= vload3(0, (__global const T1 *)(src_sums + SUMS(0, t_rows)));
        value_sum -= vload3(0, (__global const T1 *)(src_sums + SUMS(t_cols, 0)));
        value_sum += vload3(0, (__global const T1 *)(src_sums + SUMS(0, 0)));
E
Elena Gvozdeva 已提交
474

475 476 477 478
        value_sqsum  = vload3(0, (__global const T1 *)(src_sqsums + SQ_SUMS(t_cols, t_rows)));
        value_sqsum -= vload3(0, (__global const T1 *)(src_sqsums + SQ_SUMS(0, t_rows)));
        value_sqsum -= vload3(0, (__global const T1 *)(src_sqsums + SQ_SUMS(t_cols, 0)));
        value_sqsum += vload3(0, (__global const T1 *)(src_sqsums + SQ_SUMS(0, 0)));
E
Elena Gvozdeva 已提交
479

480
        float num = convertToDT(mad(value_sum, temp_sum, 0));
E
Elena Gvozdeva 已提交
481

482 483
        value_sqsum -= weight * value_sum * value_sum;
        float denum = sqrt(mad(template_sqsum, convertToDT(value_sqsum), 0));
E
Elena Gvozdeva 已提交
484 485 486 487 488 489 490

        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        __global float * dstult = (__global float *)(dst+dst_idx);
        *dstult = normAcc((*dstult) - num, denum);
    }
}

491
#elif (cn==2 || cn==4)
492

493 494 495
__kernel void matchTemplate_CCOEFF_NORMED(__global const uchar * src_sums, int src_sums_step, int src_sums_offset,
                                          __global const uchar * src_sqsums, int src_sqsums_step, int src_sqsums_offset,
                                          __global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,
496
                                          int t_rows, int t_cols, float weight, float4 template_sum, float template_sqsum)
497 498 499
{
    int x = get_global_id(0);
    int y = get_global_id(1);
500

501
    if (x < dst_cols && y < dst_rows)
502
    {
503
        int step = src_sums_step/(int)sizeof(T);
504

505
        T temp_sum;
506

507 508
        __global const T* sum   = (__global const T*)(src_sums + mad24(y, src_sums_step,     mad24(x, (int)sizeof(T), src_sums_offset)));
        __global const T* sqsum = (__global const T*)(src_sqsums + mad24(y, src_sqsums_step, mad24(x, (int)sizeof(T), src_sqsums_offset)));
E
fixed  
Elena Gvozdeva 已提交
509

510 511
        T value_sum   = sum[mad24(t_rows, step, t_cols)] - sum[mad24(t_rows, step, 0)] - sum[t_cols] + sum[0];
        T value_sqsum = sqsum[mad24(t_rows, step, t_cols)] - sqsum[mad24(t_rows, step, 0)] - sqsum[t_cols] + sqsum[0];
E
fixed  
Elena Gvozdeva 已提交
512

513 514 515 516 517 518
#if cn==2
        temp_sum.x = template_sum.x;
        temp_sum.y = template_sum.y;
#else
        temp_sum = template_sum;
#endif
519

520
        float num = convertToDT(mad(value_sum, temp_sum, 0));
521

522 523
        value_sqsum -= weight * value_sum * value_sum;
        float denum = sqrt(mad(template_sqsum, convertToDT(value_sqsum), 0));
524

525 526 527
        int dst_idx = mad24(y, dst_step, mad24(x, (int)sizeof(float), dst_offset));
        __global float * dstult = (__global float *)(dst+dst_idx);
        *dstult = normAcc((*dstult) - num, denum);
528
    }
529
}
530 531

#else
E
Elena Gvozdeva 已提交
532
#error "cn should be 1-4"
533 534 535
#endif

#endif