FreeRDP
Loading...
Searching...
No Matches
prim_shift.c
1/* FreeRDP: A Remote Desktop Protocol Client
2 * Shift 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#include <freerdp/config.h>
17#include <winpr/assert.h>
18#include <winpr/cast.h>
19
20#include <freerdp/types.h>
21#include <freerdp/primitives.h>
22
23#include "prim_internal.h"
24#include "prim_shift.h"
25
26/* ------------------------------------------------------------------------- */
27static INLINE INT16 shift(INT16 val, UINT32 sh)
28{
29 const INT16 rc = (int16_t)(((UINT32)val << sh) & 0xFFFF);
30 return WINPR_ASSERTING_INT_CAST(INT16, rc);
31}
32
33static INLINE pstatus_t general_lShiftC_16s_inplace(INT16* WINPR_RESTRICT pSrcDst, UINT32 val,
34 UINT32 len)
35{
36 if (val == 0)
37 return PRIMITIVES_SUCCESS;
38 if (val >= 16)
39 return -1;
40
41 for (UINT32 x = 0; x < len; x++)
42 pSrcDst[x] = shift(pSrcDst[x], val);
43
44 return PRIMITIVES_SUCCESS;
45}
46
47static INLINE pstatus_t general_lShiftC_16s(const INT16* WINPR_RESTRICT pSrc, UINT32 val,
48 INT16* WINPR_RESTRICT pDst, UINT32 len)
49{
50 if (val == 0)
51 return PRIMITIVES_SUCCESS;
52 if (val >= 16)
53 return -1;
54
55 for (UINT32 x = 0; x < len; x++)
56 pDst[x] = shift(pSrc[x], val);
57
58 return PRIMITIVES_SUCCESS;
59}
60
61/* ------------------------------------------------------------------------- */
62static INLINE pstatus_t general_rShiftC_16s(const INT16* WINPR_RESTRICT pSrc, UINT32 val,
63 INT16* WINPR_RESTRICT pDst, UINT32 len)
64{
65 if (val == 0)
66 return PRIMITIVES_SUCCESS;
67 if (val >= 16)
68 return -1;
69
70 for (UINT32 x = 0; x < len; x++)
71 pDst[x] = WINPR_ASSERTING_INT_CAST(int16_t, pSrc[x] >> val);
72
73 return PRIMITIVES_SUCCESS;
74}
75
76/* ------------------------------------------------------------------------- */
77static INLINE pstatus_t general_lShiftC_16u(const UINT16* WINPR_RESTRICT pSrc, UINT32 val,
78 UINT16* WINPR_RESTRICT pDst, UINT32 len)
79{
80 if (val == 0)
81 return PRIMITIVES_SUCCESS;
82 if (val >= 16)
83 return -1;
84
85 for (UINT32 x = 0; x < len; x++)
86 pDst[x] = WINPR_ASSERTING_INT_CAST(UINT16, ((pSrc[x] << val) & 0xFFFF));
87
88 return PRIMITIVES_SUCCESS;
89}
90
91/* ------------------------------------------------------------------------- */
92static INLINE pstatus_t general_rShiftC_16u(const UINT16* WINPR_RESTRICT pSrc, UINT32 val,
93 UINT16* WINPR_RESTRICT pDst, UINT32 len)
94{
95 if (val == 0)
96 return PRIMITIVES_SUCCESS;
97 if (val >= 16)
98 return -1;
99
100 for (UINT32 x = 0; x < len; x++)
101 pDst[x] = pSrc[x] >> val;
102
103 return PRIMITIVES_SUCCESS;
104}
105
106/* ------------------------------------------------------------------------- */
107static INLINE pstatus_t general_shiftC_16s(const INT16* WINPR_RESTRICT pSrc, INT32 val,
108 INT16* WINPR_RESTRICT pDst, UINT32 len)
109{
110 if (val == 0)
111 return PRIMITIVES_SUCCESS;
112
113 if (val < 0)
114 return general_rShiftC_16s(pSrc, WINPR_ASSERTING_INT_CAST(UINT32, -val), pDst, len);
115 else
116 return general_lShiftC_16s(pSrc, WINPR_ASSERTING_INT_CAST(UINT32, val), pDst, len);
117}
118
119/* ------------------------------------------------------------------------- */
120static INLINE pstatus_t general_shiftC_16u(const UINT16* WINPR_RESTRICT pSrc, INT32 val,
121 UINT16* WINPR_RESTRICT pDst, UINT32 len)
122{
123 if (val == 0)
124 return PRIMITIVES_SUCCESS;
125
126 if (val < 0)
127 return general_rShiftC_16u(pSrc, WINPR_ASSERTING_INT_CAST(UINT32, -val), pDst, len);
128 else
129 return general_lShiftC_16u(pSrc, WINPR_ASSERTING_INT_CAST(UINT32, val), pDst, len);
130}
131
132/* ------------------------------------------------------------------------- */
133void primitives_init_shift(primitives_t* WINPR_RESTRICT prims)
134{
135 /* Start with the default. */
136 prims->lShiftC_16s_inplace = general_lShiftC_16s_inplace;
137 prims->lShiftC_16s = general_lShiftC_16s;
138 prims->rShiftC_16s = general_rShiftC_16s;
139 prims->lShiftC_16u = general_lShiftC_16u;
140 prims->rShiftC_16u = general_rShiftC_16u;
141 /* Wrappers */
142 prims->shiftC_16s = general_shiftC_16s;
143 prims->shiftC_16u = general_shiftC_16u;
144}
145
146void primitives_init_shift_opt(primitives_t* WINPR_RESTRICT prims)
147{
148 primitives_init_shift(prims);
149 primitives_init_shift_sse3(prims);
150}