Skip to content

Commit

Permalink
Handle non-Intel platforms in Range and CpuId
Browse files Browse the repository at this point in the history
Summary:
Compile out the SSE versions of these functions in Range, based on a new
entry in folly-config.h.

The change to CpuId feels slightly iffy to me. It seems like it would be
more rigorous to make compiling CpuId.h on non-Intel an error, and force
clients to handle non-Intel platforms at the callsite. However, I think
that would be too susceptible to unintentional breakage on non-Intel
platforms, since most people (including automated systems) aren't
building and testing regularly on any. Falling back to saying "none of
these features exist on this processor" seems like a reasonable
alternative.

Test Plan:
fbmake runtests, with FOLLY_HAVE_EMMINTRIN_H set to 0 and 1.
Make sure the SSE functions are getting compiled in or out as
appropriate. ##autoreconf## and ##./configure## to regenerate
folly-config.h.

Reviewed By: delong.j@fb.com

FB internal diff: D746872
  • Loading branch information
oyamauchi committed Mar 27, 2013
1 parent de2cc5e commit ae95c1d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
7 changes: 7 additions & 0 deletions folly/CpuId.h
Expand Up @@ -29,7 +29,14 @@ namespace folly {
class CpuId {
public:
CpuId() {
#if defined(__x86_64__) || defined(__i386__)
__asm__("cpuid" : "=c"(c_), "=d"(d_) : "a"(1) : "ebx");
#else
// On non-Intel, none of these features exist; at least not in the same form
// as they do on Intel
c_ = 0;
d_ = 0;
#endif
}
#define X(name, r, bit) bool name() const { return r & (1U << bit); }
#define C(name, bit) X(name, c_, bit)
Expand Down
5 changes: 5 additions & 0 deletions folly/Range.cpp
Expand Up @@ -72,6 +72,7 @@ inline size_t nextAlignedIndex(const char* arr) {
- firstPossible;
}

#if FOLLY_HAVE_EMMINTRIN_H
// build sse4.2-optimized version even if -msse4.2 is not passed to GCC
size_t qfind_first_byte_of_needles16(const StringPiece& haystack,
const StringPiece& needles)
Expand Down Expand Up @@ -116,6 +117,7 @@ size_t qfind_first_byte_of_needles16(const StringPiece& haystack,
}
return StringPiece::npos;
}
#endif // FOLLY_HAVE_EMMINTRIN_H

// Aho, Hopcroft, and Ullman refer to this trick in "The Design and Analysis
// of Computer Algorithms" (1974), but the best description is here:
Expand Down Expand Up @@ -161,6 +163,8 @@ size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
return StringPiece::npos;
}

#if FOLLY_HAVE_EMMINTRIN_H

template <bool HAYSTACK_ALIGNED>
inline size_t scanHaystackBlock(const StringPiece& haystack,
const StringPiece& needles,
Expand Down Expand Up @@ -248,6 +252,7 @@ size_t qfind_first_byte_of_sse42(const StringPiece& haystack,

return StringPiece::npos;
}
#endif // FOLLY_HAVE_EMMINTRIN_H

size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
const StringPiece& needles) {
Expand Down
12 changes: 10 additions & 2 deletions folly/Range.h
Expand Up @@ -578,10 +578,11 @@ size_t qfind(const Range<T>& haystack,

namespace detail {

size_t qfind_first_byte_of_sse42(const StringPiece& haystack,
size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
const StringPiece& needles);

size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
#if FOLLY_HAVE_EMMINTRIN_H
size_t qfind_first_byte_of_sse42(const StringPiece& haystack,
const StringPiece& needles);

inline size_t qfind_first_byte_of(const StringPiece& haystack,
Expand All @@ -592,6 +593,13 @@ inline size_t qfind_first_byte_of(const StringPiece& haystack,
return qfind_first_byte_of_fn(haystack, needles);
}

#else
inline size_t qfind_first_byte_of(const StringPiece& haystack,
const StringPiece& needles) {
return qfind_first_byte_of_nosse(haystack, needles);
}
#endif // FOLLY_HAVE_EMMINTRIN_H

} // namespace detail

template <class T, class Comp>
Expand Down
2 changes: 1 addition & 1 deletion folly/configure.ac
Expand Up @@ -37,7 +37,7 @@ AX_BOOST_SYSTEM

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([fcntl.h inttypes.h limits.h stdint.h stdlib.h string.h sys/time.h unistd.h mutex.h features.h malloc.h])
AC_CHECK_HEADERS([fcntl.h inttypes.h limits.h stdint.h stdlib.h string.h sys/time.h unistd.h mutex.h features.h malloc.h emmintrin.h])

AC_CHECK_HEADER(double-conversion.h, [], [AC_MSG_ERROR(
[Couldn't find double-conversion.h, please download from \
Expand Down

0 comments on commit ae95c1d

Please sign in to comment.