FreeRDP
TestPrimitivesAndOr.c
1 /* test_andor.c
2  * vi:ts=4 sw=4
3  *
4  * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License. You may obtain
7  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 #include <freerdp/config.h>
16 
17 #include <winpr/sysinfo.h>
18 
19 #include "prim_test.h"
20 
21 #define FUNC_TEST_SIZE 65536
22 
23 #define VALUE (0xA5A5A5A5U)
24 
25 /* ========================================================================= */
26 static BOOL test_and_32u_impl(const char* name, __andC_32u_t fkt, const UINT32* src,
27  const UINT32 val, UINT32* dst, size_t size)
28 {
29  pstatus_t status = fkt(src, val, dst, size);
30  if (status != PRIMITIVES_SUCCESS)
31  return FALSE;
32 
33  for (size_t i = 0; i < size; ++i)
34  {
35  if (dst[i] != (src[i] & val))
36  {
37 
38  printf("AND %s FAIL[%" PRIuz "] 0x%08" PRIx32 "&0x%08" PRIx32 "=0x%08" PRIx32
39  ", got 0x%08" PRIx32 "\n",
40  name, i, src[i], val, (src[i] & val), dst[i]);
41 
42  return FALSE;
43  }
44  }
45 
46  return TRUE;
47 }
48 
49 static BOOL test_and_32u_func(void)
50 {
51  UINT32 ALIGN(src[FUNC_TEST_SIZE + 3]) = { 0 };
52  UINT32 ALIGN(dst[FUNC_TEST_SIZE + 3]) = { 0 };
53 
54  winpr_RAND(src, sizeof(src));
55 
56  if (!test_and_32u_impl("generic->andC_32u aligned", generic->andC_32u, src + 1, VALUE, dst + 1,
57  FUNC_TEST_SIZE))
58  return FALSE;
59  if (!test_and_32u_impl("generic->andC_32u unaligned", generic->andC_32u, src + 1, VALUE,
60  dst + 2, FUNC_TEST_SIZE))
61  return FALSE;
62  if (!test_and_32u_impl("optimized->andC_32u aligned", optimized->andC_32u, src + 1, VALUE,
63  dst + 1, FUNC_TEST_SIZE))
64  return FALSE;
65  if (!test_and_32u_impl("optimized->andC_32u unaligned", optimized->andC_32u, src + 1, VALUE,
66  dst + 2, FUNC_TEST_SIZE))
67  return FALSE;
68 
69  return TRUE;
70 }
71 
72 /* ------------------------------------------------------------------------- */
73 static BOOL test_and_32u_speed(void)
74 {
75  UINT32 ALIGN(src[MAX_TEST_SIZE + 3]) = { 0 };
76  UINT32 ALIGN(dst[MAX_TEST_SIZE + 3]) = { 0 };
77 
78  winpr_RAND(src, sizeof(src));
79 
80  if (!speed_test("andC_32u", "aligned", g_Iterations, (speed_test_fkt)generic->andC_32u,
81  (speed_test_fkt)optimized->andC_32u, src + 1, VALUE, dst + 1, MAX_TEST_SIZE))
82  return FALSE;
83  if (!speed_test("andC_32u", "unaligned", g_Iterations, (speed_test_fkt)generic->andC_32u,
84  (speed_test_fkt)optimized->andC_32u, src + 1, VALUE, dst + 2, MAX_TEST_SIZE))
85  return FALSE;
86 
87  return TRUE;
88 }
89 
90 /* ========================================================================= */
91 static BOOL check(const UINT32* src, const UINT32* dst, UINT32 size, UINT32 value)
92 {
93  for (UINT32 i = 0; i < size; ++i)
94  {
95  if (dst[i] != (src[i] | value))
96  {
97  printf("OR-general general FAIL[%" PRIu32 "] 0x%08" PRIx32 "&0x%08" PRIx32
98  "=0x%08" PRIx32 ", got 0x%08" PRIx32 "\n",
99  i, src[i], value, src[i] | value, dst[i]);
100  return FALSE;
101  }
102  }
103 
104  return TRUE;
105 }
106 
107 static BOOL test_or_32u_func(void)
108 {
109  pstatus_t status = 0;
110  UINT32 ALIGN(src[FUNC_TEST_SIZE + 3]) = { 0 };
111  UINT32 ALIGN(dst[FUNC_TEST_SIZE + 3]) = { 0 };
112 
113  winpr_RAND(src, sizeof(src));
114 
115  status = generic->orC_32u(src + 1, VALUE, dst + 1, FUNC_TEST_SIZE);
116  if (status != PRIMITIVES_SUCCESS)
117  return FALSE;
118 
119  if (!check(src + 1, dst + 1, FUNC_TEST_SIZE, VALUE))
120  return FALSE;
121 
122  status = optimized->orC_32u(src + 1, VALUE, dst + 1, FUNC_TEST_SIZE);
123  if (status != PRIMITIVES_SUCCESS)
124  return FALSE;
125 
126  if (!check(src + 1, dst + 1, FUNC_TEST_SIZE, VALUE))
127  return FALSE;
128 
129  return TRUE;
130 }
131 
132 /* ------------------------------------------------------------------------- */
133 static BOOL test_or_32u_speed(void)
134 {
135  UINT32 ALIGN(src[FUNC_TEST_SIZE + 3]) = { 0 };
136  UINT32 ALIGN(dst[FUNC_TEST_SIZE + 3]) = { 0 };
137 
138  winpr_RAND(src, sizeof(src));
139 
140  if (!speed_test("add16s", "aligned", g_Iterations, (speed_test_fkt)generic->orC_32u,
141  (speed_test_fkt)optimized->orC_32u, src + 1, VALUE, dst + 1, FUNC_TEST_SIZE))
142  return FALSE;
143 
144  return TRUE;
145 }
146 
147 int TestPrimitivesAndOr(int argc, char* argv[])
148 {
149  WINPR_UNUSED(argc);
150  WINPR_UNUSED(argv);
151 
152  prim_test_setup(FALSE);
153 
154  if (!test_and_32u_func())
155  return -1;
156 
157  if (!test_or_32u_func())
158  return -1;
159 
160  if (g_TestPrimitivesPerformance)
161  {
162  if (!test_and_32u_speed())
163  return -1;
164  if (!test_or_32u_speed())
165  return -1;
166  }
167 
168  return 0;
169 }