Tinker9 70bd052 (Thu Nov 9 12:11:35 2023 -0800)
Loading...
Searching...
No Matches
ffsn.h
1#pragma once
2
3namespace tinker {
15__device__
16inline int ffsnShift(int c1, int n)
17{
18 int c2 = c1 - ((c1 >> 1) & 0x55555555);
19 int c4 = ((c2 >> 2) & 0x33333333) + (c2 & 0x33333333);
20 int c8 = ((c4 >> 4) + c4) & 0x0f0f0f0f;
21 int c16 = ((c8 >> 8) + c8);
22 // int c32 = ((c16 >> 16) + c16) & 0x3f; // __popc
23 int r = 0, i = n - 1, t;
24 // clang-format off
25 t = (c16 ) & 0x1f; if (i >= t) { r += 16; i -= t; }
26 t = (c8 >> r) & 0x0f; if (i >= t) { r += 8; i -= t; }
27 t = (c4 >> r) & 0x07; if (i >= t) { r += 4; i -= t; }
28 t = (c2 >> r) & 0x03; if (i >= t) { r += 2; i -= t; }
29 t = (c1 >> r) & 0x01; if (i >= t) { r += 1; }
30 if (n == 0) r = -1;
31 // if (n > c32) r = 32;
32 // clang-format on
33 return r + 1;
34}
35
38__device__
39inline int ffsnLoop(int c1, int n)
40{
41 int ans = 0;
42 int i = 0;
43 while (c1 && (i++ < n)) {
44 int pos = __ffs(c1);
45 ans += pos;
46 c1 >>= pos;
47 }
48 return ans;
49}
50}
int n
Number of atoms padded by WARP_SIZE.
__device__ int ffsnShift(int c1, int n)
Find the position of the n-th least significant bit set in a 32-bit integer. Returns a value from 0 t...
Definition: ffsn.h:16
__device__ int ffsnLoop(int c1, int n)
An alternative implementation of ffsn using loop.
Definition: ffsn.h:39
Definition: testrt.h:9