title: Compiling with -mpopcnt causes Illegal instruction error
body: <p>I compile the following C++ code</p>
  
  <pre><code>// main.cpp
  #include &lt;cstdio&gt;
  
  int main() {
    unsigned char tab[4] = {0};
    printf("%d\n", __builtin_popcount(*((int *)tab)));
  }
  </code></pre>
  
  <p>using command line:</p>
  
  <pre><code>g++ -o prog main.cpp -mpopcnt
  </code></pre>
  
  <p>When I run the program I get error:</p>
  
  <pre><code>Illegal instruction
  </code></pre>
  
  <p>Compiling without -mpopcnt does not give an error (it just prints 0).</p>
  
  <p><strong>Question:</strong> what is causing this error?</p>
  
  <p>I am compiling and running the program on the same machine.
  Valgrind detects no problem. Running</p>
  
  <pre><code>valgrind --leak-check=full ./prog
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>==12917== Memcheck, a memory error detector
  ==12917== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
  ==12917== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
  ==12917== Command: ./prog
  ==12917== 
  0
  ==12917== 
  ==12917== HEAP SUMMARY:
  ==12917==     in use at exit: 0 bytes in 0 blocks
  ==12917==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
  ==12917== 
  ==12917== All heap blocks were freed -- no leaks are possible
  ==12917== 
  ==12917== For counts of detected and suppressed errors, rerun with: -v
  ==12917== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
  </code></pre>
  
  <p>Below I give some specifications of my system.</p>
  
  <p>I'm using Ubuntu 12.04. Running </p>
  
  <pre><code>uname -a
  </code></pre>
  
  <p>gives me</p>
  
  <pre><code>Linux wtu-82 3.2.0-65-generic #99-Ubuntu SMP Fri Jul 4 21:03:29 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
  </code></pre>
  
  <p>Running</p>
  
  <pre><code>g++ -v
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>Using built-in specs.
  COLLECT_GCC=g++
  COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
  Target: x86_64-linux-gnu
  Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.4-1ubuntu1~12.04' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  Thread model: posix
  gcc version 4.6.4 (Ubuntu/Linaro 4.6.4-1ubuntu1~12.04)
  </code></pre>
  
  <p>The output of </p>
  
  <pre><code>cat /proc/cpuinfo
  </code></pre>
  
  <p>is</p>
  
  <pre><code>processor   : 0
  vendor_id   : GenuineIntel
  cpu family  : 6
  model       : 23
  model name  : Intel(R) Core(TM)2 Duo CPU     E8500  @ 3.16GHz
  stepping    : 10
  microcode   : 0xa0c
  cpu MHz     : 2000.000
  cache size  : 6144 KB
  physical id : 0
  siblings    : 2
  core id     : 0
  cpu cores   : 2
  apicid      : 0
  initial apicid  : 0
  fpu     : yes
  fpu_exception   : yes
  cpuid level : 13
  wp      : yes
  flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm tpr_shadow vnmi flexpriority
  bogomips    : 6317.48
  clflush size    : 64
  cache_alignment : 64
  address sizes   : 36 bits physical, 48 bits virtual
  power management:
  
  processor   : 1
  vendor_id   : GenuineIntel
  cpu family  : 6
  model       : 23
  model name  : Intel(R) Core(TM)2 Duo CPU     E8500  @ 3.16GHz
  stepping    : 10
  microcode   : 0xa0c
  cpu MHz     : 2000.000
  cache size  : 6144 KB
  physical id : 0
  siblings    : 2
  core id     : 1
  cpu cores   : 2
  apicid      : 1
  initial apicid  : 1
  fpu     : yes
  fpu_exception   : yes
  cpuid level : 13
  wp      : yes
  flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm tpr_shadow vnmi flexpriority
  bogomips    : 6317.38
  clflush size    : 64
  cache_alignment : 64
  address sizes   : 36 bits physical, 48 bits virtual
  power management:
  </code></pre>