0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-26 08:42:34 +01:00

ircd: Optimize tolower transform.

This commit is contained in:
Jason Volk 2019-08-05 21:22:38 -07:00
parent eddc5b3b55
commit b5a6af1360

View file

@ -8,6 +8,8 @@
// copyright notice and this permission notice is present in all copies. The // copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file. // full license for this software is available in the LICENSE file.
#include <RB_INC_X86INTRIN_H
ircd::string_view ircd::string_view
ircd::toupper(const mutable_buffer &out, ircd::toupper(const mutable_buffer &out,
const string_view &in) const string_view &in)
@ -21,7 +23,7 @@ noexcept
const auto end const auto end
{ {
std::transform(begin(in), stop, begin(out), [] std::transform(begin(in), stop, begin(out), []
(const auto &c) (auto&& c)
{ {
return std::toupper(c); return std::toupper(c);
}) })
@ -34,6 +36,54 @@ noexcept
}; };
} }
#if defined(HAVE_X86INTRIN_H) && defined(__SSE2__)
ircd::string_view
ircd::tolower(const mutable_buffer &out,
const string_view &in)
noexcept
{
const __m128i *src_
{
reinterpret_cast<const __m128i *>(begin(in))
};
__m128i *dst
{
reinterpret_cast<__m128i *>(begin(out))
};
const auto stop
{
std::next(begin(in), std::min(size(in), size(out)))
};
while(reinterpret_cast<const char *>(src_) + sizeof(__m128i) < stop)
{
const __m128i lit_A1 { _mm_set1_epi8('A' - 1) };
const __m128i lit_Z1 { _mm_set1_epi8('Z' + 1) };
const __m128i addend { _mm_set1_epi8('a' - 'A') };
const __m128i src { _mm_loadu_si128(src_++) };
const __m128i gte_A { _mm_cmpgt_epi8(src, lit_A1) };
const __m128i lte_Z { _mm_cmplt_epi8(src, lit_Z1) };
const __m128i mask { _mm_and_si128(gte_A, lte_Z) };
const __m128i ctrl_mask { _mm_and_si128(mask, addend) };
const __m128i result { _mm_add_epi8(src, ctrl_mask) };
_mm_storeu_si128(dst++, result);
}
const auto end{std::transform
(
reinterpret_cast<const char *>(src_),
stop,
reinterpret_cast<char *>(dst),
::tolower
)};
assert(intptr_t(begin(out)) <= intptr_t(end));
return string_view{begin(out), end};
}
#else
ircd::string_view ircd::string_view
ircd::tolower(const mutable_buffer &out, ircd::tolower(const mutable_buffer &out,
const string_view &in) const string_view &in)
@ -46,11 +96,7 @@ noexcept
const auto end const auto end
{ {
std::transform(begin(in), stop, begin(out), [] std::transform(begin(in), stop, begin(out), ::tolower)
(const auto &c)
{
return std::tolower(c);
})
}; };
assert(intptr_t(begin(out)) <= intptr_t(end)); assert(intptr_t(begin(out)) <= intptr_t(end));
@ -59,6 +105,7 @@ noexcept
data(out), size_t(std::distance(begin(out), end)) data(out), size_t(std::distance(begin(out), end))
}; };
} }
#endif
std::string std::string
ircd::replace(const string_view &s, ircd::replace(const string_view &s,