Skip to content

Commit

Permalink
Fix build without SSE 4.2
Browse files Browse the repository at this point in the history
Summary: The addition of base64 broke the builds.

Differential Revision: D38018543

fbshipit-source-id: adfee514e235ec23db4a7b3a889fd0c1c8e40343
  • Loading branch information
Orvid authored and facebook-github-bot committed Jul 22, 2022
1 parent a07bb27 commit 437b29d
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 69 deletions.
44 changes: 24 additions & 20 deletions folly/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string_view>
#include <vector>
#include <folly/CPortability.h>
#include <folly/Portability.h>
#include <folly/detail/base64_detail/Base64Api.h>
#include <folly/detail/base64_detail/Base64Common.h>
#include <folly/lang/Exception.h>
Expand Down Expand Up @@ -78,8 +79,10 @@ inline auto base64URLDecode(std::string_view s) -> std::string;
constexpr std::size_t base64EncodedSize(std::size_t inSize) noexcept;
constexpr std::size_t base64URLEncodedSize(std::size_t inSize) noexcept;

constexpr char* base64Encode(const char* f, const char* l, char* o) noexcept;
constexpr char* base64URLEncode(const char* f, const char* l, char* o) noexcept;
inline FOLLY_CXX20_CONSTEXPR char* base64Encode(
const char* f, const char* l, char* o) noexcept;
inline FOLLY_CXX20_CONSTEXPR char* base64URLEncode(
const char* f, const char* l, char* o) noexcept;

constexpr std::size_t base64DecodedSize(const char* f, const char* l) noexcept;
constexpr std::size_t base64DecodedSize(std::string_view s) noexcept;
Expand All @@ -93,15 +96,15 @@ struct base64_decode_result {
char* o;
};

constexpr base64_decode_result base64Decode(
const char* f, const char* l, char* o) noexcept;
constexpr base64_decode_result base64Decode(
std::string_view s, char* o) noexcept;
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64Decode(const char* f, const char* l, char* o) noexcept;
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64Decode(std::string_view s, char* o) noexcept;

constexpr base64_decode_result base64URLDecode(
const char* f, const char* l, char* o) noexcept;
constexpr base64_decode_result base64URLDecode(
std::string_view s, char* o) noexcept;
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64URLDecode(const char* f, const char* l, char* o) noexcept;
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64URLDecode(std::string_view s, char* o) noexcept;

// -----------------------------------------------------------------
// implementation
Expand All @@ -118,11 +121,12 @@ constexpr std::size_t base64URLEncodedSize(std::size_t inSize) noexcept {
return detail::base64_detail::base64URLEncodedSize(inSize);
}

