23 #ifndef HIP_INCLUDE_HIP_HCC_DETAIL_DEVICE_FUNCTIONS_H
24 #define HIP_INCLUDE_HIP_HCC_DETAIL_DEVICE_FUNCTIONS_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 __builtin_bitreverse32(input);
91 __device__
static inline unsigned long long int __brevll(
unsigned long long int input) {
92 return __builtin_bitreverse64(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() {
237 return __builtin_amdgcn_mbcnt_hi(
238 -1, __builtin_amdgcn_mbcnt_lo(-1, 0));
242 static inline unsigned int __mbcnt_lo(
unsigned int x,
unsigned int y) {
return __builtin_amdgcn_mbcnt_lo(x,y);};
245 static inline unsigned int __mbcnt_hi(
unsigned int x,
unsigned int y) {
return __builtin_amdgcn_mbcnt_hi(x,y);};
251 __device__
static inline unsigned __hip_ds_bpermute(
int index,
unsigned src) {
252 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
253 tmp.i = __builtin_amdgcn_ds_bpermute(index, tmp.i);
257 __device__
static inline float __hip_ds_bpermutef(
int index,
float src) {
258 union {
int i;
unsigned u;
float f; } tmp; tmp.f = src;
259 tmp.i = __builtin_amdgcn_ds_bpermute(index, tmp.i);
263 __device__
static inline unsigned __hip_ds_permute(
int index,
unsigned src) {
264 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
265 tmp.i = __builtin_amdgcn_ds_permute(index, tmp.i);
269 __device__
static inline float __hip_ds_permutef(
int index,
float src) {
270 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
271 tmp.i = __builtin_amdgcn_ds_permute(index, tmp.i);
275 #define __hip_ds_swizzle(src, pattern) __hip_ds_swizzle_N<(pattern)>((src))
276 #define __hip_ds_swizzlef(src, pattern) __hip_ds_swizzlef_N<(pattern)>((src))
278 template <
int pattern>
279 __device__
static inline unsigned __hip_ds_swizzle_N(
unsigned int src) {
280 union {
int i;
unsigned u;
float f; } tmp; tmp.u = src;
282 tmp.i = __llvm_amdgcn_ds_swizzle(tmp.i, pattern);
284 tmp.i = __builtin_amdgcn_ds_swizzle(tmp.i, pattern);
289 template <
int pattern>
290 __device__
static inline float __hip_ds_swizzlef_N(
float src) {
291 union {
int i;
unsigned u;
float f; } tmp; tmp.f = src;
293 tmp.i = __llvm_amdgcn_ds_swizzle(tmp.i, pattern);
295 tmp.i = __builtin_amdgcn_ds_swizzle(tmp.i, pattern);
300 #define __hip_move_dpp(src, dpp_ctrl, row_mask, bank_mask, bound_ctrl) \
301 __hip_move_dpp_N<(dpp_ctrl), (row_mask), (bank_mask), (bound_ctrl)>((src))
303 template <
int dpp_ctrl,
int row_mask,
int bank_mask,
bool bound_ctrl>
304 __device__
static inline int __hip_move_dpp_N(
int src) {
305 return __builtin_amdgcn_mov_dpp(src, dpp_ctrl, row_mask, bank_mask,
309 static constexpr
int warpSize = 64;
313 int __shfl(
int var,
int src_lane,
int width = warpSize) {
314 int self = __lane_id();
315 int index = src_lane + (
self & ~(width-1));
316 return __builtin_amdgcn_ds_bpermute(index<<2, var);
320 unsigned int __shfl(
unsigned int var,
int src_lane,
int width = warpSize) {
321 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
322 tmp.i = __shfl(tmp.i, src_lane, width);
327 float __shfl(
float var,
int src_lane,
int width = warpSize) {
328 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
329 tmp.i = __shfl(tmp.i, src_lane, width);
334 double __shfl(
double var,
int src_lane,
int width = warpSize) {
335 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
336 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
338 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
339 tmp[0] = __shfl(tmp[0], src_lane, width);
340 tmp[1] = __shfl(tmp[1], src_lane, width);
342 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
343 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
348 long __shfl(
long var,
int src_lane,
int width = warpSize)
351 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
352 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
354 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
355 tmp[0] = __shfl(tmp[0], src_lane, width);
356 tmp[1] = __shfl(tmp[1], src_lane, width);
358 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
359 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
362 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
363 return static_cast<long>(__shfl(
static_cast<int>(var), src_lane, width));
368 long long __shfl(
long long var,
int src_lane,
int width = warpSize)
370 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
371 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
373 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
374 tmp[0] = __shfl(tmp[0], src_lane, width);
375 tmp[1] = __shfl(tmp[1], src_lane, width);
377 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
378 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
384 int __shfl_up(
int var,
unsigned int lane_delta,
int width = warpSize) {
385 int self = __lane_id();
386 int index =
self - lane_delta;
387 index = (index < (
self & ~(width-1)))?
self:index;
388 return __builtin_amdgcn_ds_bpermute(index<<2, var);
392 unsigned int __shfl_up(
unsigned int var,
unsigned int lane_delta,
int width = warpSize) {
393 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
394 tmp.i = __shfl_up(tmp.i, lane_delta, width);
399 float __shfl_up(
float var,
unsigned int lane_delta,
int width = warpSize) {
400 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
401 tmp.i = __shfl_up(tmp.i, lane_delta, width);
406 double __shfl_up(
double var,
unsigned int lane_delta,
int width = warpSize) {
407 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
408 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
410 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
411 tmp[0] = __shfl_up(tmp[0], lane_delta, width);
412 tmp[1] = __shfl_up(tmp[1], lane_delta, width);
414 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
415 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
420 long __shfl_up(
long var,
unsigned int lane_delta,
int width = warpSize)
423 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
424 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
426 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
427 tmp[0] = __shfl_up(tmp[0], lane_delta, width);
428 tmp[1] = __shfl_up(tmp[1], lane_delta, width);
430 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
431 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
434 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
435 return static_cast<long>(__shfl_up(
static_cast<int>(var), lane_delta, width));
440 long long __shfl_up(
long long var,
unsigned int lane_delta,
int width = warpSize)
442 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
443 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
444 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
445 tmp[0] = __shfl_up(tmp[0], lane_delta, width);
446 tmp[1] = __shfl_up(tmp[1], lane_delta, width);
447 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
448 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
454 int __shfl_down(
int var,
unsigned int lane_delta,
int width = warpSize) {
455 int self = __lane_id();
456 int index =
self + lane_delta;
457 index = (int)((
self&(width-1))+lane_delta) >= width?
self:index;
458 return __builtin_amdgcn_ds_bpermute(index<<2, var);
462 unsigned int __shfl_down(
unsigned int var,
unsigned int lane_delta,
int width = warpSize) {
463 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
464 tmp.i = __shfl_down(tmp.i, lane_delta, width);
469 float __shfl_down(
float var,
unsigned int lane_delta,
int width = warpSize) {
470 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
471 tmp.i = __shfl_down(tmp.i, lane_delta, width);
476 double __shfl_down(
double var,
unsigned int lane_delta,
int width = warpSize) {
477 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
478 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
480 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
481 tmp[0] = __shfl_down(tmp[0], lane_delta, width);
482 tmp[1] = __shfl_down(tmp[1], lane_delta, width);
484 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
485 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
490 long __shfl_down(
long var,
unsigned int lane_delta,
int width = warpSize)
493 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
494 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
496 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
497 tmp[0] = __shfl_down(tmp[0], lane_delta, width);
498 tmp[1] = __shfl_down(tmp[1], lane_delta, width);
500 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
501 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
504 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
505 return static_cast<long>(__shfl_down(
static_cast<int>(var), lane_delta, width));
510 long long __shfl_down(
long long var,
unsigned int lane_delta,
int width = warpSize)
512 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
513 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
514 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
515 tmp[0] = __shfl_down(tmp[0], lane_delta, width);
516 tmp[1] = __shfl_down(tmp[1], lane_delta, width);
517 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
518 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
524 int __shfl_xor(
int var,
int lane_mask,
int width = warpSize) {
525 int self = __lane_id();
526 int index =
self^lane_mask;
527 index = index >= ((
self+width)&~(width-1))?
self:index;
528 return __builtin_amdgcn_ds_bpermute(index<<2, var);
532 unsigned int __shfl_xor(
unsigned int var,
int lane_mask,
int width = warpSize) {
533 union {
int i;
unsigned u;
float f; } tmp; tmp.u = var;
534 tmp.i = __shfl_xor(tmp.i, lane_mask, width);
539 float __shfl_xor(
float var,
int lane_mask,
int width = warpSize) {
540 union {
int i;
unsigned u;
float f; } tmp; tmp.f = var;
541 tmp.i = __shfl_xor(tmp.i, lane_mask, width);
546 double __shfl_xor(
double var,
int lane_mask,
int width = warpSize) {
547 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
548 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
550 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
551 tmp[0] = __shfl_xor(tmp[0], lane_mask, width);
552 tmp[1] = __shfl_xor(tmp[1], lane_mask, width);
554 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
555 double tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
560 long __shfl_xor(
long var,
int lane_mask,
int width = warpSize)
563 static_assert(
sizeof(
long) == 2 *
sizeof(
int),
"");
564 static_assert(
sizeof(
long) ==
sizeof(uint64_t),
"");
566 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
567 tmp[0] = __shfl_xor(tmp[0], lane_mask, width);
568 tmp[1] = __shfl_xor(tmp[1], lane_mask, width);
570 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
571 long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
574 static_assert(
sizeof(
long) ==
sizeof(
int),
"");
575 return static_cast<long>(__shfl_xor(
static_cast<int>(var), lane_mask, width));
580 long long __shfl_xor(
long long var,
int lane_mask,
int width = warpSize)
582 static_assert(
sizeof(
long long) == 2 *
sizeof(
int),
"");
583 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"");
584 int tmp[2]; __builtin_memcpy(tmp, &var,
sizeof(tmp));
585 tmp[0] = __shfl_xor(tmp[0], lane_mask, width);
586 tmp[1] = __shfl_xor(tmp[1], lane_mask, width);
587 uint64_t tmp0 = (
static_cast<uint64_t
>(tmp[1]) << 32ull) |
static_cast<uint32_t
>(tmp[0]);
588 long long tmp1; __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
592 #define MASK1 0x00ff00ff
593 #define MASK2 0xff00ff00
597 unsigned one1 = in1.w & MASK1;
598 unsigned one2 = in2.w & MASK1;
599 out.w = (one1 + one2) & MASK1;
600 one1 = in1.w & MASK2;
601 one2 = in2.w & MASK2;
602 out.w = out.w | ((one1 + one2) & MASK2);
608 unsigned one1 = in1.w & MASK1;
609 unsigned one2 = in2.w & MASK1;
610 out.w = (one1 - one2) & MASK1;
611 one1 = in1.w & MASK2;
612 one2 = in2.w & MASK2;
613 out.w = out.w | ((one1 - one2) & MASK2);
619 unsigned one1 = in1.w & MASK1;
620 unsigned one2 = in2.w & MASK1;
621 out.w = (one1 * one2) & MASK1;
622 one1 = in1.w & MASK2;
623 one2 = in2.w & MASK2;
624 out.w = out.w | ((one1 * one2) & MASK2);
633 __device__
static inline float __double2float_rd(
double x) {
return (
double)x; }
634 __device__
static inline float __double2float_rn(
double x) {
return (
double)x; }
635 __device__
static inline float __double2float_ru(
double x) {
return (
double)x; }
636 __device__
static inline float __double2float_rz(
double x) {
return (
double)x; }
638 __device__
static inline int __double2hiint(
double x) {
639 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
642 __builtin_memcpy(tmp, &x,
sizeof(tmp));
646 __device__
static inline int __double2loint(
double x) {
647 static_assert(
sizeof(
double) == 2 *
sizeof(
int),
"");
650 __builtin_memcpy(tmp, &x,
sizeof(tmp));
655 __device__
static inline int __double2int_rd(
double x) {
return (
int)x; }
656 __device__
static inline int __double2int_rn(
double x) {
return (
int)x; }
657 __device__
static inline int __double2int_ru(
double x) {
return (
int)x; }
658 __device__
static inline int __double2int_rz(
double x) {
return (
int)x; }
660 __device__
static inline long long int __double2ll_rd(
double x) {
return (
long long int)x; }
661 __device__
static inline long long int __double2ll_rn(
double x) {
return (
long long int)x; }
662 __device__
static inline long long int __double2ll_ru(
double x) {
return (
long long int)x; }
663 __device__
static inline long long int __double2ll_rz(
double x) {
return (
long long int)x; }
665 __device__
static inline unsigned int __double2uint_rd(
double x) {
return (
unsigned int)x; }
666 __device__
static inline unsigned int __double2uint_rn(
double x) {
return (
unsigned int)x; }
667 __device__
static inline unsigned int __double2uint_ru(
double x) {
return (
unsigned int)x; }
668 __device__
static inline unsigned int __double2uint_rz(
double x) {
return (
unsigned int)x; }
670 __device__
static inline unsigned long long int __double2ull_rd(
double x) {
671 return (
unsigned long long int)x;
673 __device__
static inline unsigned long long int __double2ull_rn(
double x) {
674 return (
unsigned long long int)x;
676 __device__
static inline unsigned long long int __double2ull_ru(
double x) {
677 return (
unsigned long long int)x;
679 __device__
static inline unsigned long long int __double2ull_rz(
double x) {
680 return (
unsigned long long int)x;
683 __device__
static inline long long int __double_as_longlong(
double x) {
684 static_assert(
sizeof(
long long) ==
sizeof(
double),
"");
687 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
706 __device__
static inline int __float2int_rd(
float x) {
return (
int)__ocml_floor_f32(x); }
707 __device__
static inline int __float2int_rn(
float x) {
return (
int)__ocml_rint_f32(x); }
708 __device__
static inline int __float2int_ru(
float x) {
return (
int)__ocml_ceil_f32(x); }
709 __device__
static inline int __float2int_rz(
float x) {
return (
int)__ocml_trunc_f32(x); }
711 __device__
static inline long long int __float2ll_rd(
float x) {
return (
long long int)x; }
712 __device__
static inline long long int __float2ll_rn(
float x) {
return (
long long int)x; }
713 __device__
static inline long long int __float2ll_ru(
float x) {
return (
long long int)x; }
714 __device__
static inline long long int __float2ll_rz(
float x) {
return (
long long int)x; }
716 __device__
static inline unsigned int __float2uint_rd(
float x) {
return (
unsigned int)x; }
717 __device__
static inline unsigned int __float2uint_rn(
float x) {
return (
unsigned int)x; }
718 __device__
static inline unsigned int __float2uint_ru(
float x) {
return (
unsigned int)x; }
719 __device__
static inline unsigned int __float2uint_rz(
float x) {
return (
unsigned int)x; }
721 __device__
static inline unsigned long long int __float2ull_rd(
float x) {
722 return (
unsigned long long int)x;
724 __device__
static inline unsigned long long int __float2ull_rn(
float x) {
725 return (
unsigned long long int)x;
727 __device__
static inline unsigned long long int __float2ull_ru(
float x) {
728 return (
unsigned long long int)x;
730 __device__
static inline unsigned long long int __float2ull_rz(
float x) {
731 return (
unsigned long long int)x;
734 __device__
static inline int __float_as_int(
float x) {
735 static_assert(
sizeof(
int) ==
sizeof(
float),
"");
738 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
743 __device__
static inline unsigned int __float_as_uint(
float x) {
744 static_assert(
sizeof(
unsigned int) ==
sizeof(
float),
"");
747 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
752 __device__
static inline double __hiloint2double(
int hi,
int lo) {
753 static_assert(
sizeof(
double) ==
sizeof(uint64_t),
"");
755 uint64_t tmp0 = (
static_cast<uint64_t
>(hi) << 32ull) |
static_cast<uint32_t
>(lo);
757 __builtin_memcpy(&tmp1, &tmp0,
sizeof(tmp0));
762 __device__
static inline double __int2double_rn(
int x) {
return (
double)x; }
764 __device__
static inline float __int2float_rd(
int x) {
return (
float)x; }
765 __device__
static inline float __int2float_rn(
int x) {
return (
float)x; }
766 __device__
static inline float __int2float_ru(
int x) {
return (
float)x; }
767 __device__
static inline float __int2float_rz(
int x) {
return (
float)x; }
769 __device__
static inline float __int_as_float(
int x) {
770 static_assert(
sizeof(
float) ==
sizeof(
int),
"");
773 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
778 __device__
static inline double __ll2double_rd(
long long int x) {
return (
double)x; }
779 __device__
static inline double __ll2double_rn(
long long int x) {
return (
double)x; }
780 __device__
static inline double __ll2double_ru(
long long int x) {
return (
double)x; }
781 __device__
static inline double __ll2double_rz(
long long int x) {
return (
double)x; }
783 __device__
static inline float __ll2float_rd(
long long int x) {
return (
float)x; }
784 __device__
static inline float __ll2float_rn(
long long int x) {
return (
float)x; }
785 __device__
static inline float __ll2float_ru(
long long int x) {
return (
float)x; }
786 __device__
static inline float __ll2float_rz(
long long int x) {
return (
float)x; }
788 __device__
static inline double __longlong_as_double(
long long int x) {
789 static_assert(
sizeof(
double) ==
sizeof(
long long),
"");
792 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
797 __device__
static inline double __uint2double_rn(
int x) {
return (
double)x; }
799 __device__
static inline float __uint2float_rd(
unsigned int x) {
return (
float)x; }
800 __device__
static inline float __uint2float_rn(
unsigned int x) {
return (
float)x; }
801 __device__
static inline float __uint2float_ru(
unsigned int x) {
return (
float)x; }
802 __device__
static inline float __uint2float_rz(
unsigned int x) {
return (
float)x; }
804 __device__
static inline float __uint_as_float(
unsigned int x) {
805 static_assert(
sizeof(
float) ==
sizeof(
unsigned int),
"");
808 __builtin_memcpy(&tmp, &x,
sizeof(tmp));
813 __device__
static inline double __ull2double_rd(
unsigned long long int x) {
return (
double)x; }
814 __device__
static inline double __ull2double_rn(
unsigned long long int x) {
return (
double)x; }
815 __device__
static inline double __ull2double_ru(
unsigned long long int x) {
return (
double)x; }
816 __device__
static inline double __ull2double_rz(
unsigned long long int x) {
return (
double)x; }
818 __device__
static inline float __ull2float_rd(
unsigned long long int x) {
return (
float)x; }
819 __device__
static inline float __ull2float_rn(
unsigned long long int x) {
return (
float)x; }
820 __device__
static inline float __ull2float_ru(
unsigned long long int x) {
return (
float)x; }
821 __device__
static inline float __ull2float_rz(
unsigned long long int x) {
return (
float)x; }
824 #define __HCC_OR_HIP_CLANG__ 1
825 #elif defined(__clang__) && defined(__HIP__)
826 #define __HCC_OR_HIP_CLANG__ 1
828 #define __HCC_OR_HIP_CLANG__ 0
831 #ifdef __HCC_OR_HIP_CLANG__
834 __device__
long long int __clock64();
835 __device__
long long int __clock();
836 __device__
long long int clock64();
837 __device__
long long int clock();
839 __device__
void __named_sync(
int a,
int b);
841 #ifdef __HIP_DEVICE_COMPILE__
845 extern "C" uint64_t __clock_u64() __HC__;
849 inline __attribute((always_inline))
850 long long int __clock64() {
851 return (
long long int) __builtin_readcyclecounter();
855 inline __attribute((always_inline))
856 long long int __clock() {
return __clock64(); }
859 inline __attribute__((always_inline))
860 long long int clock64() {
return __clock64(); }
863 inline __attribute__((always_inline))
864 long long int clock() {
return __clock(); }
869 void __named_sync(
int a,
int b) { __builtin_amdgcn_s_barrier(); }
871 #endif // __HIP_DEVICE_COMPILE__
876 int __all(
int predicate) {
877 return __ockl_wfall_i32(predicate);
882 int __any(
int predicate) {
883 return __ockl_wfany_i32(predicate);
891 unsigned long long int __ballot(
int predicate) {
892 return __builtin_amdgcn_uicmp(predicate, 0, ICMP_NE);
897 unsigned long long int __ballot64(
int predicate) {
898 return __builtin_amdgcn_uicmp(predicate, 0, ICMP_NE);
904 uint64_t __lanemask_gt()
906 uint32_t lane = __ockl_lane_u32();
909 uint64_t ballot = __ballot64(1);
910 uint64_t mask = (~((uint64_t)0)) << (lane + 1);
911 return mask & ballot;
916 uint64_t __lanemask_lt()
918 uint32_t lane = __ockl_lane_u32();
919 int64_t ballot = __ballot64(1);
920 uint64_t mask = ((uint64_t)1 << lane) - (uint64_t)1;
921 return mask & ballot;
926 uint64_t __lanemask_eq()
928 uint32_t lane = __ockl_lane_u32();
929 int64_t mask = ((uint64_t)1 << lane);
934 __device__
inline void* __local_to_generic(
void* p) {
return p; }
936 #ifdef __HIP_DEVICE_COMPILE__
939 void* __get_dynamicgroupbaseptr()
942 return (
char*)__local_to_generic((
void*)__to_local(__llvm_amdgcn_groupstaticsize()));
946 void* __get_dynamicgroupbaseptr();
947 #endif // __HIP_DEVICE_COMPILE__
951 void *__amdgcn_get_dynamicgroupbaseptr() {
952 return __get_dynamicgroupbaseptr();
955 #if defined(__HCC__) && (__hcc_major__ < 3) && (__hcc_minor__ < 3)
957 #define __CLK_LOCAL_MEM_FENCE 0x01
958 typedef unsigned __cl_mem_fence_flags;
960 typedef enum __memory_scope {
961 __memory_scope_work_item = __OPENCL_MEMORY_SCOPE_WORK_ITEM,
962 __memory_scope_work_group = __OPENCL_MEMORY_SCOPE_WORK_GROUP,
963 __memory_scope_device = __OPENCL_MEMORY_SCOPE_DEVICE,
964 __memory_scope_all_svm_devices = __OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES,
965 __memory_scope_sub_group = __OPENCL_MEMORY_SCOPE_SUB_GROUP
969 typedef enum __memory_order
971 __memory_order_relaxed = __ATOMIC_RELAXED,
972 __memory_order_acquire = __ATOMIC_ACQUIRE,
973 __memory_order_release = __ATOMIC_RELEASE,
974 __memory_order_acq_rel = __ATOMIC_ACQ_REL,
975 __memory_order_seq_cst = __ATOMIC_SEQ_CST
981 __atomic_work_item_fence(__cl_mem_fence_flags flags, __memory_order order, __memory_scope scope)
984 if (order != __memory_order_relaxed) {
986 case __memory_scope_work_item:
988 case __memory_scope_sub_group:
990 case __memory_order_relaxed:
break;
991 case __memory_order_acquire: __llvm_fence_acq_sg();
break;
992 case __memory_order_release: __llvm_fence_rel_sg();
break;
993 case __memory_order_acq_rel: __llvm_fence_ar_sg();
break;
994 case __memory_order_seq_cst: __llvm_fence_sc_sg();
break;
997 case __memory_scope_work_group:
999 case __memory_order_relaxed:
break;
1000 case __memory_order_acquire: __llvm_fence_acq_wg();
break;
1001 case __memory_order_release: __llvm_fence_rel_wg();
break;
1002 case __memory_order_acq_rel: __llvm_fence_ar_wg();
break;
1003 case __memory_order_seq_cst: __llvm_fence_sc_wg();
break;
1006 case __memory_scope_device:
1008 case __memory_order_relaxed:
break;
1009 case __memory_order_acquire: __llvm_fence_acq_dev();
break;
1010 case __memory_order_release: __llvm_fence_rel_dev();
break;
1011 case __memory_order_acq_rel: __llvm_fence_ar_dev();
break;
1012 case __memory_order_seq_cst: __llvm_fence_sc_dev();
break;
1015 case __memory_scope_all_svm_devices:
1017 case __memory_order_relaxed:
break;
1018 case __memory_order_acquire: __llvm_fence_acq_sys();
break;
1019 case __memory_order_release: __llvm_fence_rel_sys();
break;
1020 case __memory_order_acq_rel: __llvm_fence_ar_sys();
break;
1021 case __memory_order_seq_cst: __llvm_fence_sc_sys();
break;
1032 static void __threadfence()
1034 __atomic_work_item_fence(0, __memory_order_seq_cst, __memory_scope_device);
1039 static void __threadfence_block()
1041 __atomic_work_item_fence(0, __memory_order_seq_cst, __memory_scope_work_group);
1046 static void __threadfence_system()
1048 __atomic_work_item_fence(0, __memory_order_seq_cst, __memory_scope_all_svm_devices);
1054 __attribute__((weak))
1056 return __builtin_trap();
1060 #endif // __HCC_OR_HIP_CLANG__
1070 #define HIP_DYNAMIC_SHARED(type, var) type* var = (type*)__get_dynamicgroupbaseptr();
1072 #define HIP_DYNAMIC_SHARED_ATTRIBUTE
1075 #elif defined(__clang__) && defined(__HIP__)
1077 #pragma push_macro("__DEVICE__")
1078 #define __DEVICE__ extern "C" __device__ __attribute__((always_inline)) \
1079 __attribute__((weak))
1083 void __assert_fail(
const char * __assertion,
1085 unsigned int __line,
1086 const char *__function)
1088 printf(
"%s:%u: %s: Device-side assertion `%s' failed.\n", __file, __line,
1089 __function, __assertion);
1096 void __assertfail(
const char * __assertion,
1098 unsigned int __line,
1099 const char *__function,
1108 static void __work_group_barrier(__cl_mem_fence_flags flags, __memory_scope scope)
1111 __atomic_work_item_fence(flags, __memory_order_release, scope);
1112 __builtin_amdgcn_s_barrier();
1113 __atomic_work_item_fence(flags, __memory_order_acquire, scope);
1115 __builtin_amdgcn_s_barrier();
1121 static void __barrier(
int n)
1123 __work_group_barrier((__cl_mem_fence_flags)n, __memory_scope_work_group);
1128 __attribute__((convergent))
1129 void __syncthreads()
1131 __barrier(__CLK_LOCAL_MEM_FENCE);
1136 __attribute__((convergent))
1137 int __syncthreads_count(
int predicate)
1139 return __ockl_wgred_add_i32(!!predicate);
1144 __attribute__((convergent))
1145 int __syncthreads_and(
int predicate)
1147 return __ockl_wgred_and_i32(!!predicate);
1152 __attribute__((convergent))
1153 int __syncthreads_or(
int predicate)
1155 return __ockl_wgred_or_i32(!!predicate);
1176 #define HW_ID_CU_ID_SIZE 4
1177 #define HW_ID_CU_ID_OFFSET 8
1179 #define HW_ID_SE_ID_SIZE 2
1180 #define HW_ID_SE_ID_OFFSET 13
1189 #define GETREG_IMMED(SZ,OFF,REG) (((SZ) << 11) | ((OFF) << 6) | (REG))
1199 unsigned __smid(
void)
1201 unsigned cu_id = __builtin_amdgcn_s_getreg(
1202 GETREG_IMMED(HW_ID_CU_ID_SIZE-1, HW_ID_CU_ID_OFFSET, HW_ID));
1203 unsigned se_id = __builtin_amdgcn_s_getreg(
1204 GETREG_IMMED(HW_ID_SE_ID_SIZE-1, HW_ID_SE_ID_OFFSET, HW_ID));
1207 return (se_id << HW_ID_CU_ID_SIZE) + cu_id;
1210 #pragma push_macro("__DEVICE__")
1214 #define HIP_DYNAMIC_SHARED(type, var) \
1215 type* var = (type*)__amdgcn_get_dynamicgroupbaseptr();
1217 #define HIP_DYNAMIC_SHARED_ATTRIBUTE
1220 #endif //defined(__clang__) && defined(__HIP__)
1224 static inline __device__
void* __hip_hc_memcpy(
void* dst,
const void* src,
size_t size) {
1225 auto dstPtr =
static_cast<unsigned char*
>(dst);
1226 auto srcPtr =
static_cast<const unsigned char*
>(src);
1228 while (size >= 4u) {
1229 dstPtr[0] = srcPtr[0];
1230 dstPtr[1] = srcPtr[1];
1231 dstPtr[2] = srcPtr[2];
1232 dstPtr[3] = srcPtr[3];
1240 dstPtr[2] = srcPtr[2];
1242 dstPtr[1] = srcPtr[1];
1244 dstPtr[0] = srcPtr[0];
1250 static inline __device__
void* __hip_hc_memset(
void* dst,
unsigned char val,
size_t size) {
1251 auto dstPtr =
static_cast<unsigned char*
>(dst);
1253 while (size >= 4u) {
1273 static inline __device__
void* memcpy(
void* dst,
const void* src,
size_t size) {
1274 return __hip_hc_memcpy(dst, src, size);
1277 static inline __device__
void* memset(
void* ptr,
int val,
size_t size) {
1278 unsigned char val8 =
static_cast<unsigned char>(val);
1279 return __hip_hc_memset(ptr, val8, size);