|
| 1 | +// clang-format off |
| 2 | +// Auto-generated file. Do not edit! |
| 3 | +// Template: src/f32-vapproxgelu/rvv-rational-12-10.c.in |
| 4 | +// Generator: tools/xngen |
| 5 | +// |
| 6 | +// Copyright 2024 Google LLC |
| 7 | +// |
| 8 | +// This source code is licensed under the BSD-style license found in the |
| 9 | +// LICENSE file in the root directory of this source tree. |
| 10 | + |
| 11 | +#include <assert.h> |
| 12 | + |
| 13 | +#include <riscv_vector.h> |
| 14 | + |
| 15 | +#include "src/xnnpack/common.h" |
| 16 | +#include "src/xnnpack/vunary.h" |
| 17 | + |
| 18 | + |
| 19 | +void xnn_f32_vapproxgelu_ukernel__rvv_rational_12_10_div_u1v( |
| 20 | + size_t batch, |
| 21 | + const float* input, |
| 22 | + float* output, |
| 23 | + const struct xnn_f32_default_params* unused_params) |
| 24 | +{ |
| 25 | + assert(batch != 0); |
| 26 | + assert(batch % sizeof(float) == 0); |
| 27 | + assert(input != NULL); |
| 28 | + assert(output != NULL); |
| 29 | + |
| 30 | + // Cap the inputs to this value as `erf(x/sqrt(2))` will always be `+/-1.0f` |
| 31 | + // beyond this point. This value is chosen as the first floating point |
| 32 | + // number as of which the interpolation returns +/-1.0f. |
| 33 | + const float vmax_x = 4.84974098e+00f; |
| 34 | + const float vmin_x = -4.84974098e+00f; |
| 35 | + |
| 36 | + // The monomial coefficients of the numerator polynomial (odd). |
| 37 | + const float valpha_1 = 7.9788458347e-01f; |
| 38 | + const float valpha_3 = 6.0803253204e-02f; |
| 39 | + const float valpha_5 = 7.2898347862e-03f; |
| 40 | + const float valpha_7 = 2.6887017884e-04f; |
| 41 | + const float valpha_9 = 1.4302649106e-05f; |
| 42 | + const float valpha_11 = 4.9544411240e-08f; |
| 43 | + |
| 44 | + // The monomial coefficients of the denominator polynomial (even). |
| 45 | + const float vbeta_2 = 2.4369759858e-01f; |
| 46 | + const float vbeta_4 = 2.4381054565e-02f; |
| 47 | + const float vbeta_6 = 1.3060354395e-03f; |
| 48 | + const float vbeta_8 = 7.6477612311e-05f; |
| 49 | + const float vbeta_10 = 1.3433452750e-06f; |
| 50 | + |
| 51 | + batch >>= XNN_LOG2_SIZEOF_FLOAT; |
| 52 | + do { |
| 53 | + const size_t n = __riscv_vsetvl_e32m1(batch); |
| 54 | + |
| 55 | + vfloat32m1_t vx_orig = __riscv_vle32_v_f32m1(input, n); |
| 56 | + input += n; |
| 57 | + |
| 58 | + // Clamp the inputs to the interpolation range. |
| 59 | + vfloat32m1_t vx = __riscv_vfmin_vf_f32m1(vx_orig, vmax_x, n); |
| 60 | + vx = __riscv_vfmax_vf_f32m1(vx, vmin_x, n); |
| 61 | + |
| 62 | + // Since the polynomials are odd/even, we need x^2. |
| 63 | + vfloat32m1_t vx2 = __riscv_vfmul_vv_f32m1(vx, vx, n); |
| 64 | + |
| 65 | + // Evaluate the numerator polynomial p. |
| 66 | + vfloat32m1_t vp = __riscv_vfmv_v_f_f32m1(valpha_9, n); |
| 67 | + vp = __riscv_vfmacc_vf_f32m1(vp, valpha_11, vx2, n); |
| 68 | + vp = __riscv_vfmadd_vv_f32m1(vp, vx2, __riscv_vfmv_v_f_f32m1(valpha_7, n), n); |
| 69 | + vp = __riscv_vfmadd_vv_f32m1(vp, vx2, __riscv_vfmv_v_f_f32m1(valpha_5, n), n); |
| 70 | + vp = __riscv_vfmadd_vv_f32m1(vp, vx2, __riscv_vfmv_v_f_f32m1(valpha_3, n), n); |
| 71 | + vp = __riscv_vfmadd_vv_f32m1(vp, vx2, __riscv_vfmv_v_f_f32m1(valpha_1, n), n); |
| 72 | + vp = __riscv_vfmul_vv_f32m1(vp, vx, n); |
| 73 | + |
| 74 | + // Evaluate the denominator polynomial q. |
| 75 | + vfloat32m1_t vq = __riscv_vfmv_v_f_f32m1(vbeta_8, n); |
| 76 | + vq = __riscv_vfmacc_vf_f32m1(vq, vbeta_10, vx2, n); |
| 77 | + vq = __riscv_vfmadd_vv_f32m1(vq, vx2, __riscv_vfmv_v_f_f32m1(vbeta_6, n), n); |
| 78 | + vq = __riscv_vfmadd_vv_f32m1(vq, vx2, __riscv_vfmv_v_f_f32m1(vbeta_4, n), n); |
| 79 | + vq = __riscv_vfmadd_vv_f32m1(vq, vx2, __riscv_vfmv_v_f_f32m1(vbeta_2, n), n); |
| 80 | + vq = __riscv_vfmadd_vv_f32m1(vq, vx2, __riscv_vfmv_v_f_f32m1(1.0f, n), n); |
| 81 | + |
| 82 | + // Divide the numerator by the denominator. |
| 83 | + vfloat32m1_t verf = __riscv_vfdiv_vv_f32m1(vp, vq, n); |
| 84 | + |
| 85 | + // Add one to the rational interpolant, and multiply by 0.5 times the |
| 86 | + // original input. |
| 87 | + vfloat32m1_t vy = __riscv_vfadd_vf_f32m1(verf, 1.0f, n); |
| 88 | + vy = __riscv_vfmul_vf_f32m1(vy, 0.5f, n); |
| 89 | + vy = __riscv_vfmul_vv_f32m1(vy, vx_orig, n); |
| 90 | + |
| 91 | + __riscv_vse32_v_f32m1(output, vy, n); |
| 92 | + output += n; |
| 93 | + |
| 94 | + batch -= n; |
| 95 | + } while (batch != 0); |
| 96 | +} |
0 commit comments