Qt 4.8
qimage_ssse3.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qimage.h>
43 #include <private/qimage_p.h>
44 #include <private/qsimd_p.h>
45 
46 #ifdef QT_HAVE_SSSE3
47 
49 
50 // Convert a scanline of RGB888 (src) to RGB32 (dst)
51 // src must be at least len * 3 bytes
52 // dst must be at least len * 4 bytes
53 Q_GUI_EXPORT void QT_FASTCALL qt_convert_rgb888_to_rgb32_ssse3(quint32 *dst, const uchar *src, int len)
54 {
55  quint32 *const end = dst + len;
56 
57  // Prologue, align dst to 16 bytes. The alignment is done on dst because it has 4 store()
58  // for each 3 load() of src.
59  const int offsetToAlignOn16Bytes = (4 - ((reinterpret_cast<quintptr>(dst) >> 2) & 0x3)) & 0x3;
60  const int prologLength = qMin(len, offsetToAlignOn16Bytes);
61 
62  for (int i = 0; i < prologLength; ++i) {
63  *dst++ = qRgb(src[0], src[1], src[2]);
64  src += 3;
65  }
66 
67  // Mask the 4 first colors of the RGB888 vector
68  const __m128i shuffleMask = _mm_set_epi8(0xff, 9, 10, 11, 0xff, 6, 7, 8, 0xff, 3, 4, 5, 0xff, 0, 1, 2);
69 
70  // Mask the 4 last colors of a RGB888 vector with an offset of 1 (so the last 3 bytes are RGB)
71  const __m128i shuffleMaskEnd = _mm_set_epi8(0xff, 13, 14, 15, 0xff, 10, 11, 12, 0xff, 7, 8, 9, 0xff, 4, 5, 6);
72 
73  // Mask to have alpha = 0xff
74  const __m128i alphaMask = _mm_set1_epi32(0xff000000);
75 
76  __m128i *inVectorPtr = (__m128i *)src;
77  __m128i *dstVectorPtr = (__m128i *)dst;
78 
79  const int simdRoundCount = (len - prologLength) / 16; // one iteration in the loop converts 16 pixels
80  for (int i = 0; i < simdRoundCount; ++i) {
81  /*
82  RGB888 has 5 pixels per vector, + 1 byte from the next pixel. The idea here is
83  to load vectors of RGB888 and use palignr to select a vector out of two vectors.
84 
85  After 3 loads of RGB888 and 3 stores of RGB32, we have 4 pixels left in the last
86  vector of RGB888, we can mask it directly to get a last store or RGB32. After that,
87  the first next byte is a R, and we can loop for the next 16 pixels.
88 
89  The conversion itself is done with a byte permutation (pshufb).
90  */
91  __m128i firstSrcVector = _mm_lddqu_si128(inVectorPtr);
92  __m128i outputVector = _mm_shuffle_epi8(firstSrcVector, shuffleMask);
93  _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
94  ++inVectorPtr;
95  ++dstVectorPtr;
96 
97  // There are 4 unused bytes left in srcVector, we need to load the next 16 bytes
98  // and load the next input with palignr
99  __m128i secondSrcVector = _mm_lddqu_si128(inVectorPtr);
100  __m128i srcVector = _mm_alignr_epi8(secondSrcVector, firstSrcVector, 12);
101  outputVector = _mm_shuffle_epi8(srcVector, shuffleMask);
102  _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
103  ++inVectorPtr;
104  ++dstVectorPtr;
105  firstSrcVector = secondSrcVector;
106 
107  // We now have 8 unused bytes left in firstSrcVector
108  secondSrcVector = _mm_lddqu_si128(inVectorPtr);
109  srcVector = _mm_alignr_epi8(secondSrcVector, firstSrcVector, 8);
110  outputVector = _mm_shuffle_epi8(srcVector, shuffleMask);
111  _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
112  ++inVectorPtr;
113  ++dstVectorPtr;
114 
115  // There are now 12 unused bytes in firstSrcVector.
116  // We can mask them directly, almost there.
117  outputVector = _mm_shuffle_epi8(secondSrcVector, shuffleMaskEnd);
118  _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
119  ++dstVectorPtr;
120  }
121  src = (uchar *)inVectorPtr;
122  dst = (quint32 *)dstVectorPtr;
123 
124  while (dst != end) {
125  *dst++ = qRgb(src[0], src[1], src[2]);
126  src += 3;
127  }
128 }
129 
130 void convert_RGB888_to_RGB32_ssse3(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
131 {
134  Q_ASSERT(src->width == dest->width);
135  Q_ASSERT(src->height == dest->height);
136 
137  const uchar *src_data = (uchar *) src->data;
138  quint32 *dest_data = (quint32 *) dest->data;
139 
140  for (int i = 0; i < src->height; ++i) {
141  qt_convert_rgb888_to_rgb32_ssse3(dest_data, src_data, src->width);
142  src_data += src->bytes_per_line;
143  dest_data = (quint32 *)((uchar*)dest_data + dest->bytes_per_line);
144  }
145 }
146 
148 
149 #endif // QT_HAVE_SSSE3
int width
Definition: qimage_p.h:76
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define QT_FASTCALL
Definition: qglobal.h:1161
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int height
Definition: qimage_p.h:77
int bytes_per_line
Definition: qimage_p.h:86
QRgb qRgb(int r, int g, int b)
Returns the ARGB quadruplet (255, {r}, {g}, {b}).
Definition: qrgb.h:69
QImage::Format format
Definition: qimage_p.h:85
unsigned int quint32
Definition: qglobal.h:938
uchar * data
Definition: qimage_p.h:81
static const KeyPair *const end