FreeRDP
Loading...
Searching...
No Matches
prim_add_sse3.c
1/* FreeRDP: A Remote Desktop Protocol Client
2 * Optimized add operations.
3 * vi:ts=4 sw=4:
4 *
5 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may
7 * not use this file except in compliance with the License. You may obtain
8 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 *
15 */
16
17#include <freerdp/config.h>
18
19#include <freerdp/types.h>
20#include <freerdp/primitives.h>
21#include <winpr/sysinfo.h>
22
23#include "prim_add.h"
24
25#include "prim_internal.h"
26#include "prim_templates.h"
27
28#if defined(SSE_AVX_INTRINSICS_ENABLED)
29#include <emmintrin.h>
30#include <pmmintrin.h>
31
32static primitives_t* generic = nullptr;
33
34/* ------------------------------------------------------------------------- */
35SSE3_SSD_ROUTINE(sse3_add_16s, INT16, generic->add_16s, _mm_adds_epi16,
36 generic->add_16s(sptr1++, sptr2++, dptr++, 1))
37
38static pstatus_t sse3_add_16s_inplace(INT16* WINPR_RESTRICT pSrcDst1,
39 INT16* WINPR_RESTRICT pSrcDst2, UINT32 ulen)
40{
41 const int shifts = 2;
42 INT16* dptr1 = pSrcDst1;
43 INT16* dptr2 = pSrcDst2;
44
45 if (ulen < 16) /* pointless if too small */
46 return generic->add_16s_inplace(pSrcDst1, pSrcDst2, ulen);
47
48 UINT32 offBeatMask = (1u << (shifts - 1)) - 1;
49 if ((ULONG_PTR)pSrcDst1 & offBeatMask)
50 {
51 /* Incrementing the pointer skips over 16-byte boundary. */
52 return generic->add_16s_inplace(pSrcDst1, pSrcDst2, ulen);
53 }
54 size_t len = ulen;
55 /* Get to the 16-byte boundary now. */
56 const size_t rem = ((UINT_PTR)dptr1 & 0xf) / sizeof(INT16);
57 if (rem != 0)
58 {
59 const UINT32 add = 16 - (UINT32)rem;
60 pstatus_t status = generic->add_16s_inplace(dptr1, dptr2, add);
61 if (status != PRIMITIVES_SUCCESS)
62 return status;
63 dptr1 += add;
64 dptr2 += add;
65 len -= add;
66 }
67 /* Use 4 128-bit SSE registers. */
68 size_t count = len >> (7 - shifts);
69 len -= count << (7 - shifts);
70 if (((const ULONG_PTR)dptr1 & 0x0f) || ((const ULONG_PTR)dptr2 & 0x0f))
71 {
72 /* Unaligned loads */
73 while (count--)
74 {
75 const __m128i* vsptr1 = WINPR_PACKED_ALIGN_CAST(const __m128i*, dptr1);
76 const __m128i* vsptr2 = WINPR_PACKED_ALIGN_CAST(const __m128i*, dptr2);
77 __m128i* vdptr1 = WINPR_PACKED_ALIGN_CAST(__m128i*, dptr1);
78 __m128i* vdptr2 = WINPR_PACKED_ALIGN_CAST(__m128i*, dptr2);
79
80 __m128i xmm0 = LOAD_SI128(vsptr1++);
81 __m128i xmm1 = LOAD_SI128(vsptr1++);
82 __m128i xmm2 = LOAD_SI128(vsptr1++);
83 __m128i xmm3 = LOAD_SI128(vsptr1++);
84 __m128i xmm4 = LOAD_SI128(vsptr2++);
85 __m128i xmm5 = LOAD_SI128(vsptr2++);
86 __m128i xmm6 = LOAD_SI128(vsptr2++);
87 __m128i xmm7 = LOAD_SI128(vsptr2++);
88
89 xmm0 = _mm_adds_epi16(xmm0, xmm4);
90 xmm1 = _mm_adds_epi16(xmm1, xmm5);
91 xmm2 = _mm_adds_epi16(xmm2, xmm6);
92 xmm3 = _mm_adds_epi16(xmm3, xmm7);
93
94 STORE_SI128(vdptr1++, xmm0);
95 STORE_SI128(vdptr1++, xmm1);
96 STORE_SI128(vdptr1++, xmm2);
97 STORE_SI128(vdptr1++, xmm3);
98
99 STORE_SI128(vdptr2++, xmm0);
100 STORE_SI128(vdptr2++, xmm1);
101 STORE_SI128(vdptr2++, xmm2);
102 STORE_SI128(vdptr2++, xmm3);
103
104 dptr1 = (INT16*)vdptr1;
105 dptr2 = (INT16*)vdptr2;
106 }
107 }
108 else
109 {
110 /* Aligned loads */
111 while (count--)
112 {
113 const __m128i* vsptr1 = WINPR_PACKED_ALIGN_CAST(const __m128i*, dptr1);
114 const __m128i* vsptr2 = WINPR_PACKED_ALIGN_CAST(const __m128i*, dptr2);
115 __m128i* vdptr1 = WINPR_PACKED_ALIGN_CAST(__m128i*, dptr1);
116 __m128i* vdptr2 = WINPR_PACKED_ALIGN_CAST(__m128i*, dptr2);
117
118 __m128i xmm0 = LOAD_SI128(vsptr1++);
119 __m128i xmm1 = LOAD_SI128(vsptr1++);
120 __m128i xmm2 = LOAD_SI128(vsptr1++);
121 __m128i xmm3 = LOAD_SI128(vsptr1++);
122 __m128i xmm4 = LOAD_SI128(vsptr2++);
123 __m128i xmm5 = LOAD_SI128(vsptr2++);
124 __m128i xmm6 = LOAD_SI128(vsptr2++);
125 __m128i xmm7 = LOAD_SI128(vsptr2++);
126
127 xmm0 = _mm_adds_epi16(xmm0, xmm4);
128 xmm1 = _mm_adds_epi16(xmm1, xmm5);
129 xmm2 = _mm_adds_epi16(xmm2, xmm6);
130 xmm3 = _mm_adds_epi16(xmm3, xmm7);
131
132 STORE_SI128(vdptr1++, xmm0);
133 STORE_SI128(vdptr1++, xmm1);
134 STORE_SI128(vdptr1++, xmm2);
135 STORE_SI128(vdptr1++, xmm3);
136
137 STORE_SI128(vdptr2++, xmm0);
138 STORE_SI128(vdptr2++, xmm1);
139 STORE_SI128(vdptr2++, xmm2);
140 STORE_SI128(vdptr2++, xmm3);
141
142 dptr1 = (INT16*)vdptr1;
143 dptr2 = (INT16*)vdptr2;
144 }
145 }
146 /* Use a single 128-bit SSE register. */
147 count = len >> (5 - shifts);
148 len -= count << (5 - shifts);
149 while (count--)
150 {
151 const __m128i* vsptr1 = WINPR_PACKED_ALIGN_CAST(const __m128i*, dptr1);
152 const __m128i* vsptr2 = WINPR_PACKED_ALIGN_CAST(const __m128i*, dptr2);
153 __m128i* vdptr1 = WINPR_PACKED_ALIGN_CAST(__m128i*, dptr1);
154 __m128i* vdptr2 = WINPR_PACKED_ALIGN_CAST(__m128i*, dptr2);
155
156 __m128i xmm0 = LOAD_SI128(vsptr1);
157 __m128i xmm1 = LOAD_SI128(vsptr2);
158
159 xmm0 = _mm_adds_epi16(xmm0, xmm1);
160
161 STORE_SI128(vdptr1++, xmm0);
162 STORE_SI128(vdptr2++, xmm0);
163
164 dptr1 = (INT16*)vdptr1;
165 dptr2 = (INT16*)vdptr2;
166 }
167 /* Finish off the remainder. */
168 if (len > 0)
169 return generic->add_16s_inplace(dptr1, dptr2, WINPR_ASSERTING_INT_CAST(uint32_t, len));
170
171 return PRIMITIVES_SUCCESS;
172}
173#endif
174
175/* ------------------------------------------------------------------------- */
176void primitives_init_add_sse3_int(primitives_t* WINPR_RESTRICT prims)
177{
178#if defined(SSE_AVX_INTRINSICS_ENABLED)
179 generic = primitives_get_generic();
180
181 WLog_VRB(PRIM_TAG, "SSE2/SSE3 optimizations");
182 prims->add_16s = sse3_add_16s;
183 prims->add_16s_inplace = sse3_add_16s_inplace;
184#else
185 WLog_VRB(PRIM_TAG, "undefined WITH_SIMD or SSE3 intrinsics not available");
186 WINPR_UNUSED(prims);
187#endif
188}
WINPR_ATTR_NODISCARD fn_add_16s_inplace_t add_16s_inplace
Do vecotor addition, store result in both input buffers pSrcDst1 = pSrcDst2 = pSrcDst1 + pSrcDst2.
Definition primitives.h:302