flat assembler
Message board for the users of flat assembler.

Index > Windows > [Solved] Processor core number

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 03 Feb 2014, 17:58
Hello!

How can I get the number of cores in a processor? I can't do that with CPUID I think.


Last edited by A$M on 04 Feb 2014, 20:38; edited 1 time in total
Post 03 Feb 2014, 17:58
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 03 Feb 2014, 18:22
There is some code example but very long.
Maybe you can strip it down. Wink

In this CPUID documentation on page 74 (middle) there is a query of the cores.
You can search for _dcp_cache_eax or ">= 2 cores?".

found here:
www.microbe.cz/docs/CPUID.pdf
Post 03 Feb 2014, 18:22
View user's profile Send private message Send e-mail Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 03 Feb 2014, 18:44
my experience with cupid for the number of cores has not been good. You might have to use OS for this.

Code:
CoreCount:
push  rbp
invoke  GetCurrentProcess
invoke  GetProcessAffinityMask,rax,ProcessAffinityMask,SystemAffinityMask
test eax,eax
jz .failed
popcnt  rax,qword[ProcessAffinityMask]
.failed:
pop  rbp
ret
                                      
    
Post 03 Feb 2014, 18:44
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 03 Feb 2014, 18:45
You can use GetSystemInfo from WinAPI to get the total number of cores; or do you really want to know for a particular core?
Post 03 Feb 2014, 18:45
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 03 Feb 2014, 19:15
With a code like this:
Code:
invoke GetSystemInfo, _systeminfo
cinvoke printf, _cores, [_systeminfo+20]
jmp $

_systeminfo rd 9    
I can get the logical cores count. How can I get the number of physical cores?
Post 03 Feb 2014, 19:15
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 03 Feb 2014, 19:44
What I posted gives the logical processor count. If you want the physical processor count check out the function GetLogicalProcessorInformation.
Post 03 Feb 2014, 19:44
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 03 Feb 2014, 21:18
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:19; edited 1 time in total
Post 03 Feb 2014, 21:18
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 03 Feb 2014, 23:14
Quote:
My processor does not support popcnt instruction. Sad

My condolences. As you know, the popcnt instruction is the only way one can count the number of bits that are set.
Post 03 Feb 2014, 23:14
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 04 Feb 2014, 00:47
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:19; edited 1 time in total
Post 04 Feb 2014, 00:47
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 04 Feb 2014, 00:58
A$M wrote:
I can get the logical cores count. How can I get the number of physical cores?


Just use GetLogicalProcessorInformationEx

Quote:

RelationProcessorCore (0)

Retrieves information about logical processors that share a single processor core.


If it is a HT processor (HyperThreading) you have double logical cores than physical cores. If you have no HT then logical cores should be same like physical cores. So if you get this structure with RelationProcessorCore there should be found either "1" or "2" which means 1 physical core=1 logical core or 1 physical core=2 logical cores.

Maybe there are more easy ways to find out if it is a Hyperthreading CPU or not. Shocked
Post 04 Feb 2014, 00:58
View user's profile Send private message Send e-mail Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 04 Feb 2014, 08:51
HaHaAnonymous
Quote:
The only way in a single instruction you mean... Right?

I have a gift for you. Here it is. Use it wisely.

_________________
Faith is a superposition of knowledge and fallacy
Post 04 Feb 2014, 08:51
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 04 Feb 2014, 09:09
Maybe the Intel docs? It is low-level though, maybe an OS will prevent it.
8.9.5 Identifying Topological Relationships in a MP System
Post 04 Feb 2014, 09:09
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 04 Feb 2014, 13:47
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:18; edited 1 time in total
Post 04 Feb 2014, 13:47
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 04 Feb 2014, 16:38
HaHaAnonymous wrote:
Quote:

Use it wisely.

I think a "[sarcasm][/sarcasm]" bbcode will do better... And that sarcasm detector probably never existed.

Looks like stupid male jokes are a common thing here...


Don't listen to him bro.

Here's another way of counting bits 5 Different ways to count bits in x86/x64 Assembly
Post 04 Feb 2014, 16:38
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 04 Feb 2014, 18:37
Well... Smile A code like this now should be okay:
Code:
invoke GetSystemInfo, _systeminfo
mov eax, 1
cpuid
mov eax, [_systeminfo+20]
test edx, 1 shl 28
jz not_htt
shr eax, 1
not_htt:
cinvoke printf, _processor, eax,[_systeminfo+20]

jmp $ 

_processor db "%i cores, %i threads.",0
_systeminfo rd 9    


That is, if the hyperthreaded cores remain as 2 logical cores... Wink

Ps.: And this, for counting bits Wink
Code:
  xor edx, edx
  mov ecx, 32
  mov ebx, 1
lp:
  test eax, ebx
  jz @f
  inc edx
@@:
  shl ebx, 1
  loop lp    
Input in EAX, output in EDX.
Post 04 Feb 2014, 18:37
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 04 Feb 2014, 19:51
oops, obviously my statement as it is should be interpreted as sarcasm, but if you put a 'NOT' in the right place, then you get the literal meaning. I simple macro will get te number of set bits
Code:
macro my_popcnt a,b {
xor  a,a
repreat 32
shr b,1
adc a,0
end repeat
}    
Post 04 Feb 2014, 19:51
View user's profile Send private message Reply with quote
A$M



Joined: 29 Feb 2012
Posts: 94
A$M 04 Feb 2014, 20:34
tthsqe wrote:
oops, obviously my statement as it is should be interpreted as sarcasm, but if you put a 'NOT' in the right place, then you get the literal meaning. I simple macro will get te number of set bits
Code:
macro my_popcnt a,b {
xor  a,a
repreat 32
shr b,1
adc a,0
end repeat
}    
I forgot of the carry existance. Laughing
Code:
  xor edx, edx 
  mov ecx, 32  
@@: 
  shr eax, 1
  adc edx, 0 
  loop @b    
Wink
Post 04 Feb 2014, 20:34
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 07 Feb 2014, 10:16
A$M wrote:
I forgot of the carry existance.

You guys need to learn optimizations. Even the following is much better, than the sum over cf:
Code:
;input in eax
xor edx,edx
test eax,eax
jz .omit
@@:
        inc edx
        lea ecx,[eax-1]
        and eax,ecx
jnz @B
.omit:
;output in edx    

This way you need to iterate over the set bits only. Not over all of them.
The following code performance is rather input independent:
Code:
;input in eax
mov edx,eax
shr edx,1
and edx,$55555555
sub eax,edx
mov ecx,$33333333
mov edx,eax
and eax,ecx
shr edx,2
and edx,ecx
add eax,edx
mov edx,eax
shr eax,4
add eax,edx
and eax,$0F0F0F0F
imul eax,$01010101
shr eax,24
;output in eax    

_________________
Faith is a superposition of knowledge and fallacy
Post 07 Feb 2014, 10:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20526
Location: In your JS exploiting you and your system
revolution 07 Feb 2014, 10:24
Hehe, what goes around comes around. There is an old thread here (that I am too lazy to search for now) discussing the population count problem with lots of code and optimisations.
Post 07 Feb 2014, 10:24
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20526
Location: In your JS exploiting you and your system
revolution 07 Feb 2014, 11:08
Post 07 Feb 2014, 11:08
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
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


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.