Repository: opencv/opencv
File: modules/dnn/src/layers/gridsample_layer.cpp (C++)

Code before:
                const float* base = Xptr + n*xNStride + c*xCStride + (size_t)py*xHStride + (size_t)px;
                return *base;
            };

            if (mode == M_NEAREST) {
                int px = int(std::floor(x + 0.5f));
                int py = int(std::floor(y + 0.5f));
                return fetch(py, px);
            } else if (mode == M_BILINEAR) {
                int x0 = (int)floorf(x), y0 = (int)floorf(y);
                float dx = x - x0, dy = y - y0;
                float v00 = fetch(y0,   x0);
                float v01 = fetch(y0,   x0+1);
                float v10 = fetch(y0+1, x0);
                float v11 = fetch(y0+1, x0+1);
                float vx0 = v00*(1.f-dx) + v01*dx;
                float vx1 = v10*(1.f-dx) + v11*dx;
                return vx0*(1.f-dy) + vx1*dy;
            } else {
                const float alpha = -0.75f;
                int x1 = (int)floorf(x);
                int y1 = (int)floorf(y);
                float tx = x - x1, ty = y - y1;

                float wx[4], wy[4];
                for (int i = 0; i < 4; ++i) {
                    wx[i] = cubicWeight((i - 1) - tx, alpha);
                    wy[i] = cubicWeight((i - 1) - ty, alpha);
                }
                float sumwx = wx[0]+wx[1]+wx[2]+wx[3];
                float sumwy = wy[0]+wy[1]+wy[2]+wy[3];
                if (sumwx != 0.f) { for (int i = 0; i < 4; ++i) wx[i] /= sumwx; }
                if (sumwy != 0.f) { for (int i = 0; i < 4; ++i) wy[i] /= sumwy; }

                float acc = 0.f;
                for (int j = 0; j < 4; ++j) {
                    float row = 0.f;
                    for (int i = 0; i < 4; ++i) {
                        row += fetch(y1 + (j - 1), x1 + (i - 1)) * wx[i];
                    }
                    acc += row * wy[j];
                }
                return acc;
            }
        };

        cv::parallel_for_(cv::Range(0, N * C * Hout * Wout), [&](const cv::Range& r) {
            for (int idx = r.start; idx < r.end; ++idx) {
                int t = idx;
                int w = t % Wout; t /= Wout;
                int h = t % Hout; t /= Hout;

Review comment (performance):
Please, optimize it. Don't use loops here to compute coefficients. In OpenCV we have optimized bicubic intepolation in `cv::resize()`, take the code from there: https://github.com/opencv/opencv/blob/6d889ee74c94124f6492eb8f0d50946d9c31d8e9/modules/imgproc/src/resize.cpp#L964. Make sure that both variants produce the same results. 

Code after:
                        const T* p = baseNC + (size_t)(y1 - 1) * xHStride + (x1 - 1);
                        float rowv0 = (float)p[0] * wx[0] + (float)p[1] * wx[1] + (float)p[2] * wx[2] + (float)p[3] * wx[3];
                        p += xHStride;
                        float rowv1 = (float)p[0] * wx[0] + (float)p[1] * wx[1] + (float)p[2] * wx[2] + (float)p[3] * wx[3];
                        p += xHStride;
                        float rowv2 = (float)p[0] * wx[0] + (float)p[1] * wx[1] + (float)p[2] * wx[2] + (float)p[3] * wx[3];
                        p += xHStride;
                        float rowv3 = (float)p[0] * wx[0] + (float)p[1] * wx[1] + (float)p[2] * wx[2] + (float)p[3] * wx[3];
                        outv = rowv0 * wy[0] + rowv1 * wy[1] + rowv2 * wy[2] + rowv3 * wy[3];
                    } else {
                        float rowv0 = fetch(baseNC, y1 - 1, x1 - 1) * wx[0] + fetch(baseNC, y1 - 1, x1    ) * wx[1] +
                                      fetch(baseNC, y1 - 1, x1 + 1) * wx[2] + fetch(baseNC, y1 - 1, x1 + 2) * wx[3];
                        float rowv1 = fetch(baseNC, y1,     x1 - 1) * wx[0] + fetch(baseNC, y1,     x1    ) * wx[1] +
                                      fetch(baseNC, y1,     x1 + 1) * wx[2] + fetch(baseNC, y1,     x1 + 2) * wx[3];
                        float rowv2 = fetch(baseNC, y1 + 1, x1 - 1) * wx[0] + fetch(baseNC, y1 + 1, x1    ) * wx[1] +
                                      fetch(baseNC, y1 + 1, x1 + 1) * wx[2] + fetch(baseNC, y1 + 1, x1 + 2) * wx[3];
                        float rowv3 = fetch(baseNC, y1 + 2, x1 - 1) * wx[0] + fetch(baseNC, y1 + 2, x1    ) * wx[1] +
                                      fetch(baseNC, y1 + 2, x1 + 1) * wx[2] + fetch(baseNC, y1 + 2, x1 + 2) * wx[3];
                        outv = rowv0 * wy[0] + rowv1 * wy[1] + rowv2 * wy[2] + rowv3 * wy[3];
                    }
                }
                Yptr[yRowBase + w] = saturate_cast<T>(outv);
            }
        }
    });
}

template<typename T>
static inline void gridSampleDispatch(
        const T* Xptr,
        const float* Gptr,
        T* Yptr,
        int N, int C, int H, int W,
        int Hout, int Wout,
        bool align_corners,
        int mode, int padding)
{
    if (mode == M_NEAREST) {
        if (padding == P_ZEROS) gridSampleComputeRows<T, M_NEAREST, P_ZEROS>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
        else if (padding == P_BORDER) gridSampleComputeRows<T, M_NEAREST, P_BORDER>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
        else gridSampleComputeRows<T, M_NEAREST, P_REFLECTION>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
    } else if (mode == M_BILINEAR) {
        if (padding == P_ZEROS) gridSampleComputeRows<T, M_BILINEAR, P_ZEROS>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
        else if (padding == P_BORDER) gridSampleComputeRows<T, M_BILINEAR, P_BORDER>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
        else gridSampleComputeRows<T, M_BILINEAR, P_REFLECTION>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
    } else {
        if (padding == P_ZEROS) gridSampleComputeRows<T, M_BICUBIC, P_ZEROS>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
        else if (padding == P_BORDER) gridSampleComputeRows<T, M_BICUBIC, P_BORDER>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
        else gridSampleComputeRows<T, M_BICUBIC, P_REFLECTION>(Xptr, Gptr, Yptr, N, C, H, W, Hout, Wout, align_corners);
    }
}