Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


CountSetBits custom type [4byte and 8byte]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Fri Jun 28, 2013 12:40 pm    Post subject: CountSetBits custom type [4byte and 8byte] Reply with quote

aka Population count (count number of bits set to 1)

For example, Metro Last Light uses this type, number of available medkids are stored that way.


CountSetBits custom type 4byte
Code:
// (only 32bit registers used)

alloc(ConvertRoutine,1024)
alloc(TypeName,256)
alloc(ByteSize,4)

TypeName:
db 'CountSetBits 4byte',0

ByteSize:
dd 4


ConvertRoutine:
[32-bit]
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov eax,[eax]
[/32-bit]

[64-bit]
mov eax,[rcx]
push ebx
[/64-bit]

mov ebx,eax
shr ebx,1
and eax,55555555
and ebx,55555555
add eax,ebx

mov ebx,eax
shr ebx,2
and eax,33333333
and ebx,33333333
add eax,ebx

mov ebx,eax
shr ebx,4
add eax,ebx
and eax,0F0F0F0F

mov ebx,eax
shr ebx,8
add eax,ebx

mov ebx,eax
shr ebx,10
add eax,ebx

and eax,3F

[32-bit]
pop ebp
ret 4
[/32-bit]

[64-bit]
pop ebx
ret
[/64-bit]




CountSetBits custom type 8byte
Code:
// (64bit MMX registers and 64bit CPU registers used)

alloc(ConvertRoutine,1024)
alloc(TypeName,256)
alloc(ByteSize,4)
alloc(PREFEREDALIGNMENT,4)

TypeName:
db 'CountSetBits 8byte',0

ByteSize:
dd 8

PREFEREDALIGNMENT:
dd 4

label(dat55)
label(dat33)
label(dat0f)


ConvertRoutine:
[32-bit]
push ebp
mov ebp,esp
mov eax,[ebp+8]

movq  mm0,[eax]

movq  mm1,mm0
psrlq mm1,01
pand  mm0,[dat55]
pand  mm1,[dat55]
paddq mm0,mm1

movq  mm1,mm0
psrlq mm1,02
pand  mm0,[dat33]
pand  mm1,[dat33]
paddq mm0,mm1

movq  mm1,mm0
psrlq mm1,04
paddq mm0,mm1
pand  mm0,[dat0f]

movq  mm1,mm0
psrlq mm1,08
paddq mm0,mm1

movq  mm1,mm0
psrlq mm1,10
paddq mm0,mm1

movq  mm1,mm0
psrlq mm1,20
paddq mm0,mm1

movd eax,mm0
and eax,7f

pop ebp
ret 4
[/32-bit]

[64-bit]
push rbx

mov rax,[rcx]

mov rbx,rax
shr rbx,1
and rax,[dat55]
and rbx,[dat55]
add rax,rbx

mov rbx,rax
shr rbx,2
and rax,[dat33]
and rbx,[dat33]
add rax,rbx

mov rbx,rax
shr rbx,4
add rax,rbx
and rax,[dat0f]

mov rbx,rax
shr rbx,8
add rax,rbx

mov rbx,rax
shr rbx,10
add rax,rbx

mov rbx,rax
shr rbx,20
add rax,rbx

and rax,7f

pop rbx
ret
[/64-bit]

ConvertRoutine+300:
dat55:
dq 5555555555555555
dat33:
dq 3333333333333333
dat0f:
dq 0f0f0f0f0f0f0f0f

_________________
Back to top
View user's profile Send private message MSN Messenger
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Sun Feb 04, 2018 2:29 pm    Post subject: Reply with quote

Custom types ported to "Streaming SIMD Extensions 4.2" (SSE4.2)

CountSetBits custom type 4byte
Code:
alloc(ConvertRoutine,1024)
alloc(TypeName,256)
alloc(ByteSize,4)

TypeName:
db 'CountSetBits 4byte (new)',0

ByteSize:
dd 4

ConvertRoutine:
[32-bit]
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov eax,[eax]
[/32-bit]

[64-bit]
mov eax,[rcx]
[/64-bit]

db F3 0F B8 C0 // popcnt eax,eax

[32-bit]
pop ebp
ret 4
[/32-bit]

[64-bit]
ret
[/64-bit]



CountSetBits custom type 8byte
Code:
alloc(ConvertRoutine,1024)
alloc(TypeName,256)
alloc(ByteSize,4)
alloc(PreferedAlignment,4)
alloc(CallMethod,1)

TypeName:
db 'CountSetBits 8byte (new)',0

ByteSize:
dd 8

PreferedAlignment:
dd 4

CallMethod:
db 1

ConvertRoutine:
[32-bit]
push ebp
mov ebp,esp
push ebx
mov eax,[ebp+8]
mov ebx,[eax+4]
mov eax,[eax]
db F3 0F B8 C0 // popcnt eax,eax
db F3 0F B8 DB // popcnt ebx,ebx
lea eax,[eax+ebx]
pop ebx
pop ebp
ret
[/32-bit]

[64-bit]
mov rax,[rcx]
db F3 48 0F B8 C0 // popcnt rax,rax
ret
[/64-bit]

_________________
Back to top
View user's profile Send private message MSN Messenger
Prehistoricman
Advanced Cheater
Reputation: 0

Joined: 02 Aug 2016
Posts: 80

PostPosted: Mon Feb 05, 2018 4:48 am    Post subject: Reply with quote

I know it's an old thread but this code is called unary code or thermometer code fyi
_________________
Er, hi
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites