VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstInlineAsm.cpp@ 25596

Last change on this file since 25596 was 25491, checked in by vboxsync, 15 years ago

IPRT,PDMCritSect: Fixing critsect regression; contains under construction rw deadlock detection code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 45.3 KB
Line 
1/* $Id: tstInlineAsm.cpp 25491 2009-12-18 15:20:48Z vboxsync $ */
2/** @file
3 * IPRT Testcase - inline assembly.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/asm.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37#include <iprt/initterm.h>
38#include <iprt/param.h>
39#include <iprt/thread.h>
40#include <iprt/test.h>
41
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47#define CHECKVAL(val, expect, fmt) \
48 do \
49 { \
50 if ((val) != (expect)) \
51 { \
52 RTTestIErrorInc(); \
53 RTPrintf("%s, %d: " #val ": expected " fmt " got " fmt "\n", __FUNCTION__, __LINE__, (expect), (val)); \
54 } \
55 } while (0)
56
57#define CHECKOP(op, expect, fmt, type) \
58 do \
59 { \
60 type val = op; \
61 if (val != (type)(expect)) \
62 { \
63 RTTestIErrorInc(); \
64 RTPrintf("%s, %d: " #op ": expected " fmt " got " fmt "\n", __FUNCTION__, __LINE__, (type)(expect), val); \
65 } \
66 } while (0)
67
68
69#if !defined(PIC) || !defined(RT_ARCH_X86)
70const char *getCacheAss(unsigned u)
71{
72 if (u == 0)
73 return "res0 ";
74 if (u == 1)
75 return "direct";
76 if (u >= 256)
77 return "???";
78
79 char *pszRet;
80 RTStrAPrintf(&pszRet, "%d way", u); /* intentional leak! */
81 return pszRet;
82}
83
84
85const char *getL2CacheAss(unsigned u)
86{
87 switch (u)
88 {
89 case 0: return "off ";
90 case 1: return "direct";
91 case 2: return "2 way ";
92 case 3: return "res3 ";
93 case 4: return "4 way ";
94 case 5: return "res5 ";
95 case 6: return "8 way ";
96 case 7: return "res7 ";
97 case 8: return "16 way";
98 case 9: return "res9 ";
99 case 10: return "res10 ";
100 case 11: return "res11 ";
101 case 12: return "res12 ";
102 case 13: return "res13 ";
103 case 14: return "res14 ";
104 case 15: return "fully ";
105 default:
106 return "????";
107 }
108}
109
110
111/**
112 * Test and dump all possible info from the CPUID instruction.
113 *
114 * @remark Bits shared with the libc cpuid.c program. This all written by me, so no worries.
115 * @todo transform the dumping into a generic runtime function. We'll need it for logging!
116 */
117void tstASMCpuId(void)
118{
119 unsigned iBit;
120 struct
121 {
122 uint32_t uEBX, uEAX, uEDX, uECX;
123 } s;
124 if (!ASMHasCpuId())
125 {
126 RTPrintf("tstInlineAsm: warning! CPU doesn't support CPUID\n");
127 return;
128 }
129
130 /*
131 * Try the 0 function and use that for checking the ASMCpuId_* variants.
132 */
133 ASMCpuId(0, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
134
135 uint32_t u32 = ASMCpuId_ECX(0);
136 CHECKVAL(u32, s.uECX, "%x");
137
138 u32 = ASMCpuId_EDX(0);
139 CHECKVAL(u32, s.uEDX, "%x");
140
141 uint32_t uECX2 = s.uECX - 1;
142 uint32_t uEDX2 = s.uEDX - 1;
143 ASMCpuId_ECX_EDX(0, &uECX2, &uEDX2);
144
145 CHECKVAL(uECX2, s.uECX, "%x");
146 CHECKVAL(uEDX2, s.uEDX, "%x");
147
148 /*
149 * Done testing, dump the information.
150 */
151 RTPrintf("tstInlineAsm: CPUID Dump\n");
152 ASMCpuId(0, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
153 const uint32_t cFunctions = s.uEAX;
154
155 /* raw dump */
156 RTPrintf("\n"
157 " RAW Standard CPUIDs\n"
158 "Function eax ebx ecx edx\n");
159 for (unsigned iStd = 0; iStd <= cFunctions + 3; iStd++)
160 {
161 ASMCpuId(iStd, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
162 RTPrintf("%08x %08x %08x %08x %08x%s\n",
163 iStd, s.uEAX, s.uEBX, s.uECX, s.uEDX, iStd <= cFunctions ? "" : "*");
164 }
165
166 /*
167 * Understandable output
168 */
169 ASMCpuId(0, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
170 RTPrintf("Name: %.04s%.04s%.04s\n"
171 "Support: 0-%u\n",
172 &s.uEBX, &s.uEDX, &s.uECX, s.uEAX);
173 bool const fIntel = ASMIsIntelCpuEx(s.uEBX, s.uECX, s.uEDX);
174
175 /*
176 * Get Features.
177 */
178 if (cFunctions >= 1)
179 {
180 ASMCpuId(1, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
181 RTPrintf("Family: %#x \tExtended: %#x \tEffective: %#x\n"
182 "Model: %#x \tExtended: %#x \tEffective: %#x\n"
183 "Stepping: %d\n"
184 "APIC ID: %#04x\n"
185 "Logical CPUs: %d\n"
186 "CLFLUSH Size: %d\n"
187 "Brand ID: %#04x\n",
188 (s.uEAX >> 8) & 0xf, (s.uEAX >> 20) & 0x7f, ASMGetCpuFamily(s.uEAX),
189 (s.uEAX >> 4) & 0xf, (s.uEAX >> 16) & 0x0f, ASMGetCpuModel(s.uEAX, fIntel),
190 ASMGetCpuStepping(s.uEAX),
191 (s.uEBX >> 24) & 0xff,
192 (s.uEBX >> 16) & 0xff,
193 (s.uEBX >> 8) & 0xff,
194 (s.uEBX >> 0) & 0xff);
195
196 RTPrintf("Features EDX: ");
197 if (s.uEDX & RT_BIT(0)) RTPrintf(" FPU");
198 if (s.uEDX & RT_BIT(1)) RTPrintf(" VME");
199 if (s.uEDX & RT_BIT(2)) RTPrintf(" DE");
200 if (s.uEDX & RT_BIT(3)) RTPrintf(" PSE");
201 if (s.uEDX & RT_BIT(4)) RTPrintf(" TSC");
202 if (s.uEDX & RT_BIT(5)) RTPrintf(" MSR");
203 if (s.uEDX & RT_BIT(6)) RTPrintf(" PAE");
204 if (s.uEDX & RT_BIT(7)) RTPrintf(" MCE");
205 if (s.uEDX & RT_BIT(8)) RTPrintf(" CX8");
206 if (s.uEDX & RT_BIT(9)) RTPrintf(" APIC");
207 if (s.uEDX & RT_BIT(10)) RTPrintf(" 10");
208 if (s.uEDX & RT_BIT(11)) RTPrintf(" SEP");
209 if (s.uEDX & RT_BIT(12)) RTPrintf(" MTRR");
210 if (s.uEDX & RT_BIT(13)) RTPrintf(" PGE");
211 if (s.uEDX & RT_BIT(14)) RTPrintf(" MCA");
212 if (s.uEDX & RT_BIT(15)) RTPrintf(" CMOV");
213 if (s.uEDX & RT_BIT(16)) RTPrintf(" PAT");
214 if (s.uEDX & RT_BIT(17)) RTPrintf(" PSE36");
215 if (s.uEDX & RT_BIT(18)) RTPrintf(" PSN");
216 if (s.uEDX & RT_BIT(19)) RTPrintf(" CLFSH");
217 if (s.uEDX & RT_BIT(20)) RTPrintf(" 20");
218 if (s.uEDX & RT_BIT(21)) RTPrintf(" DS");
219 if (s.uEDX & RT_BIT(22)) RTPrintf(" ACPI");
220 if (s.uEDX & RT_BIT(23)) RTPrintf(" MMX");
221 if (s.uEDX & RT_BIT(24)) RTPrintf(" FXSR");
222 if (s.uEDX & RT_BIT(25)) RTPrintf(" SSE");
223 if (s.uEDX & RT_BIT(26)) RTPrintf(" SSE2");
224 if (s.uEDX & RT_BIT(27)) RTPrintf(" SS");
225 if (s.uEDX & RT_BIT(28)) RTPrintf(" HTT");
226 if (s.uEDX & RT_BIT(29)) RTPrintf(" 29");
227 if (s.uEDX & RT_BIT(30)) RTPrintf(" 30");
228 if (s.uEDX & RT_BIT(31)) RTPrintf(" 31");
229 RTPrintf("\n");
230
231 /** @todo check intel docs. */
232 RTPrintf("Features ECX: ");
233 if (s.uECX & RT_BIT(0)) RTPrintf(" SSE3");
234 for (iBit = 1; iBit < 13; iBit++)
235 if (s.uECX & RT_BIT(iBit))
236 RTPrintf(" %d", iBit);
237 if (s.uECX & RT_BIT(13)) RTPrintf(" CX16");
238 for (iBit = 14; iBit < 32; iBit++)
239 if (s.uECX & RT_BIT(iBit))
240 RTPrintf(" %d", iBit);
241 RTPrintf("\n");
242 }
243
244 /*
245 * Extended.
246 * Implemented after AMD specs.
247 */
248 /** @todo check out the intel specs. */
249 ASMCpuId(0x80000000, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
250 if (!s.uEAX && !s.uEBX && !s.uECX && !s.uEDX)
251 {
252 RTPrintf("No extended CPUID info? Check the manual on how to detect this...\n");
253 return;
254 }
255 const uint32_t cExtFunctions = s.uEAX | 0x80000000;
256
257 /* raw dump */
258 RTPrintf("\n"
259 " RAW Extended CPUIDs\n"
260 "Function eax ebx ecx edx\n");
261 for (unsigned iExt = 0x80000000; iExt <= cExtFunctions + 3; iExt++)
262 {
263 ASMCpuId(iExt, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
264 RTPrintf("%08x %08x %08x %08x %08x%s\n",
265 iExt, s.uEAX, s.uEBX, s.uECX, s.uEDX, iExt <= cExtFunctions ? "" : "*");
266 }
267
268 /*
269 * Understandable output
270 */
271 ASMCpuId(0x80000000, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
272 RTPrintf("Ext Name: %.4s%.4s%.4s\n"
273 "Ext Supports: 0x80000000-%#010x\n",
274 &s.uEBX, &s.uEDX, &s.uECX, s.uEAX);
275
276 if (cExtFunctions >= 0x80000001)
277 {
278 ASMCpuId(0x80000001, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
279 RTPrintf("Family: %#x \tExtended: %#x \tEffective: %#x\n"
280 "Model: %#x \tExtended: %#x \tEffective: %#x\n"
281 "Stepping: %d\n"
282 "Brand ID: %#05x\n",
283 (s.uEAX >> 8) & 0xf, (s.uEAX >> 20) & 0x7f, ASMGetCpuFamily(s.uEAX),
284 (s.uEAX >> 4) & 0xf, (s.uEAX >> 16) & 0x0f, ASMGetCpuModel(s.uEAX, fIntel),
285 ASMGetCpuStepping(s.uEAX),
286 s.uEBX & 0xfff);
287
288 RTPrintf("Features EDX: ");
289 if (s.uEDX & RT_BIT(0)) RTPrintf(" FPU");
290 if (s.uEDX & RT_BIT(1)) RTPrintf(" VME");
291 if (s.uEDX & RT_BIT(2)) RTPrintf(" DE");
292 if (s.uEDX & RT_BIT(3)) RTPrintf(" PSE");
293 if (s.uEDX & RT_BIT(4)) RTPrintf(" TSC");
294 if (s.uEDX & RT_BIT(5)) RTPrintf(" MSR");
295 if (s.uEDX & RT_BIT(6)) RTPrintf(" PAE");
296 if (s.uEDX & RT_BIT(7)) RTPrintf(" MCE");
297 if (s.uEDX & RT_BIT(8)) RTPrintf(" CMPXCHG8B");
298 if (s.uEDX & RT_BIT(9)) RTPrintf(" APIC");
299 if (s.uEDX & RT_BIT(10)) RTPrintf(" 10");
300 if (s.uEDX & RT_BIT(11)) RTPrintf(" SysCallSysRet");
301 if (s.uEDX & RT_BIT(12)) RTPrintf(" MTRR");
302 if (s.uEDX & RT_BIT(13)) RTPrintf(" PGE");
303 if (s.uEDX & RT_BIT(14)) RTPrintf(" MCA");
304 if (s.uEDX & RT_BIT(15)) RTPrintf(" CMOV");
305 if (s.uEDX & RT_BIT(16)) RTPrintf(" PAT");
306 if (s.uEDX & RT_BIT(17)) RTPrintf(" PSE36");
307 if (s.uEDX & RT_BIT(18)) RTPrintf(" 18");
308 if (s.uEDX & RT_BIT(19)) RTPrintf(" 19");
309 if (s.uEDX & RT_BIT(20)) RTPrintf(" NX");
310 if (s.uEDX & RT_BIT(21)) RTPrintf(" 21");
311 if (s.uEDX & RT_BIT(22)) RTPrintf(" MmxExt");
312 if (s.uEDX & RT_BIT(23)) RTPrintf(" MMX");
313 if (s.uEDX & RT_BIT(24)) RTPrintf(" FXSR");
314 if (s.uEDX & RT_BIT(25)) RTPrintf(" FastFXSR");
315 if (s.uEDX & RT_BIT(26)) RTPrintf(" 26");
316 if (s.uEDX & RT_BIT(27)) RTPrintf(" RDTSCP");
317 if (s.uEDX & RT_BIT(28)) RTPrintf(" 28");
318 if (s.uEDX & RT_BIT(29)) RTPrintf(" LongMode");
319 if (s.uEDX & RT_BIT(30)) RTPrintf(" 3DNowExt");
320 if (s.uEDX & RT_BIT(31)) RTPrintf(" 3DNow");
321 RTPrintf("\n");
322
323 RTPrintf("Features ECX: ");
324 if (s.uECX & RT_BIT(0)) RTPrintf(" LahfSahf");
325 if (s.uECX & RT_BIT(1)) RTPrintf(" CmpLegacy");
326 if (s.uECX & RT_BIT(2)) RTPrintf(" SVM");
327 if (s.uECX & RT_BIT(3)) RTPrintf(" 3");
328 if (s.uECX & RT_BIT(4)) RTPrintf(" AltMovCr8");
329 for (iBit = 5; iBit < 32; iBit++)
330 if (s.uECX & RT_BIT(iBit))
331 RTPrintf(" %d", iBit);
332 RTPrintf("\n");
333 }
334
335 char szString[4*4*3+1] = {0};
336 if (cExtFunctions >= 0x80000002)
337 ASMCpuId(0x80000002, &szString[0 + 0], &szString[0 + 4], &szString[0 + 8], &szString[0 + 12]);
338 if (cExtFunctions >= 0x80000003)
339 ASMCpuId(0x80000003, &szString[16 + 0], &szString[16 + 4], &szString[16 + 8], &szString[16 + 12]);
340 if (cExtFunctions >= 0x80000004)
341 ASMCpuId(0x80000004, &szString[32 + 0], &szString[32 + 4], &szString[32 + 8], &szString[32 + 12]);
342 if (cExtFunctions >= 0x80000002)
343 RTPrintf("Full Name: %s\n", szString);
344
345 if (cExtFunctions >= 0x80000005)
346 {
347 ASMCpuId(0x80000005, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
348 RTPrintf("TLB 2/4M Instr/Uni: %s %3d entries\n"
349 "TLB 2/4M Data: %s %3d entries\n",
350 getCacheAss((s.uEAX >> 8) & 0xff), (s.uEAX >> 0) & 0xff,
351 getCacheAss((s.uEAX >> 24) & 0xff), (s.uEAX >> 16) & 0xff);
352 RTPrintf("TLB 4K Instr/Uni: %s %3d entries\n"
353 "TLB 4K Data: %s %3d entries\n",
354 getCacheAss((s.uEBX >> 8) & 0xff), (s.uEBX >> 0) & 0xff,
355 getCacheAss((s.uEBX >> 24) & 0xff), (s.uEBX >> 16) & 0xff);
356 RTPrintf("L1 Instr Cache Line Size: %d bytes\n"
357 "L1 Instr Cache Lines Per Tag: %d\n"
358 "L1 Instr Cache Associativity: %s\n"
359 "L1 Instr Cache Size: %d KB\n",
360 (s.uEDX >> 0) & 0xff,
361 (s.uEDX >> 8) & 0xff,
362 getCacheAss((s.uEDX >> 16) & 0xff),
363 (s.uEDX >> 24) & 0xff);
364 RTPrintf("L1 Data Cache Line Size: %d bytes\n"
365 "L1 Data Cache Lines Per Tag: %d\n"
366 "L1 Data Cache Associativity: %s\n"
367 "L1 Data Cache Size: %d KB\n",
368 (s.uECX >> 0) & 0xff,
369 (s.uECX >> 8) & 0xff,
370 getCacheAss((s.uECX >> 16) & 0xff),
371 (s.uECX >> 24) & 0xff);
372 }
373
374 if (cExtFunctions >= 0x80000006)
375 {
376 ASMCpuId(0x80000006, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
377 RTPrintf("L2 TLB 2/4M Instr/Uni: %s %4d entries\n"
378 "L2 TLB 2/4M Data: %s %4d entries\n",
379 getL2CacheAss((s.uEAX >> 12) & 0xf), (s.uEAX >> 0) & 0xfff,
380 getL2CacheAss((s.uEAX >> 28) & 0xf), (s.uEAX >> 16) & 0xfff);
381 RTPrintf("L2 TLB 4K Instr/Uni: %s %4d entries\n"
382 "L2 TLB 4K Data: %s %4d entries\n",
383 getL2CacheAss((s.uEBX >> 12) & 0xf), (s.uEBX >> 0) & 0xfff,
384 getL2CacheAss((s.uEBX >> 28) & 0xf), (s.uEBX >> 16) & 0xfff);
385 RTPrintf("L2 Cache Line Size: %d bytes\n"
386 "L2 Cache Lines Per Tag: %d\n"
387 "L2 Cache Associativity: %s\n"
388 "L2 Cache Size: %d KB\n",
389 (s.uEDX >> 0) & 0xff,
390 (s.uEDX >> 8) & 0xf,
391 getL2CacheAss((s.uEDX >> 12) & 0xf),
392 (s.uEDX >> 16) & 0xffff);
393 }
394
395 if (cExtFunctions >= 0x80000007)
396 {
397 ASMCpuId(0x80000007, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
398 RTPrintf("APM Features: ");
399 if (s.uEDX & RT_BIT(0)) RTPrintf(" TS");
400 if (s.uEDX & RT_BIT(1)) RTPrintf(" FID");
401 if (s.uEDX & RT_BIT(2)) RTPrintf(" VID");
402 if (s.uEDX & RT_BIT(3)) RTPrintf(" TTP");
403 if (s.uEDX & RT_BIT(4)) RTPrintf(" TM");
404 if (s.uEDX & RT_BIT(5)) RTPrintf(" STC");
405 if (s.uEDX & RT_BIT(6)) RTPrintf(" 6");
406 if (s.uEDX & RT_BIT(7)) RTPrintf(" 7");
407 if (s.uEDX & RT_BIT(8)) RTPrintf(" TscInvariant");
408 for (iBit = 9; iBit < 32; iBit++)
409 if (s.uEDX & RT_BIT(iBit))
410 RTPrintf(" %d", iBit);
411 RTPrintf("\n");
412 }
413
414 if (cExtFunctions >= 0x80000008)
415 {
416 ASMCpuId(0x80000008, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
417 RTPrintf("Physical Address Width: %d bits\n"
418 "Virtual Address Width: %d bits\n",
419 (s.uEAX >> 0) & 0xff,
420 (s.uEAX >> 8) & 0xff);
421 RTPrintf("Physical Core Count: %d\n",
422 ((s.uECX >> 0) & 0xff) + 1);
423 if ((s.uECX >> 12) & 0xf)
424 RTPrintf("ApicIdCoreIdSize: %d bits\n", (s.uECX >> 12) & 0xf);
425 }
426
427 if (cExtFunctions >= 0x8000000a)
428 {
429 ASMCpuId(0x8000000a, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
430 RTPrintf("SVM Revision: %d (%#x)\n"
431 "Number of Address Space IDs: %d (%#x)\n",
432 s.uEAX & 0xff, s.uEAX & 0xff,
433 s.uEBX, s.uEBX);
434 }
435}
436#endif /* !PIC || !X86 */
437
438
439static void tstASMAtomicXchgU8(void)
440{
441 struct
442 {
443 uint8_t u8Dummy0;
444 uint8_t u8;
445 uint8_t u8Dummy1;
446 } s;
447
448 s.u8 = 0;
449 s.u8Dummy0 = s.u8Dummy1 = 0x42;
450 CHECKOP(ASMAtomicXchgU8(&s.u8, 1), 0, "%#x", uint8_t);
451 CHECKVAL(s.u8, 1, "%#x");
452
453 CHECKOP(ASMAtomicXchgU8(&s.u8, 0), 1, "%#x", uint8_t);
454 CHECKVAL(s.u8, 0, "%#x");
455
456 CHECKOP(ASMAtomicXchgU8(&s.u8, 0xff), 0, "%#x", uint8_t);
457 CHECKVAL(s.u8, 0xff, "%#x");
458
459 CHECKOP(ASMAtomicXchgU8(&s.u8, 0x87), 0xffff, "%#x", uint8_t);
460 CHECKVAL(s.u8, 0x87, "%#x");
461 CHECKVAL(s.u8Dummy0, 0x42, "%#x");
462 CHECKVAL(s.u8Dummy1, 0x42, "%#x");
463}
464
465
466static void tstASMAtomicXchgU16(void)
467{
468 struct
469 {
470 uint16_t u16Dummy0;
471 uint16_t u16;
472 uint16_t u16Dummy1;
473 } s;
474
475 s.u16 = 0;
476 s.u16Dummy0 = s.u16Dummy1 = 0x1234;
477 CHECKOP(ASMAtomicXchgU16(&s.u16, 1), 0, "%#x", uint16_t);
478 CHECKVAL(s.u16, 1, "%#x");
479
480 CHECKOP(ASMAtomicXchgU16(&s.u16, 0), 1, "%#x", uint16_t);
481 CHECKVAL(s.u16, 0, "%#x");
482
483 CHECKOP(ASMAtomicXchgU16(&s.u16, 0xffff), 0, "%#x", uint16_t);
484 CHECKVAL(s.u16, 0xffff, "%#x");
485
486 CHECKOP(ASMAtomicXchgU16(&s.u16, 0x8765), 0xffff, "%#x", uint16_t);
487 CHECKVAL(s.u16, 0x8765, "%#x");
488 CHECKVAL(s.u16Dummy0, 0x1234, "%#x");
489 CHECKVAL(s.u16Dummy1, 0x1234, "%#x");
490}
491
492
493static void tstASMAtomicXchgU32(void)
494{
495 struct
496 {
497 uint32_t u32Dummy0;
498 uint32_t u32;
499 uint32_t u32Dummy1;
500 } s;
501
502 s.u32 = 0;
503 s.u32Dummy0 = s.u32Dummy1 = 0x11223344;
504
505 CHECKOP(ASMAtomicXchgU32(&s.u32, 1), 0, "%#x", uint32_t);
506 CHECKVAL(s.u32, 1, "%#x");
507
508 CHECKOP(ASMAtomicXchgU32(&s.u32, 0), 1, "%#x", uint32_t);
509 CHECKVAL(s.u32, 0, "%#x");
510
511 CHECKOP(ASMAtomicXchgU32(&s.u32, ~0U), 0, "%#x", uint32_t);
512 CHECKVAL(s.u32, ~0U, "%#x");
513
514 CHECKOP(ASMAtomicXchgU32(&s.u32, 0x87654321), ~0U, "%#x", uint32_t);
515 CHECKVAL(s.u32, 0x87654321, "%#x");
516
517 CHECKVAL(s.u32Dummy0, 0x11223344, "%#x");
518 CHECKVAL(s.u32Dummy1, 0x11223344, "%#x");
519}
520
521
522static void tstASMAtomicXchgU64(void)
523{
524 struct
525 {
526 uint64_t u64Dummy0;
527 uint64_t u64;
528 uint64_t u64Dummy1;
529 } s;
530
531 s.u64 = 0;
532 s.u64Dummy0 = s.u64Dummy1 = 0x1122334455667788ULL;
533
534 CHECKOP(ASMAtomicXchgU64(&s.u64, 1), 0ULL, "%#llx", uint64_t);
535 CHECKVAL(s.u64, 1ULL, "%#llx");
536
537 CHECKOP(ASMAtomicXchgU64(&s.u64, 0), 1ULL, "%#llx", uint64_t);
538 CHECKVAL(s.u64, 0ULL, "%#llx");
539
540 CHECKOP(ASMAtomicXchgU64(&s.u64, ~0ULL), 0ULL, "%#llx", uint64_t);
541 CHECKVAL(s.u64, ~0ULL, "%#llx");
542
543 CHECKOP(ASMAtomicXchgU64(&s.u64, 0xfedcba0987654321ULL), ~0ULL, "%#llx", uint64_t);
544 CHECKVAL(s.u64, 0xfedcba0987654321ULL, "%#llx");
545
546 CHECKVAL(s.u64Dummy0, 0x1122334455667788ULL, "%#llx");
547 CHECKVAL(s.u64Dummy1, 0x1122334455667788ULL, "%#llx");
548}
549
550
551static void tstASMAtomicXchgPtr(void)
552{
553 void *pv = NULL;
554
555 CHECKOP(ASMAtomicXchgPtr(&pv, (void *)(~(uintptr_t)0)), NULL, "%p", void *);
556 CHECKVAL(pv, (void *)(~(uintptr_t)0), "%p");
557
558 CHECKOP(ASMAtomicXchgPtr(&pv, (void *)0x87654321), (void *)(~(uintptr_t)0), "%p", void *);
559 CHECKVAL(pv, (void *)0x87654321, "%p");
560
561 CHECKOP(ASMAtomicXchgPtr(&pv, NULL), (void *)0x87654321, "%p", void *);
562 CHECKVAL(pv, NULL, "%p");
563}
564
565
566static void tstASMAtomicCmpXchgU8(void)
567{
568 struct
569 {
570 uint8_t u8Before;
571 uint8_t u8;
572 uint8_t u8After;
573 } u = { 0xcc, 0xff, 0xaa };
574
575 CHECKOP(ASMAtomicCmpXchgU8(&u.u8, 0, 0), false, "%d", bool);
576 CHECKVAL(u.u8, 0xff, "%x"); CHECKVAL(u.u8Before, 0xcc, "%x"); CHECKVAL(u.u8After, 0xaa, "%x");
577
578 CHECKOP(ASMAtomicCmpXchgU8(&u.u8, 0, 0xff), true, "%d", bool);
579 CHECKVAL(u.u8, 0, "%x"); CHECKVAL(u.u8Before, 0xcc, "%x"); CHECKVAL(u.u8After, 0xaa, "%x");
580
581 CHECKOP(ASMAtomicCmpXchgU8(&u.u8, 0x79, 0xff), false, "%d", bool);
582 CHECKVAL(u.u8, 0, "%x"); CHECKVAL(u.u8Before, 0xcc, "%x"); CHECKVAL(u.u8After, 0xaa, "%x");
583
584 CHECKOP(ASMAtomicCmpXchgU8(&u.u8, 0x97, 0), true, "%d", bool);
585 CHECKVAL(u.u8, 0x97, "%x"); CHECKVAL(u.u8Before, 0xcc, "%x"); CHECKVAL(u.u8After, 0xaa, "%x");
586}
587
588static void tstASMAtomicCmpXchgU32(void)
589{
590 uint32_t u32 = 0xffffffff;
591
592 CHECKOP(ASMAtomicCmpXchgU32(&u32, 0, 0), false, "%d", bool);
593 CHECKVAL(u32, 0xffffffff, "%x");
594
595 CHECKOP(ASMAtomicCmpXchgU32(&u32, 0, 0xffffffff), true, "%d", bool);
596 CHECKVAL(u32, 0, "%x");
597
598 CHECKOP(ASMAtomicCmpXchgU32(&u32, 0x8008efd, 0xffffffff), false, "%d", bool);
599 CHECKVAL(u32, 0, "%x");
600
601 CHECKOP(ASMAtomicCmpXchgU32(&u32, 0x8008efd, 0), true, "%d", bool);
602 CHECKVAL(u32, 0x8008efd, "%x");
603}
604
605
606static void tstASMAtomicCmpXchgU64(void)
607{
608 uint64_t u64 = 0xffffffffffffffULL;
609
610 CHECKOP(ASMAtomicCmpXchgU64(&u64, 0, 0), false, "%d", bool);
611 CHECKVAL(u64, 0xffffffffffffffULL, "%#llx");
612
613 CHECKOP(ASMAtomicCmpXchgU64(&u64, 0, 0xffffffffffffffULL), true, "%d", bool);
614 CHECKVAL(u64, 0, "%x");
615
616 CHECKOP(ASMAtomicCmpXchgU64(&u64, 0x80040008008efdULL, 0xffffffff), false, "%d", bool);
617 CHECKVAL(u64, 0, "%x");
618
619 CHECKOP(ASMAtomicCmpXchgU64(&u64, 0x80040008008efdULL, 0xffffffff00000000ULL), false, "%d", bool);
620 CHECKVAL(u64, 0, "%x");
621
622 CHECKOP(ASMAtomicCmpXchgU64(&u64, 0x80040008008efdULL, 0), true, "%d", bool);
623 CHECKVAL(u64, 0x80040008008efdULL, "%#llx");
624}
625
626
627static void tstASMAtomicCmpXchgExU32(void)
628{
629 uint32_t u32 = 0xffffffff;
630 uint32_t u32Old = 0x80005111;
631
632 CHECKOP(ASMAtomicCmpXchgExU32(&u32, 0, 0, &u32Old), false, "%d", bool);
633 CHECKVAL(u32, 0xffffffff, "%x");
634 CHECKVAL(u32Old, 0xffffffff, "%x");
635
636 CHECKOP(ASMAtomicCmpXchgExU32(&u32, 0, 0xffffffff, &u32Old), true, "%d", bool);
637 CHECKVAL(u32, 0, "%x");
638 CHECKVAL(u32Old, 0xffffffff, "%x");
639
640 CHECKOP(ASMAtomicCmpXchgExU32(&u32, 0x8008efd, 0xffffffff, &u32Old), false, "%d", bool);
641 CHECKVAL(u32, 0, "%x");
642 CHECKVAL(u32Old, 0, "%x");
643
644 CHECKOP(ASMAtomicCmpXchgExU32(&u32, 0x8008efd, 0, &u32Old), true, "%d", bool);
645 CHECKVAL(u32, 0x8008efd, "%x");
646 CHECKVAL(u32Old, 0, "%x");
647
648 CHECKOP(ASMAtomicCmpXchgExU32(&u32, 0, 0x8008efd, &u32Old), true, "%d", bool);
649 CHECKVAL(u32, 0, "%x");
650 CHECKVAL(u32Old, 0x8008efd, "%x");
651}
652
653
654static void tstASMAtomicCmpXchgExU64(void)
655{
656 uint64_t u64 = 0xffffffffffffffffULL;
657 uint64_t u64Old = 0x8000000051111111ULL;
658
659 CHECKOP(ASMAtomicCmpXchgExU64(&u64, 0, 0, &u64Old), false, "%d", bool);
660 CHECKVAL(u64, 0xffffffffffffffffULL, "%llx");
661 CHECKVAL(u64Old, 0xffffffffffffffffULL, "%llx");
662
663 CHECKOP(ASMAtomicCmpXchgExU64(&u64, 0, 0xffffffffffffffffULL, &u64Old), true, "%d", bool);
664 CHECKVAL(u64, 0ULL, "%llx");
665 CHECKVAL(u64Old, 0xffffffffffffffffULL, "%llx");
666
667 CHECKOP(ASMAtomicCmpXchgExU64(&u64, 0x80040008008efdULL, 0xffffffff, &u64Old), false, "%d", bool);
668 CHECKVAL(u64, 0ULL, "%llx");
669 CHECKVAL(u64Old, 0ULL, "%llx");
670
671 CHECKOP(ASMAtomicCmpXchgExU64(&u64, 0x80040008008efdULL, 0xffffffff00000000ULL, &u64Old), false, "%d", bool);
672 CHECKVAL(u64, 0ULL, "%llx");
673 CHECKVAL(u64Old, 0ULL, "%llx");
674
675 CHECKOP(ASMAtomicCmpXchgExU64(&u64, 0x80040008008efdULL, 0, &u64Old), true, "%d", bool);
676 CHECKVAL(u64, 0x80040008008efdULL, "%llx");
677 CHECKVAL(u64Old, 0ULL, "%llx");
678
679 CHECKOP(ASMAtomicCmpXchgExU64(&u64, 0, 0x80040008008efdULL, &u64Old), true, "%d", bool);
680 CHECKVAL(u64, 0ULL, "%llx");
681 CHECKVAL(u64Old, 0x80040008008efdULL, "%llx");
682}
683
684
685static void tstASMAtomicReadU64(void)
686{
687 uint64_t u64 = 0;
688
689 CHECKOP(ASMAtomicReadU64(&u64), 0ULL, "%#llx", uint64_t);
690 CHECKVAL(u64, 0ULL, "%#llx");
691
692 u64 = ~0ULL;
693 CHECKOP(ASMAtomicReadU64(&u64), ~0ULL, "%#llx", uint64_t);
694 CHECKVAL(u64, ~0ULL, "%#llx");
695
696 u64 = 0xfedcba0987654321ULL;
697 CHECKOP(ASMAtomicReadU64(&u64), 0xfedcba0987654321ULL, "%#llx", uint64_t);
698 CHECKVAL(u64, 0xfedcba0987654321ULL, "%#llx");
699}
700
701
702static void tstASMAtomicAddS32(void)
703{
704 int32_t i32Rc;
705 int32_t i32 = 10;
706#define MYCHECK(op, rc, val) \
707 do { \
708 i32Rc = op; \
709 if (i32Rc != (rc)) \
710 { \
711 RTPrintf("%s, %d: FAILURE: %s -> %d expected %d\n", __FUNCTION__, __LINE__, #op, i32Rc, rc); \
712 RTTestIErrorInc(); \
713 } \
714 if (i32 != (val)) \
715 { \
716 RTPrintf("%s, %d: FAILURE: %s => i32=%d expected %d\n", __FUNCTION__, __LINE__, #op, i32, val); \
717 RTTestIErrorInc(); \
718 } \
719 } while (0)
720 MYCHECK(ASMAtomicAddS32(&i32, 1), 10, 11);
721 MYCHECK(ASMAtomicAddS32(&i32, -2), 11, 9);
722 MYCHECK(ASMAtomicAddS32(&i32, -9), 9, 0);
723 MYCHECK(ASMAtomicAddS32(&i32, -0x7fffffff), 0, -0x7fffffff);
724 MYCHECK(ASMAtomicAddS32(&i32, 0), -0x7fffffff, -0x7fffffff);
725 MYCHECK(ASMAtomicAddS32(&i32, 0x7fffffff), -0x7fffffff, 0);
726 MYCHECK(ASMAtomicAddS32(&i32, 0), 0, 0);
727#undef MYCHECK
728}
729
730
731static void tstASMAtomicDecIncS32(void)
732{
733 int32_t i32Rc;
734 int32_t i32 = 10;
735#define MYCHECK(op, rc) \
736 do { \
737 i32Rc = op; \
738 if (i32Rc != (rc)) \
739 { \
740 RTPrintf("%s, %d: FAILURE: %s -> %d expected %d\n", __FUNCTION__, __LINE__, #op, i32Rc, rc); \
741 RTTestIErrorInc(); \
742 } \
743 if (i32 != (rc)) \
744 { \
745 RTPrintf("%s, %d: FAILURE: %s => i32=%d expected %d\n", __FUNCTION__, __LINE__, #op, i32, rc); \
746 RTTestIErrorInc(); \
747 } \
748 } while (0)
749 MYCHECK(ASMAtomicDecS32(&i32), 9);
750 MYCHECK(ASMAtomicDecS32(&i32), 8);
751 MYCHECK(ASMAtomicDecS32(&i32), 7);
752 MYCHECK(ASMAtomicDecS32(&i32), 6);
753 MYCHECK(ASMAtomicDecS32(&i32), 5);
754 MYCHECK(ASMAtomicDecS32(&i32), 4);
755 MYCHECK(ASMAtomicDecS32(&i32), 3);
756 MYCHECK(ASMAtomicDecS32(&i32), 2);
757 MYCHECK(ASMAtomicDecS32(&i32), 1);
758 MYCHECK(ASMAtomicDecS32(&i32), 0);
759 MYCHECK(ASMAtomicDecS32(&i32), -1);
760 MYCHECK(ASMAtomicDecS32(&i32), -2);
761 MYCHECK(ASMAtomicIncS32(&i32), -1);
762 MYCHECK(ASMAtomicIncS32(&i32), 0);
763 MYCHECK(ASMAtomicIncS32(&i32), 1);
764 MYCHECK(ASMAtomicIncS32(&i32), 2);
765 MYCHECK(ASMAtomicIncS32(&i32), 3);
766 MYCHECK(ASMAtomicDecS32(&i32), 2);
767 MYCHECK(ASMAtomicIncS32(&i32), 3);
768 MYCHECK(ASMAtomicDecS32(&i32), 2);
769 MYCHECK(ASMAtomicIncS32(&i32), 3);
770#undef MYCHECK
771}
772
773
774static void tstASMAtomicAndOrU32(void)
775{
776 uint32_t u32 = 0xffffffff;
777
778 ASMAtomicOrU32(&u32, 0xffffffff);
779 CHECKVAL(u32, 0xffffffff, "%x");
780
781 ASMAtomicAndU32(&u32, 0xffffffff);
782 CHECKVAL(u32, 0xffffffff, "%x");
783
784 ASMAtomicAndU32(&u32, 0x8f8f8f8f);
785 CHECKVAL(u32, 0x8f8f8f8f, "%x");
786
787 ASMAtomicOrU32(&u32, 0x70707070);
788 CHECKVAL(u32, 0xffffffff, "%x");
789
790 ASMAtomicAndU32(&u32, 1);
791 CHECKVAL(u32, 1, "%x");
792
793 ASMAtomicOrU32(&u32, 0x80000000);
794 CHECKVAL(u32, 0x80000001, "%x");
795
796 ASMAtomicAndU32(&u32, 0x80000000);
797 CHECKVAL(u32, 0x80000000, "%x");
798
799 ASMAtomicAndU32(&u32, 0);
800 CHECKVAL(u32, 0, "%x");
801
802 ASMAtomicOrU32(&u32, 0x42424242);
803 CHECKVAL(u32, 0x42424242, "%x");
804}
805
806
807void tstASMMemZeroPage(void)
808{
809 struct
810 {
811 uint64_t u64Magic1;
812 uint8_t abPage[PAGE_SIZE];
813 uint64_t u64Magic2;
814 } Buf1, Buf2, Buf3;
815
816 Buf1.u64Magic1 = UINT64_C(0xffffffffffffffff);
817 memset(Buf1.abPage, 0x55, sizeof(Buf1.abPage));
818 Buf1.u64Magic2 = UINT64_C(0xffffffffffffffff);
819 Buf2.u64Magic1 = UINT64_C(0xffffffffffffffff);
820 memset(Buf2.abPage, 0x77, sizeof(Buf2.abPage));
821 Buf2.u64Magic2 = UINT64_C(0xffffffffffffffff);
822 Buf3.u64Magic1 = UINT64_C(0xffffffffffffffff);
823 memset(Buf3.abPage, 0x99, sizeof(Buf3.abPage));
824 Buf3.u64Magic2 = UINT64_C(0xffffffffffffffff);
825 ASMMemZeroPage(Buf1.abPage);
826 ASMMemZeroPage(Buf2.abPage);
827 ASMMemZeroPage(Buf3.abPage);
828 if ( Buf1.u64Magic1 != UINT64_C(0xffffffffffffffff)
829 || Buf1.u64Magic2 != UINT64_C(0xffffffffffffffff)
830 || Buf2.u64Magic1 != UINT64_C(0xffffffffffffffff)
831 || Buf2.u64Magic2 != UINT64_C(0xffffffffffffffff)
832 || Buf3.u64Magic1 != UINT64_C(0xffffffffffffffff)
833 || Buf3.u64Magic2 != UINT64_C(0xffffffffffffffff))
834 {
835 RTPrintf("tstInlineAsm: ASMMemZeroPage violated one/both magic(s)!\n");
836 RTTestIErrorInc();
837 }
838 for (unsigned i = 0; i < sizeof(Buf1.abPage); i++)
839 if (Buf1.abPage[i])
840 {
841 RTPrintf("tstInlineAsm: ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
842 RTTestIErrorInc();
843 }
844 for (unsigned i = 0; i < sizeof(Buf2.abPage); i++)
845 if (Buf2.abPage[i])
846 {
847 RTPrintf("tstInlineAsm: ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
848 RTTestIErrorInc();
849 }
850 for (unsigned i = 0; i < sizeof(Buf3.abPage); i++)
851 if (Buf3.abPage[i])
852 {
853 RTPrintf("tstInlineAsm: ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
854 RTTestIErrorInc();
855 }
856}
857
858
859void tstASMMemIsZeroPage(RTTEST hTest)
860{
861 RTTestSub(hTest, "ASMMemIsZeroPage");
862
863 void *pvPage1 = RTTestGuardedAllocHead(hTest, PAGE_SIZE);
864 void *pvPage2 = RTTestGuardedAllocTail(hTest, PAGE_SIZE);
865 RTTESTI_CHECK_RETV(pvPage1 && pvPage2);
866
867 memset(pvPage1, 0, PAGE_SIZE);
868 memset(pvPage2, 0, PAGE_SIZE);
869 RTTESTI_CHECK(ASMMemIsZeroPage(pvPage1));
870 RTTESTI_CHECK(ASMMemIsZeroPage(pvPage2));
871
872 memset(pvPage1, 0xff, PAGE_SIZE);
873 memset(pvPage2, 0xff, PAGE_SIZE);
874 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
875 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
876
877 memset(pvPage1, 0, PAGE_SIZE);
878 memset(pvPage2, 0, PAGE_SIZE);
879 for (unsigned off = 0; off < PAGE_SIZE; off++)
880 {
881 ((uint8_t *)pvPage1)[off] = 1;
882 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
883 ((uint8_t *)pvPage1)[off] = 0;
884
885 ((uint8_t *)pvPage2)[off] = 0x80;
886 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
887 ((uint8_t *)pvPage2)[off] = 0;
888 }
889
890 RTTestSubDone(hTest);
891}
892
893
894void tstASMMemZero32(void)
895{
896 struct
897 {
898 uint64_t u64Magic1;
899 uint8_t abPage[PAGE_SIZE - 32];
900 uint64_t u64Magic2;
901 } Buf1, Buf2, Buf3;
902
903 Buf1.u64Magic1 = UINT64_C(0xffffffffffffffff);
904 memset(Buf1.abPage, 0x55, sizeof(Buf1.abPage));
905 Buf1.u64Magic2 = UINT64_C(0xffffffffffffffff);
906 Buf2.u64Magic1 = UINT64_C(0xffffffffffffffff);
907 memset(Buf2.abPage, 0x77, sizeof(Buf2.abPage));
908 Buf2.u64Magic2 = UINT64_C(0xffffffffffffffff);
909 Buf3.u64Magic1 = UINT64_C(0xffffffffffffffff);
910 memset(Buf3.abPage, 0x99, sizeof(Buf3.abPage));
911 Buf3.u64Magic2 = UINT64_C(0xffffffffffffffff);
912 ASMMemZero32(Buf1.abPage, sizeof(Buf1.abPage));
913 ASMMemZero32(Buf2.abPage, sizeof(Buf2.abPage));
914 ASMMemZero32(Buf3.abPage, sizeof(Buf3.abPage));
915 if ( Buf1.u64Magic1 != UINT64_C(0xffffffffffffffff)
916 || Buf1.u64Magic2 != UINT64_C(0xffffffffffffffff)
917 || Buf2.u64Magic1 != UINT64_C(0xffffffffffffffff)
918 || Buf2.u64Magic2 != UINT64_C(0xffffffffffffffff)
919 || Buf3.u64Magic1 != UINT64_C(0xffffffffffffffff)
920 || Buf3.u64Magic2 != UINT64_C(0xffffffffffffffff))
921 {
922 RTPrintf("tstInlineAsm: ASMMemZero32 violated one/both magic(s)!\n");
923 RTTestIErrorInc();
924 }
925 for (unsigned i = 0; i < RT_ELEMENTS(Buf1.abPage); i++)
926 if (Buf1.abPage[i])
927 {
928 RTPrintf("tstInlineAsm: ASMMemZero32 didn't clear byte at offset %#x!\n", i);
929 RTTestIErrorInc();
930 }
931 for (unsigned i = 0; i < RT_ELEMENTS(Buf2.abPage); i++)
932 if (Buf2.abPage[i])
933 {
934 RTPrintf("tstInlineAsm: ASMMemZero32 didn't clear byte at offset %#x!\n", i);
935 RTTestIErrorInc();
936 }
937 for (unsigned i = 0; i < RT_ELEMENTS(Buf3.abPage); i++)
938 if (Buf3.abPage[i])
939 {
940 RTPrintf("tstInlineAsm: ASMMemZero32 didn't clear byte at offset %#x!\n", i);
941 RTTestIErrorInc();
942 }
943}
944
945
946void tstASMMemFill32(void)
947{
948 struct
949 {
950 uint64_t u64Magic1;
951 uint32_t au32Page[PAGE_SIZE / 4];
952 uint64_t u64Magic2;
953 } Buf1;
954 struct
955 {
956 uint64_t u64Magic1;
957 uint32_t au32Page[(PAGE_SIZE / 4) - 3];
958 uint64_t u64Magic2;
959 } Buf2;
960 struct
961 {
962 uint64_t u64Magic1;
963 uint32_t au32Page[(PAGE_SIZE / 4) - 1];
964 uint64_t u64Magic2;
965 } Buf3;
966
967 Buf1.u64Magic1 = UINT64_C(0xffffffffffffffff);
968 memset(Buf1.au32Page, 0x55, sizeof(Buf1.au32Page));
969 Buf1.u64Magic2 = UINT64_C(0xffffffffffffffff);
970 Buf2.u64Magic1 = UINT64_C(0xffffffffffffffff);
971 memset(Buf2.au32Page, 0x77, sizeof(Buf2.au32Page));
972 Buf2.u64Magic2 = UINT64_C(0xffffffffffffffff);
973 Buf3.u64Magic1 = UINT64_C(0xffffffffffffffff);
974 memset(Buf3.au32Page, 0x99, sizeof(Buf3.au32Page));
975 Buf3.u64Magic2 = UINT64_C(0xffffffffffffffff);
976 ASMMemFill32(Buf1.au32Page, sizeof(Buf1.au32Page), 0xdeadbeef);
977 ASMMemFill32(Buf2.au32Page, sizeof(Buf2.au32Page), 0xcafeff01);
978 ASMMemFill32(Buf3.au32Page, sizeof(Buf3.au32Page), 0xf00dd00f);
979 if ( Buf1.u64Magic1 != UINT64_C(0xffffffffffffffff)
980 || Buf1.u64Magic2 != UINT64_C(0xffffffffffffffff)
981 || Buf2.u64Magic1 != UINT64_C(0xffffffffffffffff)
982 || Buf2.u64Magic2 != UINT64_C(0xffffffffffffffff)
983 || Buf3.u64Magic1 != UINT64_C(0xffffffffffffffff)
984 || Buf3.u64Magic2 != UINT64_C(0xffffffffffffffff))
985 {
986 RTPrintf("tstInlineAsm: ASMMemFill32 violated one/both magic(s)!\n");
987 RTTestIErrorInc();
988 }
989 for (unsigned i = 0; i < RT_ELEMENTS(Buf1.au32Page); i++)
990 if (Buf1.au32Page[i] != 0xdeadbeef)
991 {
992 RTPrintf("tstInlineAsm: ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf1.au32Page[i], 0xdeadbeef);
993 RTTestIErrorInc();
994 }
995 for (unsigned i = 0; i < RT_ELEMENTS(Buf2.au32Page); i++)
996 if (Buf2.au32Page[i] != 0xcafeff01)
997 {
998 RTPrintf("tstInlineAsm: ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf2.au32Page[i], 0xcafeff01);
999 RTTestIErrorInc();
1000 }
1001 for (unsigned i = 0; i < RT_ELEMENTS(Buf3.au32Page); i++)
1002 if (Buf3.au32Page[i] != 0xf00dd00f)
1003 {
1004 RTPrintf("tstInlineAsm: ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf3.au32Page[i], 0xf00dd00f);
1005 RTTestIErrorInc();
1006 }
1007}
1008
1009
1010
1011void tstASMMath(void)
1012{
1013 uint64_t u64 = ASMMult2xU32RetU64(UINT32_C(0x80000000), UINT32_C(0x10000000));
1014 CHECKVAL(u64, UINT64_C(0x0800000000000000), "%#018RX64");
1015
1016 uint32_t u32 = ASMDivU64ByU32RetU32(UINT64_C(0x0800000000000000), UINT32_C(0x10000000));
1017 CHECKVAL(u32, UINT32_C(0x80000000), "%#010RX32");
1018
1019 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x0000000000000001), UINT32_C(0x00000001), UINT32_C(0x00000001));
1020 CHECKVAL(u64, UINT64_C(0x0000000000000001), "%#018RX64");
1021 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x0000000100000000), UINT32_C(0x80000000), UINT32_C(0x00000002));
1022 CHECKVAL(u64, UINT64_C(0x4000000000000000), "%#018RX64");
1023 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xfedcba9876543210), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
1024 CHECKVAL(u64, UINT64_C(0xfedcba9876543210), "%#018RX64");
1025 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xffffffffffffffff), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
1026 CHECKVAL(u64, UINT64_C(0xffffffffffffffff), "%#018RX64");
1027 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xffffffffffffffff), UINT32_C(0xfffffff0), UINT32_C(0xffffffff));
1028 CHECKVAL(u64, UINT64_C(0xfffffff0fffffff0), "%#018RX64");
1029 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x3415934810359583), UINT32_C(0x58734981), UINT32_C(0xf8694045));
1030 CHECKVAL(u64, UINT64_C(0x128b9c3d43184763), "%#018RX64");
1031 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x3415934810359583), UINT32_C(0xf8694045), UINT32_C(0x58734981));
1032 CHECKVAL(u64, UINT64_C(0x924719355cd35a27), "%#018RX64");
1033
1034#if 0 /* bird: question is whether this should trap or not:
1035 *
1036 * frank: Of course it must trap:
1037 *
1038 * 0xfffffff8 * 0x77d7daf8 = 0x77d7daf441412840
1039 *
1040 * During the following division, the quotient must fit into a 32-bit register.
1041 * Therefore the smallest valid divisor is
1042 *
1043 * (0x77d7daf441412840 >> 32) + 1 = 0x77d7daf5
1044 *
1045 * which is definitely greater than 0x3b9aca00.
1046 *
1047 * bird: No, the C version does *not* crash. So, the question is whether there's any
1048 * code depending on it not crashing.
1049 *
1050 * Of course the assembly versions of the code crash right now for the reasons you've
1051 * given, but the 32-bit MSC version does not crash.
1052 *
1053 * frank: The C version does not crash but delivers incorrect results for this case.
1054 * The reason is
1055 *
1056 * u.s.Hi = (unsigned long)(u64Hi / u32C);
1057 *
1058 * Here the division is actually 64-bit by 64-bit but the 64-bit result is truncated
1059 * to 32 bit. If using this (optimized and fast) function we should just be sure that
1060 * the operands are in a valid range.
1061 */
1062 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xfffffff8c65d6731), UINT32_C(0x77d7daf8), UINT32_C(0x3b9aca00));
1063 CHECKVAL(u64, UINT64_C(0x02b8f9a2aa74e3dc), "%#018RX64");
1064#endif
1065
1066 u32 = ASMModU64ByU32RetU32(UINT64_C(0x0ffffff8c65d6731), UINT32_C(0x77d7daf8));
1067 CHECKVAL(u32, UINT32_C(0x3B642451), "%#010RX32");
1068
1069 int32_t i32;
1070 i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(-2));
1071 CHECKVAL(i32, INT32_C(-1), "%010RI32");
1072 i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(2));
1073 CHECKVAL(i32, INT32_C(-1), "%010RI32");
1074 i32 = ASMModS64ByS32RetS32(INT64_C(11), INT32_C(-2));
1075 CHECKVAL(i32, INT32_C(1), "%010RI32");
1076
1077 i32 = ASMModS64ByS32RetS32(INT64_C(92233720368547758), INT32_C(2147483647));
1078 CHECKVAL(i32, INT32_C(2104533974), "%010RI32");
1079 i32 = ASMModS64ByS32RetS32(INT64_C(-92233720368547758), INT32_C(2147483647));
1080 CHECKVAL(i32, INT32_C(-2104533974), "%010RI32");
1081}
1082
1083
1084void tstASMByteSwap(void)
1085{
1086 RTPrintf("tstInlineASM: TESTING - ASMByteSwap*\n");
1087
1088 uint64_t u64In = UINT64_C(0x0011223344556677);
1089 uint64_t u64Out = ASMByteSwapU64(u64In);
1090 CHECKVAL(u64In, UINT64_C(0x0011223344556677), "%#018RX64");
1091 CHECKVAL(u64Out, UINT64_C(0x7766554433221100), "%#018RX64");
1092 u64Out = ASMByteSwapU64(u64Out);
1093 CHECKVAL(u64Out, u64In, "%#018RX64");
1094 u64In = UINT64_C(0x0123456789abcdef);
1095 u64Out = ASMByteSwapU64(u64In);
1096 CHECKVAL(u64In, UINT64_C(0x0123456789abcdef), "%#018RX64");
1097 CHECKVAL(u64Out, UINT64_C(0xefcdab8967452301), "%#018RX64");
1098 u64Out = ASMByteSwapU64(u64Out);
1099 CHECKVAL(u64Out, u64In, "%#018RX64");
1100 u64In = 0;
1101 u64Out = ASMByteSwapU64(u64In);
1102 CHECKVAL(u64Out, u64In, "%#018RX64");
1103 u64In = ~(uint64_t)0;
1104 u64Out = ASMByteSwapU64(u64In);
1105 CHECKVAL(u64Out, u64In, "%#018RX64");
1106
1107 uint32_t u32In = UINT32_C(0x00112233);
1108 uint32_t u32Out = ASMByteSwapU32(u32In);
1109 CHECKVAL(u32In, UINT32_C(0x00112233), "%#010RX32");
1110 CHECKVAL(u32Out, UINT32_C(0x33221100), "%#010RX32");
1111 u32Out = ASMByteSwapU32(u32Out);
1112 CHECKVAL(u32Out, u32In, "%#010RX32");
1113 u32In = UINT32_C(0x12345678);
1114 u32Out = ASMByteSwapU32(u32In);
1115 CHECKVAL(u32In, UINT32_C(0x12345678), "%#010RX32");
1116 CHECKVAL(u32Out, UINT32_C(0x78563412), "%#010RX32");
1117 u32Out = ASMByteSwapU32(u32Out);
1118 CHECKVAL(u32Out, u32In, "%#010RX32");
1119 u32In = 0;
1120 u32Out = ASMByteSwapU32(u32In);
1121 CHECKVAL(u32Out, u32In, "%#010RX32");
1122 u32In = ~(uint32_t)0;
1123 u32Out = ASMByteSwapU32(u32In);
1124 CHECKVAL(u32Out, u32In, "%#010RX32");
1125
1126 uint16_t u16In = UINT16_C(0x0011);
1127 uint16_t u16Out = ASMByteSwapU16(u16In);
1128 CHECKVAL(u16In, UINT16_C(0x0011), "%#06RX16");
1129 CHECKVAL(u16Out, UINT16_C(0x1100), "%#06RX16");
1130 u16Out = ASMByteSwapU16(u16Out);
1131 CHECKVAL(u16Out, u16In, "%#06RX16");
1132 u16In = UINT16_C(0x1234);
1133 u16Out = ASMByteSwapU16(u16In);
1134 CHECKVAL(u16In, UINT16_C(0x1234), "%#06RX16");
1135 CHECKVAL(u16Out, UINT16_C(0x3412), "%#06RX16");
1136 u16Out = ASMByteSwapU16(u16Out);
1137 CHECKVAL(u16Out, u16In, "%#06RX16");
1138 u16In = 0;
1139 u16Out = ASMByteSwapU16(u16In);
1140 CHECKVAL(u16Out, u16In, "%#06RX16");
1141 u16In = ~(uint16_t)0;
1142 u16Out = ASMByteSwapU16(u16In);
1143 CHECKVAL(u16Out, u16In, "%#06RX16");
1144}
1145
1146
1147void tstASMBench(void)
1148{
1149 /*
1150 * Make this static. We don't want to have this located on the stack.
1151 */
1152 static uint8_t volatile s_u8;
1153 static int8_t volatile s_i8;
1154 static uint16_t volatile s_u16;
1155 static int16_t volatile s_i16;
1156 static uint32_t volatile s_u32;
1157 static int32_t volatile s_i32;
1158 static uint64_t volatile s_u64;
1159 static int64_t volatile s_i64;
1160 register unsigned i;
1161 const unsigned cRounds = 1000000;
1162 register uint64_t u64Elapsed;
1163
1164 RTPrintf("tstInlineASM: Benchmarking:\n");
1165
1166#define BENCH(op, str) \
1167 RTThreadYield(); \
1168 u64Elapsed = ASMReadTSC(); \
1169 for (i = cRounds; i > 0; i--) \
1170 op; \
1171 u64Elapsed = ASMReadTSC() - u64Elapsed; \
1172 RTPrintf(" %-30s %3llu cycles\n", str, u64Elapsed / cRounds);
1173
1174 BENCH(s_u32 = 0, "s_u32 = 0:");
1175 BENCH(ASMAtomicUoWriteU8(&s_u8, 0), "ASMAtomicUoWriteU8:");
1176 BENCH(ASMAtomicUoWriteS8(&s_i8, 0), "ASMAtomicUoWriteS8:");
1177 BENCH(ASMAtomicUoWriteU16(&s_u16, 0), "ASMAtomicUoWriteU16:");
1178 BENCH(ASMAtomicUoWriteS16(&s_i16, 0), "ASMAtomicUoWriteS16:");
1179 BENCH(ASMAtomicUoWriteU32(&s_u32, 0), "ASMAtomicUoWriteU32:");
1180 BENCH(ASMAtomicUoWriteS32(&s_i32, 0), "ASMAtomicUoWriteS32:");
1181 BENCH(ASMAtomicUoWriteU64(&s_u64, 0), "ASMAtomicUoWriteU64:");
1182 BENCH(ASMAtomicUoWriteS64(&s_i64, 0), "ASMAtomicUoWriteS64:");
1183 BENCH(ASMAtomicWriteU8(&s_u8, 0), "ASMAtomicWriteU8:");
1184 BENCH(ASMAtomicWriteS8(&s_i8, 0), "ASMAtomicWriteS8:");
1185 BENCH(ASMAtomicWriteU16(&s_u16, 0), "ASMAtomicWriteU16:");
1186 BENCH(ASMAtomicWriteS16(&s_i16, 0), "ASMAtomicWriteS16:");
1187 BENCH(ASMAtomicWriteU32(&s_u32, 0), "ASMAtomicWriteU32:");
1188 BENCH(ASMAtomicWriteS32(&s_i32, 0), "ASMAtomicWriteS32:");
1189 BENCH(ASMAtomicWriteU64(&s_u64, 0), "ASMAtomicWriteU64:");
1190 BENCH(ASMAtomicWriteS64(&s_i64, 0), "ASMAtomicWriteS64:");
1191 BENCH(ASMAtomicXchgU8(&s_u8, 0), "ASMAtomicXchgU8:");
1192 BENCH(ASMAtomicXchgS8(&s_i8, 0), "ASMAtomicXchgS8:");
1193 BENCH(ASMAtomicXchgU16(&s_u16, 0), "ASMAtomicXchgU16:");
1194 BENCH(ASMAtomicXchgS16(&s_i16, 0), "ASMAtomicXchgS16:");
1195 BENCH(ASMAtomicXchgU32(&s_u32, 0), "ASMAtomicXchgU32:");
1196 BENCH(ASMAtomicXchgS32(&s_i32, 0), "ASMAtomicXchgS32:");
1197 BENCH(ASMAtomicXchgU64(&s_u64, 0), "ASMAtomicXchgU64:");
1198 BENCH(ASMAtomicXchgS64(&s_i64, 0), "ASMAtomicXchgS64:");
1199 BENCH(ASMAtomicCmpXchgU32(&s_u32, 0, 0), "ASMAtomicCmpXchgU32:");
1200 BENCH(ASMAtomicCmpXchgS32(&s_i32, 0, 0), "ASMAtomicCmpXchgS32:");
1201 BENCH(ASMAtomicCmpXchgU64(&s_u64, 0, 0), "ASMAtomicCmpXchgU64:");
1202 BENCH(ASMAtomicCmpXchgS64(&s_i64, 0, 0), "ASMAtomicCmpXchgS64:");
1203 BENCH(ASMAtomicCmpXchgU32(&s_u32, 0, 1), "ASMAtomicCmpXchgU32/neg:");
1204 BENCH(ASMAtomicCmpXchgS32(&s_i32, 0, 1), "ASMAtomicCmpXchgS32/neg:");
1205 BENCH(ASMAtomicCmpXchgU64(&s_u64, 0, 1), "ASMAtomicCmpXchgU64/neg:");
1206 BENCH(ASMAtomicCmpXchgS64(&s_i64, 0, 1), "ASMAtomicCmpXchgS64/neg:");
1207 BENCH(ASMAtomicIncU32(&s_u32), "ASMAtomicIncU32:");
1208 BENCH(ASMAtomicIncS32(&s_i32), "ASMAtomicIncS32:");
1209 BENCH(ASMAtomicDecU32(&s_u32), "ASMAtomicDecU32:");
1210 BENCH(ASMAtomicDecS32(&s_i32), "ASMAtomicDecS32:");
1211 BENCH(ASMAtomicAddU32(&s_u32, 5), "ASMAtomicAddU32:");
1212 BENCH(ASMAtomicAddS32(&s_i32, 5), "ASMAtomicAddS32:");
1213
1214 RTPrintf("Done.\n");
1215
1216#undef BENCH
1217}
1218
1219
1220int main(int argc, char *argv[])
1221{
1222 RTTEST hTest;
1223 int rc = RTTestInitAndCreate("tstInlineAsm", &hTest);
1224 if (rc)
1225 return rc;
1226 RTTestBanner(hTest);
1227
1228 /*
1229 * Execute the tests.
1230 */
1231#if !defined(PIC) || !defined(RT_ARCH_X86)
1232 tstASMCpuId();
1233#endif
1234 tstASMAtomicXchgU8();
1235 tstASMAtomicXchgU16();
1236 tstASMAtomicXchgU32();
1237 tstASMAtomicXchgU64();
1238 tstASMAtomicXchgPtr();
1239 tstASMAtomicCmpXchgU8();
1240 tstASMAtomicCmpXchgU32();
1241 tstASMAtomicCmpXchgU64();
1242 tstASMAtomicCmpXchgExU32();
1243 tstASMAtomicCmpXchgExU64();
1244 tstASMAtomicReadU64();
1245 tstASMAtomicAddS32();
1246 tstASMAtomicDecIncS32();
1247 tstASMAtomicAndOrU32();
1248 tstASMMemZeroPage();
1249 tstASMMemIsZeroPage(hTest);
1250 tstASMMemZero32();
1251 tstASMMemFill32();
1252 tstASMMath();
1253 tstASMByteSwap();
1254 tstASMBench();
1255
1256 /*
1257 * Show the result.
1258 */
1259 return RTTestSummaryAndDestroy(hTest);
1260}
1261
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette