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 = sycl;
23 namespace d = s::detail;
24 
25 namespace __host_std {
26 namespace {
27 
28 template <typename T> inline T __fclamp(T x, T minval, T maxval) {
29  return std::fmin(std::fmax(x, minval), maxval);
30 }
31 
32 template <typename T> inline T __degrees(T radians) {
33  return (180 / M_PI) * radians;
34 }
35 
36 template <typename T> inline T __mix(T x, T y, T a) { return x + (y - x) * a; }
37 
38 template <typename T> inline T __radians(T degrees) {
39  return (M_PI / 180) * degrees;
40 }
41 
42 template <typename T> inline T __step(T edge, T x) {
43  return (x < edge) ? 0.0 : 1.0;
44 }
45 
46 template <typename T> inline T __smoothstep(T edge0, T edge1, T x) {
47  T t;
48  T v = (x - edge0) / (edge1 - edge0);
49  t = __fclamp(v, T(0), T(1));
50  return t * t * (3 - 2 * t);
51 }
52 
53 template <typename T> inline T __sign(T x) {
55  return T(0.0);
56  if (x > 0)
57  return T(1.0);
58  if (x < 0)
59  return T(-1.0);
60  /* x is +0.0 or -0.0 */
61  return x;
62 }
63 
64 } // namespace
65 
66 // --------------- 4.13.5 Common functions. Host implementations ---------------
67 // fclamp
69  s::cl_float maxval) __NOEXC {
70  return __fclamp(x, minval, maxval);
71 }
73  s::cl_double maxval) __NOEXC {
74  return __fclamp(x, minval, maxval);
75 }
77  s::cl_half maxval) __NOEXC {
78  return __fclamp(x, minval, maxval);
79 }
85 
86 // degrees
87 __SYCL_EXPORT s::cl_float sycl_host_degrees(s::cl_float radians) __NOEXC {
88  return __degrees(radians);
89 }
91  return __degrees(radians);
92 }
94  return __degrees(radians);
95 }
99 
100 // fmin_common
102  s::cl_float y) __NOEXC {
103  return std::fmin(x, y);
104 }
106  s::cl_double y) __NOEXC {
107  return std::fmin(x, y);
108 }
110  s::cl_half y) __NOEXC {
111  return std::fmin(x, y);
112 }
116 
117 // fmax_common
119  s::cl_float y) __NOEXC {
120  return std::fmax(x, y);
121 }
123  s::cl_double y) __NOEXC {
124  return std::fmax(x, y);
125 }
127  s::cl_half y) __NOEXC {
128  return std::fmax(x, y);
129 }
133 
134 // mix
135 __SYCL_EXPORT s::cl_float sycl_host_mix(s::cl_float x, s::cl_float y,
136  s::cl_float a) __NOEXC {
137  return __mix(x, y, a);
138 }
140  s::cl_double a) __NOEXC {
141  return __mix(x, y, a);
142 }
144  s::cl_half a) __NOEXC {
145  return __mix(x, y, a);
146 }
149  s::cl_double)
151 
152 // radians
153 __SYCL_EXPORT s::cl_float sycl_host_radians(s::cl_float degrees) __NOEXC {
154  return __radians(degrees);
155 }
156 __SYCL_EXPORT s::cl_double sycl_host_radians(s::cl_double degrees) __NOEXC {
157  return __radians(degrees);
158 }
159 __SYCL_EXPORT s::cl_half sycl_host_radians(s::cl_half degrees) __NOEXC {
160  return __radians(degrees);
161 }
162 MAKE_1V(sycl_host_radians, s::cl_float, s::cl_float)
163 MAKE_1V(sycl_host_radians, s::cl_double, s::cl_double)
164 MAKE_1V(sycl_host_radians, s::cl_half, s::cl_half)
165 
166 // step
167 __SYCL_EXPORT s::cl_float sycl_host_step(s::cl_float edge,
168  s::cl_float x) __NOEXC {
169  return __step(edge, x);
170 }
171 __SYCL_EXPORT s::cl_double sycl_host_step(s::cl_double edge,
172  s::cl_double x) __NOEXC {
173  return __step(edge, x);
174 }
175 __SYCL_EXPORT s::cl_half sycl_host_step(s::cl_half edge, s::cl_half x) __NOEXC {
176  return __step(edge, x);
177 }
180 MAKE_1V_2V(sycl_host_step, s::cl_half, s::cl_half, s::cl_half)
181 
182 // smoothstep
183 __SYCL_EXPORT s::cl_float sycl_host_smoothstep(s::cl_float edge0,
184  s::cl_float edge1,
185  s::cl_float x) __NOEXC {
186  return __smoothstep(edge0, edge1, x);
187 }
188 __SYCL_EXPORT s::cl_double sycl_host_smoothstep(s::cl_double edge0,
189  s::cl_double edge1,
190  s::cl_double x) __NOEXC {
191  return __smoothstep(edge0, edge1, x);
192 }
193 __SYCL_EXPORT s::cl_half
194 sycl_host_smoothstep(s::cl_half edge0, s::cl_half edge1, s::cl_half x) __NOEXC {
195  return __smoothstep(edge0, edge1, x);
196 }
197 MAKE_1V_2V_3V(sycl_host_smoothstep, s::cl_float, s::cl_float, s::cl_float,
198  s::cl_float)
199 MAKE_1V_2V_3V(sycl_host_smoothstep, s::cl_double, s::cl_double, s::cl_double,
200  s::cl_double)
201 MAKE_1V_2V_3V(sycl_host_smoothstep, s::cl_half, s::cl_half, s::cl_half,
202  s::cl_half)
203 
204 // sign
205 __SYCL_EXPORT s::cl_float sycl_host_sign(s::cl_float x) __NOEXC {
206  return __sign(x);
207 }
208 __SYCL_EXPORT s::cl_double sycl_host_sign(s::cl_double x) __NOEXC {
209  return __sign(x);
210 }
211 __SYCL_EXPORT s::cl_half sycl_host_sign(s::cl_half x) __NOEXC {
212  return __sign(x);
213 }
214 MAKE_1V(sycl_host_sign, s::cl_float, s::cl_float)
215 MAKE_1V(sycl_host_sign, s::cl_double, s::cl_double)
216 MAKE_1V(sycl_host_sign, s::cl_half, s::cl_half)
217 
218 } // namespace __host_std
__NOEXC
#define __NOEXC
Definition: builtins.hpp:18
__host_std::sycl_host_fclamp
s::cl_float sycl_host_fclamp(s::cl_float x, s::cl_float minval, s::cl_float maxval) __NOEXC
Definition: builtins_common.cpp:68
__host_std
Definition: builtins.hpp:106
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
MAKE_1V_2V
#define MAKE_1V_2V(Fun, Ret, Arg1, Arg2)
Definition: builtins_helper.hpp:125
__host_std::sycl_host_degrees
s::cl_double s::cl_float sycl_host_degrees(s::cl_float radians) __NOEXC
Definition: builtins_common.cpp:87
sycl::_V1::ext::oneapi::experimental::isnan
std::enable_if_t< std::is_same< T, bfloat16 >::value, bool > isnan(T x)
Definition: bfloat16_math.hpp:36
__host_std::MAKE_1V_2V_3V
MAKE_1V_2V_3V(sycl_host_fclamp, s::cl_float, s::cl_float, s::cl_float, s::cl_float) MAKE_1V_2V_3V(sycl_host_fclamp
builtins_helper.hpp
__host_std::sycl_host_fmax_common
s::cl_float sycl_host_fmax_common(s::cl_float x, s::cl_float y) __NOEXC
Definition: builtins_common.cpp:118
sycl::_V1::opencl::cl_half
half cl_half
Definition: aliases.hpp:140
sycl::_V1::detail::cast_if_host_half
T cast_if_host_half(T val)
Definition: half_type.hpp:571
MAKE_1V
#define MAKE_1V(Fun, Ret, Arg1)
Definition: builtins_helper.hpp:115
sycl::_V1::opencl::cl_double
double cl_double
Definition: aliases.hpp:142
__host_std::sycl_host_mix
s::cl_float sycl_host_mix(s::cl_float x, s::cl_float y, s::cl_float a) __NOEXC
Definition: builtins_common.cpp:135
__host_std::sycl_host_fmin_common
s::cl_float sycl_host_fmin_common(s::cl_float x, s::cl_float y) __NOEXC
Definition: builtins_common.cpp:101
sycl::_V1::opencl::cl_float
float cl_float
Definition: aliases.hpp:141