Skip to content

Commit

Permalink
Add code for detecting SSE2 support
Browse files Browse the repository at this point in the history
Standard cpusupport framework -- the .../Build/ bits detect whether
the compiler supports this and what flags are needed, while the new
cpusupport_x86_sse2.c code checks CPUID.
  • Loading branch information
cperciva committed Jul 30, 2015
1 parent b59448d commit 527e7a8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions libcperciva/cpusupport/Build/cpusupport-X86-SSE2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <emmintrin.h>

static char a[16];

int main(void)
{
__m128i x;

x = _mm_loadu_si128((__m128i *)a);
_mm_storeu_si128((__m128i *)a, x);
return (a[0]);
}
1 change: 1 addition & 0 deletions libcperciva/cpusupport/Build/cpusupport.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ feature() {
}

feature X86 CPUID ""
feature X86 SSE2 "" "-msse2" "-msse2 -Wno-cast-align"
feature X86 AESNI "" "-maes" "-maes -Wno-cast-align" "-maes -Wno-missing-prototypes -Wno-cast-qual"
1 change: 1 addition & 0 deletions libcperciva/cpusupport/cpusupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
* from this list so that the associated C files can be omitted.
*/
CPUSUPPORT_FEATURE(x86, aesni);
CPUSUPPORT_FEATURE(x86, sse2);

#endif /* !_CPUSUPPORT_H_ */
30 changes: 30 additions & 0 deletions libcperciva/cpusupport/cpusupport_x86_sse2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "cpusupport.h"

#ifdef CPUSUPPORT_X86_CPUID
#include <cpuid.h>
#endif

#define CPUID_SSE2_BIT (1 << 26)

CPUSUPPORT_FEATURE_DECL(x86, sse2)
{
#ifdef CPUSUPPORT_X86_CPUID
unsigned int eax, ebx, ecx, edx;

/* Check if CPUID supports the level we need. */
if (!__get_cpuid(0, &eax, &ebx, &ecx, &edx))
goto unsupported;
if (eax < 1)
goto unsupported;

/* Ask about CPU features. */
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
goto unsupported;

/* Return the relevant feature bit. */
return (edx & CPUID_SSE2_BIT);

unsupported:
#endif
return (0);
}

0 comments on commit 527e7a8

Please sign in to comment.