23 #ifndef HIP_INCLUDE_HIP_HCC_DETAIL_DEVICE_FUNCTIONS_H 24 #define HIP_INCLUDE_HIP_HCC_DETAIL_DEVICE_FUNCTIONS_H 29 #include <hip/hip_runtime_api.h> 33 #include <hip/hip_vector_types.h> 37 #if __HIP_CLANG_ONLY__ && __HIP_ROCclr__ && !_WIN32 38 extern "C" __device__
int printf(
const char *fmt, ...);
41 template <
typename... All>
42 static inline __device__
void printf(
const char* format, All... all) {
43 hc::printf(format, all...);
46 template <
typename... All>
47 static inline __device__
void printf(
const char* format, All... all) {}
48 #endif // HC_FEATURE_PRINTF 49 #endif // __HIP_CLANG_ONLY__ && __HIP_ROCclr__ 56 __device__
static inline unsigned int __popc(
unsigned int input) {
57 return __builtin_popcount(input);
59 __device__
static inline unsigned int __popcll(
unsigned long long int input) {
60 return __builtin_popcountll(input);
63 __device__
static inline int __clz(
int input) {
64 return __ockl_clz_u32((uint)input);
67 __device__
static inline int __clzll(
long long int input) {
68 return __ockl_clz_u64((ullong)input);
71 __device__
static inline unsigned int __ffs(
unsigned int input) {
72 return ( input == 0 ? -1 : __builtin_ctz(input) ) + 1;
75 __device__
static inline unsigned int __ffsll(
unsigned long long int input) {
76 return ( input == 0 ? -1 : __builtin_ctzll(input) ) + 1;
79 __device__
static inline unsigned int __ffs(
int input) {
80 return ( input == 0 ? -1 : __builtin_ctz(input) ) + 1;
83 __device__
static inline unsigned int __ffsll(
long long int input) {
84 return ( input == 0 ? -1 : __builtin_ctzll(input) ) + 1;
87 __device__
static inline unsigned int __brev(
unsigned int input) {
88 return __llvm_bitrev_b32(input);
91 __device__
static inline unsigned long long int __brevll(
unsigned long long int input) {
92 return __llvm_bitrev_b64(input);
95 __device__
static inline unsigned int __lastbit_u32_u64(uint64_t input) {
96 return input == 0 ? -1 : __builtin_ctzl(input);
99 __device__
static inline unsigned int __bitextract_u32(
unsigned int src0,
unsigned int src1,
unsigned int src2) {
100 uint32_t offset = src1 & 31;
101 uint32_t width = src2 & 31;
102 return width == 0 ? 0 : (src0 << (32 - offset - width)) >> (32 - width);
105 __device__
static inline uint64_t __bitextract_u64(uint64_t src0,
unsigned int src1,
unsigned int src2) {
106 uint64_t offset = src1 & 63;
107 uint64_t width = src2 & 63;
108 return width == 0 ? 0 : (src0 << (64 - offset - width)) >> (64 - width);
111 __device__
static inline unsigned int __bitinsert_u32(
unsigned int src0,
unsigned int src1,
unsigned int src2,
unsigned int src3) {
112 uint32_t offset = src2 & 31;
113 uint32_t width = src3 & 31;
114 uint32_t mask = (1 << width) - 1;
115 return ((src0 & ~(mask << offset)) | ((src1 & mask) << offset));
118 __device__
static inline uint64_t __bitinsert_u64(uint64_t src0, uint64_t src1,
unsigned int src2,
unsigned int src3) {
119 uint64_t offset = src2 & 63;
120 uint64_t width = src3 & 63;
121 uint64_t mask = (1ULL << width) - 1;
122 return ((src0 & ~(mask << offset)) | ((src1 & mask) << offset));
125 __device__
static unsigned int __byte_perm(
unsigned int x,
unsigned int y,
unsigned int s);
126 __device__
static unsigned int __hadd(
int x,
int y);
127 __device__
static int __mul24(
int x,
int y);
128 __device__
static long long int __mul64hi(
long long int x,
long long int y);
129 __device__
static int __mulhi(
int x,
int y);
130 __device__
static int __rhadd(
int x,
int y);
131 __device__
static unsigned int __sad(
int x,
int y,
unsigned int z);
132 __device__
static unsigned int __uhadd(
unsigned int x,
unsigned int y);
133 __device__
static int __umul24(
unsigned int x,
unsigned int y);
134 __device__
static unsigned long long int __umul64hi(
unsigned long long int x,
unsigned long long int y);
135 __device__
static unsigned int __umulhi(
unsigned int x,
unsigned int y);
136 __device__
static unsigned int __urhadd(
unsigned int x,
unsigned int y);
137 __device__
static unsigned int __usad(
unsigned int x,
unsigned int y,
unsigned int z);
144 } __attribute__((aligned(4)));
151 } __attribute__((aligned(8)));
154 static inline unsigned int __byte_perm(
unsigned int x,
unsigned int y,
unsigned int s) {
161 cHoldOut.c[0] = cHoldVal.c[cHoldKey.c[0]];
162 cHoldOut.c[1] = cHoldVal.c[cHoldKey.c[1]];
163 cHoldOut.c[2] = cHoldVal.c[cHoldKey.c[2]];
164 cHoldOut.c[3] = cHoldVal.c[cHoldKey.c[3]];
168 __device__
static inline unsigned int __hadd(
int x,
int y) {
170 int sign = z & 0x8000000;
171 int value = z & 0x7FFFFFFF;
172 return ((value) >> 1 || sign);
175 __device__
static inline int __mul24(
int x,
int y) {
176 return __ockl_mul24_i32(x, y);
179 __device__
static inline long long __mul64hi(
long long int x,
long long int y) {
180 ulong x0 = (ulong)x & 0xffffffffUL;
182 ulong y0 = (ulong)y & 0xffffffffUL;
185 long t = x1*y0 + (z0 >> 32);
186 long z1 = t & 0xffffffffL;
189 return x1*y1 + z2 + (z1 >> 32);
192 __device__
static inline int __mulhi(
int x,
int y) {
193 return __ockl_mul_hi_i32(x, y);
196 __device__
static inline int __rhadd(
int x,
int y) {
198 int sign = z & 0x8000000;
199 int value = z & 0x7FFFFFFF;
200 return ((value) >> 1 || sign);
202 __device__
static inline unsigned int __sad(
int x,
int y,
unsigned int z) {
203 return x > y ? x - y + z : y - x + z;
205 __device__
static inline unsigned int __uhadd(
unsigned int x,
unsigned int y) {
208 __device__
static inline int __umul24(
unsigned int x,
unsigned int y) {
209 return __ockl_mul24_u32(x, y);
213 static inline unsigned long long __umul64hi(
unsigned long long int x,
unsigned long long int y) {
214 ulong x0 = x & 0xffffffffUL;
216 ulong y0 = y & 0xffffffffUL;
219 ulong t = x1*y0 + (z0 >> 32);
220 ulong z1 = t & 0xffffffffUL;
223 return x1*y1 + z2 + (z1 >> 32);
226 __device__
static inline unsigned int __umulhi(
unsigned int x,
unsigned int y) {
227 return __ockl_mul_hi_u32(x, y);
229 __device__
static inline unsigned int __urhadd(
unsigned int x,
unsigned int y) {
230 return (x + y + 1) >> 1;
232 __device__
static inline unsigned int __usad(
unsigned int x,
unsigned int y,
unsigned int z) {
233 return __ockl_sadd_u32(x, y, z);
236 __device__
static inline unsigned int __lane_id() {
return __mbcnt_hi(-1, __mbcnt_lo(-1, 0)); }
242 __device__
static inline unsigned __hip_ds_bpermute(
int index,
unsigned src) {
243 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
244 tmp.i = __llvm_amdgcn_ds_bpermute(index, tmp.i);
248 __device__
static inline float __hip_ds_bpermutef(
int index,
float src) {
249 union {
int i;
unsigned u;
float f; } tmp; tmp.f = src;
250 tmp.i = __llvm_amdgcn_ds_bpermute(index, tmp.i);
254 __device__
static inline unsigned __hip_ds_permute(
int index,
unsigned src) {
255 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
256 tmp.i = __llvm_amdgcn_ds_permute(index, tmp.i);
260 __device__
static inline float __hip_ds_permutef(
int index,
float src) {
261 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
262 tmp.i = __llvm_amdgcn_ds_permute(index, tmp.i);
266 #define __hip_ds_swizzle(src, pattern) __hip_ds_swizzle_N<(pattern)>((src)) 267 #define __hip_ds_swizzlef(src, pattern) __hip_ds_swizzlef_N<(pattern)>((src)) 269 template <
int pattern>
270 __device__
static inline unsigned __hip_ds_swizzle_N(
unsigned int src) {
271 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
273 tmp.i = __llvm_amdgcn_ds_swizzle(tmp.i, pattern);
275 tmp.i = __builtin_amdgcn_ds_swizzle(tmp.i, pattern);
280 template <
int pattern>
281 __device__
static inline float __hip_ds_swizzlef_N(
float src) {
282 union {
int i;
unsigned u;
float f; } tmp; tmp.f = src;
284 tmp.i = __llvm_amdgcn_ds_swizzle(tmp.i, pattern);
286 tmp.i = __builtin_amdgcn_ds_swizzle(tmp.i, pattern);
291 #define __hip_move_dpp(src, dpp_ctrl, row_mask, bank_mask, bound_ctrl) \ 292 __hip_move_dpp_N<(dpp_ctrl), (row_mask), (bank_mask), (bound_ctrl)>((src)) 294 template <
int dpp_ctrl,
int row_mask,
int bank_mask,
bool bound_ctrl>
295 __device__
static inline int __hip_move_dpp_N(
int src) {
296 return __llvm_amdgcn_move_dpp(src, dpp_ctrl, row_mask, bank_mask,
300 static constexpr
int warpSize = 64;
304 int __shfl(
int var,
int src_lane,
int width = warpSize) {
305 int self = __lane_id();
306 int index = src_lane + (
self & ~(width-1));
307 return __llvm_amdgcn_ds_bpermute(index<<2, var);
311 unsigned int __shfl(
unsigned int var,
int src_lane,
int width = warpSize) {
312 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
313 tmp.i = __shfl(tmp.i, src_lane, width);
318 float __shfl(
float var,
int src_lane,
int width = warpSize) {
319 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
320 tmp.i = __shfl(tmp.i, src_lane, width);
325 double __shfl(
double var,
int src_lane,
int width = warpSize) {
326 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
327 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
329 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
330 tmp[0] = __shfl(tmp[0], src_lane, width);
331 tmp[1] = __shfl(tmp[1], src_lane, width);
333 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
334 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
339 long __shfl(
long var,
int src_lane,
int width = warpSize)
342 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
343 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
345 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
346 tmp[0] = __shfl(tmp[0], src_lane, width);
347 tmp[1] = __shfl(tmp[1], src_lane, width);
349 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
350 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
353 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
354 return static_cast<long>(__shfl(static_cast<int>(var), src_lane, width));
359 long long __shfl(
long long var,
int src_lane,
int width = warpSize)
361 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
362 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
364 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
365 tmp[0] = __shfl(tmp[0], src_lane, width);
366 tmp[1] = __shfl(tmp[1], src_lane, width);
368 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
369 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
375 int __shfl_up(
int var,
unsigned int lane_delta,
int width = warpSize) {
376 int self = __lane_id();
377 int index =
self - lane_delta;
378 index = (index < (
self & ~(width-1)))?
self:index;
379 return __llvm_amdgcn_ds_bpermute(index<<2, var);
383 unsigned int __shfl_up(
unsigned int var,
unsigned int lane_delta,
int width = warpSize) {
384 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
385 tmp.i = __shfl_up(tmp.i, lane_delta, width);
390 float __shfl_up(
float var,
unsigned int lane_delta,
int width = warpSize) {
391 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
392 tmp.i = __shfl_up(tmp.i, lane_delta, width);
397 double __shfl_up(
double var,
unsigned int lane_delta,
int width = warpSize) {
398 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
399 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
401 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
402 tmp[0] = __shfl_up(tmp[0], lane_delta, width);
403 tmp[1] = __shfl_up(tmp[1], lane_delta, width);
405 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
406 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
411 long __shfl_up(
long var,
unsigned int lane_delta,
int width = warpSize)
414 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
415 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
417 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
418 tmp[0] = __shfl_up(tmp[0], lane_delta, width);
419 tmp[1] = __shfl_up(tmp[1], lane_delta, width);
421 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
422 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
425 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
426 return static_cast<long>(__shfl_up(static_cast<int>(var), lane_delta, width));
431 long long __shfl_up(
long long var,
unsigned int lane_delta,
int width = warpSize)
433 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
434 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
435 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
436 tmp[0] = __shfl_up(tmp[0], lane_delta, width);
437 tmp[1] = __shfl_up(tmp[1], lane_delta, width);
438 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
439 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
445 int __shfl_down(
int var,
unsigned int lane_delta,
int width = warpSize) {
446 int self = __lane_id();
447 int index =
self + lane_delta;
448 index = (int)((
self&(width-1))+lane_delta) >= width?
self:index;
449 return __llvm_amdgcn_ds_bpermute(index<<2, var);
453 unsigned int __shfl_down(
unsigned int var,
unsigned int lane_delta,
int width = warpSize) {
454 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
455 tmp.i = __shfl_down(tmp.i, lane_delta, width);
460 float __shfl_down(
float var,
unsigned int lane_delta,
int width = warpSize) {
461 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
462 tmp.i = __shfl_down(tmp.i, lane_delta, width);
467 double __shfl_down(
double var,
unsigned int lane_delta,
int width = warpSize) {
468 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
469 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
471 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
472 tmp[0] = __shfl_down(tmp[0], lane_delta, width);
473 tmp[1] = __shfl_down(tmp[1], lane_delta, width);
475 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
476 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
481 long __shfl_down(
long var,
unsigned int lane_delta,
int width = warpSize)
484 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
485 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
487 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
488 tmp[0] = __shfl_down(tmp[0], lane_delta, width);
489 tmp[1] = __shfl_down(tmp[1], lane_delta, width);
491 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
492 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
495 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
496 return static_cast<long>(__shfl_down(static_cast<int>(var), lane_delta, width));
501 long long __shfl_down(
long long var,
unsigned int lane_delta,
int width = warpSize)
503 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
504 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
505 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
506 tmp[0] = __shfl_down(tmp[0], lane_delta, width);
507 tmp[1] = __shfl_down(tmp[1], lane_delta, width);
508 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
509 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
515 int __shfl_xor(
int var,
int lane_mask,
int width = warpSize) {
516 int self = __lane_id();
517 int index =
self^lane_mask;
518 index = index >= ((
self+width)&~(width-1))?
self:index;
519 return __llvm_amdgcn_ds_bpermute(index<<2, var);
523 unsigned int __shfl_xor(
unsigned int var,
int lane_mask,
int width = warpSize) {
524 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
525 tmp.i = __shfl_xor(tmp.i, lane_mask, width);
530 float __shfl_xor(
float var,
int lane_mask,
int width = warpSize) {
531 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
532 tmp.i = __shfl_xor(tmp.i, lane_mask, width);
537 double __shfl_xor(
double var,
int lane_mask,
int width = warpSize) {
538 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
539 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
541 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
542 tmp[0] = __shfl_xor(tmp[0], lane_mask, width);
543 tmp[1] = __shfl_xor(tmp[1], lane_mask, width);
545 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
546 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
551 long __shfl_xor(
long var,
int lane_mask,
int width = warpSize)
554 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
555 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
557 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
558 tmp[0] = __shfl_xor(tmp[0], lane_mask, width);
559 tmp[1] = __shfl_xor(tmp[1], lane_mask, width);
561 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
562 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
565 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
566 return static_cast<long>(__shfl_xor(static_cast<int>(var), lane_mask, width));
571 long long __shfl_xor(
long long var,
int lane_mask,
int width = warpSize)
573 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
574 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
575 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
576 tmp[0] = __shfl_xor(tmp[0], lane_mask, width);
577 tmp[1] = __shfl_xor(tmp[1], lane_mask, width);
578 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
579 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
583 #define MASK1 0x00ff00ff 584 #define MASK2 0xff00ff00 588 unsigned one1 = in1.w & MASK1;
589 unsigned one2 = in2.w & MASK1;
590 out.w = (one1 + one2) & MASK1;
591 one1 = in1.w & MASK2;
592 one2 = in2.w & MASK2;
593 out.w = out.w | ((one1 + one2) & MASK2);
599 unsigned one1 = in1.w & MASK1;
600 unsigned one2 = in2.w & MASK1;
601 out.w = (one1 - one2) & MASK1;
602 one1 = in1.w & MASK2;
603 one2 = in2.w & MASK2;
604 out.w = out.w | ((one1 - one2) & MASK2);
610 unsigned one1 = in1.w & MASK1;
611 unsigned one2 = in2.w & MASK1;
612 out.w = (one1 * one2) & MASK1;
613 one1 = in1.w & MASK2;
614 one2 = in2.w & MASK2;
615 out.w = out.w | ((one1 * one2) & MASK2);
624 __device__
static inline float __double2float_rd(
double x) {
return (
double)x; }
625 __device__
static inline float __double2float_rn(
double x) {
return (
double)x; }
626 __device__
static inline float __double2float_ru(
double x) {
return (
double)x; }
627 __device__
static inline float __double2float_rz(
double x) {
return (
double)x; }
629 __device__
static inline int __double2hiint(
double x) {
630 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
633 __builtin_memcpy(tmp, &x,
sizeof(tmp));
637 __device__
static inline int __double2loint(
double x) {
638 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
641 __builtin_memcpy(tmp, &x,
sizeof(tmp));
646 __device__
static inline int __double2int_rd(
double x) {
return (
int)x; }
647 __device__
static inline int __double2int_rn(
double x) {
return (
int)x; }
648 __device__
static inline int __double2int_ru(
double x) {
return (
int)x; }
649 __device__
static inline int __double2int_rz(
double x) {
return (
int)x; }
651 __device__
static inline long long int __double2ll_rd(
double x) {
return (
long long int)x; }
652 __device__
static inline long long int __double2ll_rn(
double x) {
return (
long long int)x; }
653 __device__
static inline long long int __double2ll_ru(
double x) {
return (
long long int)x; }
654 __device__
static inline long long int __double2ll_rz(
double x) {
return (
long long int)x; }
656 __device__
static inline unsigned int __double2uint_rd(
double x) {
return (
unsigned int)x; }
657 __device__
static inline unsigned int __double2uint_rn(
double x) {
return (
unsigned int)x; }
658 __device__
static inline unsigned int __double2uint_ru(
double x) {
return (
unsigned int)x; }
659 __device__
static inline unsigned int __double2uint_rz(
double x) {
return (
unsigned int)x; }
661 __device__
static inline unsigned long long int __double2ull_rd(
double x) {
662 return (
unsigned long long int)x;
664 __device__
static inline unsigned long long int __double2ull_rn(
double x) {
665 return (
unsigned long long int)x;
667 __device__
static inline unsigned long long int __double2ull_ru(
double x) {
668 return (
unsigned long long int)x;
670 __device__
static inline unsigned long long int __double2ull_rz(
double x) {
671 return (
unsigned long long int)x;
674 __device__
static inline long long int __double_as_longlong(
double x) {
675 static_assert(
sizeof(
long long) ==
sizeof(
double),
"");
678 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
697 __device__
static inline int __float2int_rd(
float x) {
return (
int)__ocml_floor_f32(x); }
698 __device__
static inline int __float2int_rn(
float x) {
return (
int)__ocml_rint_f32(x); }
699 __device__
static inline int __float2int_ru(
float x) {
return (
int)__ocml_ceil_f32(x); }
700 __device__
static inline int __float2int_rz(
float x) {
return (
int)__ocml_trunc_f32(x); }
702 __device__
static inline long long int __float2ll_rd(
float x) {
return (
long long int)x; }
703 __device__
static inline long long int __float2ll_rn(
float x) {
return (
long long int)x; }
704 __device__
static inline long long int __float2ll_ru(
float x) {
return (
long long int)x; }
705 __device__
static inline long long int __float2ll_rz(
float x) {
return (
long long int)x; }
707 __device__
static inline unsigned int __float2uint_rd(
float x) {
return (
unsigned int)x; }
708 __device__
static inline unsigned int __float2uint_rn(
float x) {
return (
unsigned int)x; }
709 __device__
static inline unsigned int __float2uint_ru(
float x) {
return (
unsigned int)x; }
710 __device__
static inline unsigned int __float2uint_rz(
float x) {
return (
unsigned int)x; }
712 __device__
static inline unsigned long long int __float2ull_rd(
float x) {
713 return (
unsigned long long int)x;
715 __device__
static inline unsigned long long int __float2ull_rn(
float x) {
716 return (
unsigned long long int)x;
718 __device__
static inline unsigned long long int __float2ull_ru(
float x) {
719 return (
unsigned long long int)x;
721 __device__
static inline unsigned long long int __float2ull_rz(
float x) {
722 return (
unsigned long long int)x;
725 __device__
static inline int __float_as_int(
float x) {
726 static_assert(
sizeof(
int) ==
sizeof(
float),
"");
729 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
734 __device__
static inline unsigned int __float_as_uint(
float x) {
735 static_assert(
sizeof(
unsigned int) ==
sizeof(
float),
"");
738 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
743 __device__
static inline double __hiloint2double(
int hi,
int lo) {
744 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
746 uint64_t tmp0 = (
static_cast<uint64_t
>(hi) << 32ull) |
static_cast<uint32_t
>(lo);
748 __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
753 __device__
static inline double __int2double_rn(
int x) {
return (
double)x; }
755 __device__
static inline float __int2float_rd(
int x) {
return (
float)x; }
756 __device__
static inline float __int2float_rn(
int x) {
return (
float)x; }
757 __device__
static inline float __int2float_ru(
int x) {
return (
float)x; }
758 __device__
static inline float __int2float_rz(
int x) {
return (
float)x; }
760 __device__
static inline float __int_as_float(
int x) {
761 static_assert(
sizeof(
float) ==
sizeof(
int),
"");
764 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
769 __device__
static inline double __ll2double_rd(
long long int x) {
return (
double)x; }
770 __device__
static inline double __ll2double_rn(
long long int x) {
return (
double)x; }
771 __device__
static inline double __ll2double_ru(
long long int x) {
return (
double)x; }
772 __device__
static inline double __ll2double_rz(
long long int x) {
return (
double)x; }
774 __device__
static inline float __ll2float_rd(
long long int x) {
return (
float)x; }
775 __device__
static inline float __ll2float_rn(
long long int x) {
return (
float)x; }
776 __device__
static inline float __ll2float_ru(
long long int x) {
return (
float)x; }
777 __device__
static inline float __ll2float_rz(
long long int x) {
return (
float)x; }
779 __device__
static inline double __longlong_as_double(
long long int x) {
780 static_assert(
sizeof(
double) ==
sizeof(
long long),
"");
783 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
788 __device__
static inline double __uint2double_rn(
int x) {
return (
double)x; }
790 __device__
static inline float __uint2float_rd(
unsigned int x) {
return (
float)x; }
791 __device__
static inline float __uint2float_rn(
unsigned int x) {
return (
float)x; }
792 __device__
static inline float __uint2float_ru(
unsigned int x) {
return (
float)x; }
793 __device__
static inline float __uint2float_rz(
unsigned int x) {
return (
float)x; }
795 __device__
static inline float __uint_as_float(
unsigned int x) {
796 static_assert(
sizeof(
float) ==
sizeof(
unsigned int),
"");
799 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
804 __device__
static inline double __ull2double_rd(
unsigned long long int x) {
return (
double)x; }
805 __device__
static inline double __ull2double_rn(
unsigned long long int x) {
return (
double)x; }
806 __device__
static inline double __ull2double_ru(
unsigned long long int x) {
return (
double)x; }
807 __device__
static inline double __ull2double_rz(
unsigned long long int x) {
return (
double)x; }
809 __device__
static inline float __ull2float_rd(
unsigned long long int x) {
return (
float)x; }
810 __device__
static inline float __ull2float_rn(
unsigned long long int x) {
return (
float)x; }
811 __device__
static inline float __ull2float_ru(
unsigned long long int x) {
return (
float)x; }
812 __device__
static inline float __ull2float_rz(
unsigned long long int x) {
return (
float)x; }
815 #define __HCC_OR_HIP_CLANG__ 1 816 #elif defined(__clang__) && defined(__HIP__) 817 #define __HCC_OR_HIP_CLANG__ 1 819 #define __HCC_OR_HIP_CLANG__ 0 822 #ifdef __HCC_OR_HIP_CLANG__ 825 __device__
long long int __clock64();
826 __device__
long long int __clock();
827 __device__
long long int clock64();
828 __device__
long long int clock();
830 __device__
void __named_sync(
int a,
int b);
832 #ifdef __HIP_DEVICE_COMPILE__ 836 extern "C" uint64_t __clock_u64() __HC__;
840 inline __attribute((always_inline))
841 long long int __clock64() {
842 return (
long long int) __builtin_readcyclecounter();
846 inline __attribute((always_inline))
847 long long int __clock() {
return __clock64(); }
850 inline __attribute__((always_inline))
851 long long int clock64() {
return __clock64(); }
854 inline __attribute__((always_inline))
855 long long int clock() {
return __clock(); }
860 void __named_sync(
int a,
int b) { __builtin_amdgcn_s_barrier(); }
862 #endif // __HIP_DEVICE_COMPILE__ 867 int __all(
int predicate) {
868 return __ockl_wfall_i32(predicate);
873 int __any(
int predicate) {
874 return __ockl_wfany_i32(predicate);
882 unsigned long long int __ballot(
int predicate) {
883 return __builtin_amdgcn_uicmp(predicate, 0, ICMP_NE);
888 unsigned long long int __ballot64(
int predicate) {
889 return __builtin_amdgcn_uicmp(predicate, 0, ICMP_NE);
895 uint64_t __lanemask_gt()
897 uint32_t lane = __ockl_lane_u32();
900 uint64_t ballot = __ballot64(1);
901 uint64_t mask = (~((uint64_t)0)) << (lane + 1);
902 return mask & ballot;
907 uint64_t __lanemask_lt()
909 uint32_t lane = __ockl_lane_u32();
910 int64_t ballot = __ballot64(1);
911 uint64_t mask = ((uint64_t)1 << lane) - (uint64_t)1;
912 return mask & ballot;
917 uint64_t __lanemask_eq()
919 uint32_t lane = __ockl_lane_u32();
920 int64_t mask = ((uint64_t)1 << lane);
925 __device__
inline void* __local_to_generic(
void* p) {
return p; }
927 #ifdef __HIP_DEVICE_COMPILE__ 930 void* __get_dynamicgroupbaseptr()
933 return (
char*)__local_to_generic((
void*)__to_local(__llvm_amdgcn_groupstaticsize()));
937 void* __get_dynamicgroupbaseptr();
938 #endif // __HIP_DEVICE_COMPILE__ 942 void *__amdgcn_get_dynamicgroupbaseptr() {
943 return __get_dynamicgroupbaseptr();
946 #if defined(__HCC__) && (__hcc_major__ < 3) && (__hcc_minor__ < 3) 948 #define __CLK_LOCAL_MEM_FENCE 0x01 949 typedef unsigned __cl_mem_fence_flags;
951 typedef enum __memory_scope {
952 __memory_scope_work_item = __OPENCL_MEMORY_SCOPE_WORK_ITEM,
953 __memory_scope_work_group = __OPENCL_MEMORY_SCOPE_WORK_GROUP,
954 __memory_scope_device = __OPENCL_MEMORY_SCOPE_DEVICE,
955 __memory_scope_all_svm_devices = __OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES,
956 __memory_scope_sub_group = __OPENCL_MEMORY_SCOPE_SUB_GROUP
960 typedef enum __memory_order
962 __memory_order_relaxed = __ATOMIC_RELAXED,
963 __memory_order_acquire = __ATOMIC_ACQUIRE,
964 __memory_order_release = __ATOMIC_RELEASE,
965 __memory_order_acq_rel = __ATOMIC_ACQ_REL,
966 __memory_order_seq_cst = __ATOMIC_SEQ_CST
972 __atomic_work_item_fence(__cl_mem_fence_flags flags, __memory_order order, __memory_scope scope)
975 if (order != __memory_order_relaxed) {
977 case __memory_scope_work_item:
979 case __memory_scope_sub_group:
981 case __memory_order_relaxed:
break;
982 case __memory_order_acquire: __llvm_fence_acq_sg();
break;
983 case __memory_order_release: __llvm_fence_rel_sg();
break;
984 case __memory_order_acq_rel: __llvm_fence_ar_sg();
break;
985 case __memory_order_seq_cst: __llvm_fence_sc_sg();
break;
988 case __memory_scope_work_group:
990 case __memory_order_relaxed:
break;
991 case __memory_order_acquire: __llvm_fence_acq_wg();
break;
992 case __memory_order_release: __llvm_fence_rel_wg();
break;
993 case __memory_order_acq_rel: __llvm_fence_ar_wg();
break;
994 case __memory_order_seq_cst: __llvm_fence_sc_wg();
break;
997 case __memory_scope_device:
999 case __memory_order_relaxed:
break;
1000 case __memory_order_acquire: __llvm_fence_acq_dev();
break;
1001 case __memory_order_release: __llvm_fence_rel_dev();
break;
1002 case __memory_order_acq_rel: __llvm_fence_ar_dev();
break;
1003 case __memory_order_seq_cst: __llvm_fence_sc_dev();
break;
1006 case __memory_scope_all_svm_devices:
1008 case __memory_order_relaxed:
break;
1009 case __memory_order_acquire: __llvm_fence_acq_sys();
break;
1010 case __memory_order_release: __llvm_fence_rel_sys();
break;
1011 case __memory_order_acq_rel: __llvm_fence_ar_sys();
break;
1012 case __memory_order_seq_cst: __llvm_fence_sc_sys();
break;
1023 static void __threadfence()
1025 __atomic_work_item_fence(0, __memory_order_seq_cst, __memory_scope_device);
1030 static void __threadfence_block()
1032 __atomic_work_item_fence(0, __memory_order_seq_cst, __memory_scope_work_group);
1037 static void __threadfence_system()
1039 __atomic_work_item_fence(0, __memory_order_seq_cst, __memory_scope_all_svm_devices);
1045 __attribute__((weak))
1047 return __builtin_trap();
1051 #endif // __HCC_OR_HIP_CLANG__ 1061 #define HIP_DYNAMIC_SHARED(type, var) type* var = (type*)__get_dynamicgroupbaseptr(); 1063 #define HIP_DYNAMIC_SHARED_ATTRIBUTE 1066 #elif defined(__clang__) && defined(__HIP__) 1068 #pragma push_macro("__DEVICE__") 1069 #define __DEVICE__ extern "C" __device__ __attribute__((always_inline)) \ 1070 __attribute__((weak)) 1074 void __assert_fail(
const char * __assertion,
1076 unsigned int __line,
1077 const char *__function)
1079 printf(
"%s:%u: %s: Device-side assertion `%s' failed.\n", __file, __line,
1080 __function, __assertion);
1087 void __assertfail(
const char * __assertion,
1089 unsigned int __line,
1090 const char *__function,
1099 static void __work_group_barrier(__cl_mem_fence_flags flags, __memory_scope scope)
1102 __atomic_work_item_fence(flags, __memory_order_release, scope);
1103 __builtin_amdgcn_s_barrier();
1104 __atomic_work_item_fence(flags, __memory_order_acquire, scope);
1106 __builtin_amdgcn_s_barrier();
1112 static void __barrier(
int n)
1114 __work_group_barrier((__cl_mem_fence_flags)n, __memory_scope_work_group);
1119 __attribute__((convergent))
1120 void __syncthreads()
1122 __barrier(__CLK_LOCAL_MEM_FENCE);
1143 #define HW_ID_CU_ID_SIZE 4 1144 #define HW_ID_CU_ID_OFFSET 8 1146 #define HW_ID_SE_ID_SIZE 2 1147 #define HW_ID_SE_ID_OFFSET 13 1156 #define GETREG_IMMED(SZ,OFF,REG) (((SZ) << 11) | ((OFF) << 6) | (REG)) 1166 unsigned __smid(
void)
1168 unsigned cu_id = __builtin_amdgcn_s_getreg(
1169 GETREG_IMMED(HW_ID_CU_ID_SIZE-1, HW_ID_CU_ID_OFFSET, HW_ID));
1170 unsigned se_id = __builtin_amdgcn_s_getreg(
1171 GETREG_IMMED(HW_ID_SE_ID_SIZE-1, HW_ID_SE_ID_OFFSET, HW_ID));
1174 return (se_id << HW_ID_CU_ID_SIZE) + cu_id;
1177 #pragma push_macro("__DEVICE__") 1181 #define HIP_DYNAMIC_SHARED(type, var) \ 1182 type* var = (type*)__amdgcn_get_dynamicgroupbaseptr(); 1184 #define HIP_DYNAMIC_SHARED_ATTRIBUTE 1187 #endif //defined(__clang__) && defined(__HIP__) 1191 static inline __device__
void* __hip_hc_memcpy(
void* dst,
const void* src,
size_t size) {
1192 auto dstPtr =
static_cast<unsigned char*
>(dst);
1193 auto srcPtr =
static_cast<const unsigned char*
>(src);
1195 while (size >= 4u) {
1196 dstPtr[0] = srcPtr[0];
1197 dstPtr[1] = srcPtr[1];
1198 dstPtr[2] = srcPtr[2];
1199 dstPtr[3] = srcPtr[3];
1207 dstPtr[2] = srcPtr[2];
1209 dstPtr[1] = srcPtr[1];
1211 dstPtr[0] = srcPtr[0];
1217 static inline __device__
void* __hip_hc_memset(
void* dst,
unsigned char val,
size_t size) {
1218 auto dstPtr =
static_cast<unsigned char*
>(dst);
1220 while (size >= 4u) {
1240 static inline __device__
void* memcpy(
void* dst,
const void* src,
size_t size) {
1241 return __hip_hc_memcpy(dst, src, size);
1244 static inline __device__
void* memset(
void* ptr,
int val,
size_t size) {
1245 unsigned char val8 =
static_cast<unsigned char>(val);
1246 return __hip_hc_memset(ptr, val8, size);
Contains declarations for types and functions in device library.
Definition: device_functions.h:139
Definition: hip_vector_types.h:1356
Definition: device_functions.h:146
Contains declarations for wrapper functions for llvm intrinsics like llvm.amdgcn.s.barrier.