-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnybble.cpp
More file actions
72 lines (72 loc) · 2.06 KB
/
Copy pathnybble.cpp
File metadata and controls
72 lines (72 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "lifealgo.h"
#include "util.h"
#include <algorithm>
#include <cstdlib>
using namespace std ;
typedef unsigned long long ull ;
class nybblealgo : public lifealgo {
public:
virtual void init(int w, int h) ;
virtual void setcell(int x, int y) ;
virtual int getpopulation() ;
virtual int nextstep(int, int, int) ;
virtual void swap() ;
int w, h, wordwidth ;
long long wh ;
ull *u0, *u1 ;
} ;
static class nybblealgofactory : public lifealgofactory {
public:
nybblealgofactory() ;
virtual lifealgo *createInstance() {
return new nybblealgo() ;
}
} factory ;
nybblealgofactory::nybblealgofactory() {
registerAlgo("nybble", &factory) ;
}
void nybblealgo::init(int w_, int h_) {
w = w_ ;
h = h_ ;
wordwidth = (w + 15) >> 4 ;
wh = wordwidth * h ;
u0 = (ull *)calloc(wordwidth*sizeof(ull), h+1) ;
u1 = (ull *)calloc(wordwidth*sizeof(ull), h+1) ;
}
void nybblealgo::setcell(int x, int y) {
u0[(x >> 4) + wordwidth * y] |= 1LL << (4 * (x & 15)) ;
}
int nybblealgo::getpopulation() {
int r = 0 ;
for (int i=0; i<wh; i++)
r += __builtin_popcountll(u0[i]) ;
return r ;
}
void nybblealgo::swap() { ::swap(u0, u1) ; }
int nybblealgo::nextstep(int id, int n, int needpop) {
ull r = 0 ;
int loi = id * (h - 2) / n + 1 ;
int hii = (id + 1) * (h - 2) / n + 1 ;
for (int i=loi; i<hii; i++) {
ull *pwp = u0 + wordwidth * (i - 1) ;
ull *cwp = u0 + wordwidth * i ;
ull *nwp = u0 + wordwidth * (i + 1) ;
ull *wr = u1 + wordwidth * i ;
ull pws = 0 ;
ull cws = *pwp + *cwp + *nwp ;
for (int j=0; j<wordwidth; j++, pwp++, cwp++, nwp++, wr++) {
ull nws = 0 ;
if (j+1 < wordwidth)
nws = pwp[1] + cwp[1] + nwp[1] ;
ull cw = *cwp ;
ull ng = cw | ((pws >> 60) + (cws >> 4) + cws + (cws << 4) + (nws << 60) - cw) ;
ng &= (ng >> 1) & (~((ng >> 2) | (ng >> 3))) & 0x1111111111111111LL ;
if (needpop)
r += __builtin_popcountll(ng) ;
*wr = ng ;
pws = cws ;
cws = nws ;
}
}
return (int)r ;
}