A more complete answer would be that for Intel processors made before 2010 that kind code was usually okay. (I'd like to hear about counter-examples though!) That kind of counting routine was even given in an older Intel presentation dating to 2007(?) or so https://software.intel.com/en-us/articles/hyper-threading-technology-and-multi-core-processor-detection (Don't let the posting date the fool you; when that presentation was made the Intel Core wasn't yet public and the Pentium Extreme Edition was Intel's top offering.) Rather shamefully, that algorithm is still given in the MSDN page about __cpuid, even for their 2013 edition! (http://msdn.microsoft.com/en-us/library/hskdteyh.aspx) The MSDN sample code hasn't been updated since 2008...
In early 2010 Intel put out Westmere processors that have gaps in their APIC id space. So these and later processors need a more refined method of counting their cores. These processors all have x2APIC and support leaf 0xB of cpuid as well... which can tell you about the gaps in the APIC id space, via EAX[4:0] and exactly how many logical processors are present at each level (package/core) via EBX. The link provided by n.m. is the authoritative one, albeit rather dry. Basically all you have to do is call cpuid with EAX=0xB, ECX=1 and you'll have the correct number of logical processors per package returned in EBX.
Also, the part of question about AMD was not answered. AMD has a different methodology because they don't support the same cpuid leaves as Intel. The most up to date info I could find about AMD (from 2013) is at http://developer.amd.com/resources/documentation-articles/articles-whitepapers/processor-and-core-enumeration-using-cpuid/
I dug up all this info while updating the Wikipedia page on CPUID today, by the way.
CPUID
coming from? That's not the name of the compiler-provided instrinsic function, so it must be some non-standard wrapper. – Beeswax