Repository: opencv/opencv
File: modules/imgproc/src/median_blur.simd.hpp (C++)

Code before:

#if (CV_SIMD || CV_SIMD_SCALABLE)
                int nlanes = VTraits<typename VecOp::arg_type>::vlanes();
#else
                int nlanes = 1;
#endif
                if(_dst.cols > 2*nlanes) //minimum size to safely allow extra vectorized store into next row.
                {
                    /* Reducing bottleneck in non-vectorized path beyond (size.width - nlanes - cn)
                    /* In the last iteration of the loop below, even if residue is less than nlanes, we allow it to be completed in vectorized path.
                    /* Though we do additional store in every row, it will be overwritten by correct value in the next row. */
                    for( ; j < size.width - cn ; )
                    {
                        VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
                        VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
                        VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);

                        vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
                        vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
                        vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
                        vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
                        vop(p4, p2); vop(p6, p4); vop(p4, p2);
                        vop.store(dst+j, p4);

                        int residue = size.width - j -cn;
                        j <= size.width - nlanes - cn? j += nlanes : j += residue;

                        /*In the last row(height-1) processing, additional store would overwrite outside output buffer.
                        Its avoided with below check. */
                        if((i == size.height-1) && ( j >= size.width - cn - nlanes))
                        {
                            break;
                        }
                    }
                }

                limit = size.width;
            }
        }
    }
    else if( m == 5 )
    {
        if( size.width == 1 || size.height == 1 )
        {
            int len = size.width + size.height - 1;
            int sdelta = size.height == 1 ? cn : sstep;
            int sdelta0 = size.height == 1 ? 0 : sstep - cn;
            int ddelta = size.height == 1 ? cn : dstep;

            for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
                for( j = 0; j < cn; j++, src++ )

Review comment (suggestion):
I propose to replace it with if..else or more usual `offset = ? : ; j+= offset;` 

Code after:

#if (CV_SIMD || CV_SIMD_SCALABLE)
                int nlanes = VTraits<typename VecOp::arg_type>::vlanes();
#else
                int nlanes = 1;
#endif
                for (; j < size.width - cn; j += nlanes)
                {
                    //handling tail in vectorized path itself
                    if ( j > size.width - cn - nlanes ) {
                        if (j == cn || src == dst) {
                            break;
                        }
                        j = size.width - cn - nlanes;
                    }

                    VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
                    VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
                    VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);

                    vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
                    vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
                    vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
                    vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
                    vop(p4, p2); vop(p6, p4); vop(p4, p2);
                    vop.store(dst+j, p4);

                }

                limit = size.width;
            }
        }
    }
    else if( m == 5 )
    {
        if( size.width == 1 || size.height == 1 )
        {
            int len = size.width + size.height - 1;
            int sdelta = size.height == 1 ? cn : sstep;
            int sdelta0 = size.height == 1 ? 0 : sstep - cn;
            int ddelta = size.height == 1 ? cn : dstep;

            for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
                for( j = 0; j < cn; j++, src++ )
                {
                    int i1 = i > 0 ? -sdelta : 0;
                    int i0 = i > 1 ? -sdelta*2 : i1;
                    int i3 = i < len-1 ? sdelta : 0;
                    int i4 = i < len-2 ? sdelta*2 : i3;
                    WT p0 = src[i0], p1 = src[i1], p2 = src[0], p3 = src[i3], p4 = src[i4];