summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r--src/corelib/tools/qstring.cpp102
1 files changed, 62 insertions, 40 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index a018b81c38..7aaddb9b52 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -223,8 +223,15 @@ void qt_from_latin1(ushort *dst, const char *str, size_t size)
// we're going to read str[offset..offset+15] (16 bytes)
for ( ; str + offset + 15 < e; offset += 16) {
- const __m128i nullMask = _mm_set1_epi32(0);
const __m128i chunk = _mm_loadu_si128((__m128i*)(str + offset)); // load
+#ifdef __AVX2__
+ // zero extend to an YMM register
+ const __m256i extended = _mm256_cvtepu8_epi16(chunk);
+
+ // store
+ _mm256_storeu_si256((__m256i*)(dst + offset), extended);
+#else
+ const __m128i nullMask = _mm_set1_epi32(0);
// unpack the first 8 bytes, padding with zeros
const __m128i firstHalf = _mm_unpacklo_epi8(chunk, nullMask);
@@ -233,6 +240,7 @@ void qt_from_latin1(ushort *dst, const char *str, size_t size)
// unpack the last 8 bytes, padding with zeros
const __m128i secondHalf = _mm_unpackhi_epi8 (chunk, nullMask);
_mm_storeu_si128((__m128i*)(dst + offset + 8), secondHalf); // store
+#endif
}
size = size % 16;
@@ -540,8 +548,20 @@ static int ucstrncmp(const QChar *a, const uchar *c, int l)
// and c[offset..offset+15] (16 bytes)
for ( ; uc + offset + 15 < e; offset += 16) {
// similar to fromLatin1_helper:
- // load Latin 1 data and expand to UTF-16
+ // load 16 bytes of Latin 1 data
__m128i chunk = _mm_loadu_si128((__m128i*)(c + offset));
+
+# ifdef __AVX2__
+ // expand Latin 1 data via zero extension
+ __m256i ldata = _mm256_cvtepu8_epi16(chunk);
+
+ // load UTF-16 data and compare
+ __m256i ucdata = _mm256_loadu_si256((__m256i*)(uc + offset));
+ __m256i result = _mm256_cmpeq_epi16(ldata, ucdata);
+
+ uint mask = ~_mm256_movemask_epi8(result);
+# else
+ // expand via unpacking
__m128i firstHalf = _mm_unpacklo_epi8(chunk, nullmask);
__m128i secondHalf = _mm_unpackhi_epi8(chunk, nullmask);
@@ -552,6 +572,7 @@ static int ucstrncmp(const QChar *a, const uchar *c, int l)
__m128i result2 = _mm_cmpeq_epi16(secondHalf, ucdata2);
uint mask = ~(_mm_movemask_epi8(result1) | _mm_movemask_epi8(result2) << 16);
+# endif
if (mask) {
// found a different character
uint idx = uint(_bit_scan_forward(mask));
@@ -1309,7 +1330,7 @@ const QString::Null QString::null = { };
This constructor is only available if Qt is configured with STL
compatibility enabled.
- \sa fromLatin1(), fromLocal8Bit(), fromUtf8()
+ \sa fromLatin1(), fromLocal8Bit(), fromUtf8(), QByteArray::fromStdString()
*/
/*! \fn QString QString::fromStdWString(const std::wstring &str)
@@ -4090,21 +4111,22 @@ QString QString::right(int n) const
QString QString::mid(int position, int n) const
{
- if (position > d->size)
+ using namespace QtPrivate;
+ switch (QContainerImplHelper::mid(d->size, &position, &n)) {
+ case QContainerImplHelper::Null:
return QString();
- if (position < 0) {
- if (n < 0 || n + position >= d->size)
- return *this;
- if (n + position <= 0)
- return QString();
-
- n += position;
- position = 0;
- } else if (uint(n) > uint(d->size - position))
- n = d->size - position;
- if (position == 0 && n == d->size)
+ case QContainerImplHelper::Empty:
+ {
+ QStringDataPtr empty = { Data::allocate(0) };
+ return QString(empty);
+ }
+ case QContainerImplHelper::Full:
return *this;
- return QString((const QChar*) d->data() + position, n);
+ case QContainerImplHelper::Subset:
+ return QString((const QChar*)d->data() + position, n);
+ }
+ Q_UNREACHABLE();
+ return QString();
}
/*!
@@ -7726,7 +7748,7 @@ bool QString::isRightToLeft() const
If the QString contains non-Latin1 Unicode characters, using this
can lead to loss of information.
- \sa toLatin1(), toUtf8(), toLocal8Bit()
+ \sa toLatin1(), toUtf8(), toLocal8Bit(), QByteArray::toStdString()
*/
/*!
@@ -8794,19 +8816,19 @@ QStringRef QString::rightRef(int n) const
*/
QStringRef QStringRef::mid(int pos, int n) const
{
- if (pos > m_size)
+ using namespace QtPrivate;
+ switch (QContainerImplHelper::mid(m_size, &pos, &n)) {
+ case QContainerImplHelper::Null:
return QStringRef();
- if (pos < 0) {
- if (n < 0 || n + pos >= m_size)
- return QStringRef(m_string, m_position, m_size);
- if (n + pos <= 0)
- return QStringRef();
- n += pos;
- pos = 0;
- } else if (uint(n) > uint(m_size - pos)) {
- n = m_size - pos;
+ case QContainerImplHelper::Empty:
+ return QStringRef(m_string, 0, 0);
+ case QContainerImplHelper::Full:
+ return *this;
+ case QContainerImplHelper::Subset:
+ return QStringRef(m_string, pos + m_position, n);
}
- return QStringRef(m_string, pos + m_position, n);
+ Q_UNREACHABLE();
+ return QStringRef();
}
/*!
@@ -8831,19 +8853,19 @@ QStringRef QStringRef::mid(int pos, int n) const
*/
QStringRef QString::midRef(int position, int n) const
{
- if (position > d->size)
+ using namespace QtPrivate;
+ switch (QContainerImplHelper::mid(d->size, &position, &n)) {
+ case QContainerImplHelper::Null:
return QStringRef();
- if (position < 0) {
- if (n < 0 || n + position >= d->size)
- return QStringRef(this, 0, d->size);
- if (n + position <= 0)
- return QStringRef();
-
- n += position;
- position = 0;
- } else if (uint(n) > uint(d->size - position))
- n = d->size - position;
- return QStringRef(this, position, n);
+ case QContainerImplHelper::Empty:
+ return QStringRef(this, 0, 0);
+ case QContainerImplHelper::Full:
+ return QStringRef(this, 0, d->size);
+ case QContainerImplHelper::Subset:
+ return QStringRef(this, position, n);
+ }
+ Q_UNREACHABLE();
+ return QStringRef();
}
/*!