2016-06-30 1 views
3

512MB 메모리에서 1 개가 얼마나 많은지 알고 싶었는데 gcc 개의 내장 코드에 _mm_popcnt_u64()__builtin_popcountll() 두 가지 가능한 방법이 있습니다.__builtin_popcountll과 _mm_popcnt_u64의 차이점은 무엇입니까?

_mm_popcnt_u64()는 가장 빠른 것으로 보이는 CPU 소개 SSE4.2를 사용한다고하며, __builtin_popcountll()은 테이블 조회를 사용하는 것을 제외합니다.

따라서 은 _mm_popcnt_u64()보다 약간 느려야합니다.

Test result

그것은 두 가지 방법이 거의 같은 시간이 걸렸습니다 :

그러나이 같은 결과를 얻었다. 나는 그들이 일하는 데 같은 방법을 사용했다는 것을 매우 의심한다.

는 또한 그래서

/* Calculate a number of bits set to 1. */ 
extern __inline int __attribute__((__gnu_inline__, __always_inline__, __artificial___)) 
_mm_popcnt_u32 (unsigned int __X) 
{ 
    return __builtin_popcount (__X); 
} 

#ifdef __x86_64__ 
extern __inline long long __attribute__((__gnu_inline__, __always_inline__, __artificial__)) 
_mm_popcnt_u64 (unsigned long long __X) 
{ 
    return __builtin_popcountll (__X); 
} 
#endif 

popcntintrin.h이있어, 나는 지구

+0

내 Skylake CPU에서 타이밍이 비슷합니다 (거의 같습니다). –

답변

6

_mm_popcnt_u64__builtin_popcountll() 작품 <nmmintrin.h>의 일부가 얼마나 혼란 스러워요, SSE 4.2에 액세스하기위한 유틸리티 함수에 대한 인텔이 고안 헤더 명령.

__builtin_popcountll은 GCC 확장입니다.

_mm_popcnt_u64은 GNU 이외의 컴파일러로 이식 가능하고 __builtin_popcountll은 비 SSE-4.2 CPU로 이식 할 수 있습니다. 그러나 둘 다 사용할 수있는 시스템에서는 모두 정확히 동일한 코드로 컴파일해야합니다.