Skip to content

Basic math functions

abs function

template <typename T1, KFR_ENABLE_IF(is_numeric<T1>)>
T1 abs(const T1 &x)

Returns the absolute value of x.

Source code
template <typename T1, KFR_ENABLE_IF(is_numeric<T1>)>
KFR_INTRINSIC T1 abs(const T1& x)
{
    return intrinsics::abs(x);
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/abs.hpp#L39

absmax function

template <typename T1, typename T2,
          KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
Tout absmax(const T1 &x, const T2 &y)

Returns the greater in magnitude of two values.

Source code
template <typename T1, typename T2, KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
KFR_INTRINSIC Tout absmax(const T1& x, const T2& y)
{
    return intrinsics::absmax(x, y);
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/min_max.hpp#L70

absmin function

template <typename T1, typename T2,
          KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
Tout absmin(const T1 &x, const T2 &y)

Returns the smaller in magnitude of two values.

Source code
template <typename T1, typename T2, KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
KFR_INTRINSIC Tout absmin(const T1& x, const T2& y)
{
    return intrinsics::absmin(x, y);
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/min_max.hpp#L60

clamp function

template <typename T1, typename T2, typename T3,
          KFR_ENABLE_IF(is_numeric_args<T1, T2, T3>),
          typename Tout = std::common_type_t<T1, T2, T3>>
Tout clamp(const T1 &x, const T2 &lo, const T3 &hi)

Returns the first argument clamped to a range [lo, hi]

Source code
template <typename T1, typename T2, typename T3, KFR_ENABLE_IF(is_numeric_args<T1, T2, T3>),
          typename Tout = std::common_type_t<T1, T2, T3>>
KFR_INTRINSIC Tout clamp(const T1& x, const T2& lo, const T3& hi)
{
    return intrinsics::clamp(static_cast<Tout>(x), static_cast<Tout>(lo), static_cast<Tout>(hi));
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/clamp.hpp#L38

template <typename T1, typename T2,
          KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
Tout clamp(const T1 &x, const T2 &hi)

Returns the first argument clamped to a range [0, hi]

Source code
template <typename T1, typename T2, KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
KFR_INTRINSIC Tout clamp(const T1& x, const T2& hi)
{
    return intrinsics::clamp(static_cast<Tout>(x), static_cast<Tout>(hi));
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/clamp.hpp#L46

matrix2d

matrix2d class

template <typename T> matrix2d

2D matrix

Source code
template <typename T>
struct matrix2d

https://github.com/kfrlib/kfr/blob//include/kfr/graphics/geometry.hpp#L42

matrix2d

template <typename T> matrix2d

2D matrix

Source code
template <typename T>
struct matrix2d
{
    static_assert(std::is_floating_point_v<T>);
    union
    {
        vec<T, 6> v;
        struct
        {
            T a, b, c, d, e, f;
        };
    };

    matrix2d() : v{ 1, 0, 0, 1, 0, 0 } {}

    matrix2d(T a, T b, T c, T d, T e, T f) : v{ a, b, c, d, e, f } {}
    constexpr matrix2d(const matrix2d& m) : v(m.v) {}

    explicit matrix2d(const vec<T, 6>& v) : v(v) {}

    static matrix2d translate(T x, T y) { return matrix2d{ 1, 0, 0, 1, x, y }; }
    static matrix2d scale(T x, T y) { return matrix2d{ x, 0, 0, y, 0, 0 }; }
    static matrix2d rotate(T angle)
    {
        return matrix2d{ cos(angle), sin(angle), -sin(angle), cos(angle), 0, 0 };
    }

    static matrix2d rotate90(int angle)
    {
        static constexpr portable_vec<T, 6> m[4] = {
            { 1, 0, 0, 1, 0, 0 }, { 0, 1, -1, 0, 0, 0 }, { -1, 0, 0, -1, 0, 0 }, { 0, -1, 1, 0, 0, 0 }
        };
        return matrix2d(vec<T, 6>(m[angle % 4]));
    }

    std::array<std::array<T, 3>, 3> full() const
    {
        return { { { a, b, T() }, { c, d, T() }, { e, f, T(1) } } };
    }

    friend matrix2d<T> operator*(const matrix2d<T>& m, const matrix2d<T>& n)
    {
        const std::array<std::array<T, 3>, 3> mm = m.full();
        const std::array<std::array<T, 3>, 3> nn = n.full();
        std::array<std::array<T, 3>, 3> a;
        for (size_t i = 0; i < 3; i++)
        {
            for (size_t j = 0; j < 3; j++)
            {
                T sum = 0;
                for (size_t k = 0; k < 3; k++)
                {
                    sum += mm[i][k] * nn[k][j];
                }
                a[i][j] = sum;
            }
        }
        return { a[0][0], a[0][1], a[1][0], a[1][1], a[2][0], a[2][1] };
    }

    friend point<T> operator*(const point<T>& pt, const matrix2d<T>& m)
    {
        return { pt.x * m.a + pt.y * m.c + m.e, pt.y * m.d + pt.x * m.b + m.f };
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/graphics/geometry.hpp#L518

max function

template <typename T1, typename T2,
          KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
Tout max(const T1 &x, const T2 &y)

Returns the greater of two values.

Source code
template <typename T1, typename T2, KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
KFR_INTRINSIC Tout max(const T1& x, const T2& y)
{
    return intrinsics::max(x, y);
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/min_max.hpp#L50

min function

template <typename T1, typename T2,
          KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
Tout min(const T1 &x, const T2 &y)

Returns the smaller of two values.

Source code
template <typename T1, typename T2, KFR_ENABLE_IF(is_numeric_args<T1, T2>),
          typename Tout = std::common_type_t<T1, T2>>
KFR_INTRINSIC Tout min(const T1& x, const T2& y)
{
    return intrinsics::min(x, y);
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/min_max.hpp#L40

select function

template <
    typename T1, size_t N, typename T2, typename T3,
    KFR_ENABLE_IF(is_numeric_args<T1, T2, T3>),
    typename Tout = subtype<std::common_type_t<T2, T3>>>
vec<Tout, N> select(const mask<T1, N> &m, const T2 &x,
                    const T3 &y)

Returns x if m is true, otherwise return y. Order of the arguments is same as in ternary operator.

return m ? x : y

Source code
template <typename T1, size_t N, typename T2, typename T3, KFR_ENABLE_IF(is_numeric_args<T1, T2, T3>),
          typename Tout = subtype<std::common_type_t<T2, T3>>>
KFR_INTRINSIC vec<Tout, N> select(const mask<T1, N>& m, const T2& x, const T3& y)
{
    return intrinsics::select(bitcast<Tout>(cast<itype<Tout>>(bitcast<itype<T1>>(m.asvec()))).asmask(),
                              broadcastto<Tout>(x), broadcastto<Tout>(y));
}

https://github.com/kfrlib/kfr/blob//include/kfr/simd/select.hpp#L43

sqrt function

template <typename T1, KFR_ENABLE_IF(is_numeric<T1>)>
flt_type<T1> sqrt(const T1 &x)

Returns the positive square root of the x. \(\sqrt{x}\)

Source code
template <typename T1, KFR_ENABLE_IF(is_numeric<T1>)>
KFR_INTRINSIC flt_type<T1> sqrt(const T1& x)
{
    return intrinsics::sqrt(x);
}

https://github.com/kfrlib/kfr/blob//include/kfr/math/sqrt.hpp#L39


Auto-generated from sources, Revision , https://github.com/kfrlib/kfr/blob//include/kfr/