FreeRDP
Loading...
Searching...
No Matches
prim_set.c
1/* FreeRDP: A Remote Desktop Protocol Client
2 * Routines to set a chunk of memory to a constant.
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 <string.h>
20
21#include <freerdp/types.h>
22#include <freerdp/primitives.h>
23
24#include "prim_internal.h"
25#include "prim_set.h"
26
27/* ========================================================================= */
28static pstatus_t general_set_8u(BYTE val, BYTE* WINPR_RESTRICT pDst, UINT32 len)
29{
30 memset((void*)pDst, (int)val, (size_t)len);
31 return PRIMITIVES_SUCCESS;
32}
33
34/* ------------------------------------------------------------------------- */
35static pstatus_t general_zero(void* WINPR_RESTRICT pDst, size_t len)
36{
37 memset(pDst, 0, len);
38 return PRIMITIVES_SUCCESS;
39}
40
41/* ========================================================================= */
42static pstatus_t general_set_32s(INT32 val, INT32* WINPR_RESTRICT pDst, UINT32 len)
43{
44 INT32* dptr = pDst;
45 size_t span = 0;
46 size_t remaining = 0;
47
48 if (len < 256)
49 {
50 while (len--)
51 *dptr++ = val;
52
53 return PRIMITIVES_SUCCESS;
54 }
55
56 /* else quadratic growth memcpy algorithm */
57 span = 1;
58 *dptr = val;
59 remaining = len - 1;
60 primitives_t* prims = primitives_get();
61
62 while (remaining)
63 {
64 size_t thiswidth = span;
65
66 if (thiswidth > remaining)
67 thiswidth = remaining;
68
69 const size_t s = thiswidth << 2;
70 WINPR_ASSERT(thiswidth <= INT32_MAX);
71 const pstatus_t rc = prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), (INT32)s);
72 if (rc != PRIMITIVES_SUCCESS)
73 return rc;
74 remaining -= thiswidth;
75 span <<= 1;
76 }
77
78 return PRIMITIVES_SUCCESS;
79}
80
81/* ------------------------------------------------------------------------- */
82static pstatus_t general_set_32u(UINT32 val, UINT32* WINPR_RESTRICT pDst, UINT32 len)
83{
84 UINT32* dptr = pDst;
85 size_t span = 0;
86 size_t remaining = 0;
87 primitives_t* prims = nullptr;
88
89 if (len < 256)
90 {
91 while (len--)
92 *dptr++ = val;
93
94 return PRIMITIVES_SUCCESS;
95 }
96
97 /* else quadratic growth memcpy algorithm */
98 span = 1;
99 *dptr = val;
100 remaining = len - 1;
101 prims = primitives_get();
102
103 while (remaining)
104 {
105 size_t thiswidth = span;
106
107 if (thiswidth > remaining)
108 thiswidth = remaining;
109
110 const size_t s = thiswidth << 2;
111 WINPR_ASSERT(thiswidth <= INT32_MAX);
112 const pstatus_t rc = prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), (INT32)s);
113 if (rc != PRIMITIVES_SUCCESS)
114 return rc;
115
116 remaining -= thiswidth;
117 span <<= 1;
118 }
119
120 return PRIMITIVES_SUCCESS;
121}
122
123/* ------------------------------------------------------------------------- */
124void primitives_init_set(primitives_t* WINPR_RESTRICT prims)
125{
126 /* Start with the default. */
127 prims->set_8u = general_set_8u;
128 prims->set_32s = general_set_32s;
129 prims->set_32u = general_set_32u;
130 prims->zero = general_zero;
131}
132
133void primitives_init_set_opt(primitives_t* WINPR_RESTRICT prims)
134{
135 primitives_init_set(prims);
136 primitives_init_set_sse2(prims);
137}