// File: kernel.cpp
#include "pooling_global_avg_contract.h"
#include "pooling_arm.h"
#include "mat.h"
#include "option.h"
#include <cstring>

int ncnn::pooling_global_avg_kernel(
    const Mat& bottom_blob, Mat& top_blob,
    const Option& opt)
{
    Pooling_arm pool;
    pool.pooling_type              = 1; // PoolMethod_AVE
    pool.kernel_w                  = 0;
    pool.kernel_h                  = 0;
    pool.stride_w                  = 1;
    pool.stride_h                  = 1;
    pool.pad_left                  = 0;
    pool.pad_right                 = 0;
    pool.pad_top                   = 0;
    pool.pad_bottom                = 0;
    pool.global_pooling            = 1;
    pool.pad_mode                  = 1;
    pool.avgpool_count_include_pad = 0;
    pool.adaptive_pooling          = 0;
    pool.out_w                     = 0;
    pool.out_h                     = 0;

    if (pool.create_pipeline(opt) != 0) return -1;

    // ncnn's Pooling::forward global-pooling path creates top_blob as a flat
    // 1D Mat (w=C) directly (Mat::create(channels, elemsize, allocator)) — no
    // per-row/channel padding concern, unlike the windowed-pooling 3D case.
    Mat local_top;
    if (pool.forward(bottom_blob, local_top, opt) != 0) return -1;

    std::memcpy((float*)top_blob.data, (const float*)local_top.data,
                top_blob.w * sizeof(float));
    return 0;
}