FreeRDP
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 /* ========================================================================= */
28 static pstatus_t general_set_8u(BYTE val, BYTE* pDst, UINT32 len)
29 {
30  memset((void*)pDst, (int)val, (size_t)len);
31  return PRIMITIVES_SUCCESS;
32 }
33 
34 /* ------------------------------------------------------------------------- */
35 static pstatus_t general_zero(void* pDst, size_t len)
36 {
37  memset(pDst, 0, len);
38  return PRIMITIVES_SUCCESS;
39 }
40 
41 /* ========================================================================= */
42 static pstatus_t general_set_32s(INT32 val, INT32* pDst, UINT32 len)
43 {
44  INT32* dptr = pDst;
45  size_t span = 0;
46  size_t remaining = 0;
47  primitives_t* prims = NULL;
48 
49  if (len < 256)
50  {
51  while (len--)
52  *dptr++ = val;
53 
54  return PRIMITIVES_SUCCESS;
55  }
56 
57  /* else quadratic growth memcpy algorithm */
58  span = 1;
59  *dptr = val;
60  remaining = len - 1;
61  prims = primitives_get();
62 
63  while (remaining)
64  {
65  size_t thiswidth = span;
66 
67  if (thiswidth > remaining)
68  thiswidth = remaining;
69 
70  const size_t s = thiswidth << 2;
71  WINPR_ASSERT(thiswidth <= INT32_MAX);
72  prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), (INT32)s);
73  remaining -= thiswidth;
74  span <<= 1;
75  }
76 
77  return PRIMITIVES_SUCCESS;
78 }
79 
80 /* ------------------------------------------------------------------------- */
81 static pstatus_t general_set_32u(UINT32 val, UINT32* pDst, UINT32 len)
82 {
83  UINT32* dptr = pDst;
84  size_t span = 0;
85  size_t remaining = 0;
86  primitives_t* prims = NULL;
87 
88  if (len < 256)
89  {
90  while (len--)
91  *dptr++ = val;
92 
93  return PRIMITIVES_SUCCESS;
94  }
95 
96  /* else quadratic growth memcpy algorithm */
97  span = 1;
98  *dptr = val;
99  remaining = len - 1;
100  prims = primitives_get();
101 
102  while (remaining)
103  {
104  size_t thiswidth = span;
105 
106  if (thiswidth > remaining)
107  thiswidth = remaining;
108 
109  const size_t s = thiswidth << 2;
110  WINPR_ASSERT(thiswidth <= INT32_MAX);
111  prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), (INT32)s);
112  remaining -= thiswidth;
113  span <<= 1;
114  }
115 
116  return PRIMITIVES_SUCCESS;
117 }
118 
119 /* ------------------------------------------------------------------------- */
120 void primitives_init_set(primitives_t* WINPR_RESTRICT prims)
121 {
122  /* Start with the default. */
123  prims->set_8u = general_set_8u;
124  prims->set_32s = general_set_32s;
125  prims->set_32u = general_set_32u;
126  prims->zero = general_zero;
127 }
128 
129 void primitives_init_set_opt(primitives_t* WINPR_RESTRICT prims)
130 {
131  primitives_init_set_sse2(prims);
132 }