DPC++ Runtime
Runtime libraries for oneAPI DPC++
common_functions.cpp
Go to the documentation of this file.
1 //==------------------- common_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 
9 // Define _USE_MATH_DEFINES to enforce math defines of macros like M_PI in
10 // <cmath>. _USE_MATH_DEFINES is defined here before includes of SYCL header
11 // files to avoid include of <cmath> via those SYCL headers with unset
12 // _USE_MATH_DEFINES.
13 //
14 // Note that C++20 has std::numbers containing the constants but we're limited
15 // to C++17.
16 #define _USE_MATH_DEFINES
17 #include <cmath>
18 
20 
21 #include "host_helper_macros.hpp"
22 
23 namespace sycl {
24 inline namespace _V1 {
25 #define BUILTIN_COMMON(NUM_ARGS, NAME, IMPL) \
26  HOST_IMPL(NAME, IMPL) \
27  EXPORT_SCALAR_AND_VEC_1_16(NUM_ARGS, NAME, FP_TYPES)
28 
29 BUILTIN_COMMON(ONE_ARG, degrees,
30  [](auto x) -> decltype(x) { return (180 / M_PI) * x; })
31 
33  [](auto x) -> decltype(x) { return (M_PI / 180) * x; })
34 
35 BUILTIN_COMMON(ONE_ARG, sign, [](auto x) -> decltype(x) {
36  using T = decltype(x);
37  if (std::isnan(x))
38  return T(0.0);
39  if (x > 0)
40  return T(1.0);
41  if (x < 0)
42  return T(-1.0);
43  /* x is +0.0 or -0.0 */
44  return x;
45 })
46 
47 BUILTIN_COMMON(THREE_ARGS, mix, [](auto x, auto y, auto z) -> decltype(x) {
48  return x + (y - x) * z;
49 })
50 
51 BUILTIN_COMMON(TWO_ARGS, step,
52  [](auto x, auto y) -> decltype(x) { return y < x ? 0.0 : 1.0; })
53 
55  [](auto x, auto y, auto z) -> decltype(x) {
56  using T = decltype(x);
57  auto t = sycl::clamp((z - x) / (y - x), T{0}, T{1});
58  return t * t * (3 - 2 * t);
59  })
60 
61 BUILTIN_COMMON(TWO_ARGS, max,
62  [](auto x, auto y) -> decltype(x) { return (x < y ? y : x); })
63 BUILTIN_COMMON(TWO_ARGS, min,
64  [](auto x, auto y) -> decltype(x) { return (y < x ? y : x); })
65 
66 BUILTIN_COMMON(THREE_ARGS, clamp, [](auto x, auto y, auto z) -> decltype(x) {
67  return std::fmin(std::fmax(x, y), z);
68 })
69 } // namespace _V1
70 } // namespace sycl
std::enable_if_t< std::is_same_v< T, bfloat16 >, bool > isnan(T x)
BUILTIN_COMMON(ONE_ARG, degrees, [](auto x) -> decltype(x) { return(180/M_PI) *x;}) BUILTIN_COMMON(ONE_ARG
auto auto autodecltype(x) z
autodecltype(x) x
Definition: access.hpp:18