Skip to content

Commit

Permalink
Backport htslib fix: configure checks for broken SIMD intrinsics
Browse files Browse the repository at this point in the history
Apply PR samtools/htslib#1886. GCC 4.8.5 and earlier accept SSSE3
intrinsics with -mssse3; these compiler versions also accept
__attribute__((target("ssse3"))) but the attribute fails to enable
compilation of the intrinsics.
  • Loading branch information
jmarshall committed Feb 10, 2025
1 parent c52203a commit 3e3c8b0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion htslib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ config.h:
echo '#define HAVE_ATTRIBUTE_CONSTRUCTOR 1' >> $@
echo '#endif' >> $@
echo '#if (defined(__x86_64__) || defined(_M_X64))' >> $@
echo '#define HAVE_ATTRIBUTE_TARGET 1' >> $@
echo '#define HAVE_ATTRIBUTE_TARGET_SSSE3 1' >> $@
echo '#define HAVE_BUILTIN_CPU_SUPPORT_SSSE3 1' >> $@
echo '#endif' >> $@

Expand Down
4 changes: 2 additions & 2 deletions htslib/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
/* Define if __attribute__((constructor)) is available. */
#undef HAVE_ATTRIBUTE_CONSTRUCTOR

/* Define if __attribute__((target(...))) is available. */
#undef HAVE_ATTRIBUTE_TARGET
/* Define if __attribute__((target("ssse3"))) works. */
#undef HAVE_ATTRIBUTE_TARGET_SSSE3

/* Defined to 1 if rANS source using AVX2 can be compiled. */
#undef HAVE_AVX2
Expand Down
20 changes: 14 additions & 6 deletions htslib/configure
Original file line number Diff line number Diff line change
Expand Up @@ -4686,20 +4686,28 @@ fi
rm -f core conftest.err conftest.$ac_objext conftest.beam \
conftest$ac_exeext conftest.$ac_ext

{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for __attribute__((target))" >&5
printf %s "checking for __attribute__((target))... " >&6; }
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for working __attribute__((target(\"ssse3\")))" >&5
printf %s "checking for working __attribute__((target(\"ssse3\")))... " >&6; }
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
#ifdef __x86_64__
#include "x86intrin.h"
__attribute__((target("ssse3")))
int zero(void) {
return 0;
void shuffle(char *aptr, char *bptr) {
__m128i a = _mm_lddqu_si128((__m128i *)aptr);
__m128i b = _mm_shuffle_epi8(a, a);
_mm_storeu_si128((__m128i *)bptr, b);
}
#else
void shuffle(char *aptr, char *bptr) { }
#endif
int
main (void)
{
zero();
shuffle(0, 0);
;
return 0;
}
Expand All @@ -4710,7 +4718,7 @@ then :
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
printf "%s\n" "yes" >&6; }

printf "%s\n" "#define HAVE_ATTRIBUTE_TARGET 1" >>confdefs.h
printf "%s\n" "#define HAVE_ATTRIBUTE_TARGET_SSSE3 1" >>confdefs.h


else case e in #(
Expand Down
21 changes: 15 additions & 6 deletions htslib/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,25 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([],[
])
dnl Check for function attribute used in conjunction with __builtin_cpu_supports
AC_MSG_CHECKING([for __attribute__((target))])
dnl and that it does enable the corresponding intrinsics (which is broken on ancient GCCs)
AC_MSG_CHECKING([for working __attribute__((target("ssse3")))])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#ifdef __x86_64__
#include "x86intrin.h"
__attribute__((target("ssse3")))
int zero(void) {
return 0;
void shuffle(char *aptr, char *bptr) {
__m128i a = _mm_lddqu_si128((__m128i *)aptr);
__m128i b = _mm_shuffle_epi8(a, a);
_mm_storeu_si128((__m128i *)bptr, b);
}
]], [[zero();]])], [
#else
void shuffle(char *aptr, char *bptr) { }
#endif
]], [[shuffle(0, 0);]])], [
AC_MSG_RESULT([yes])
AC_DEFINE([HAVE_ATTRIBUTE_TARGET], 1,
[Define if __attribute__((target(...))) is available.])
AC_DEFINE([HAVE_ATTRIBUTE_TARGET_SSSE3], 1,
[Define if __attribute__((target("ssse3"))) works.])
], [
AC_MSG_RESULT([no])
])
Expand Down
2 changes: 1 addition & 1 deletion htslib/sam_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static inline void nibble2base_default(uint8_t *nib, char *seq, int len) {
}

#if defined HAVE_ATTRIBUTE_CONSTRUCTOR && \
((defined __x86_64__ && defined HAVE_ATTRIBUTE_TARGET && defined HAVE_BUILTIN_CPU_SUPPORT_SSSE3) || \
((defined __x86_64__ && defined HAVE_ATTRIBUTE_TARGET_SSSE3 && defined HAVE_BUILTIN_CPU_SUPPORT_SSSE3) || \
(defined __ARM_NEON))
#define BUILDING_SIMD_NIBBLE2BASE
#endif
Expand Down

0 comments on commit 3e3c8b0

Please sign in to comment.