Search code examples
c++cintelcpuid

Logical CPU count return 16 instead of 4


I have a Intel Core i5-2450m (2 physical processors and 4 logical processors) and I want to find a way to count logical and physical cores on AMD and Intel CPUs. But, after some searches I noticed something strange. Instead of returning 4 logical units, my code give me 16.

static int8_t LogicalProcCount(void)
{
    if ( !Hyperthreading )
        return 1;

    uint32_t unused, ebx;
    CPUID(1, unused, ebx, unused, unused);

    return (int8_t) ( (ebx >> 16 ) & 0xFF );
}

Solution

  • CPUID.1:EBX[23:16] represents the maximum number of addressable IDs (initial APIC ID) that can be assigned to logical processors in a physical package.

    Source.

    So 16 has nothing to do with the actual number of your logical CPUs. On my machine CPUID.1:EBX[23:16] also returns 16, though it has 8 logical CPUs.

    Sample code to count actual logical CPUs can be also found in the linked whire paper.