constexpr char* base64Encode(const char* f, const char* l, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR char* base64Encode(
const char* f, const char* l, char* o) noexcept {
return detail::base64_detail::base64Encode(f, l, o);
}

constexpr char* base64URLEncode(
inline FOLLY_CXX20_CONSTEXPR char* base64URLEncode(
const char* f, const char* l, char* o) noexcept {
return detail::base64_detail::base64URLEncode(f, l, o);
}
Expand Down Expand Up @@ -160,25 +164,25 @@ constexpr std::size_t base64URLDecodedSize(std::string_view s) noexcept {
return folly::base64URLDecodedSize(s.data(), s.data() + s.size());
}

constexpr base64_decode_result base64Decode(
const char* f, const char* l, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64Decode(const char* f, const char* l, char* o) noexcept {
auto detailResult = folly::detail::base64_detail::base64Decode(f, l, o);
return {detailResult.isSuccess, detailResult.o};
}

constexpr base64_decode_result base64Decode(
std::string_view s, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64Decode(std::string_view s, char* o) noexcept {
return folly::base64Decode(s.data(), s.data() + s.size(), o);
}

constexpr base64_decode_result base64URLDecode(
const char* f, const char* l, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64URLDecode(const char* f, const char* l, char* o) noexcept {
auto detailResult = detail::base64_detail::base64URLDecode(f, l, o);
return {detailResult.isSuccess, detailResult.o};
}

constexpr base64_decode_result base64URLDecode(
std::string_view s, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR base64_decode_result
base64URLDecode(std::string_view s, char* o) noexcept {
return folly::base64URLDecode(s.data(), s.data() + s.size(), o);
}

Expand Down
18 changes: 2 additions & 16 deletions folly/detail/base64_detail/Base64Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,23 @@
#include <folly/Portability.h>
#include <folly/detail/base64_detail/Base64Api.h>
#include <folly/detail/base64_detail/Base64SWAR.h>

#if FOLLY_X64
#include <folly/detail/base64_detail/Base64_SSE4_2.h>
#endif

namespace folly::detail::base64_detail {

#if FOLLY_X64
Base64RuntimeImpl base64EncodeSelectImplementation() {
#if FOLLY_SSE_PREREQ(4, 2)
if (folly::CpuId().sse42()) {
return {
base64Encode_SSE4_2,
base64URLEncode_SSE4_2,
base64Decode_SSE4_2,
base64URLDecodeSWAR};
} else {
return {
base64EncodeScalar,
base64URLEncode,
base64DecodeSWAR,
base64URLDecodeSWAR};
}
}
#else // FOLLY_X64
Base64RuntimeImpl base64EncodeSelectImplementation() {
#endif
return {
base64EncodeScalar,
base64URLEncode,
base64DecodeSWAR,
base64URLDecodeSWAR};
}
#endif

} // namespace folly::detail::base64_detail
14 changes: 8 additions & 6 deletions folly/detail/base64_detail/Base64Api.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <folly/Portability.h>
#include <folly/detail/base64_detail/Base64Common.h>
#include <folly/detail/base64_detail/Base64Scalar.h>

Expand Down Expand Up @@ -48,15 +49,16 @@ inline const auto& base64EncodeRuntime() {
#endif
#endif

constexpr char* base64Encode(const char* f, const char* l, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR char* base64Encode(
const char* f, const char* l, char* o) noexcept {
if (BASE64_IS_CONSTANT_EVALUATED) {
return base64EncodeScalar(f, l, o);
} else {
return base64EncodeRuntime().encode(f, l, o);
}
}

constexpr char* base64URLEncode(
inline FOLLY_CXX20_CONSTEXPR char* base64URLEncode(
const char* f, const char* l, char* o) noexcept {
if (BASE64_IS_CONSTANT_EVALUATED) {
return base64URLEncodeScalar(f, l, o);
Expand All @@ -65,17 +67,17 @@ constexpr char* base64URLEncode(
}
}

constexpr Base64DecodeResult base64Decode(
const char* f, const char* l, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR Base64DecodeResult
base64Decode(const char* f, const char* l, char* o) noexcept {
if (BASE64_IS_CONSTANT_EVALUATED) {
return base64DecodeScalar(f, l, o);
} else {
return base64EncodeRuntime().decode(f, l, o);
}
}

constexpr Base64DecodeResult base64URLDecode(
const char* f, const char* l, char* o) noexcept {
inline FOLLY_CXX20_CONSTEXPR Base64DecodeResult
base64URLDecode(const char* f, const char* l, char* o) noexcept {
if (BASE64_IS_CONSTANT_EVALUATED) {
return base64URLDecodeScalar(f, l, o);
} else {
Expand Down
6 changes: 4 additions & 2 deletions folly/detail/base64_detail/Base64_SSE4_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
* limitations under the License.
*/

#if defined(__SSE4_2__)
#include <folly/detail/base64_detail/Base64_SSE4_2.h>

#include <folly/Portability.h>
#include <folly/detail/base64_detail/Base64Simd.h>
#include <folly/detail/base64_detail/Base64_SSE4_2.h>
#include <folly/detail/base64_detail/Base64_SSE4_2_Platform.h>

#if FOLLY_SSE_PREREQ(4, 2)

namespace folly::detail::base64_detail {

char* base64Encode_SSE4_2(const char* f, const char* l, char* o) noexcept {
Expand Down
3 changes: 3 additions & 0 deletions folly/detail/base64_detail/Base64_SSE4_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#pragma once

#include <cstdint>
#include <folly/Portability.h>
#include <folly/detail/base64_detail/Base64Common.h>

#if FOLLY_SSE_PREREQ(4, 2)
namespace folly::detail::base64_detail {

char* base64Encode_SSE4_2(const char* f, const char* l, char* o) noexcept;
Expand All @@ -28,3 +30,4 @@ Base64DecodeResult base64Decode_SSE4_2(
const char* f, const char* l, char* o) noexcept;

} // namespace folly::detail::base64_detail
#endif
8 changes: 5 additions & 3 deletions folly/detail/base64_detail/Base64_SSE4_2_Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

#pragma once

#if defined(__SSE4_2__)
#include <immintrin.h>
#include <cstdint>
#include <folly/Portability.h>
#include <folly/detail/base64_detail/Base64HiddenConstants.h>

#if FOLLY_SSE_PREREQ(4, 2)
#include <immintrin.h>

namespace folly::detail::base64_detail {

/*
Expand Down Expand Up @@ -162,4 +164,4 @@ struct Base64_SSE4_2_Platform {

} // namespace folly::detail::base64_detail

#endif // defined(__SSE4_2__)
#endif // FOLLY_SSE_PREREQ(4, 2)
11 changes: 4 additions & 7 deletions folly/detail/base64_detail/tests/Base64AgainstScalarTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
#include <folly/detail/base64_detail/Base64Common.h>
#include <folly/detail/base64_detail/Base64SWAR.h>
#include <folly/detail/base64_detail/Base64Scalar.h>
#include <folly/portability/GTest.h>

#if defined(__SSE4_2__)
#include <folly/detail/base64_detail/Base64_SSE4_2.h>
#endif // defined(__SSE4_2__)
#include <folly/portability/GTest.h>

namespace folly::detail::base64_detail {
namespace {
Expand Down Expand Up @@ -73,22 +70,22 @@ auto callDecode(std::string_view encoded, Decode decode)

constexpr Encode kEncodes[] = {
base64EncodeScalar,
#ifdef __SSE4_2__
#if FOLLY_SSE_PREREQ(4, 2)
base64Encode_SSE4_2,
#endif
};

constexpr Encode kEncodesURL[] = {
base64URLEncodeScalar,
#ifdef __SSE4_2__
#if FOLLY_SSE_PREREQ(4, 2)
base64URLEncode_SSE4_2,
#endif
};

constexpr Decode kDecodes[] = {
base64DecodeScalar,
base64DecodeSWAR,
#ifdef __SSE4_2__
#if FOLLY_SSE_PREREQ(4, 2)
base64Decode_SSE4_2,
#endif
};
Expand Down
12 changes: 3 additions & 9 deletions folly/detail/base64_detail/tests/Base64PlatformTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@
#include <vector>
#include <folly/portability/GTest.h>

#if defined(__SSE4_2__)
#include <folly/detail/base64_detail/Base64_SSE4_2_Platform.h>
#endif // defined(__SSE4_2__)

namespace folly::detail::base64_detail {
namespace {

#if defined(__SSE4_2__)
using PlatformsToTest = ::testing::Types<Base64_SSE4_2_Platform>;
#endif // defined(__SSE4_2__)
#if FOLLY_SSE_PREREQ(4, 2)

constexpr std::array<std::uint8_t, 16> expectedEncodeToIndexes(
std::array<std::uint8_t, 16> in) {
Expand Down Expand Up @@ -164,8 +159,7 @@ struct Base64PlatformTest : ::testing::Test {
}
};

#if defined(__SSE4_2__)
TYPED_TEST_SUITE(Base64PlatformTest, PlatformsToTest);
TYPED_TEST_SUITE(Base64PlatformTest, ::testing::Types<Base64_SSE4_2_Platform>);

TYPED_TEST(Base64PlatformTest, EncodeToIndexes) {
using RegBytes = typename TestFixture::RegBytes;
Expand Down Expand Up @@ -273,7 +267,7 @@ TYPED_TEST(Base64PlatformTest, packIndexesToBytes) {
ASSERT_EQ(expected, actual);
}
}
#endif // defined(__SSE4_2__)
#endif // FOLLY_SSE_PREREQ(4, 2)

} // namespace
} // namespace folly::detail::base64_detail
9 changes: 3 additions & 6 deletions folly/detail/base64_detail/tests/Base64SpecialCasesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
#include <type_traits>
#include <folly/detail/base64_detail/Base64Scalar.h>
#include <folly/detail/base64_detail/Base64Simd.h>
#include <folly/portability/GTest.h>

#if defined(__SSE4_2__)
#include <folly/detail/base64_detail/Base64_SSE4_2.h>
#endif // defined(__SSE4_2__)
#include <folly/portability/GTest.h>

namespace folly::detail::base64_detail {
namespace {
Expand Down Expand Up @@ -376,7 +373,7 @@ TEST(Base64, SpecialCases) {
base64URLEncodeScalar,
base64DecodeSWAR,
base64URLDecodeSWAR}));
#ifdef __SSE4_2__
#if FOLLY_SSE_PREREQ(4, 2)
ASSERT_TRUE(runEncodeTests(SimdTester{
base64Encode_SSE4_2,
base64URLEncode_SSE4_2,
Expand Down Expand Up @@ -514,7 +511,7 @@ TEST(Base64, DecodingErrorDeteciton) {
ASSERT_TRUE(decodingErrorDectionTest<true>(base64URLDecodeScalar));
ASSERT_TRUE(decodingErrorDectionTest<false>(base64DecodeSWAR));
ASSERT_TRUE(decodingErrorDectionTest<true>(base64URLDecodeSWAR));
#ifdef __SSE4_2__
#if FOLLY_SSE_PREREQ(4, 2)
ASSERT_TRUE(decodingErrorDectionTest<false>(base64Decode_SSE4_2));
#endif
}
Expand Down

0 comments on commit 437b29d

Please sign in to comment.