コメントを受けて64bit環境でも試してみました。
ダウンロード(popcnt64.zip)
ただ、VS2005だと新しいSSE命令はインラインできないようです…。
32bit環境ではMMXを使ってやってみました(popcnt646_cとpopcnt64_d)が、普通に64bit変数を使うほうが速かったです(これはコードが悪い??)。
以下の結果では PhenomIIX4 940 の64bit環境ではpopcnt_a(1)<popcnt_a(2)<popcnt_a(4)<popcnt_b()なので、常にpopcnt_a()を使うのがいいのかなぁ~。
C:\home\popcnt>popcnt64_x64
64 bit環境
32bit演算
popcnt_a:00000000000000000000000000000000: 0: 0.7810 sec
popcnt_b:00000000000000000000000000000000: 0: 1.2340 sec
popcnt_a:00000000000000000000000000000001: 1: 0.6100 sec
popcnt_b:00000000000000000000000000000001: 1: 1.2340 sec
popcnt_a:10000000000000000000000000000000: 1: 0.6090 sec
popcnt_b:10000000000000000000000000000000: 1: 1.1100 sec
popcnt_a:00000000000000000000000000010001: 2: 0.7650 sec
popcnt_b:00000000000000000000000000010001: 2: 1.2500 sec
popcnt_a:00000000000000000001000100010001: 4: 1.1100 sec
popcnt_b:00000000000000000001000100010001: 4: 1.2340 sec
popcnt_a:00010001000100010001000100010001: 8: 1.7970 sec
popcnt_b:00010001000100010001000100010001: 8: 1.2500 sec
popcnt_a:01010101010101010101010101010101:16: 5.2810 sec
popcnt_b:01010101010101010101010101010101:16: 1.2500 sec
popcnt_a:11111111111111111111111111111111:32: 7.9220 sec
popcnt_b:11111111111111111111111111111111:32: 1.5310 sec
ここから64bit
popcnt64_a:0000000000000000000000000000000000000000000000000000000000000000: 0: 0.7660 sec
popcnt64_b:0000000000000000000000000000000000000000000000000000000000000000: 0: 1.5310 sec
popcnt64_a:0000000000000000000000000000000100000000000000000000000000000001: 1: 0.5940 sec
popcnt64_b:0000000000000000000000000000000100000000000000000000000000000001: 1: 1.3750 sec
popcnt64_a:0000000000000000000000000000000100000000000000000000000000000001: 2: 1.2350 sec
popcnt64_b:0000000000000000000000000000000100000000000000000000000000000001: 2: 1.3590 sec
popcnt64_a:0000000000000001000000000000000100000000000000010000000000000001: 4: 1.1870 sec
popcnt64_b:0000000000000001000000000000000100000000000000010000000000000001: 4: 1.3750 sec
popcnt64_a:0000000100000001000000010000000100000001000000010000000100000001: 8: 1.8750 sec
popcnt64_b:0000000100000001000000010000000100000001000000010000000100000001: 8: 1.3750 sec
popcnt64_a:0001000100010001000100010001000100010001000100010001000100010001:16: 5.1250 sec
popcnt64_b:0001000100010001000100010001000100010001000100010001000100010001:16: 1.3590 sec
popcnt64_a:0101010101010101010101010101010101010101010101010101010101010101:32: 7.9380 sec
popcnt64_b:0101010101010101010101010101010101010101010101010101010101010101:32: 1.3750 sec
popcnt64_a:1111111111111111111111111111111111111111111111111111111111111111:64:13.3750 sec
popcnt64_b:1111111111111111111111111111111111111111111111111111111111111111:64: 1.8750 sec
C:\home\popcnt>popcnt64_x86
32 bit環境
32bit演算
popcnt_a:00000000000000000000000000000000: 0: 1.7810 sec
popcnt_b:00000000000000000000000000000000: 0: 1.3750 sec
popcnt_a:00000000000000000000000000000001: 1: 0.7650 sec
popcnt_b:00000000000000000000000000000001: 1: 1.3590 sec
popcnt_a:10000000000000000000000000000000: 1: 0.7820 sec
popcnt_b:10000000000000000000000000000000: 1: 1.3590 sec
popcnt_a:00000000000000000000000000010001: 2: 4.5470 sec
popcnt_b:00000000000000000000000000010001: 2: 1.3590 sec
popcnt_a:00000000000000000001000100010001: 4: 5.0470 sec
popcnt_b:00000000000000000001000100010001: 4: 1.3600 sec
popcnt_a:00010001000100010001000100010001: 8: 6.2180 sec
popcnt_b:00010001000100010001000100010001: 8: 1.3600 sec
popcnt_a:01010101010101010101010101010101:16: 7.8430 sec
popcnt_b:01010101010101010101010101010101:16: 1.3750 sec
popcnt_a:11111111111111111111111111111111:32:10.5630 sec
popcnt_b:11111111111111111111111111111111:32: 1.3590 sec
ここから64bit
popcnt64_a:0000000000000000000000000000000000000000000000000000000000000000: 0: 0.6870 sec
popcnt64_b:0000000000000000000000000000000000000000000000000000000000000000: 0: 3.5790 sec
popcnt64_c:0000000000000000000000000000000000000000000000000000000000000000: 0: 6.1410 sec
popcnt64_d:0000000000000000000000000000000000000000000000000000000000000000: 0: 4.8120 sec
popcnt64_a:0000000000000000000000000000000000000000000000000000000000000001: 1: 0.9850 sec
popcnt64_b:0000000000000000000000000000000000000000000000000000000000000001: 1: 3.5780 sec
popcnt64_c:0000000000000000000000000000000000000000000000000000000000000001: 1: 6.1400 sec
popcnt64_d:0000000000000000000000000000000000000000000000000000000000000001: 1: 4.8130 sec
popcnt64_a:0000000000000000000000000000000100000000000000000000000000000001: 2: 1.2970 sec
popcnt64_b:0000000000000000000000000000000100000000000000000000000000000001: 2: 3.5780 sec
popcnt64_c:0000000000000000000000000000000100000000000000000000000000000001: 2: 6.1410 sec
popcnt64_d:0000000000000000000000000000000100000000000000000000000000000001: 2: 4.8120 sec
popcnt64_a:0000000000000001000000000000000100000000000000010000000000000001: 4: 2.2190 sec
popcnt64_b:0000000000000001000000000000000100000000000000010000000000000001: 4: 3.5790 sec
popcnt64_c:0000000000000001000000000000000100000000000000010000000000000001: 4: 6.1400 sec
popcnt64_d:0000000000000001000000000000000100000000000000010000000000000001: 4: 4.8130 sec
popcnt64_a:0000000100000001000000010000000100000001000000010000000100000001: 8: 3.9220 sec
popcnt64_b:0000000100000001000000010000000100000001000000010000000100000001: 8: 3.5780 sec
popcnt64_c:0000000100000001000000010000000100000001000000010000000100000001: 8: 6.1400 sec
popcnt64_d:0000000100000001000000010000000100000001000000010000000100000001: 8: 4.8130 sec
popcnt64_a:0001000100010001000100010001000100010001000100010001000100010001:16: 8.9380 sec
popcnt64_b:0001000100010001000100010001000100010001000100010001000100010001:16: 3.5780 sec
popcnt64_c:0001000100010001000100010001000100010001000100010001000100010001:16: 6.1250 sec
popcnt64_d:0001000100010001000100010001000100010001000100010001000100010001:16: 4.8130 sec
popcnt64_a:0101010101010101010101010101010101010101010101010101010101010101:32:15.5470 sec
popcnt64_b:0101010101010101010101010101010101010101010101010101010101010101:32: 3.5780 sec
popcnt64_c:0101010101010101010101010101010101010101010101010101010101010101:32: 6.1400 sec
popcnt64_d:0101010101010101010101010101010101010101010101010101010101010101:32: 4.8130 sec
popcnt64_a:1111111111111111111111111111111111111111111111111111111111111111:64:29.1410 sec
popcnt64_b:1111111111111111111111111111111111111111111111111111111111111111:64: 3.5930 sec
popcnt64_c:1111111111111111111111111111111111111111111111111111111111111111:64: 6.1410 sec
popcnt64_d:1111111111111111111111111111111111111111111111111111111111111111:64: 4.8120 sec
PR