DPC++ Runtime
Runtime libraries for oneAPI DPC++
geometric_functions.cpp
Go to the documentation of this file.
1 //==------------------- geometric_functions.cpp ----------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "host_helper_macros.hpp"
12 
13 #include <cmath>
14 
15 namespace sycl {
16 inline namespace _V1 {
17 template <typename T> static inline T cross_host_impl(T p0, T p1) {
18  T result(0);
19  result.x() = p0.y() * p1.z() - p0.z() * p1.y();
20  result.y() = p0.z() * p1.x() - p0.x() * p1.z();
21  result.z() = p0.x() * p1.y() - p0.y() * p1.x();
22  return result;
23 }
24 EXPORT_VEC_3_4(TWO_ARGS, cross, FP_TYPES)
25 
26 template <typename T0, typename T1>
27 static inline auto dot_host_impl(T0 x, T1 y) {
28  if constexpr (detail::is_scalar_arithmetic<T0>::value) {
29  return x * y;
30  } else {
31  auto R = x[0] * y[0];
32  for (size_t i = 1; i < detail::num_elements<T0>::value; ++i)
33  R += x[i] * y[i];
34  return R;
35  }
36 }
37 EXPORT_SCALAR_AND_VEC_2_4(TWO_ARGS, dot, FP_TYPES)
38 
39 #if defined(__GNUC__) && !defined(__clang__)
40 // GCC miscompiles if using dot (instead of dot_host_impl) *or* if
41 // optimizations aren't disabled here. Not sure if a bug in GCC or some UB in
42 // sycl::vec/sycl::half (like ansi-alias violations).
43 #pragma GCC push_options
44 #pragma GCC optimize("O0")
45 #endif
46 template <typename T> static inline auto length_host_impl(T x) {
47  auto d = dot_host_impl(x, x);
48  return static_cast<decltype(d)>(std::sqrt(d));
49 }
50 #if defined(__GNUC__) && !defined(__clang__)
51 #pragma GCC pop_options
52 #endif
54 
55 // fast_length on host is the same as just length.
56 template <typename T> static inline auto fast_length_host_impl(T x) {
57  return length_host_impl(x);
58 }
60 
61 template <typename T0, typename T1>
62 static inline auto distance_host_impl(T0 x, T1 y) {
63  return length(x - y);
64 }
65 EXPORT_SCALAR_AND_VEC_2_4(TWO_ARGS, distance, FP_TYPES)
66 // fast_distance on host is the same as just distance.
67 template <typename T0, typename T1>
68 static inline auto fast_distance_host_impl(T0 x, T1 y) {
69  return distance_host_impl(x, y);
70 }
71 EXPORT_SCALAR_AND_VEC_2_4(TWO_ARGS, fast_distance, float)
72 
73 template <typename T> static inline auto normalize_host_impl(T x) {
74  auto len = length(x);
75  if (len == 0)
76  return x;
77  return x / len;
78 }
79 EXPORT_SCALAR_AND_VEC_2_4(ONE_ARG, normalize, FP_TYPES)
80 // fast_normalize on host is the same as just normalize.
81 template <typename T> static inline auto fast_normalize_host_impl(T x) {
82  return normalize_host_impl(x);
83 }
84 EXPORT_SCALAR_AND_VEC_2_4(ONE_ARG, fast_normalize, float)
85 } // namespace _V1
86 } // namespace sycl
__ESIMD_API simd< T, N > sqrt(simd< T, N > src, Sat sat={})
Square root.
Definition: math.hpp:389
#define FP_TYPES
#define EXPORT_SCALAR_AND_VEC_2_4(NUM_ARGS, NAME,...)
#define EXPORT_VEC_3_4(NUM_ARGS, NAME,...)
static auto fast_length_host_impl(T x)
static auto length_host_impl(T x)
static T cross_host_impl(T p0, T p1)
static auto dot_host_impl(T0 x, T1 y)
static auto normalize_host_impl(T x)
static auto fast_normalize_host_impl(T x)
static auto fast_distance_host_impl(T0 x, T1 y)
static auto distance_host_impl(T0 x, T1 y)
autodecltype(x) x
Definition: access.hpp:18
float fast_length(const float *a, int len)
Compute fast_length for variable-length array.
Definition: math.hpp:135
ValueT length(const ValueT *a, const int len)
Calculate the square root of the input array.
Definition: math.hpp:160