-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement length and distance, + a few fixes
- Loading branch information
Showing
8 changed files
with
149 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#pragma once | ||
|
||
#include "math.hh" | ||
#include "vec.hh" | ||
|
||
namespace simsycl::detail { | ||
|
||
template<typename T> | ||
concept SyclFloat = std::is_same_v<T, float> || std::is_same_v<T, double> | ||
#if SIMSYCL_FEATURE_HALF_TYPE | ||
|| std::is_same_v<T, sycl::half> | ||
#endif | ||
; | ||
|
||
template<typename T> | ||
concept GeoFloat = // | ||
SyclFloat<T> | ||
|| ((is_swizzle_v<T> || is_vec_v<T>)&&(num_elements_v<T> > 0 && num_elements_v<T> <= 4) | ||
&& SyclFloat<typename T::element_type>); // TODO: marray | ||
|
||
template<typename T> | ||
requires(is_vec_v<T> || is_swizzle_v<T>) | ||
auto sum(const T &f) { | ||
auto ret = f[0]; | ||
for(int i = 1; i < num_elements_v<T>; ++i) { ret += f[i]; } | ||
return ret; | ||
} | ||
template<SyclFloat T> | ||
auto sum(const T &f) { | ||
return f; | ||
} | ||
|
||
template<GeoFloat T> | ||
struct element_type { | ||
using type = T; | ||
}; | ||
template<GeoFloat T> | ||
requires(is_vec_v<T> || is_swizzle_v<T>) | ||
struct element_type<T> { | ||
using type = typename T::element_type; | ||
}; | ||
template<GeoFloat T> | ||
using element_type_t = typename element_type<T>::type; | ||
|
||
template<typename VT, typename T> | ||
auto to_matching_vec(const T &v) { | ||
return detail::to_vec<detail::element_type_t<VT>, detail::num_elements_v<VT>>(v); | ||
} | ||
|
||
} // namespace simsycl::detail | ||
|
||
namespace simsycl::sycl { | ||
|
||
// Note: arguments are passed by const ref rather than value to avoid gcc warnings | ||
// the standard requires pass-by-value, but I'm not sure if this is visible to the user | ||
|
||
// TODO cross | ||
// TODO dot | ||
|
||
template<detail::GeoFloat T> | ||
auto length(const T &f) { | ||
return sqrt(detail::sum(pow(detail::to_matching_vec<T>(f), detail::to_matching_vec<T>(2)))); | ||
} | ||
|
||
template<detail::GeoFloat T1, detail::GeoFloat T2> | ||
auto distance(const T1 &p0, const T2 &p1) { | ||
return length(p1 - p0); | ||
} | ||
|
||
// TODO normalize | ||
// TODO fast_distance | ||
// TODO fast_length | ||
// TODO fast_normalize | ||
|
||
} // namespace simsycl::sycl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <catch2/catch_approx.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#define SYCL_SIMPLE_SWIZZLES | ||
#include <sycl/sycl.hpp> | ||
|
||
using namespace sycl; | ||
|
||
TEST_CASE("Length function works as expected", "[math][geometric]") { | ||
double x = 8.0f; | ||
float y = 7.0f; | ||
vec<double, 2> v1 = {1.0, 2.0}; | ||
vec<float, 2> v2 = {3.0f, 4.0f}; | ||
vec<double, 3> v3 = {5.0, 6.0, 7.0}; | ||
vec<float, 3> v4 = {8.0f, 9.0f, 10.0f}; | ||
vec<double, 4> v5 = {11.0, 12.0, 13.0, 14.0}; | ||
vec<float, 4> v6 = {15.0f, 16.0f, 17.0f, 18.0f}; | ||
|
||
CHECK(length(x) == Catch::Approx(8.0)); | ||
CHECK(length(y) == Catch::Approx(7.0f)); | ||
CHECK(length(v1) == Catch::Approx(2.23606797749979)); | ||
CHECK(length(v2) == Catch::Approx(5.0f)); | ||
CHECK(length(v3) == Catch::Approx(10.488088481701515)); | ||
CHECK(length(v4) == Catch::Approx(15.6524758425f)); | ||
CHECK(length(v5) == Catch::Approx(25.099800796)); | ||
CHECK(length(v6) == Catch::Approx(33.07567f)); | ||
CHECK(length(v1.xx()) == Catch::Approx(1.4142135624)); | ||
CHECK(length(v6.argb()) == Catch::Approx(33.07567f)); | ||
|
||
#if SIMSYCL_FEATURE_HALF_TYPE | ||
using sycl::half; | ||
half h = 7.0f; | ||
vec<half, 2> vh1 = vec<half, 2>(3.0f, 4.0f); | ||
CHECK(length(h) == Catch::Approx(7.0f)); | ||
CHECK(length(vh1) == Catch::Approx(5.0f)); | ||
CHECK(length(vh1.yx()) == Catch::Approx(5.0f)); | ||
#endif | ||
} | ||
|
||
TEST_CASE("Distance function works as expected", "[math][geometric]") { | ||
double x = 8.0f; | ||
float y = 7.0f; | ||
vec<double, 2> v1 = {1.0, 2.0}; | ||
vec<float, 2> v2 = {3.0f, 4.0f}; | ||
vec<double, 3> v3 = {5.0, 6.0, 7.0}; | ||
vec<float, 3> v4 = {8.0f, 9.0f, 10.0f}; | ||
vec<double, 4> v5 = {11.0, 12.0, 13.0, 14.0}; | ||
vec<float, 4> v6 = {15.0f, 16.0f, 17.0f, 18.0f}; | ||
|
||
CHECK(distance(x, 2.0) == Catch::Approx(6.0)); | ||
CHECK(distance(y, -1.0f) == Catch::Approx(8.0f)); | ||
CHECK(distance(v1, vec<double, 2>{0.0, 0.0}) == Catch::Approx(2.23606797749979)); | ||
CHECK(distance(v2, vec<float, 2>{0.0f, 0.0f}) == Catch::Approx(5.0f)); | ||
CHECK(distance(v3, vec<double, 3>{0.0, 0.0, 0.0}) == Catch::Approx(10.488088481701515)); | ||
CHECK(distance(v4, vec<float, 3>{0.0f, 0.0f, 0.0f}) == Catch::Approx(15.6524758425f)); | ||
CHECK(distance(v5, vec<double, 4>{0.0, 0.0, 0.0, 0.0}) == Catch::Approx(25.099800796)); | ||
CHECK(distance(v6, vec<float, 4>{0.0f, 0.0f, 0.0f, 0.0f}) == Catch::Approx(33.07567f)); | ||
CHECK(distance(v1.xx(), v1.yx()) == Catch::Approx(1.0)); | ||
CHECK(distance(v6.argb(), vec<float, 4>{0.0f, 0.0f, 0.0f, 0.0f}) == Catch::Approx(33.07567f)); | ||
CHECK(distance(v6.argb(), v2.xyxy()) == Catch::Approx(26.15339f)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters