DPC++ Runtime
Runtime libraries for oneAPI DPC++
builtins_common.cpp
Go to the documentation of this file.
1 //==----------- builtins_common.cpp - SYCL built-in common functions -------==//
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 // This file defines the host versions of functions defined
10 // in SYCL SPEC section - 4.13.5 Common functions.
11 
12 // Define _USE_MATH_DEFINES to enforce math defines of macros like M_PI in
13 // <cmath>. _USE_MATH_DEFINES is defined here before includes of SYCL header
14 // files to avoid include of <cmath> via those SYCL headers with unset
15 // _USE_MATH_DEFINES.
16 #define _USE_MATH_DEFINES
17 
18 #include "builtins_helper.hpp"
19 
20 #include <cmath>
21 
22 namespace s = cl::sycl;
23 namespace d = s::detail;
24 
26 namespace __host_std {
27 namespace {
28 
29 template <typename T> inline T __fclamp(T x, T minval, T maxval) {
30  return std::fmin(std::fmax(x, minval), maxval);
31 }
32 
33 template <typename T> inline T __degrees(T radians) {
34  return (180 / M_PI) * radians;
35 }
36 
37 template <typename T> inline T __mix(T x, T y, T a) { return x + (y - x) * a; }
38 
39 template <typename T> inline T __radians(T degrees) {
40  return (M_PI / 180) * degrees;
41 }
42 
43 template <typename T> inline T __step(T edge, T x) {
44  return (x < edge) ? 0.0 : 1.0;
45 }
46 
47 template <typename T> inline T __smoothstep(T edge0, T edge1, T x) {
48  T t;
49  T v = (x - edge0) / (edge1 - edge0);
50  t = __fclamp(v, T(0), T(1));
51  return t * t * (3 - 2 * t);
52 }
53 
54 template <typename T> inline T __sign(T x) {
56  return T(0.0);
57  if (x > 0)
58  return T(1.0);
59  if (x < 0)
60  return T(-1.0);
61  /* x is +0.0 or -0.0 */
62  return x;
63 }
64 
65 } // namespace
66 
67 // --------------- 4.13.5 Common functions. Host implementations ---------------
68 // fclamp
69 __SYCL_EXPORT s::cl_float fclamp(s::cl_float x, s::cl_float minval,
70  s::cl_float maxval) __NOEXC {
71  return __fclamp(x, minval, maxval);
72 }
73 __SYCL_EXPORT s::cl_double fclamp(s::cl_double x, s::cl_double minval,
74  s::cl_double maxval) __NOEXC {
75  return __fclamp(x, minval, maxval);
76 }
77 __SYCL_EXPORT s::cl_half fclamp(s::cl_half x, s::cl_half minval,
78  s::cl_half maxval) __NOEXC {
79  return __fclamp(x, minval, maxval);
80 }
84 
85 // degrees
87  return __degrees(radians);
88 }
90  return __degrees(radians);
91 }
93  return __degrees(radians);
94 }
98 
99 // fmin_common
100 __SYCL_EXPORT s::cl_float fmin_common(s::cl_float x, s::cl_float y) __NOEXC {
101  return std::fmin(x, y);
102 }
104  return std::fmin(x, y);
105 }
107  return std::fmin(x, y);
108 }
112 
113 // fmax_common
114 __SYCL_EXPORT s::cl_float fmax_common(s::cl_float x, s::cl_float y) __NOEXC {
115  return std::fmax(x, y);
116 }
118  return std::fmax(x, y);
119 }
121  return std::fmax(x, y);
122 }
126 
127 // mix
128 __SYCL_EXPORT s::cl_float mix(s::cl_float x, s::cl_float y,
129  s::cl_float a) __NOEXC {
130  return __mix(x, y, a);
131 }
133  s::cl_double a) __NOEXC {
134  return __mix(x, y, a);
135 }
137  return __mix(x, y, a);
138 }
142 
143 // radians
145  return __radians(degrees);
146 }
148  return __radians(degrees);
149 }
151  return __radians(degrees);
152 }
156 
157 // step
158 __SYCL_EXPORT s::cl_float step(s::cl_float edge, s::cl_float x) __NOEXC {
159  return __step(edge, x);
160 }
162  return __step(edge, x);
163 }
164 __SYCL_EXPORT s::cl_half step(s::cl_half edge, s::cl_half x) __NOEXC {
165  return __step(edge, x);
166 }
170 
171 // smoothstep
172 __SYCL_EXPORT s::cl_float smoothstep(s::cl_float edge0, s::cl_float edge1,
173  s::cl_float x) __NOEXC {
174  return __smoothstep(edge0, edge1, x);
175 }
177  s::cl_double x) __NOEXC {
178  return __smoothstep(edge0, edge1, x);
179 }
180 __SYCL_EXPORT s::cl_half smoothstep(s::cl_half edge0, s::cl_half edge1,
181  s::cl_half x) __NOEXC {
182  return __smoothstep(edge0, edge1, x);
183 }
186  s::cl_double)
188 
189 // sign
190 __SYCL_EXPORT s::cl_float sign(s::cl_float x) __NOEXC { return __sign(x); }
191 __SYCL_EXPORT s::cl_double sign(s::cl_double x) __NOEXC { return __sign(x); }
192 __SYCL_EXPORT s::cl_half sign(s::cl_half x) __NOEXC { return __sign(x); }
196 
197 } // namespace __host_std
198 } // __SYCL_INLINE_NAMESPACE(cl)
cl::__host_std::degrees
s::cl_half degrees(s::cl_half radians) __NOEXC
Definition: builtins_common.cpp:92
cl::sycl::detail::cast_if_host_half
T cast_if_host_half(T val)
Definition: half_type.hpp:617
cl::sycl::cl_double
double cl_double
Definition: aliases.hpp:89
MAKE_1V_2V_3V
#define MAKE_1V_2V_3V(Fun, Ret, Arg1, Arg2, Arg3)
Definition: builtins_helper.hpp:140
T
cl::sycl
Definition: access.hpp:14
MAKE_1V_2V
#define MAKE_1V_2V(Fun, Ret, Arg1, Arg2)
Definition: builtins_helper.hpp:129
cl::sycl::isnan
detail::common_rel_ret_t< T > isnan(T x) __NOEXC
Definition: builtins.hpp:1228
cl::__host_std::smoothstep
s::cl_half smoothstep(s::cl_half edge0, s::cl_half edge1, s::cl_half x) __NOEXC
Definition: builtins_common.cpp:180
cl::sycl::detail::half_impl::half
Definition: half_type.hpp:329
cl::__host_std::fmax_common
s::cl_half fmax_common(s::cl_half x, s::cl_half y) __NOEXC
Definition: builtins_common.cpp:120
__NOEXC
#define __NOEXC
Definition: builtins.hpp:18
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::sign
detail::enable_if_t< detail::is_genfloat< T >::value, T > sign(T x) __NOEXC
Definition: builtins.hpp:622
builtins_helper.hpp
cl::__host_std::step
s::cl_half step(s::cl_half edge, s::cl_half x) __NOEXC
Definition: builtins_common.cpp:164
cl::sycl::cl_float
float cl_float
Definition: aliases.hpp:88
cl::__host_std::fclamp
s::cl_half fclamp(s::cl_half x, s::cl_half minval, s::cl_half maxval) __NOEXC
Definition: builtins_common.cpp:77
cl::__host_std::mix
s::cl_half mix(s::cl_half x, s::cl_half y, s::cl_half a) __NOEXC
Definition: builtins_common.cpp:136
MAKE_1V
#define MAKE_1V(Fun, Ret, Arg1)
Definition: builtins_helper.hpp:119
cl::__host_std::fmin_common
s::cl_half fmin_common(s::cl_half x, s::cl_half y) __NOEXC
Definition: builtins_common.cpp:106
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12
cl::__host_std::radians
s::cl_half radians(s::cl_half degrees) __NOEXC
Definition: builtins_common.cpp:150