// Copyright 2019 Tencent
// SPDX-License-Identifier: BSD-3-Clause

#version 450

layout (constant_id = 0) const uint n = 0;

layout (binding = 0) buffer bottom_top_blob { sfpvec4 bottom_top_blob_data[]; };

layout (push_constant) uniform parameter
{
    uint n;
} p;

afpvec4 erf(afpvec4 x)
{
    afpvec4 a1 = afpvec4(0.254829592f);
    afpvec4 a2 = afpvec4(-0.284496736f);
    afpvec4 a3 = afpvec4(1.421413741f);
    afpvec4 a4 = afpvec4(-1.453152027f);
    afpvec4 a5 = afpvec4(1.061405429f);
    afpvec4 p = afpvec4(0.3275911f);
    afpvec4 s = sign(x);
    afpvec4 x_abs = abs(x);
    afpvec4 t = 1.0f / (1.0f + p * x_abs);
    afpvec4 y = 1.0f - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * exp(-x_abs * x_abs);
    return s * y;
}

void main()
{
    const uint gi = gl_GlobalInvocationID.x;

    if (gi >= psc(n))
        return;


    afpvec4 v = buffer_ld4(bottom_top_blob_data, gi);

    v = erf(v);

    buffer_st4(bottom_top_blob_data, gi, v);
}
