Skip to content

Commit

Permalink
update vector function
Browse files Browse the repository at this point in the history
  • Loading branch information
YGXXD committed Dec 26, 2024
1 parent 66fbaac commit 6fe4a78
Show file tree
Hide file tree
Showing 17 changed files with 746 additions and 566 deletions.
2 changes: 1 addition & 1 deletion ktm/detail/function/geometric.inl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "../../type/basic.h"
#include "../../type/vec_fwd.h"
#include "../../function/exponential.h"
#include "../../function/common.h"
#include "../../function/vector.h"

template<size_t N, typename T, typename Void>
struct ktm::detail::geometric_implement::dot
Expand Down
55 changes: 55 additions & 0 deletions ktm/detail/function/vector.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// MIT License
//
// Copyright (c) 2023-2024 有个小小杜
//
// Created by 有个小小杜
//

#ifndef _KTM_VECTOR_INL_
#define _KTM_VECTOR_INL_

#include "vector_fwd.h"
#include "../loop_util.h"
#include "../../type/vec_fwd.h"
#include "../../function/arithmetic.h"

template<size_t N, typename T, typename Void>
struct ktm::detail::vector_implement::reduce_add
{
using V = vec<N, T>;
static KTM_INLINE T call(const V& x) noexcept
{
T ret = x[0];
loop_op<N - 1, void>::call(
[&ret](const T& x) -> void { ret += x; }, &x[1]);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::vector_implement::reduce_min
{
using V = vec<N, T>;
static KTM_INLINE T call(const V& x) noexcept
{
T ret = x[0];
loop_op<N - 1, void>::call(
[&ret](const T& x) -> void { ret = ktm::min<T>(ret, x); }, &x[1]);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::vector_implement::reduce_max
{
using V = vec<N, T>;
static KTM_INLINE T call(const V& x) noexcept
{
T ret = x[0];
loop_op<N - 1, void>::call(
[&ret](const T& x) -> void { ret = ktm::max<T>(ret, x); }, &x[1]);
return ret;
}
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,16 @@
// Created by 有个小小杜
//

#ifndef _KTM_COMMON_INL_
#define _KTM_COMMON_INL_
#ifndef _KTM_VECTOR_ARITHMETIC_INL_
#define _KTM_VECTOR_ARITHMETIC_INL_

#include "common_fwd.h"
#include "vector_arithmetic_fwd.h"
#include "../loop_util.h"
#include "../../type/vec_fwd.h"
#include "../../function/arithmetic.h"
#include "../../function/exponential.h"

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::reduce_add
{
using V = vec<N, T>;
static KTM_INLINE T call(const V& x) noexcept
{
T ret = x[0];
loop_op<N - 1, void>::call(
[&ret](const T& x) -> void { ret += x; }, &x[1]);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::reduce_min
{
using V = vec<N, T>;
static KTM_INLINE T call(const V& x) noexcept
{
T ret = x[0];
loop_op<N - 1, void>::call(
[&ret](const T& x) -> void { ret = ktm::min<T>(ret, x); }, &x[1]);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::reduce_max
{
using V = vec<N, T>;
static KTM_INLINE T call(const V& x) noexcept
{
T ret = x[0];
loop_op<N - 1, void>::call(
[&ret](const T& x) -> void { ret = ktm::max<T>(ret, x); }, &x[1]);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::abs
struct ktm::detail::vector_arithmetic_implement::abs
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
Expand All @@ -66,7 +26,7 @@ struct ktm::detail::common_implement::abs
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::min
struct ktm::detail::vector_arithmetic_implement::min
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x, const V& y) noexcept
Expand All @@ -78,7 +38,7 @@ struct ktm::detail::common_implement::min
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::max
struct ktm::detail::vector_arithmetic_implement::max
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x, const V& y) noexcept
Expand All @@ -90,7 +50,7 @@ struct ktm::detail::common_implement::max
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::clamp
struct ktm::detail::vector_arithmetic_implement::clamp
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& v, const V& min, const V& max) noexcept
Expand All @@ -102,7 +62,7 @@ struct ktm::detail::common_implement::clamp
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::floor
struct ktm::detail::vector_arithmetic_implement::floor
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
Expand All @@ -114,7 +74,7 @@ struct ktm::detail::common_implement::floor
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::ceil
struct ktm::detail::vector_arithmetic_implement::ceil
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
Expand All @@ -126,7 +86,7 @@ struct ktm::detail::common_implement::ceil
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::round
struct ktm::detail::vector_arithmetic_implement::round
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
Expand All @@ -138,7 +98,7 @@ struct ktm::detail::common_implement::round
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::fract
struct ktm::detail::vector_arithmetic_implement::fract
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
Expand All @@ -150,7 +110,7 @@ struct ktm::detail::common_implement::fract
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::mod
struct ktm::detail::vector_arithmetic_implement::mod
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x, const V& y) noexcept
Expand All @@ -162,7 +122,7 @@ struct ktm::detail::common_implement::mod
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::lerp
struct ktm::detail::vector_arithmetic_implement::lerp
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x, const V& y, T t) noexcept
Expand All @@ -175,7 +135,7 @@ struct ktm::detail::common_implement::lerp
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::mix
struct ktm::detail::vector_arithmetic_implement::mix
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x, const V& y, const V& t) noexcept
Expand All @@ -187,7 +147,7 @@ struct ktm::detail::common_implement::mix
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::step
struct ktm::detail::vector_arithmetic_implement::step
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& edge, const V& x) noexcept
Expand All @@ -199,7 +159,7 @@ struct ktm::detail::common_implement::step
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::smoothstep
struct ktm::detail::vector_arithmetic_implement::smoothstep
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& edge0, const V& edge1, const V& x) noexcept
Expand All @@ -210,76 +170,4 @@ struct ktm::detail::common_implement::smoothstep
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::sqrt
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
{
V ret;
loop_op<N, V>::call(ret, ktm::sqrt<T>, x);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::rsqrt
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
{
V ret;
loop_op<N, V>::call(ret, ktm::rsqrt<T>, x);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::recip
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
{
V ret;
loop_op<N, V>::call(ret, ktm::recip<T>, x);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::fast_sqrt
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
{
V ret;
loop_op<N, V>::call(ret, ktm::fast::sqrt<T>, x);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::fast_rsqrt
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
{
V ret;
loop_op<N, V>::call(ret, ktm::fast::rsqrt<T>, x);
return ret;
}
};

template<size_t N, typename T, typename Void>
struct ktm::detail::common_implement::fast_recip
{
using V = vec<N, T>;
static KTM_INLINE V call(const V& x) noexcept
{
V ret;
loop_op<N, V>::call(ret, ktm::fast::recip<T>, x);
return ret;
}
};

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Created by 有个小小杜
//

#ifndef _KTM_COMMON_FWD_H_
#define _KTM_COMMON_FWD_H_
#ifndef _KTM_VECTOR_ARITHMETIC_FWD_H_
#define _KTM_VECTOR_ARITHMETIC_FWD_H_

#include <cstddef>
#include "../../traits/type_traits_ext.h"
Expand All @@ -15,18 +15,9 @@ namespace ktm
{
namespace detail
{
namespace common_implement
namespace vector_arithmetic_implement
{

template<size_t N, typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
struct reduce_add;

template<size_t N, typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
struct reduce_min;

template<size_t N, typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
struct reduce_max;

template<size_t N, typename T, typename = std::enable_if_t<std::is_arithmetic_v<T> && !std::is_unsigned_v<T>>>
struct abs;

Expand Down Expand Up @@ -66,26 +57,8 @@ struct step;
template<size_t N, typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
struct smoothstep;

template<size_t N, typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
struct sqrt;

template<size_t N, typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
struct rsqrt;

template<size_t N, typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
struct recip;

template<size_t N, typename T, typename = std::enable_if_t<std::is_exist_same_vs<float, double, T>>>
struct fast_sqrt;

template<size_t N, typename T, typename = std::enable_if_t<std::is_exist_same_vs<float, double, T>>>
struct fast_rsqrt;

template<size_t N, typename T, typename = std::enable_if_t<std::is_exist_same_vs<float, double, T>>>
struct fast_recip;

}
}
}

#endif
#endif
Loading

0 comments on commit 6fe4a78

Please sign in to comment.