VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTInlineAsm.cpp@ 93755

Last change on this file since 93755 was 93755, checked in by vboxsync, 3 years ago

tstRTInlineAsm: Use picoseconds instead of nanoseconds when benchmarking inline assembly operations. bugref:9898

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 151.2 KB
Line 
1/* $Id: tstRTInlineAsm.cpp 93755 2022-02-15 14:32:55Z vboxsync $ */
2/** @file
3 * IPRT Testcase - inline assembly.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
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
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/asm.h>
32#include <iprt/asm-math.h>
33
34/* See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44018. Only gcc version 4.4
35 * is affected. No harm for the VBox code: If the cpuid code compiles, it works
36 * fine. */
37#if defined(__GNUC__) && defined(RT_ARCH_X86) && defined(__PIC__)
38# if __GNUC__ == 4 && __GNUC_MINOR__ == 4
39# define GCC44_32BIT_PIC
40# endif
41#endif
42
43#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
44# include <iprt/asm-amd64-x86.h>
45# include <iprt/x86.h>
46#elif defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
47# include <iprt/asm-arm.h>
48# include <iprt/time.h>
49#else
50# include <iprt/time.h>
51#endif
52#include <iprt/mem.h>
53#include <iprt/param.h>
54#include <iprt/rand.h>
55#include <iprt/stream.h>
56#include <iprt/string.h>
57#include <iprt/thread.h>
58#include <iprt/test.h>
59#include <iprt/time.h>
60
61
62
63/*********************************************************************************************************************************
64* Defined Constants And Macros *
65*********************************************************************************************************************************/
66#define CHECKVAL(val, expect, fmt) \
67 do \
68 { \
69 if ((val) != (expect)) \
70 { \
71 RTTestFailed(g_hTest, "%s, %d: " #val ": expected " fmt " got " fmt "\n", __FUNCTION__, __LINE__, (expect), (val)); \
72 } \
73 } while (0)
74
75#define CHECKOP(op, expect, fmt, type) \
76 do \
77 { \
78 type val = op; \
79 if (val != (type)(expect)) \
80 { \
81 RTTestFailed(g_hTest, "%s, %d: " #op ": expected " fmt " got " fmt "\n", __FUNCTION__, __LINE__, (type)(expect), val); \
82 } \
83 } while (0)
84
85#define CHECK_OP_AND_VAL(a_Type, a_Fmt, a_pVar, a_Operation, a_ExpectRetVal, a_ExpectVarVal) \
86 do { \
87 CHECKOP(a_Operation, a_ExpectRetVal, a_Fmt, a_Type); \
88 CHECKVAL(*a_pVar, a_ExpectVarVal, a_Fmt); \
89 } while (0)
90
91#define CHECK_OP_AND_VAL_EX(a_TypeRet, a_FmtRet, a_FmtVar, a_pVar, a_Operation, a_ExpectRetVal, a_ExpectVarVal) \
92 do { \
93 CHECKOP(a_Operation, a_ExpectRetVal, a_FmtRet, a_TypeRet); \
94 CHECKVAL(*a_pVar, a_ExpectVarVal, a_FmtVar); \
95 } while (0)
96
97#define CHECK_OP_AND_VAL_EX2(a_TypeRet, a_FmtRet, a_FmtVar, a_pVar, a_uVar2, a_Operation, a_ExpectRetVal, a_ExpectVarVal, a_ExpectVarVal2) \
98 do { \
99 CHECKOP(a_Operation, a_ExpectRetVal, a_FmtRet, a_TypeRet); \
100 CHECKVAL(*a_pVar, a_ExpectVarVal, a_FmtVar); \
101 CHECKVAL(a_uVar2, a_ExpectVarVal2, a_FmtVar); \
102 } while (0)
103
104#define CHECKVAL128(a_pu128Val, a_u64HiExpect, a_u64LoExpect) \
105 do \
106 { \
107 if ((a_pu128Val)->s.Hi != (a_u64HiExpect) || (a_pu128Val)->s.Lo != (a_u64LoExpect)) \
108 RTTestFailed(g_hTest, "%s, %d: " #a_pu128Val ": expected %#RX64'%016RX64 got %#RX64'%016RX64\n", \
109 __FUNCTION__, __LINE__, (a_u64HiExpect), (a_u64LoExpect), (a_pu128Val)->s.Hi, (a_pu128Val)->s.Lo); \
110 } while (0)
111#define CHECKVAL128_C(a_pu128Val, a_u64HiExpect, a_u64LoExpect) \
112 do \
113 { \
114 if ((a_pu128Val)->s.Hi != UINT64_C(a_u64HiExpect) || (a_pu128Val)->s.Lo != UINT64_C(a_u64LoExpect)) \
115 RTTestFailed(g_hTest, "%s, %d: " #a_pu128Val ": expected %#RX64'%016RX64 got %#RX64'%016RX64\n", \
116 __FUNCTION__, __LINE__, UINT64_C(a_u64HiExpect), UINT64_C(a_u64LoExpect), \
117 (a_pu128Val)->s.Hi, (a_pu128Val)->s.Lo); \
118 } while (0)
119#define CHECK_OP_AND_VAL_128(a_TypeRet, a_FmtRet, a_pu128Val, a_Operation, a_ExpectRetVal, a_u64HiExpect, a_u64LoExpect) \
120 do { \
121 CHECKOP(a_Operation, a_ExpectRetVal, a_FmtRet, a_TypeRet); \
122 CHECKVAL128(a_pu128Val, a_u64HiExpect, a_u64LoExpect); \
123 } while (0)
124#define CHECK_OP_AND_VAL_128_C(a_TypeRet, a_FmtRet, a_pu128Val, a_Operation, a_ExpectRetVal, a_u64HiExpect, a_u64LoExpect) \
125 do { \
126 CHECKOP(a_Operation, a_ExpectRetVal, a_FmtRet, a_TypeRet); \
127 CHECKVAL128_C(a_pu128Val, a_u64HiExpect, a_u64LoExpect); \
128 } while (0)
129
130/**
131 * Calls a worker function with different worker variable storage types.
132 */
133#define DO_SIMPLE_TEST_NO_SUB_NO_STACK(a_WorkerFunction, type) \
134 do \
135 { \
136 type *pVar = (type *)RTTestGuardedAllocHead(g_hTest, sizeof(type)); \
137 RTTEST_CHECK_BREAK(g_hTest, pVar); \
138 a_WorkerFunction(pVar); \
139 RTTestGuardedFree(g_hTest, pVar); \
140 \
141 pVar = (type *)RTTestGuardedAllocTail(g_hTest, sizeof(type)); \
142 RTTEST_CHECK_BREAK(g_hTest, pVar); \
143 a_WorkerFunction(pVar); \
144 RTTestGuardedFree(g_hTest, pVar); \
145 } while (0)
146
147
148/**
149 * Calls a worker function with different worker variable storage types.
150 */
151#define DO_SIMPLE_TEST_NO_SUB(a_WorkerFunction, type) \
152 do \
153 { \
154 type StackVar; \
155 a_WorkerFunction(&StackVar); \
156 DO_SIMPLE_TEST_NO_SUB_NO_STACK(a_WorkerFunction, type); \
157 } while (0)
158
159/**
160 * Calls a worker function with different worker variable storage types.
161 */
162#define DO_SIMPLE_TEST(name, type) \
163 do \
164 { \
165 RTTestISub(#name); \
166 DO_SIMPLE_TEST_NO_SUB(tst ## name ## Worker, type); \
167 } while (0)
168
169
170/*********************************************************************************************************************************
171* Global Variables *
172*********************************************************************************************************************************/
173/** The test instance. */
174static RTTEST g_hTest;
175
176
177
178#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
179
180const char *getCacheAss(unsigned u)
181{
182 if (u == 0)
183 return "res0 ";
184 if (u == 1)
185 return "direct";
186 if (u >= 256)
187 return "???";
188
189 char *pszRet = NULL;
190 RTStrAPrintf(&pszRet, "%d way", u);
191 RTMEM_WILL_LEAK(pszRet);
192 return pszRet;
193}
194
195
196const char *getL2CacheAss(unsigned u)
197{
198 switch (u)
199 {
200 case 0: return "off ";
201 case 1: return "direct";
202 case 2: return "2 way ";
203 case 3: return "res3 ";
204 case 4: return "4 way ";
205 case 5: return "res5 ";
206 case 6: return "8 way ";
207 case 7: return "res7 ";
208 case 8: return "16 way";
209 case 9: return "res9 ";
210 case 10: return "res10 ";
211 case 11: return "res11 ";
212 case 12: return "res12 ";
213 case 13: return "res13 ";
214 case 14: return "res14 ";
215 case 15: return "fully ";
216 default:
217 return "????";
218 }
219}
220
221
222/**
223 * Test and dump all possible info from the CPUID instruction.
224 *
225 * @remark Bits shared with the libc cpuid.c program. This all written by me, so no worries.
226 * @todo transform the dumping into a generic runtime function. We'll need it for logging!
227 */
228void tstASMCpuId(void)
229{
230 RTTestISub("ASMCpuId");
231
232 unsigned iBit;
233 struct
234 {
235 uint32_t uEBX, uEAX, uEDX, uECX;
236 } s;
237 if (!ASMHasCpuId())
238 {
239 RTTestIPrintf(RTTESTLVL_ALWAYS, "warning! CPU doesn't support CPUID\n");
240 return;
241 }
242
243 /*
244 * Try the 0 function and use that for checking the ASMCpuId_* variants.
245 */
246 ASMCpuId(0, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
247
248 uint32_t u32;
249
250 u32 = ASMCpuId_EAX(0);
251 CHECKVAL(u32, s.uEAX, "%x");
252 u32 = ASMCpuId_EBX(0);
253 CHECKVAL(u32, s.uEBX, "%x");
254 u32 = ASMCpuId_ECX(0);
255 CHECKVAL(u32, s.uECX, "%x");
256 u32 = ASMCpuId_EDX(0);
257 CHECKVAL(u32, s.uEDX, "%x");
258
259 uint32_t uECX2 = s.uECX - 1;
260 uint32_t uEDX2 = s.uEDX - 1;
261 ASMCpuId_ECX_EDX(0, &uECX2, &uEDX2);
262 CHECKVAL(uECX2, s.uECX, "%x");
263 CHECKVAL(uEDX2, s.uEDX, "%x");
264
265 uint32_t uEAX2 = s.uEAX - 1;
266 uint32_t uEBX2 = s.uEBX - 1;
267 uECX2 = s.uECX - 1;
268 uEDX2 = s.uEDX - 1;
269 ASMCpuIdExSlow(0, 0, 0, 0, &uEAX2, &uEBX2, &uECX2, &uEDX2);
270 CHECKVAL(uEAX2, s.uEAX, "%x");
271 CHECKVAL(uEBX2, s.uEBX, "%x");
272 CHECKVAL(uECX2, s.uECX, "%x");
273 CHECKVAL(uEDX2, s.uEDX, "%x");
274
275 /*
276 * Check the extended APIC stuff.
277 */
278 uint32_t idExtApic;
279 if (ASMCpuId_EAX(0) >= 0xb)
280 {
281 uint8_t idApic = ASMGetApicId();
282 do
283 {
284 uEAX2 = uEBX2 = uECX2 = uEDX2 = UINT32_C(0x50486744);
285 ASMCpuIdExSlow(0xb, 0, 0, 0, &uEAX2, &uEBX2, &uECX2, &uEDX2);
286 idExtApic = ASMGetApicIdExt0B();
287 } while (ASMGetApicId() != idApic);
288
289 CHECKVAL(uEDX2, idExtApic, "%x");
290 if (idApic != (uint8_t)idExtApic && uECX2 != 0)
291 RTTestIFailed("ASMGetApicIdExt0B() -> %#x vs ASMGetApicId() -> %#x", idExtApic, idApic);
292 }
293 if (ASMCpuId_EAX(UINT32_C(0x80000000)) >= UINT32_C(0x8000001E))
294 {
295 uint8_t idApic = ASMGetApicId();
296 do
297 {
298 uEAX2 = uEBX2 = uECX2 = uEDX2 = UINT32_C(0x50486744);
299 ASMCpuIdExSlow(0x8000001e, 0, 0, 0, &uEAX2, &uEBX2, &uECX2, &uEDX2);
300 idExtApic = ASMGetApicIdExt8000001E();
301 } while (ASMGetApicId() != idApic);
302 CHECKVAL(uEAX2, idExtApic, "%x");
303 if (idApic != (uint8_t)idExtApic)
304 RTTestIFailed("ASMGetApicIdExt8000001E() -> %#x vs ASMGetApicId() -> %#x", idExtApic, idApic);
305 }
306
307 /*
308 * Done testing, dump the information.
309 */
310 RTTestIPrintf(RTTESTLVL_ALWAYS, "CPUID Dump\n");
311 ASMCpuId(0, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
312 const uint32_t cFunctions = s.uEAX;
313
314 /* raw dump */
315 RTTestIPrintf(RTTESTLVL_ALWAYS,
316 "\n"
317 " RAW Standard CPUIDs\n"
318 "Function eax ebx ecx edx\n");
319 for (unsigned iStd = 0; iStd <= cFunctions + 3; iStd++)
320 {
321 ASMCpuId_Idx_ECX(iStd, 0, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
322 RTTestIPrintf(RTTESTLVL_ALWAYS, "%08x %08x %08x %08x %08x%s\n",
323 iStd, s.uEAX, s.uEBX, s.uECX, s.uEDX, iStd <= cFunctions ? "" : "*");
324
325 /* Some leafs output depend on the initial value of ECX.
326 * The same seems to apply to invalid standard functions */
327 if (iStd > cFunctions)
328 continue;
329 if (iStd == 0x04) /* Deterministic Cache Parameters Leaf */
330 for (uint32_t uECX = 1; s.uEAX & 0x1f; uECX++)
331 {
332 ASMCpuId_Idx_ECX(iStd, uECX, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
333 RTTestIPrintf(RTTESTLVL_ALWAYS, " [%02x] %08x %08x %08x %08x\n", uECX, s.uEAX, s.uEBX, s.uECX, s.uEDX);
334 RTTESTI_CHECK_BREAK(uECX < 128);
335 }
336 else if (iStd == 0x07) /* Structured Extended Feature Flags */
337 {
338 uint32_t uMax = s.uEAX;
339 for (uint32_t uECX = 1; uECX < uMax; uECX++)
340 {
341 ASMCpuId_Idx_ECX(iStd, uECX, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
342 RTTestIPrintf(RTTESTLVL_ALWAYS, " [%02x] %08x %08x %08x %08x\n", uECX, s.uEAX, s.uEBX, s.uECX, s.uEDX);
343 RTTESTI_CHECK_BREAK(uECX < 128);
344 }
345 }
346 else if (iStd == 0x0b) /* Extended Topology Enumeration Leafs */
347 for (uint32_t uECX = 1; (s.uEAX & 0x1f) && (s.uEBX & 0xffff); uECX++)
348 {
349 ASMCpuId_Idx_ECX(iStd, uECX, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
350 RTTestIPrintf(RTTESTLVL_ALWAYS, " [%02x] %08x %08x %08x %08x\n", uECX, s.uEAX, s.uEBX, s.uECX, s.uEDX);
351 RTTESTI_CHECK_BREAK(uECX < 128);
352 }
353 else if (iStd == 0x0d) /* Extended State Enumeration Leafs */
354 for (uint32_t uECX = 1; s.uEAX != 0 || s.uEBX != 0 || s.uECX != 0 || s.uEDX != 0; uECX++)
355 {
356 ASMCpuId_Idx_ECX(iStd, uECX, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
357 RTTestIPrintf(RTTESTLVL_ALWAYS, " [%02x] %08x %08x %08x %08x\n", uECX, s.uEAX, s.uEBX, s.uECX, s.uEDX);
358 RTTESTI_CHECK_BREAK(uECX < 128);
359 }
360 else if ( iStd == 0x0f /* Platform quality of service monitoring (PQM) */
361 || iStd == 0x10 /* Platform quality of service enforcement (PQE) */
362 || iStd == 0x12 /* SGX Enumeration */
363 || iStd == 0x14 /* Processor Trace Enumeration */
364 || iStd == 0x17 /* SoC Vendor Attribute Enumeration */
365 || iStd == 0x18 /* Deterministic Address Translation Parameters */)
366 {
367 /** @todo */
368 }
369 else
370 {
371 u32 = ASMCpuId_EAX(iStd);
372 CHECKVAL(u32, s.uEAX, "%x");
373
374 uint32_t u32EbxMask = UINT32_MAX;
375 if (iStd == 1)
376 u32EbxMask = UINT32_C(0x00ffffff); /* Omit the local apic ID in case we're rescheduled. */
377 u32 = ASMCpuId_EBX(iStd);
378 CHECKVAL(u32 & u32EbxMask, s.uEBX & u32EbxMask, "%x");
379
380 u32 = ASMCpuId_ECX(iStd);
381 CHECKVAL(u32, s.uECX, "%x");
382 u32 = ASMCpuId_EDX(iStd);
383 CHECKVAL(u32, s.uEDX, "%x");
384
385 uECX2 = s.uECX - 1;
386 uEDX2 = s.uEDX - 1;
387 ASMCpuId_ECX_EDX(iStd, &uECX2, &uEDX2);
388 CHECKVAL(uECX2, s.uECX, "%x");
389 CHECKVAL(uEDX2, s.uEDX, "%x");
390
391 uEAX2 = s.uEAX - 1;
392 uEBX2 = s.uEBX - 1;
393 uECX2 = s.uECX - 1;
394 uEDX2 = s.uEDX - 1;
395 ASMCpuId(iStd, &uEAX2, &uEBX2, &uECX2, &uEDX2);
396 CHECKVAL(uEAX2, s.uEAX, "%x");
397 CHECKVAL(uEBX2 & u32EbxMask, s.uEBX & u32EbxMask, "%x");
398 CHECKVAL(uECX2, s.uECX, "%x");
399 CHECKVAL(uEDX2, s.uEDX, "%x");
400 }
401 }
402
403 /*
404 * Understandable output
405 */
406 ASMCpuId(0, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
407 RTTestIPrintf(RTTESTLVL_ALWAYS,
408 "Name: %.04s%.04s%.04s\n"
409 "Support: 0-%u\n",
410 &s.uEBX, &s.uEDX, &s.uECX, s.uEAX);
411 bool const fIntel = RTX86IsIntelCpu(s.uEBX, s.uECX, s.uEDX);
412
413 /*
414 * Get Features.
415 */
416 if (cFunctions >= 1)
417 {
418 static const char * const s_apszTypes[4] = { "primary", "overdrive", "MP", "reserved" };
419 ASMCpuId(1, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
420 RTTestIPrintf(RTTESTLVL_ALWAYS,
421 "Family: %#x \tExtended: %#x \tEffective: %#x\n"
422 "Model: %#x \tExtended: %#x \tEffective: %#x\n"
423 "Stepping: %d\n"
424 "Type: %d (%s)\n"
425 "APIC ID: %#04x\n"
426 "Logical CPUs: %d\n"
427 "CLFLUSH Size: %d\n"
428 "Brand ID: %#04x\n",
429 (s.uEAX >> 8) & 0xf, (s.uEAX >> 20) & 0x7f, RTX86GetCpuFamily(s.uEAX),
430 (s.uEAX >> 4) & 0xf, (s.uEAX >> 16) & 0x0f, RTX86GetCpuModel(s.uEAX, fIntel),
431 RTX86GetCpuStepping(s.uEAX),
432 (s.uEAX >> 12) & 0x3, s_apszTypes[(s.uEAX >> 12) & 0x3],
433 (s.uEBX >> 24) & 0xff,
434 (s.uEBX >> 16) & 0xff,
435 (s.uEBX >> 8) & 0xff,
436 (s.uEBX >> 0) & 0xff);
437
438 RTTestIPrintf(RTTESTLVL_ALWAYS, "Features EDX: ");
439 if (s.uEDX & RT_BIT(0)) RTTestIPrintf(RTTESTLVL_ALWAYS, " FPU");
440 if (s.uEDX & RT_BIT(1)) RTTestIPrintf(RTTESTLVL_ALWAYS, " VME");
441 if (s.uEDX & RT_BIT(2)) RTTestIPrintf(RTTESTLVL_ALWAYS, " DE");
442 if (s.uEDX & RT_BIT(3)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PSE");
443 if (s.uEDX & RT_BIT(4)) RTTestIPrintf(RTTESTLVL_ALWAYS, " TSC");
444 if (s.uEDX & RT_BIT(5)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MSR");
445 if (s.uEDX & RT_BIT(6)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PAE");
446 if (s.uEDX & RT_BIT(7)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MCE");
447 if (s.uEDX & RT_BIT(8)) RTTestIPrintf(RTTESTLVL_ALWAYS, " CX8");
448 if (s.uEDX & RT_BIT(9)) RTTestIPrintf(RTTESTLVL_ALWAYS, " APIC");
449 if (s.uEDX & RT_BIT(10)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 10");
450 if (s.uEDX & RT_BIT(11)) RTTestIPrintf(RTTESTLVL_ALWAYS, " SEP");
451 if (s.uEDX & RT_BIT(12)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MTRR");
452 if (s.uEDX & RT_BIT(13)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PGE");
453 if (s.uEDX & RT_BIT(14)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MCA");
454 if (s.uEDX & RT_BIT(15)) RTTestIPrintf(RTTESTLVL_ALWAYS, " CMOV");
455 if (s.uEDX & RT_BIT(16)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PAT");
456 if (s.uEDX & RT_BIT(17)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PSE36");
457 if (s.uEDX & RT_BIT(18)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PSN");
458 if (s.uEDX & RT_BIT(19)) RTTestIPrintf(RTTESTLVL_ALWAYS, " CLFSH");
459 if (s.uEDX & RT_BIT(20)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 20");
460 if (s.uEDX & RT_BIT(21)) RTTestIPrintf(RTTESTLVL_ALWAYS, " DS");
461 if (s.uEDX & RT_BIT(22)) RTTestIPrintf(RTTESTLVL_ALWAYS, " ACPI");
462 if (s.uEDX & RT_BIT(23)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MMX");
463 if (s.uEDX & RT_BIT(24)) RTTestIPrintf(RTTESTLVL_ALWAYS, " FXSR");
464 if (s.uEDX & RT_BIT(25)) RTTestIPrintf(RTTESTLVL_ALWAYS, " SSE");
465 if (s.uEDX & RT_BIT(26)) RTTestIPrintf(RTTESTLVL_ALWAYS, " SSE2");
466 if (s.uEDX & RT_BIT(27)) RTTestIPrintf(RTTESTLVL_ALWAYS, " SS");
467 if (s.uEDX & RT_BIT(28)) RTTestIPrintf(RTTESTLVL_ALWAYS, " HTT");
468 if (s.uEDX & RT_BIT(29)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 29");
469 if (s.uEDX & RT_BIT(30)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 30");
470 if (s.uEDX & RT_BIT(31)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 31");
471 RTTestIPrintf(RTTESTLVL_ALWAYS, "\n");
472
473 /** @todo check intel docs. */
474 RTTestIPrintf(RTTESTLVL_ALWAYS, "Features ECX: ");
475 if (s.uECX & RT_BIT(0)) RTTestIPrintf(RTTESTLVL_ALWAYS, " SSE3");
476 for (iBit = 1; iBit < 13; iBit++)
477 if (s.uECX & RT_BIT(iBit))
478 RTTestIPrintf(RTTESTLVL_ALWAYS, " %d", iBit);
479 if (s.uECX & RT_BIT(13)) RTTestIPrintf(RTTESTLVL_ALWAYS, " CX16");
480 for (iBit = 14; iBit < 32; iBit++)
481 if (s.uECX & RT_BIT(iBit))
482 RTTestIPrintf(RTTESTLVL_ALWAYS, " %d", iBit);
483 RTTestIPrintf(RTTESTLVL_ALWAYS, "\n");
484 }
485 if (ASMCpuId_EAX(0) >= 0xb)
486 RTTestIPrintf(RTTESTLVL_ALWAYS, "APIC ID(Ext 0b): %#010x\n", ASMGetApicIdExt0B());
487
488 /*
489 * Extended.
490 * Implemented after AMD specs.
491 */
492 /** @todo check out the intel specs. */
493 ASMCpuId(0x80000000, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
494 if (!s.uEAX && !s.uEBX && !s.uECX && !s.uEDX)
495 {
496 RTTestIPrintf(RTTESTLVL_ALWAYS, "No extended CPUID info? Check the manual on how to detect this...\n");
497 return;
498 }
499 const uint32_t cExtFunctions = s.uEAX | 0x80000000;
500
501 /* raw dump */
502 RTTestIPrintf(RTTESTLVL_ALWAYS,
503 "\n"
504 " RAW Extended CPUIDs\n"
505 "Function eax ebx ecx edx\n");
506 for (unsigned iExt = 0x80000000; iExt <= cExtFunctions + 3; iExt++)
507 {
508 ASMCpuId(iExt, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
509 RTTestIPrintf(RTTESTLVL_ALWAYS, "%08x %08x %08x %08x %08x%s\n",
510 iExt, s.uEAX, s.uEBX, s.uECX, s.uEDX, iExt <= cExtFunctions ? "" : "*");
511
512 if (iExt > cExtFunctions)
513 continue; /* Invalid extended functions seems change the value if ECX changes */
514 if (iExt == 0x8000001d)
515 continue; /* Takes cache level in ecx. */
516
517 u32 = ASMCpuId_EAX(iExt);
518 CHECKVAL(u32, s.uEAX, "%x");
519 u32 = ASMCpuId_EBX(iExt);
520 CHECKVAL(u32, s.uEBX, "%x");
521 u32 = ASMCpuId_ECX(iExt);
522 CHECKVAL(u32, s.uECX, "%x");
523 u32 = ASMCpuId_EDX(iExt);
524 CHECKVAL(u32, s.uEDX, "%x");
525
526 uECX2 = s.uECX - 1;
527 uEDX2 = s.uEDX - 1;
528 ASMCpuId_ECX_EDX(iExt, &uECX2, &uEDX2);
529 CHECKVAL(uECX2, s.uECX, "%x");
530 CHECKVAL(uEDX2, s.uEDX, "%x");
531
532 uEAX2 = s.uEAX - 1;
533 uEBX2 = s.uEBX - 1;
534 uECX2 = s.uECX - 1;
535 uEDX2 = s.uEDX - 1;
536 ASMCpuId(iExt, &uEAX2, &uEBX2, &uECX2, &uEDX2);
537 CHECKVAL(uEAX2, s.uEAX, "%x");
538 CHECKVAL(uEBX2, s.uEBX, "%x");
539 CHECKVAL(uECX2, s.uECX, "%x");
540 CHECKVAL(uEDX2, s.uEDX, "%x");
541 }
542
543 /*
544 * Understandable output
545 */
546 ASMCpuId(0x80000000, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
547 RTTestIPrintf(RTTESTLVL_ALWAYS,
548 "Ext Name: %.4s%.4s%.4s\n"
549 "Ext Supports: 0x80000000-%#010x\n",
550 &s.uEBX, &s.uEDX, &s.uECX, s.uEAX);
551
552 if (cExtFunctions >= 0x80000001)
553 {
554 ASMCpuId(0x80000001, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
555 RTTestIPrintf(RTTESTLVL_ALWAYS,
556 "Family: %#x \tExtended: %#x \tEffective: %#x\n"
557 "Model: %#x \tExtended: %#x \tEffective: %#x\n"
558 "Stepping: %d\n"
559 "Brand ID: %#05x\n",
560 (s.uEAX >> 8) & 0xf, (s.uEAX >> 20) & 0x7f, RTX86GetCpuFamily(s.uEAX),
561 (s.uEAX >> 4) & 0xf, (s.uEAX >> 16) & 0x0f, RTX86GetCpuModel(s.uEAX, fIntel),
562 RTX86GetCpuStepping(s.uEAX),
563 s.uEBX & 0xfff);
564
565 RTTestIPrintf(RTTESTLVL_ALWAYS, "Features EDX: ");
566 if (s.uEDX & RT_BIT(0)) RTTestIPrintf(RTTESTLVL_ALWAYS, " FPU");
567 if (s.uEDX & RT_BIT(1)) RTTestIPrintf(RTTESTLVL_ALWAYS, " VME");
568 if (s.uEDX & RT_BIT(2)) RTTestIPrintf(RTTESTLVL_ALWAYS, " DE");
569 if (s.uEDX & RT_BIT(3)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PSE");
570 if (s.uEDX & RT_BIT(4)) RTTestIPrintf(RTTESTLVL_ALWAYS, " TSC");
571 if (s.uEDX & RT_BIT(5)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MSR");
572 if (s.uEDX & RT_BIT(6)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PAE");
573 if (s.uEDX & RT_BIT(7)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MCE");
574 if (s.uEDX & RT_BIT(8)) RTTestIPrintf(RTTESTLVL_ALWAYS, " CMPXCHG8B");
575 if (s.uEDX & RT_BIT(9)) RTTestIPrintf(RTTESTLVL_ALWAYS, " APIC");
576 if (s.uEDX & RT_BIT(10)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 10");
577 if (s.uEDX & RT_BIT(11)) RTTestIPrintf(RTTESTLVL_ALWAYS, " SysCallSysRet");
578 if (s.uEDX & RT_BIT(12)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MTRR");
579 if (s.uEDX & RT_BIT(13)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PGE");
580 if (s.uEDX & RT_BIT(14)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MCA");
581 if (s.uEDX & RT_BIT(15)) RTTestIPrintf(RTTESTLVL_ALWAYS, " CMOV");
582 if (s.uEDX & RT_BIT(16)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PAT");
583 if (s.uEDX & RT_BIT(17)) RTTestIPrintf(RTTESTLVL_ALWAYS, " PSE36");
584 if (s.uEDX & RT_BIT(18)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 18");
585 if (s.uEDX & RT_BIT(19)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 19");
586 if (s.uEDX & RT_BIT(20)) RTTestIPrintf(RTTESTLVL_ALWAYS, " NX");
587 if (s.uEDX & RT_BIT(21)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 21");
588 if (s.uEDX & RT_BIT(22)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MmxExt");
589 if (s.uEDX & RT_BIT(23)) RTTestIPrintf(RTTESTLVL_ALWAYS, " MMX");
590 if (s.uEDX & RT_BIT(24)) RTTestIPrintf(RTTESTLVL_ALWAYS, " FXSR");
591 if (s.uEDX & RT_BIT(25)) RTTestIPrintf(RTTESTLVL_ALWAYS, " FastFXSR");
592 if (s.uEDX & RT_BIT(26)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 26");
593 if (s.uEDX & RT_BIT(27)) RTTestIPrintf(RTTESTLVL_ALWAYS, " RDTSCP");
594 if (s.uEDX & RT_BIT(28)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 28");
595 if (s.uEDX & RT_BIT(29)) RTTestIPrintf(RTTESTLVL_ALWAYS, " LongMode");
596 if (s.uEDX & RT_BIT(30)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 3DNowExt");
597 if (s.uEDX & RT_BIT(31)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 3DNow");
598 RTTestIPrintf(RTTESTLVL_ALWAYS, "\n");
599
600 RTTestIPrintf(RTTESTLVL_ALWAYS, "Features ECX: ");
601 if (s.uECX & RT_BIT(0)) RTTestIPrintf(RTTESTLVL_ALWAYS, " LahfSahf");
602 if (s.uECX & RT_BIT(1)) RTTestIPrintf(RTTESTLVL_ALWAYS, " CmpLegacy");
603 if (s.uECX & RT_BIT(2)) RTTestIPrintf(RTTESTLVL_ALWAYS, " SVM");
604 if (s.uECX & RT_BIT(3)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 3");
605 if (s.uECX & RT_BIT(4)) RTTestIPrintf(RTTESTLVL_ALWAYS, " AltMovCr8");
606 for (iBit = 5; iBit < 32; iBit++)
607 if (s.uECX & RT_BIT(iBit))
608 RTTestIPrintf(RTTESTLVL_ALWAYS, " %d", iBit);
609 RTTestIPrintf(RTTESTLVL_ALWAYS, "\n");
610 }
611
612 char szString[4*4*3+1] = {0};
613 if (cExtFunctions >= 0x80000002)
614 ASMCpuId(0x80000002, &szString[0 + 0], &szString[0 + 4], &szString[0 + 8], &szString[0 + 12]);
615 if (cExtFunctions >= 0x80000003)
616 ASMCpuId(0x80000003, &szString[16 + 0], &szString[16 + 4], &szString[16 + 8], &szString[16 + 12]);
617 if (cExtFunctions >= 0x80000004)
618 ASMCpuId(0x80000004, &szString[32 + 0], &szString[32 + 4], &szString[32 + 8], &szString[32 + 12]);
619 if (cExtFunctions >= 0x80000002)
620 RTTestIPrintf(RTTESTLVL_ALWAYS, "Full Name: %s\n", szString);
621
622 if (cExtFunctions >= 0x80000005)
623 {
624 ASMCpuId(0x80000005, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
625 RTTestIPrintf(RTTESTLVL_ALWAYS,
626 "TLB 2/4M Instr/Uni: %s %3d entries\n"
627 "TLB 2/4M Data: %s %3d entries\n",
628 getCacheAss((s.uEAX >> 8) & 0xff), (s.uEAX >> 0) & 0xff,
629 getCacheAss((s.uEAX >> 24) & 0xff), (s.uEAX >> 16) & 0xff);
630 RTTestIPrintf(RTTESTLVL_ALWAYS,
631 "TLB 4K Instr/Uni: %s %3d entries\n"
632 "TLB 4K Data: %s %3d entries\n",
633 getCacheAss((s.uEBX >> 8) & 0xff), (s.uEBX >> 0) & 0xff,
634 getCacheAss((s.uEBX >> 24) & 0xff), (s.uEBX >> 16) & 0xff);
635 RTTestIPrintf(RTTESTLVL_ALWAYS,
636 "L1 Instr Cache Line Size: %d bytes\n"
637 "L1 Instr Cache Lines Per Tag: %d\n"
638 "L1 Instr Cache Associativity: %s\n"
639 "L1 Instr Cache Size: %d KB\n",
640 (s.uEDX >> 0) & 0xff,
641 (s.uEDX >> 8) & 0xff,
642 getCacheAss((s.uEDX >> 16) & 0xff),
643 (s.uEDX >> 24) & 0xff);
644 RTTestIPrintf(RTTESTLVL_ALWAYS,
645 "L1 Data Cache Line Size: %d bytes\n"
646 "L1 Data Cache Lines Per Tag: %d\n"
647 "L1 Data Cache Associativity: %s\n"
648 "L1 Data Cache Size: %d KB\n",
649 (s.uECX >> 0) & 0xff,
650 (s.uECX >> 8) & 0xff,
651 getCacheAss((s.uECX >> 16) & 0xff),
652 (s.uECX >> 24) & 0xff);
653 }
654
655 if (cExtFunctions >= 0x80000006)
656 {
657 ASMCpuId(0x80000006, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
658 RTTestIPrintf(RTTESTLVL_ALWAYS,
659 "L2 TLB 2/4M Instr/Uni: %s %4d entries\n"
660 "L2 TLB 2/4M Data: %s %4d entries\n",
661 getL2CacheAss((s.uEAX >> 12) & 0xf), (s.uEAX >> 0) & 0xfff,
662 getL2CacheAss((s.uEAX >> 28) & 0xf), (s.uEAX >> 16) & 0xfff);
663 RTTestIPrintf(RTTESTLVL_ALWAYS,
664 "L2 TLB 4K Instr/Uni: %s %4d entries\n"
665 "L2 TLB 4K Data: %s %4d entries\n",
666 getL2CacheAss((s.uEBX >> 12) & 0xf), (s.uEBX >> 0) & 0xfff,
667 getL2CacheAss((s.uEBX >> 28) & 0xf), (s.uEBX >> 16) & 0xfff);
668 RTTestIPrintf(RTTESTLVL_ALWAYS,
669 "L2 Cache Line Size: %d bytes\n"
670 "L2 Cache Lines Per Tag: %d\n"
671 "L2 Cache Associativity: %s\n"
672 "L2 Cache Size: %d KB\n",
673 (s.uEDX >> 0) & 0xff,
674 (s.uEDX >> 8) & 0xf,
675 getL2CacheAss((s.uEDX >> 12) & 0xf),
676 (s.uEDX >> 16) & 0xffff);
677 }
678
679 if (cExtFunctions >= 0x80000007)
680 {
681 ASMCpuId(0x80000007, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
682 RTTestIPrintf(RTTESTLVL_ALWAYS, "APM Features: ");
683 if (s.uEDX & RT_BIT(0)) RTTestIPrintf(RTTESTLVL_ALWAYS, " TS");
684 if (s.uEDX & RT_BIT(1)) RTTestIPrintf(RTTESTLVL_ALWAYS, " FID");
685 if (s.uEDX & RT_BIT(2)) RTTestIPrintf(RTTESTLVL_ALWAYS, " VID");
686 if (s.uEDX & RT_BIT(3)) RTTestIPrintf(RTTESTLVL_ALWAYS, " TTP");
687 if (s.uEDX & RT_BIT(4)) RTTestIPrintf(RTTESTLVL_ALWAYS, " TM");
688 if (s.uEDX & RT_BIT(5)) RTTestIPrintf(RTTESTLVL_ALWAYS, " STC");
689 if (s.uEDX & RT_BIT(6)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 6");
690 if (s.uEDX & RT_BIT(7)) RTTestIPrintf(RTTESTLVL_ALWAYS, " 7");
691 if (s.uEDX & RT_BIT(8)) RTTestIPrintf(RTTESTLVL_ALWAYS, " TscInvariant");
692 for (iBit = 9; iBit < 32; iBit++)
693 if (s.uEDX & RT_BIT(iBit))
694 RTTestIPrintf(RTTESTLVL_ALWAYS, " %d", iBit);
695 RTTestIPrintf(RTTESTLVL_ALWAYS, "\n");
696 }
697
698 if (cExtFunctions >= 0x80000008)
699 {
700 ASMCpuId(0x80000008, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
701 RTTestIPrintf(RTTESTLVL_ALWAYS,
702 "Physical Address Width: %d bits\n"
703 "Virtual Address Width: %d bits\n"
704 "Guest Physical Address Width: %d bits\n",
705 (s.uEAX >> 0) & 0xff,
706 (s.uEAX >> 8) & 0xff,
707 (s.uEAX >> 16) & 0xff);
708 RTTestIPrintf(RTTESTLVL_ALWAYS,
709 "Physical Core Count: %d\n",
710 ((s.uECX >> 0) & 0xff) + 1);
711 if ((s.uECX >> 12) & 0xf)
712 RTTestIPrintf(RTTESTLVL_ALWAYS, "ApicIdCoreIdSize: %d bits\n", (s.uECX >> 12) & 0xf);
713 }
714
715 if (cExtFunctions >= 0x8000000a)
716 {
717 ASMCpuId(0x8000000a, &s.uEAX, &s.uEBX, &s.uECX, &s.uEDX);
718 RTTestIPrintf(RTTESTLVL_ALWAYS,
719 "SVM Revision: %d (%#x)\n"
720 "Number of Address Space IDs: %d (%#x)\n",
721 s.uEAX & 0xff, s.uEAX & 0xff,
722 s.uEBX, s.uEBX);
723 }
724 if (ASMCpuId_EAX(UINT32_C(0x80000000)) >= UINT32_C(0x8000001E))
725 RTTestIPrintf(RTTESTLVL_ALWAYS, "APIC ID(Ext 8000001b): %#010x\n", ASMGetApicIdExt8000001E());
726}
727
728# if 0
729static void bruteForceCpuId(void)
730{
731 RTTestISub("brute force CPUID leafs");
732 uint32_t auPrevValues[4] = { 0, 0, 0, 0};
733 uint32_t uLeaf = 0;
734 do
735 {
736 uint32_t auValues[4];
737 ASMCpuIdExSlow(uLeaf, 0, 0, 0, &auValues[0], &auValues[1], &auValues[2], &auValues[3]);
738 if ( (auValues[0] != auPrevValues[0] && auValues[0] != uLeaf)
739 || (auValues[1] != auPrevValues[1] && auValues[1] != 0)
740 || (auValues[2] != auPrevValues[2] && auValues[2] != 0)
741 || (auValues[3] != auPrevValues[3] && auValues[3] != 0)
742 || (uLeaf & (UINT32_C(0x08000000) - UINT32_C(1))) == 0)
743 {
744 RTTestIPrintf(RTTESTLVL_ALWAYS,
745 "%08x: %08x %08x %08x %08x\n", uLeaf,
746 auValues[0], auValues[1], auValues[2], auValues[3]);
747 }
748 auPrevValues[0] = auValues[0];
749 auPrevValues[1] = auValues[1];
750 auPrevValues[2] = auValues[2];
751 auPrevValues[3] = auValues[3];
752
753 //uint32_t uSubLeaf = 0;
754 //do
755 //{
756 //
757 //
758 //} while (false);
759 } while (uLeaf++ < UINT32_MAX);
760}
761# endif
762
763#endif /* AMD64 || X86 */
764
765#define TEST_READ(a_pVar, a_Type, a_Fmt, a_Function, a_Val) \
766 do { *a_pVar = a_Val; CHECKOP(a_Function(a_pVar), a_Val, a_Fmt, a_Type); CHECKVAL(*a_pVar, a_Val, a_Fmt); } while (0)
767
768DECLINLINE(void) tstASMAtomicReadU8Worker(uint8_t volatile *pu8)
769{
770 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 0);
771 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 1);
772 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 2);
773 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 16);
774 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 32);
775 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 32);
776 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 127);
777 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 128);
778 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 169);
779 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 239);
780 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 254);
781 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicReadU8, 255);
782
783 int8_t volatile *pi8 = (int8_t volatile *)pu8;
784 TEST_READ(pi8, uint8_t, "%d", ASMAtomicReadS8, INT8_MAX);
785 TEST_READ(pi8, uint8_t, "%d", ASMAtomicReadS8, INT8_MIN);
786 TEST_READ(pi8, uint8_t, "%d", ASMAtomicReadS8, 42);
787 TEST_READ(pi8, uint8_t, "%d", ASMAtomicReadS8, -21);
788
789 bool volatile *pf = (bool volatile *)pu8;
790 TEST_READ(pf, bool, "%d", ASMAtomicReadBool, true);
791 TEST_READ(pf, bool, "%d", ASMAtomicReadBool, false);
792}
793
794
795DECLINLINE(void) tstASMAtomicUoReadU8Worker(uint8_t volatile *pu8)
796{
797 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 0);
798 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 1);
799 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 2);
800 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 16);
801 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 32);
802 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 32);
803 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 127);
804 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 128);
805 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 169);
806 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 239);
807 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 254);
808 TEST_READ(pu8, uint8_t, "%#x", ASMAtomicUoReadU8, 255);
809
810 int8_t volatile *pi8 = (int8_t volatile *)pu8;
811 TEST_READ(pi8, uint8_t, "%d", ASMAtomicUoReadS8, INT8_MAX);
812 TEST_READ(pi8, uint8_t, "%d", ASMAtomicUoReadS8, INT8_MIN);
813 TEST_READ(pi8, uint8_t, "%d", ASMAtomicUoReadS8, 42);
814 TEST_READ(pi8, uint8_t, "%d", ASMAtomicUoReadS8, -21);
815
816 bool volatile *pf = (bool volatile *)pu8;
817 TEST_READ(pf, bool, "%d", ASMAtomicUoReadBool, true);
818 TEST_READ(pf, bool, "%d", ASMAtomicUoReadBool, false);
819}
820
821
822DECLINLINE(void) tstASMAtomicReadU16Worker(uint16_t volatile *pu16)
823{
824 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicReadU16, 0);
825 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicReadU16, 19983);
826 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicReadU16, INT16_MAX);
827 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicReadU16, UINT16_MAX);
828
829 int16_t volatile *pi16 = (int16_t volatile *)pu16;
830 TEST_READ(pi16, uint16_t, "%d", ASMAtomicReadS16, INT16_MAX);
831 TEST_READ(pi16, uint16_t, "%d", ASMAtomicReadS16, INT16_MIN);
832 TEST_READ(pi16, uint16_t, "%d", ASMAtomicReadS16, 42);
833 TEST_READ(pi16, uint16_t, "%d", ASMAtomicReadS16, -21);
834}
835
836
837DECLINLINE(void) tstASMAtomicUoReadU16Worker(uint16_t volatile *pu16)
838{
839 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicUoReadU16, 0);
840 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicUoReadU16, 19983);
841 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicUoReadU16, INT16_MAX);
842 TEST_READ(pu16, uint16_t, "%#x", ASMAtomicUoReadU16, UINT16_MAX);
843
844 int16_t volatile *pi16 = (int16_t volatile *)pu16;
845 TEST_READ(pi16, uint16_t, "%d", ASMAtomicUoReadS16, INT16_MAX);
846 TEST_READ(pi16, uint16_t, "%d", ASMAtomicUoReadS16, INT16_MIN);
847 TEST_READ(pi16, uint16_t, "%d", ASMAtomicUoReadS16, 42);
848 TEST_READ(pi16, uint16_t, "%d", ASMAtomicUoReadS16, -21);
849}
850
851
852DECLINLINE(void) tstASMAtomicReadU32Worker(uint32_t volatile *pu32)
853{
854 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, 0);
855 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, 19983);
856 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, INT16_MAX);
857 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, UINT16_MAX);
858 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, _1M-1);
859 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, _1M+1);
860 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, _1G-1);
861 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, _1G+1);
862 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, INT32_MAX);
863 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicReadU32, UINT32_MAX);
864
865 int32_t volatile *pi32 = (int32_t volatile *)pu32;
866 TEST_READ(pi32, uint32_t, "%d", ASMAtomicReadS32, INT32_MAX);
867 TEST_READ(pi32, uint32_t, "%d", ASMAtomicReadS32, INT32_MIN);
868 TEST_READ(pi32, uint32_t, "%d", ASMAtomicReadS32, 42);
869 TEST_READ(pi32, uint32_t, "%d", ASMAtomicReadS32, -21);
870
871#if ARCH_BITS == 32
872 size_t volatile *pcb = (size_t volatile *)pu32;
873 TEST_READ(pcb, size_t, "%#llz", ASMAtomicReadZ, 0);
874 TEST_READ(pcb, size_t, "%#llz", ASMAtomicReadZ, ~(size_t)2);
875 TEST_READ(pcb, size_t, "%#llz", ASMAtomicReadZ, ~(size_t)0 / 4);
876
877 void * volatile *ppv = (void * volatile *)pu32;
878 TEST_READ(ppv, void *, "%p", ASMAtomicReadPtr, NULL);
879 TEST_READ(ppv, void *, "%p", ASMAtomicReadPtr, (void *)~(uintptr_t)42);
880
881 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu32;
882 RTSEMEVENT hEvt = ASMAtomicReadPtrT(phEvt, RTSEMEVENT);
883 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
884
885 ASMAtomicReadHandle(phEvt, &hEvt);
886 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
887#endif
888}
889
890
891DECLINLINE(void) tstASMAtomicUoReadU32Worker(uint32_t volatile *pu32)
892{
893 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, 0);
894 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, 19983);
895 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, INT16_MAX);
896 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, UINT16_MAX);
897 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, _1M-1);
898 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, _1M+1);
899 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, _1G-1);
900 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, _1G+1);
901 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, INT32_MAX);
902 TEST_READ(pu32, uint32_t, "%#x", ASMAtomicUoReadU32, UINT32_MAX);
903
904 int32_t volatile *pi32 = (int32_t volatile *)pu32;
905 TEST_READ(pi32, uint32_t, "%d", ASMAtomicUoReadS32, INT32_MAX);
906 TEST_READ(pi32, uint32_t, "%d", ASMAtomicUoReadS32, INT32_MIN);
907 TEST_READ(pi32, uint32_t, "%d", ASMAtomicUoReadS32, 42);
908 TEST_READ(pi32, uint32_t, "%d", ASMAtomicUoReadS32, -21);
909
910#if ARCH_BITS == 32
911 size_t volatile *pcb = (size_t volatile *)pu32;
912 TEST_READ(pcb, size_t, "%#llz", ASMAtomicUoReadZ, 0);
913 TEST_READ(pcb, size_t, "%#llz", ASMAtomicUoReadZ, ~(size_t)2);
914 TEST_READ(pcb, size_t, "%#llz", ASMAtomicUoReadZ, ~(size_t)0 / 4);
915
916 void * volatile *ppv = (void * volatile *)pu32;
917 TEST_READ(ppv, void *, "%p", ASMAtomicUoReadPtr, NULL);
918 TEST_READ(ppv, void *, "%p", ASMAtomicUoReadPtr, (void *)~(uintptr_t)42);
919
920 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu32;
921 RTSEMEVENT hEvt = ASMAtomicUoReadPtrT(phEvt, RTSEMEVENT);
922 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
923
924 ASMAtomicUoReadHandle(phEvt, &hEvt);
925 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
926#endif
927}
928
929
930DECLINLINE(void) tstASMAtomicReadU64Worker(uint64_t volatile *pu64)
931{
932 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, 0);
933 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, 19983);
934 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, INT16_MAX);
935 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, UINT16_MAX);
936 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, _1M-1);
937 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, _1M+1);
938 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, _1G-1);
939 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, _1G+1);
940 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, INT32_MAX);
941 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, UINT32_MAX);
942 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, INT64_MAX);
943 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, UINT64_MAX);
944 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicReadU64, UINT64_C(0x450872549687134));
945
946 int64_t volatile *pi64 = (int64_t volatile *)pu64;
947 TEST_READ(pi64, uint64_t, "%d", ASMAtomicReadS64, INT64_MAX);
948 TEST_READ(pi64, uint64_t, "%d", ASMAtomicReadS64, INT64_MIN);
949 TEST_READ(pi64, uint64_t, "%d", ASMAtomicReadS64, 42);
950 TEST_READ(pi64, uint64_t, "%d", ASMAtomicReadS64, -21);
951
952#if ARCH_BITS == 64
953 size_t volatile *pcb = (size_t volatile *)pu64;
954 TEST_READ(pcb, size_t, "%#llz", ASMAtomicReadZ, 0);
955 TEST_READ(pcb, size_t, "%#llz", ASMAtomicReadZ, ~(size_t)2);
956 TEST_READ(pcb, size_t, "%#llz", ASMAtomicReadZ, ~(size_t)0 / 4);
957
958 void * volatile *ppv = (void * volatile *)pu64;
959 TEST_READ(ppv, void *, "%p", ASMAtomicReadPtr, NULL);
960 TEST_READ(ppv, void *, "%p", ASMAtomicReadPtr, (void *)~(uintptr_t)42);
961
962 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu64;
963 RTSEMEVENT hEvt = ASMAtomicReadPtrT(phEvt, RTSEMEVENT);
964 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
965
966 ASMAtomicReadHandle(phEvt, &hEvt);
967 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
968#endif
969}
970
971
972DECLINLINE(void) tstASMAtomicUoReadU64Worker(uint64_t volatile *pu64)
973{
974 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, 0);
975 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, 19983);
976 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, INT16_MAX);
977 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, UINT16_MAX);
978 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, _1M-1);
979 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, _1M+1);
980 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, _1G-1);
981 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, _1G+1);
982 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, INT32_MAX);
983 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, UINT32_MAX);
984 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, INT64_MAX);
985 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, UINT64_MAX);
986 TEST_READ(pu64, uint64_t, "%#llx", ASMAtomicUoReadU64, UINT64_C(0x450872549687134));
987
988 int64_t volatile *pi64 = (int64_t volatile *)pu64;
989 TEST_READ(pi64, uint64_t, "%d", ASMAtomicUoReadS64, INT64_MAX);
990 TEST_READ(pi64, uint64_t, "%d", ASMAtomicUoReadS64, INT64_MIN);
991 TEST_READ(pi64, uint64_t, "%d", ASMAtomicUoReadS64, 42);
992 TEST_READ(pi64, uint64_t, "%d", ASMAtomicUoReadS64, -21);
993
994#if ARCH_BITS == 64
995 size_t volatile *pcb = (size_t volatile *)pu64;
996 TEST_READ(pcb, size_t, "%#llz", ASMAtomicUoReadZ, 0);
997 TEST_READ(pcb, size_t, "%#llz", ASMAtomicUoReadZ, ~(size_t)2);
998 TEST_READ(pcb, size_t, "%#llz", ASMAtomicUoReadZ, ~(size_t)0 / 4);
999
1000 void * volatile *ppv = (void * volatile *)pu64;
1001 TEST_READ(ppv, void *, "%p", ASMAtomicUoReadPtr, NULL);
1002 TEST_READ(ppv, void *, "%p", ASMAtomicUoReadPtr, (void *)~(uintptr_t)42);
1003
1004 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu64;
1005 RTSEMEVENT hEvt = ASMAtomicUoReadPtrT(phEvt, RTSEMEVENT);
1006 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
1007
1008 ASMAtomicUoReadHandle(phEvt, &hEvt);
1009 CHECKVAL(hEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
1010#endif
1011}
1012
1013
1014static void tstASMAtomicRead(void)
1015{
1016 DO_SIMPLE_TEST(ASMAtomicReadU8, uint8_t);
1017 DO_SIMPLE_TEST(ASMAtomicUoReadU8, uint8_t);
1018
1019 DO_SIMPLE_TEST(ASMAtomicReadU16, uint16_t);
1020 DO_SIMPLE_TEST(ASMAtomicUoReadU16, uint16_t);
1021
1022 DO_SIMPLE_TEST(ASMAtomicReadU32, uint32_t);
1023 DO_SIMPLE_TEST(ASMAtomicUoReadU32, uint32_t);
1024
1025 DO_SIMPLE_TEST(ASMAtomicReadU64, uint64_t);
1026 DO_SIMPLE_TEST(ASMAtomicUoReadU64, uint64_t);
1027}
1028
1029
1030#define TEST_WRITE(a_pVar, a_Type, a_Fmt, a_Function, a_Val) \
1031 do { a_Function(a_pVar, a_Val); CHECKVAL(*a_pVar, a_Val, a_Fmt); } while (0)
1032
1033DECLINLINE(void) tstASMAtomicWriteU8Worker(uint8_t volatile *pu8)
1034{
1035 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 0);
1036 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 1);
1037 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 2);
1038 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 16);
1039 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 32);
1040 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 32);
1041 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 127);
1042 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 128);
1043 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 169);
1044 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 239);
1045 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 254);
1046 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicWriteU8, 255);
1047
1048 volatile int8_t *pi8 = (volatile int8_t *)pu8;
1049 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicWriteS8, INT8_MIN);
1050 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicWriteS8, INT8_MAX);
1051 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicWriteS8, 42);
1052 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicWriteS8, -41);
1053
1054 volatile bool *pf = (volatile bool *)pu8;
1055 TEST_WRITE(pf, bool, "%d", ASMAtomicWriteBool, true);
1056 TEST_WRITE(pf, bool, "%d", ASMAtomicWriteBool, false);
1057}
1058
1059
1060DECLINLINE(void) tstASMAtomicUoWriteU8Worker(uint8_t volatile *pu8)
1061{
1062 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 0);
1063 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 1);
1064 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 2);
1065 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 16);
1066 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 32);
1067 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 32);
1068 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 127);
1069 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 128);
1070 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 169);
1071 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 239);
1072 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 254);
1073 TEST_WRITE(pu8, uint8_t, "%#x", ASMAtomicUoWriteU8, 255);
1074
1075 volatile int8_t *pi8 = (volatile int8_t *)pu8;
1076 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicUoWriteS8, INT8_MIN);
1077 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicUoWriteS8, INT8_MAX);
1078 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicUoWriteS8, 42);
1079 TEST_WRITE(pi8, int8_t, "%d", ASMAtomicUoWriteS8, -41);
1080
1081 volatile bool *pf = (volatile bool *)pu8;
1082 TEST_WRITE(pf, bool, "%d", ASMAtomicUoWriteBool, true);
1083 TEST_WRITE(pf, bool, "%d", ASMAtomicUoWriteBool, false);
1084}
1085
1086
1087DECLINLINE(void) tstASMAtomicWriteU16Worker(uint16_t volatile *pu16)
1088{
1089 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicWriteU16, 0);
1090 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicWriteU16, 19983);
1091 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicWriteU16, INT16_MAX);
1092 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicWriteU16, UINT16_MAX);
1093
1094 volatile int16_t *pi16 = (volatile int16_t *)pu16;
1095 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicWriteS16, INT16_MIN);
1096 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicWriteS16, INT16_MAX);
1097 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicWriteS16, 42);
1098 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicWriteS16, -41);
1099}
1100
1101
1102DECLINLINE(void) tstASMAtomicUoWriteU16Worker(uint16_t volatile *pu16)
1103{
1104 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicUoWriteU16, 0);
1105 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicUoWriteU16, 19983);
1106 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicUoWriteU16, INT16_MAX);
1107 TEST_WRITE(pu16, uint16_t, "%#x", ASMAtomicUoWriteU16, UINT16_MAX);
1108
1109 volatile int16_t *pi16 = (volatile int16_t *)pu16;
1110 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicUoWriteS16, INT16_MIN);
1111 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicUoWriteS16, INT16_MAX);
1112 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicUoWriteS16, 42);
1113 TEST_WRITE(pi16, int16_t, "%d", ASMAtomicUoWriteS16, -41);
1114}
1115
1116
1117DECLINLINE(void) tstASMAtomicWriteU32Worker(uint32_t volatile *pu32)
1118{
1119 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, 0);
1120 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, 19983);
1121 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, INT16_MAX);
1122 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, UINT16_MAX);
1123 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, _1M-1);
1124 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, _1M+1);
1125 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, _1G-1);
1126 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, _1G+1);
1127 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, INT32_MAX);
1128 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicWriteU32, UINT32_MAX);
1129
1130 volatile int32_t *pi32 = (volatile int32_t *)pu32;
1131 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicWriteS32, INT32_MIN);
1132 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicWriteS32, INT32_MAX);
1133 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicWriteS32, 42);
1134 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicWriteS32, -41);
1135
1136#if ARCH_BITS == 32
1137 size_t volatile *pcb = (size_t volatile *)pu32;
1138 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicWriteZ, ~(size_t)42);
1139 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicWriteZ, 42);
1140
1141 void * volatile *ppv = (void * volatile *)pu32;
1142 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicWritePtrVoid, NULL);
1143 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicWritePtrVoid, (void *)~(uintptr_t)12938754);
1144
1145 ASMAtomicWriteNullPtr(ppv); CHECKVAL(*ppv, NULL, "%p");
1146 ASMAtomicWritePtr(ppv, (void *)~(intptr_t)2322434); CHECKVAL(*ppv, (void *)~(intptr_t)2322434, "%p");
1147
1148 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu32;
1149 ASMAtomicWriteHandle(phEvt, (RTSEMEVENT)(uintptr_t)99753456); CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)99753456, "%p");
1150#endif
1151}
1152
1153
1154DECLINLINE(void) tstASMAtomicUoWriteU32Worker(uint32_t volatile *pu32)
1155{
1156 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, 0);
1157 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, 19983);
1158 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, INT16_MAX);
1159 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, UINT16_MAX);
1160 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, _1M-1);
1161 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, _1M+1);
1162 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, _1G-1);
1163 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, _1G+1);
1164 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, INT32_MAX);
1165 TEST_WRITE(pu32, uint32_t, "%#x", ASMAtomicUoWriteU32, UINT32_MAX);
1166
1167 volatile int32_t *pi32 = (volatile int32_t *)pu32;
1168 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicUoWriteS32, INT32_MIN);
1169 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicUoWriteS32, INT32_MAX);
1170 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicUoWriteS32, 42);
1171 TEST_WRITE(pi32, int32_t, "%d", ASMAtomicUoWriteS32, -41);
1172
1173#if ARCH_BITS == 32
1174 size_t volatile *pcb = (size_t volatile *)pu32;
1175 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicUoWriteZ, ~(size_t)42);
1176 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicUoWriteZ, 42);
1177
1178 void * volatile *ppv = (void * volatile *)pu32;
1179 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicUoWritePtrVoid, NULL);
1180 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicUoWritePtrVoid, (void *)~(uintptr_t)12938754);
1181
1182 ASMAtomicUoWriteNullPtr(ppv); CHECKVAL(*ppv, NULL, "%p");
1183 ASMAtomicUoWritePtr(ppv, (void *)~(intptr_t)2322434); CHECKVAL(*ppv, (void *)~(intptr_t)2322434, "%p");
1184
1185 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu32;
1186 ASMAtomicUoWriteHandle(phEvt, (RTSEMEVENT)(uintptr_t)99753456); CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)99753456, "%p");
1187#endif
1188}
1189
1190
1191DECLINLINE(void) tstASMAtomicWriteU64Worker(uint64_t volatile *pu64)
1192{
1193 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, 0);
1194 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, 19983);
1195 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, INT16_MAX);
1196 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, UINT16_MAX);
1197 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, _1M-1);
1198 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, _1M+1);
1199 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, _1G-1);
1200 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, _1G+1);
1201 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, INT32_MAX);
1202 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, UINT32_MAX);
1203 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, INT64_MAX);
1204 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, UINT64_MAX);
1205 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicWriteU64, UINT64_C(0x450872549687134));
1206
1207 volatile int64_t *pi64 = (volatile int64_t *)pu64;
1208 TEST_WRITE(pi64, int64_t, "%d", ASMAtomicWriteS64, INT64_MIN);
1209 TEST_WRITE(pi64, int64_t, "%d", ASMAtomicWriteS64, INT64_MAX);
1210 TEST_WRITE(pi64, int64_t, "%d", ASMAtomicWriteS64, 42);
1211
1212#if ARCH_BITS == 64
1213 size_t volatile *pcb = (size_t volatile *)pu64;
1214 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicWriteZ, ~(size_t)42);
1215 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicWriteZ, 42);
1216
1217 void * volatile *ppv = (void * volatile *)pu64;
1218 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicWritePtrVoid, NULL);
1219 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicWritePtrVoid, (void *)~(uintptr_t)12938754);
1220
1221 ASMAtomicWriteNullPtr(ppv); CHECKVAL(*ppv, NULL, "%p");
1222 ASMAtomicWritePtr(ppv, (void *)~(intptr_t)2322434); CHECKVAL(*ppv, (void *)~(intptr_t)2322434, "%p");
1223
1224 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu64;
1225 ASMAtomicWriteHandle(phEvt, (RTSEMEVENT)(uintptr_t)99753456); CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)99753456, "%p");
1226#endif
1227}
1228
1229
1230DECLINLINE(void) tstASMAtomicUoWriteU64Worker(uint64_t volatile *pu64)
1231{
1232 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, 0);
1233 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, 19983);
1234 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, INT16_MAX);
1235 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, UINT16_MAX);
1236 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, _1M-1);
1237 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, _1M+1);
1238 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, _1G-1);
1239 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, _1G+1);
1240 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, INT32_MAX);
1241 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, UINT32_MAX);
1242 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, INT64_MAX);
1243 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, UINT64_MAX);
1244 TEST_WRITE(pu64, uint64_t, "%#llx", ASMAtomicUoWriteU64, UINT64_C(0x450872549687134));
1245
1246 volatile int64_t *pi64 = (volatile int64_t *)pu64;
1247 TEST_WRITE(pi64, int64_t, "%d", ASMAtomicUoWriteS64, INT64_MIN);
1248 TEST_WRITE(pi64, int64_t, "%d", ASMAtomicUoWriteS64, INT64_MAX);
1249 TEST_WRITE(pi64, int64_t, "%d", ASMAtomicUoWriteS64, 42);
1250
1251#if ARCH_BITS == 64
1252 size_t volatile *pcb = (size_t volatile *)pu64;
1253 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicUoWriteZ, ~(size_t)42);
1254 TEST_WRITE(pcb, size_t, "%#zx", ASMAtomicUoWriteZ, 42);
1255
1256 void * volatile *ppv = (void * volatile *)pu64;
1257 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicUoWritePtrVoid, NULL);
1258 TEST_WRITE(ppv, void *, "%#zx", ASMAtomicUoWritePtrVoid, (void *)~(uintptr_t)12938754);
1259
1260 ASMAtomicUoWriteNullPtr(ppv); CHECKVAL(*ppv, NULL, "%p");
1261 ASMAtomicUoWritePtr(ppv, (void *)~(intptr_t)2322434); CHECKVAL(*ppv, (void *)~(intptr_t)2322434, "%p");
1262
1263 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu64;
1264 ASMAtomicUoWriteHandle(phEvt, (RTSEMEVENT)(uintptr_t)99753456); CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)99753456, "%p");
1265#endif
1266}
1267
1268static void tstASMAtomicWrite(void)
1269{
1270 DO_SIMPLE_TEST(ASMAtomicWriteU8, uint8_t);
1271 DO_SIMPLE_TEST(ASMAtomicUoWriteU8, uint8_t);
1272
1273 DO_SIMPLE_TEST(ASMAtomicWriteU16, uint16_t);
1274 DO_SIMPLE_TEST(ASMAtomicUoWriteU16, uint16_t);
1275
1276 DO_SIMPLE_TEST(ASMAtomicWriteU32, uint32_t);
1277 DO_SIMPLE_TEST(ASMAtomicUoWriteU32, uint32_t);
1278
1279 DO_SIMPLE_TEST(ASMAtomicWriteU64, uint64_t);
1280 DO_SIMPLE_TEST(ASMAtomicUoWriteU64, uint64_t);
1281}
1282
1283
1284DECLINLINE(void) tstASMAtomicXchgU8Worker(uint8_t volatile *pu8)
1285{
1286 *pu8 = 0;
1287 CHECK_OP_AND_VAL(uint8_t, "%#x", pu8, ASMAtomicXchgU8(pu8, 1), 0, 1);
1288 CHECK_OP_AND_VAL(uint8_t, "%#x", pu8, ASMAtomicXchgU8(pu8, UINT8_C(0xff)), 1, UINT8_C(0xff));
1289 CHECK_OP_AND_VAL(uint8_t, "%#x", pu8, ASMAtomicXchgU8(pu8, UINT8_C(0x87)), UINT8_C(0xff), UINT8_C(0x87));
1290 CHECK_OP_AND_VAL(uint8_t, "%#x", pu8, ASMAtomicXchgU8(pu8, UINT8_C(0xfe)), UINT8_C(0x87), UINT8_C(0xfe));
1291
1292 int8_t volatile *pi8 = (int8_t volatile *)pu8;
1293 CHECK_OP_AND_VAL(int8_t, "%d", pi8, ASMAtomicXchgS8(pi8, INT8_C(-4)), INT8_C(-2), INT8_C(-4));
1294 CHECK_OP_AND_VAL(int8_t, "%d", pi8, ASMAtomicXchgS8(pi8, INT8_C(4)), INT8_C(-4), INT8_C(4));
1295 CHECK_OP_AND_VAL(int8_t, "%d", pi8, ASMAtomicXchgS8(pi8, INT8_MAX), INT8_C(4), INT8_MAX);
1296 CHECK_OP_AND_VAL(int8_t, "%d", pi8, ASMAtomicXchgS8(pi8, INT8_MIN), INT8_MAX, INT8_MIN);
1297 CHECK_OP_AND_VAL(int8_t, "%d", pi8, ASMAtomicXchgS8(pi8, 1), INT8_MIN, 1);
1298
1299 bool volatile *pf = (bool volatile *)pu8;
1300 CHECK_OP_AND_VAL(bool, "%d", pf, ASMAtomicXchgBool(pf, false), true, false);
1301 CHECK_OP_AND_VAL(bool, "%d", pf, ASMAtomicXchgBool(pf, false), false, false);
1302 CHECK_OP_AND_VAL(bool, "%d", pf, ASMAtomicXchgBool(pf, true), false, true);
1303}
1304
1305
1306DECLINLINE(void) tstASMAtomicXchgU16Worker(uint16_t volatile *pu16)
1307{
1308 *pu16 = 0;
1309 CHECK_OP_AND_VAL(uint16_t, "%#x", pu16, ASMAtomicXchgU16(pu16, 1), 0, 1);
1310 CHECK_OP_AND_VAL(uint16_t, "%#x", pu16, ASMAtomicXchgU16(pu16, 0), 1, 0);
1311 CHECK_OP_AND_VAL(uint16_t, "%#x", pu16, ASMAtomicXchgU16(pu16, UINT16_MAX), 0, UINT16_MAX);
1312 CHECK_OP_AND_VAL(uint16_t, "%#x", pu16, ASMAtomicXchgU16(pu16, UINT16_C(0x7fff)), UINT16_MAX, UINT16_C(0x7fff));
1313 CHECK_OP_AND_VAL(uint16_t, "%#x", pu16, ASMAtomicXchgU16(pu16, UINT16_C(0x8765)), UINT16_C(0x7fff), UINT16_C(0x8765));
1314 CHECK_OP_AND_VAL(uint16_t, "%#x", pu16, ASMAtomicXchgU16(pu16, UINT16_C(0xfffe)), UINT16_C(0x8765), UINT16_C(0xfffe));
1315
1316 int16_t volatile *pi16 = (int16_t volatile *)pu16;
1317 CHECK_OP_AND_VAL(int16_t, "%d", pi16, ASMAtomicXchgS16(pi16, INT16_MIN), INT16_C(-2), INT16_MIN);
1318 CHECK_OP_AND_VAL(int16_t, "%d", pi16, ASMAtomicXchgS16(pi16, INT16_MAX), INT16_MIN, INT16_MAX);
1319 CHECK_OP_AND_VAL(int16_t, "%d", pi16, ASMAtomicXchgS16(pi16, -8), INT16_MAX, -8);
1320 CHECK_OP_AND_VAL(int16_t, "%d", pi16, ASMAtomicXchgS16(pi16, 8), -8, 8);
1321}
1322
1323
1324DECLINLINE(void) tstASMAtomicXchgU32Worker(uint32_t volatile *pu32)
1325{
1326 *pu32 = 0;
1327 CHECK_OP_AND_VAL(uint32_t, "%#x", pu32, ASMAtomicXchgU32(pu32, 1), 0, 1);
1328 CHECK_OP_AND_VAL(uint32_t, "%#x", pu32, ASMAtomicXchgU32(pu32, 0), 1, 0);
1329 CHECK_OP_AND_VAL(uint32_t, "%#x", pu32, ASMAtomicXchgU32(pu32, UINT32_MAX), 0, UINT32_MAX);
1330 CHECK_OP_AND_VAL(uint32_t, "%#x", pu32, ASMAtomicXchgU32(pu32, UINT32_C(0x87654321)), UINT32_MAX, UINT32_C(0x87654321));
1331 CHECK_OP_AND_VAL(uint32_t, "%#x", pu32, ASMAtomicXchgU32(pu32, UINT32_C(0xfffffffe)), UINT32_C(0x87654321), UINT32_C(0xfffffffe));
1332
1333 int32_t volatile *pi32 = (int32_t volatile *)pu32;
1334 CHECK_OP_AND_VAL(int32_t, "%d", pi32, ASMAtomicXchgS32(pi32, INT32_MIN), INT32_C(-2), INT32_MIN);
1335 CHECK_OP_AND_VAL(int32_t, "%d", pi32, ASMAtomicXchgS32(pi32, INT32_MAX), INT32_MIN, INT32_MAX);
1336 CHECK_OP_AND_VAL(int32_t, "%d", pi32, ASMAtomicXchgS32(pi32, -16), INT32_MAX, -16);
1337 CHECK_OP_AND_VAL(int32_t, "%d", pi32, ASMAtomicXchgS32(pi32, 16), -16, 16);
1338
1339#if ARCH_BITS == 32
1340 size_t volatile *pcb = (size_t volatile *)pu32;
1341 CHECK_OP_AND_VAL(size_t, "%#zx", pcb, ASMAtomicXchgZ(pcb, UINT32_C(0x9481239b)), 0x10, UINT32_C(0x9481239b));
1342 CHECK_OP_AND_VAL(size_t, "%#zx", pcb, ASMAtomicXchgZ(pcb, UINT32_C(0xcdef1234)), UINT32_C(0x9481239b), UINT32_C(0xcdef1234));
1343#endif
1344
1345#if R0_ARCH_BITS == 32
1346 RTR0PTR volatile *pR0Ptr = (RTR0PTR volatile *)pu32;
1347 CHECK_OP_AND_VAL(size_t, "%#llx", pcb, ASMAtomicXchgR0Ptr(pR0Ptr, UINT32_C(0x80341237)), UINT32_C(0xcdef1234), UINT32_C(0x80341237));
1348#endif
1349}
1350
1351
1352DECLINLINE(void) tstASMAtomicXchgU64Worker(uint64_t volatile *pu64)
1353{
1354 *pu64 = 0;
1355 CHECK_OP_AND_VAL(uint64_t, "%#llx", pu64, ASMAtomicXchgU64(pu64, 1), 0, 1);
1356 CHECK_OP_AND_VAL(uint64_t, "%#llx", pu64, ASMAtomicXchgU64(pu64, 0), 1, 0);
1357 CHECK_OP_AND_VAL(uint64_t, "%#llx", pu64, ASMAtomicXchgU64(pu64, UINT64_MAX), 0, UINT64_MAX);
1358 CHECK_OP_AND_VAL(uint64_t, "%#llx", pu64, ASMAtomicXchgU64(pu64, UINT64_C(0xfedcba0987654321)), UINT64_MAX, UINT64_C(0xfedcba0987654321));
1359 CHECK_OP_AND_VAL(uint64_t, "%#llx", pu64, ASMAtomicXchgU64(pu64, UINT64_C(0xfffffffffffffffe)), UINT64_C(0xfedcba0987654321), UINT64_C(0xfffffffffffffffe));
1360
1361 int64_t volatile *pi64 = (int64_t volatile *)pu64;
1362 CHECK_OP_AND_VAL(int64_t, "%lld", pi64, ASMAtomicXchgS64(pi64, INT64_MAX), -2, INT64_MAX);
1363 CHECK_OP_AND_VAL(int64_t, "%lld", pi64, ASMAtomicXchgS64(pi64, INT64_MIN), INT64_MAX, INT64_MIN);
1364 CHECK_OP_AND_VAL(int64_t, "%lld", pi64, ASMAtomicXchgS64(pi64, -32), INT64_MIN, -32);
1365 CHECK_OP_AND_VAL(int64_t, "%lld", pi64, ASMAtomicXchgS64(pi64, 32), -32, 32);
1366
1367#if ARCH_BITS == 64
1368 size_t volatile *pcb = (size_t volatile *)pu64;
1369 CHECK_OP_AND_VAL(size_t, "%#zx", pcb, ASMAtomicXchgZ(pcb, UINT64_C(0x94812396759)), 0x20, UINT64_C(0x94812396759));
1370 CHECK_OP_AND_VAL(size_t, "%#zx", pcb, ASMAtomicXchgZ(pcb, UINT64_C(0xcdef1234abdf7896)), UINT64_C(0x94812396759), UINT64_C(0xcdef1234abdf7896));
1371#endif
1372
1373#if R0_ARCH_BITS == 64
1374 RTR0PTR volatile *pR0Ptr = (RTR0PTR volatile *)pu64;
1375 CHECK_OP_AND_VAL(size_t, "%#llx", pcb, ASMAtomicXchgR0Ptr(pR0Ptr, UINT64_C(0xfedc1234567890ab)), UINT64_C(0xcdef1234abdf7896), UINT64_C(0xfedc1234567890ab));
1376#endif
1377}
1378
1379
1380DECLINLINE(void) tstASMAtomicXchgPtrWorker(void * volatile *ppv)
1381{
1382 *ppv = NULL;
1383 CHECK_OP_AND_VAL(void *, "%p", ppv, ASMAtomicXchgPtr(ppv, (void *)(~(uintptr_t)0)), NULL, (void *)(~(uintptr_t)0));
1384 CHECK_OP_AND_VAL(void *, "%p", ppv, ASMAtomicXchgPtr(ppv, (void *)(~(uintptr_t)0x87654321)), (void *)(~(uintptr_t)0), (void *)(~(uintptr_t)0x87654321));
1385 CHECK_OP_AND_VAL(void *, "%p", ppv, ASMAtomicXchgPtr(ppv, NULL), (void *)(~(uintptr_t)0x87654321), NULL);
1386
1387 CHECK_OP_AND_VAL(void *, "%p", ppv, ASMAtomicXchgR3Ptr(ppv, (void *)ppv), NULL, (void *)ppv);
1388
1389 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)ppv;
1390 RTSEMEVENT hRet;
1391 ASMAtomicXchgHandle(phEvt, (RTSEMEVENT)(~(uintptr_t)12345), &hRet);
1392 CHECKVAL(hRet, (RTSEMEVENT)ppv, "%p");
1393 CHECKVAL(*phEvt, (RTSEMEVENT)(~(uintptr_t)12345), "%p");
1394}
1395
1396
1397static void tstASMAtomicXchg(void)
1398{
1399 DO_SIMPLE_TEST(ASMAtomicXchgU8, uint8_t);
1400 DO_SIMPLE_TEST(ASMAtomicXchgU16, uint16_t);
1401 DO_SIMPLE_TEST(ASMAtomicXchgU32, uint32_t);
1402 DO_SIMPLE_TEST(ASMAtomicXchgU64, uint64_t);
1403 DO_SIMPLE_TEST(ASMAtomicXchgPtr, void *);
1404}
1405
1406
1407DECLINLINE(void) tstASMAtomicCmpXchgU8Worker(uint8_t volatile *pu8)
1408{
1409 *pu8 = 0xff;
1410 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu8, ASMAtomicCmpXchgU8(pu8, 0, 0), false, 0xff);
1411 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu8, ASMAtomicCmpXchgU8(pu8, 0, 0xff), true, 0);
1412 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu8, ASMAtomicCmpXchgU8(pu8, 0x97, 0), true, 0x97);
1413 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu8, ASMAtomicCmpXchgU8(pu8, 0x97, 0), false, 0x97);
1414 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu8, ASMAtomicCmpXchgU8(pu8, 0x7f, 0x97), true, 0x7f);
1415
1416 int8_t volatile *pi8 = (int8_t volatile *)pu8;
1417 CHECK_OP_AND_VAL(bool, "%d", pi8, ASMAtomicCmpXchgS8(pi8, -2, 0x7f), true, -2);
1418 CHECK_OP_AND_VAL(bool, "%d", pi8, ASMAtomicCmpXchgS8(pi8, INT8_MAX, -2), true, INT8_MAX);
1419 CHECK_OP_AND_VAL(bool, "%d", pi8, ASMAtomicCmpXchgS8(pi8, INT8_MAX, INT8_MIN), false, INT8_MAX);
1420 CHECK_OP_AND_VAL(bool, "%d", pi8, ASMAtomicCmpXchgS8(pi8, INT8_MIN, INT8_MAX), true, INT8_MIN);
1421 CHECK_OP_AND_VAL(bool, "%d", pi8, ASMAtomicCmpXchgS8(pi8, 1, INT8_MIN), true, 1);
1422
1423 bool volatile *pf = (bool volatile *)pu8;
1424 CHECK_OP_AND_VAL(bool, "%d", pf, ASMAtomicCmpXchgBool(pf, true, true), true, true);
1425 CHECK_OP_AND_VAL(bool, "%d", pf, ASMAtomicCmpXchgBool(pf, false, true), true, false);
1426 CHECK_OP_AND_VAL(bool, "%d", pf, ASMAtomicCmpXchgBool(pf, false, true), false, false);
1427 CHECK_OP_AND_VAL(bool, "%d", pf, ASMAtomicCmpXchgBool(pf, false, false), true, false);
1428}
1429
1430
1431DECLINLINE(void) tstASMAtomicCmpXchgU32Worker(uint32_t volatile *pu32)
1432{
1433 *pu32 = UINT32_C(0xffffffff);
1434 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu32, ASMAtomicCmpXchgU32(pu32, 0, 0), false, UINT32_C(0xffffffff));
1435 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu32, ASMAtomicCmpXchgU32(pu32, 0, UINT32_C(0xffffffff)), true, 0);
1436 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu32, ASMAtomicCmpXchgU32(pu32, UINT32_C(0x80088efd), UINT32_C(0x12345678)), false, 0);
1437 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu32, ASMAtomicCmpXchgU32(pu32, UINT32_C(0x80088efd), 0), true, UINT32_C(0x80088efd));
1438 CHECK_OP_AND_VAL_EX(bool, "%d", "%#x", pu32, ASMAtomicCmpXchgU32(pu32, UINT32_C(0xfffffffe), UINT32_C(0x80088efd)), true, UINT32_C(0xfffffffe));
1439
1440 int32_t volatile *pi32 = (int32_t volatile *)pu32;
1441 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, INT32_MIN, 2), false, -2);
1442 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, INT32_MIN, -2), true, INT32_MIN);
1443 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, -19, -2), false, INT32_MIN);
1444 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, -19, INT32_MIN), true, -19);
1445 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, -19, INT32_MIN), false, -19);
1446 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, 19, -19), true, 19);
1447 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, INT32_MAX, -234), false, 19);
1448 CHECK_OP_AND_VAL_EX(bool, "%d", "%d", pi32, ASMAtomicCmpXchgS32(pi32, INT32_MAX, 19), true, INT32_MAX);
1449
1450#if ARCH_BITS == 32
1451 void * volatile *ppv = (void * volatile *)pu32;
1452 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, NULL, (void *)(intptr_t)-29), false, (void *)(intptr_t)29);
1453 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, NULL, (void *)(intptr_t)29), true, NULL);
1454 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, NULL, (void *)(intptr_t)29), false, NULL);
1455 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, (void *)~(uintptr_t)42, NULL), true, (void *)~(uintptr_t)42);
1456
1457 bool fRc;
1458 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu32;
1459 ASMAtomicCmpXchgHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12356389, (RTSEMEVENT)NULL, fRc);
1460 CHECKVAL(fRc, false, "%d");
1461 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
1462
1463 ASMAtomicCmpXchgHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12356389, (RTSEMEVENT)~(uintptr_t)42, fRc);
1464 CHECKVAL(fRc, true, "%d");
1465 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)0x12356389, "%p");
1466#endif
1467}
1468
1469
1470DECLINLINE(void) tstASMAtomicCmpXchgU64Worker(uint64_t volatile *pu64)
1471{
1472 *pu64 = UINT64_C(0xffffffffffffff);
1473 CHECK_OP_AND_VAL_EX(bool, "%d", "%#llx", pu64, ASMAtomicCmpXchgU64(pu64, 0, 0), false, UINT64_C(0xffffffffffffff));
1474 CHECK_OP_AND_VAL_EX(bool, "%d", "%#llx", pu64, ASMAtomicCmpXchgU64(pu64, 0, UINT64_C(0xffffffffffffff)), true, 0);
1475 CHECK_OP_AND_VAL_EX(bool, "%d", "%#llx", pu64, ASMAtomicCmpXchgU64(pu64, UINT64_C(0x80040008008efd), 1), false, 0);
1476 CHECK_OP_AND_VAL_EX(bool, "%d", "%#llx", pu64, ASMAtomicCmpXchgU64(pu64, UINT64_C(0x80040008008efd), 0), true, UINT64_C(0x80040008008efd));
1477 CHECK_OP_AND_VAL_EX(bool, "%d", "%#llx", pu64, ASMAtomicCmpXchgU64(pu64, UINT64_C(0x80040008008efd), 0), false, UINT64_C(0x80040008008efd));
1478 CHECK_OP_AND_VAL_EX(bool, "%d", "%#llx", pu64, ASMAtomicCmpXchgU64(pu64, UINT64_C(0xfffffffffffffffd), UINT64_C(0x80040008008efd)), true, UINT64_C(0xfffffffffffffffd));
1479
1480 int64_t volatile *pi64 = (int64_t volatile *)pu64;
1481 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, INT64_MAX, 0), false, -3);
1482 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, INT64_MAX, -3), true, INT64_MAX);
1483 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, INT64_MIN, INT64_MIN), false, INT64_MAX);
1484 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, INT64_MIN, INT64_MAX), true, INT64_MIN);
1485 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, -29, -29), false, INT64_MIN);
1486 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, -29, INT64_MIN), true, -29);
1487 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, -29, INT64_MIN), false, -29);
1488 CHECK_OP_AND_VAL_EX(bool, "%d", "%#lld", pi64, ASMAtomicCmpXchgS64(pi64, 29, -29), true, 29);
1489
1490#if ARCH_BITS == 64
1491 void * volatile *ppv = (void * volatile *)pu64;
1492 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, NULL, (void *)(intptr_t)-29), false, (void *)(intptr_t)29);
1493 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, NULL, (void *)(intptr_t)29), true, NULL);
1494 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, NULL, (void *)(intptr_t)29), false, NULL);
1495 CHECK_OP_AND_VAL_EX(bool, "%d", "%p", ppv, ASMAtomicCmpXchgPtrVoid(ppv, (void *)~(uintptr_t)42, NULL), true, (void *)~(uintptr_t)42);
1496
1497 bool fRc;
1498 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu64;
1499 ASMAtomicCmpXchgHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12356389, (RTSEMEVENT)NULL, fRc);
1500 CHECKVAL(fRc, false, "%d");
1501 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)42, "%p");
1502
1503 ASMAtomicCmpXchgHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12356389, (RTSEMEVENT)~(uintptr_t)42, fRc);
1504 CHECKVAL(fRc, true, "%d");
1505 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)0x12356389, "%p");
1506#endif
1507}
1508
1509
1510#ifdef RTASM_HAVE_CMP_WRITE_U128
1511DECLINLINE(void) tstASMAtomicCmpWriteU128Worker(RTUINT128U volatile *pu128)
1512{
1513 pu128->s.Lo = UINT64_C(0xffffffffffffff);
1514 pu128->s.Hi = UINT64_C(0xffffffffffffff);
1515
1516 RTUINT128U u128A, u128B;
1517 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1518 u128A = RTUINT128_INIT_C(0, 0),
1519 u128B = RTUINT128_INIT_C(0, 0)),
1520 false, 0xffffffffffffff, 0xffffffffffffff);
1521 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1522 u128A = RTUINT128_INIT_C(0, 0),
1523 u128B = RTUINT128_INIT_C(0xffffffffffffff, 0xffffffffffffff)),
1524 true, 0, 0);
1525
1526 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1527 u128A = RTUINT128_INIT_C(0x80040008008efd, 0x40080004004def),
1528 u128B = RTUINT128_INIT_C(0, 1)),
1529 false, 0, 0);
1530 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1531 u128A = RTUINT128_INIT_C(0x80040008008efd, 0x40080004004def),
1532 u128B = RTUINT128_INIT_C(1, 0)),
1533 false, 0, 0);
1534 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1535 u128A = RTUINT128_INIT_C(0x80040008008efd, 0x40080004004def),
1536 u128B = RTUINT128_INIT_C(0, 0)),
1537 true, 0x80040008008efd, 0x40080004004def);
1538
1539 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1540 u128A = RTUINT128_INIT_C(0xfff40ff8f08ef3, 0x4ee8ee04cc4de4),
1541 u128B = RTUINT128_INIT_C(0x80040008008efd, 0)),
1542 false, 0x80040008008efd, 0x40080004004def);
1543 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1544 u128A = RTUINT128_INIT_C(0xfff40ff8f08ef3, 0x4ee8ee04cc4de4),
1545 u128B = RTUINT128_INIT_C(0, 0x40080004004def)),
1546 false, 0x80040008008efd, 0x40080004004def);
1547 CHECK_OP_AND_VAL_128_C(bool, "%d", pu128, ASMAtomicCmpWriteU128U(pu128,
1548 u128A = RTUINT128_INIT_C(0xfff40ff8f08ef3, 0x4ee8ee04cc4de4),
1549 u128B = RTUINT128_INIT_C(0x80040008008efd, 0x40080004004def)),
1550 true, 0xfff40ff8f08ef3, 0x4ee8ee04cc4de4);
1551}
1552#endif /* RTASM_HAVE_CMP_WRITE_U128 */
1553
1554
1555static void tstASMAtomicCmpXchg(void)
1556{
1557 DO_SIMPLE_TEST(ASMAtomicCmpXchgU8, uint8_t);
1558 DO_SIMPLE_TEST(ASMAtomicCmpXchgU32, uint32_t);
1559 DO_SIMPLE_TEST(ASMAtomicCmpXchgU64, uint64_t);
1560#ifdef RTASM_HAVE_CMP_WRITE_U128
1561# ifdef RT_ARCH_AMD64
1562 if (ASMCpuId_ECX(1) & X86_CPUID_FEATURE_ECX_CX16)
1563# endif
1564 {
1565 RTTestISub("ASMAtomicCmpWriteU128U");
1566 DO_SIMPLE_TEST_NO_SUB_NO_STACK(tstASMAtomicCmpWriteU128Worker, RTUINT128U);
1567 }
1568#endif
1569}
1570
1571
1572DECLINLINE(void) tstASMAtomicCmpXchgExU8Worker(uint8_t volatile *pu8)
1573{
1574 *pu8 = UINT8_C(0xff);
1575 uint8_t u8Old = UINT8_C(0x11);
1576 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu8, u8Old, ASMAtomicCmpXchgExU8(pu8, 0, 0, &u8Old), false, UINT8_C(0xff), UINT8_C(0xff));
1577 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu8, u8Old, ASMAtomicCmpXchgExU8(pu8, 0, UINT8_C(0xff), &u8Old), true, 0, UINT8_C(0xff));
1578 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu8, u8Old, ASMAtomicCmpXchgExU8(pu8, 0, UINT8_C(0xff), &u8Old), false, 0, UINT8_C(0x00));
1579 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu8, u8Old, ASMAtomicCmpXchgExU8(pu8, UINT8_C(0xfd), 0, &u8Old), true, UINT8_C(0xfd), 0);
1580 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu8, u8Old, ASMAtomicCmpXchgExU8(pu8, UINT8_C(0xfd), 0, &u8Old), false, UINT8_C(0xfd), UINT8_C(0xfd));
1581 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu8, u8Old, ASMAtomicCmpXchgExU8(pu8, UINT8_C(0xe0), UINT8_C(0xfd), &u8Old), true, UINT8_C(0xe0), UINT8_C(0xfd));
1582
1583 int8_t volatile *pi8 = (int8_t volatile *)pu8;
1584 int8_t i8Old = 0;
1585 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi8, i8Old, ASMAtomicCmpXchgExS8(pi8, 32, 32, &i8Old), false, -32, -32);
1586 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi8, i8Old, ASMAtomicCmpXchgExS8(pi8, 32, -32, &i8Old), true, 32, -32);
1587 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi8, i8Old, ASMAtomicCmpXchgExS8(pi8, INT8_MIN, 32, &i8Old), true, INT8_MIN, 32);
1588 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi8, i8Old, ASMAtomicCmpXchgExS8(pi8, INT8_MIN, 32, &i8Old), false, INT8_MIN, INT8_MIN);
1589 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi8, i8Old, ASMAtomicCmpXchgExS8(pi8, INT8_MAX, INT8_MAX, &i8Old), false, INT8_MIN, INT8_MIN);
1590 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi8, i8Old, ASMAtomicCmpXchgExS8(pi8, INT8_MAX, INT8_MIN, &i8Old), true, INT8_MAX, INT8_MIN);
1591 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi8, i8Old, ASMAtomicCmpXchgExS8(pi8, 42, INT8_MAX, &i8Old), true, 42, INT8_MAX);
1592}
1593
1594
1595DECLINLINE(void) tstASMAtomicCmpXchgExU16Worker(uint16_t volatile *pu16)
1596{
1597 *pu16 = UINT16_C(0xffff);
1598 uint16_t u16Old = UINT16_C(0x5111);
1599 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu16, u16Old, ASMAtomicCmpXchgExU16(pu16, 0, 0, &u16Old), false, UINT16_C(0xffff), UINT16_C(0xffff));
1600 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu16, u16Old, ASMAtomicCmpXchgExU16(pu16, 0, UINT16_C(0xffff), &u16Old), true, 0, UINT16_C(0xffff));
1601 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu16, u16Old, ASMAtomicCmpXchgExU16(pu16, 0, UINT16_C(0xffff), &u16Old), false, 0, UINT16_C(0x0000));
1602 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu16, u16Old, ASMAtomicCmpXchgExU16(pu16, UINT16_C(0x8efd), 0, &u16Old), true, UINT16_C(0x8efd), 0);
1603 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu16, u16Old, ASMAtomicCmpXchgExU16(pu16, UINT16_C(0x8efd), 0, &u16Old), false, UINT16_C(0x8efd), UINT16_C(0x8efd));
1604 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu16, u16Old, ASMAtomicCmpXchgExU16(pu16, UINT16_C(0xffe0), UINT16_C(0x8efd), &u16Old), true, UINT16_C(0xffe0), UINT16_C(0x8efd));
1605
1606 int16_t volatile *pi16 = (int16_t volatile *)pu16;
1607 int16_t i16Old = 0;
1608 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi16, i16Old, ASMAtomicCmpXchgExS16(pi16, 32, 32, &i16Old), false, -32, -32);
1609 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi16, i16Old, ASMAtomicCmpXchgExS16(pi16, 32, -32, &i16Old), true, 32, -32);
1610 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi16, i16Old, ASMAtomicCmpXchgExS16(pi16, INT16_MIN, 32, &i16Old), true, INT16_MIN, 32);
1611 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi16, i16Old, ASMAtomicCmpXchgExS16(pi16, INT16_MIN, 32, &i16Old), false, INT16_MIN, INT16_MIN);
1612 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi16, i16Old, ASMAtomicCmpXchgExS16(pi16, INT16_MAX, INT16_MAX, &i16Old), false, INT16_MIN, INT16_MIN);
1613 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi16, i16Old, ASMAtomicCmpXchgExS16(pi16, INT16_MAX, INT16_MIN, &i16Old), true, INT16_MAX, INT16_MIN);
1614 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi16, i16Old, ASMAtomicCmpXchgExS16(pi16, 42, INT16_MAX, &i16Old), true, 42, INT16_MAX);
1615}
1616
1617
1618DECLINLINE(void) tstASMAtomicCmpXchgExU32Worker(uint32_t volatile *pu32)
1619{
1620 *pu32 = UINT32_C(0xffffffff);
1621 uint32_t u32Old = UINT32_C(0x80005111);
1622 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, 0, 0, &u32Old), false, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
1623 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, 0, UINT32_C(0xffffffff), &u32Old), true, 0, UINT32_C(0xffffffff));
1624 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, 0, UINT32_C(0xffffffff), &u32Old), false, 0, UINT32_C(0x00000000));
1625 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, UINT32_C(0x80088efd), 0, &u32Old), true, UINT32_C(0x80088efd), 0);
1626 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, UINT32_C(0x80088efd), 0, &u32Old), false, UINT32_C(0x80088efd), UINT32_C(0x80088efd));
1627 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, UINT32_C(0xffffffe0), UINT32_C(0x80088efd), &u32Old), true, UINT32_C(0xffffffe0), UINT32_C(0x80088efd));
1628
1629 int32_t volatile *pi32 = (int32_t volatile *)pu32;
1630 int32_t i32Old = 0;
1631 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, 32, 32, &i32Old), false, -32, -32);
1632 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, 32, -32, &i32Old), true, 32, -32);
1633 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MIN, 32, &i32Old), true, INT32_MIN, 32);
1634 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MIN, 32, &i32Old), false, INT32_MIN, INT32_MIN);
1635 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MAX, INT32_MAX, &i32Old), false, INT32_MIN, INT32_MIN);
1636 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MAX, INT32_MIN, &i32Old), true, INT32_MAX, INT32_MIN);
1637 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, 42, INT32_MAX, &i32Old), true, 42, INT32_MAX);
1638
1639#if ARCH_BITS == 32
1640 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu32;
1641 RTSEMEVENT hEvtOld = (RTSEMEVENT)~(uintptr_t)31;
1642 bool fRc = true;
1643 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)~(uintptr_t)0, fRc, &hEvtOld);
1644 CHECKVAL(fRc, false, "%d");
1645 CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)42, "%p");
1646 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1647
1648 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)(uintptr_t)42, fRc, &hEvtOld);
1649 CHECKVAL(fRc, true, "%d");
1650 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, "%p");
1651 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1652#endif
1653}
1654
1655
1656DECLINLINE(void) tstASMAtomicCmpXchgExU64Worker(uint64_t volatile *pu64)
1657{
1658 *pu64 = UINT64_C(0xffffffffffffffff);
1659 uint64_t u64Old = UINT64_C(0x8000000051111111);
1660 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, 0, 0, &u64Old), false, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
1661 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, 0, UINT64_C(0xffffffffffffffff), &u64Old), true, 0, UINT64_C(0xffffffffffffffff));
1662 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, UINT64_C(0x0080040008008efd), 0x342, &u64Old), false, 0, 0);
1663 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, UINT64_C(0x0080040008008efd), 0, &u64Old), true, UINT64_C(0x0080040008008efd), 0);
1664 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, UINT64_C(0xffffffffffffffc0), UINT64_C(0x0080040008008efd), &u64Old), true, UINT64_C(0xffffffffffffffc0), UINT64_C(0x0080040008008efd));
1665
1666 int64_t volatile *pi64 = (int64_t volatile *)pu64;
1667 int64_t i64Old = -3;
1668 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 64, 64, &i64Old), false, -64, -64);
1669 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 64, -64, &i64Old), true, 64, -64);
1670 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 64, -64, &i64Old), false, 64, 64);
1671 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, INT64_MIN, -64, &i64Old), false, 64, 64);
1672 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, INT64_MIN, 64, &i64Old), true, INT64_MIN, 64);
1673 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, INT64_MAX, INT64_MIN, &i64Old), true, INT64_MAX, INT64_MIN);
1674 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 42, INT64_MAX, &i64Old), true, 42, INT64_MAX);
1675
1676#if ARCH_BITS == 64
1677 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu64;
1678 RTSEMEVENT hEvtOld = (RTSEMEVENT)~(uintptr_t)31;
1679 bool fRc = true;
1680 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)~(uintptr_t)0, fRc, &hEvtOld);
1681 CHECKVAL(fRc, false, "%d");
1682 CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)42, "%p");
1683 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1684
1685 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)(uintptr_t)42, fRc, &hEvtOld);
1686 CHECKVAL(fRc, true, "%d");
1687 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, "%p");
1688 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1689
1690 void * volatile *ppv = (void * volatile *)pu64;
1691 void *pvOld;
1692 CHECK_OP_AND_VAL_EX2(bool, "%d", "%p", ppv, pvOld, ASMAtomicCmpXchgExPtrVoid(ppv, (void *)(intptr_t)12345678, NULL, &pvOld), false, (void *)~(uintptr_t)0x12380964, (void *)~(uintptr_t)0x12380964);
1693 CHECK_OP_AND_VAL_EX2(bool, "%d", "%p", ppv, pvOld, ASMAtomicCmpXchgExPtrVoid(ppv, (void *)(intptr_t)12345678, (void *)~(uintptr_t)0x12380964, &pvOld), true, (void *)(intptr_t)12345678, (void *)~(uintptr_t)0x12380964);
1694
1695 CHECK_OP_AND_VAL_EX2(bool, "%d", "%p", ppv, pvOld, ASMAtomicCmpXchgExPtr(ppv, (void *)~(uintptr_t)99, (void *)~(uintptr_t)99, &pvOld), false, (void *)(intptr_t)12345678, (void *)(intptr_t)12345678);
1696 CHECK_OP_AND_VAL_EX2(bool, "%d", "%p", ppv, pvOld, ASMAtomicCmpXchgExPtr(ppv, (void *)~(uintptr_t)99, (void *)(intptr_t)12345678, &pvOld), true, (void *)~(intptr_t)99, (void *)(intptr_t)12345678);
1697#endif
1698}
1699
1700
1701static void tstASMAtomicCmpXchgEx(void)
1702{
1703 DO_SIMPLE_TEST(ASMAtomicCmpXchgExU8, uint8_t);
1704 DO_SIMPLE_TEST(ASMAtomicCmpXchgExU16, uint16_t);
1705 DO_SIMPLE_TEST(ASMAtomicCmpXchgExU32, uint32_t);
1706 DO_SIMPLE_TEST(ASMAtomicCmpXchgExU64, uint64_t);
1707}
1708
1709
1710#define TEST_RET_OLD(a_Type, a_Fmt, a_pVar, a_Function, a_uVal, a_VarExpect) do { \
1711 a_Type const uOldExpect = *(a_pVar); \
1712 a_Type uOldRet = a_Function(a_pVar, a_uVal); \
1713 if (RT_LIKELY( uOldRet == (uOldExpect) && *(a_pVar) == (a_VarExpect) )) { } \
1714 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s," a_Fmt ") -> " a_Fmt ", expected " a_Fmt "; %s=" a_Fmt ", expected " a_Fmt "\n", \
1715 __FUNCTION__, __LINE__, #a_Function, #a_pVar, a_uVal, uOldRet, uOldExpect, #a_pVar, *(a_pVar), (a_VarExpect)); \
1716 } while (0)
1717
1718
1719DECLINLINE(void) tstASMAtomicAddU32Worker(uint32_t *pu32)
1720{
1721 *pu32 = 10;
1722 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 1, 11);
1723 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0xfffffffe), 9);
1724 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0xfffffff7), 0);
1725 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0x7fffffff), UINT32_C(0x7fffffff));
1726 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 1, UINT32_C(0x80000000));
1727 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 1, UINT32_C(0x80000001));
1728 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0x7fffffff), 0);
1729 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 0, 0);
1730
1731 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, 0, 0);
1732 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, 32, UINT32_C(0xffffffe0));
1733 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, UINT32_C(0x7fffffff), UINT32_C(0x7fffffe1));
1734 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, UINT32_C(0x7fffffde), UINT32_C(0x00000003));
1735}
1736
1737
1738DECLINLINE(void) tstASMAtomicAddS32Worker(int32_t *pi32)
1739{
1740 *pi32 = 10;
1741 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 1, 11);
1742 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, -2, 9);
1743 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, -9, 0);
1744 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, -0x7fffffff, -0x7fffffff);
1745 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 0, -0x7fffffff);
1746 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 0x7fffffff, 0);
1747 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 0, 0);
1748
1749 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicSubS32, 0, 0);
1750 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicSubS32, 1, -1);
1751 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicSubS32, INT32_MIN, INT32_MAX);
1752}
1753
1754
1755DECLINLINE(void) tstASMAtomicAddU64Worker(uint64_t volatile *pu64)
1756{
1757 *pu64 = 10;
1758 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, 1, 11);
1759 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0xfffffffffffffffe), UINT64_C(0x0000000000000009));
1760 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0xfffffffffffffff7), UINT64_C(0x0000000000000000));
1761 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x7ffffffffffffff0), UINT64_C(0x7ffffffffffffff0));
1762 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x7ffffffffffffff0), UINT64_C(0xffffffffffffffe0));
1763 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x0000000000000000), UINT64_C(0xffffffffffffffe0));
1764 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x000000000000001f), UINT64_C(0xffffffffffffffff));
1765 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000000));
1766
1767 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
1768 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x0000000000000020), UINT64_C(0xffffffffffffffe0));
1769 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x7fffffffffffffff), UINT64_C(0x7fffffffffffffe1));
1770 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x7fffffffffffffdd), UINT64_C(0x0000000000000004));
1771}
1772
1773
1774DECLINLINE(void) tstASMAtomicAddS64Worker(int64_t volatile *pi64)
1775{
1776 *pi64 = 10;
1777 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 1, 11);
1778 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -2, 9);
1779 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -9, 0);
1780 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -INT64_MAX, -INT64_MAX);
1781 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 0, -INT64_MAX);
1782 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -1, INT64_MIN);
1783 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, INT64_MAX, -1);
1784 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 1, 0);
1785 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 0, 0);
1786
1787 TEST_RET_OLD(int64_t, "%d", pi64, ASMAtomicSubS64, 0, 0);
1788 TEST_RET_OLD(int64_t, "%d", pi64, ASMAtomicSubS64, 1, -1);
1789 TEST_RET_OLD(int64_t, "%d", pi64, ASMAtomicSubS64, INT64_MIN, INT64_MAX);
1790}
1791
1792
1793
1794DECLINLINE(void) tstASMAtomicAddZWorker(size_t volatile *pcb)
1795{
1796 *pcb = 10;
1797 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicAddZ, 1, 11);
1798 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicAddZ, ~(size_t)1, 9);
1799 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicAddZ, ~(size_t)8, 0);
1800
1801 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicSubZ, 0, 0);
1802 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicSubZ, 10, ~(size_t)9);
1803}
1804
1805static void tstASMAtomicAdd(void)
1806{
1807 DO_SIMPLE_TEST(ASMAtomicAddU32, uint32_t);
1808 DO_SIMPLE_TEST(ASMAtomicAddS32, int32_t);
1809 DO_SIMPLE_TEST(ASMAtomicAddU64, uint64_t);
1810 DO_SIMPLE_TEST(ASMAtomicAddS64, int64_t);
1811 DO_SIMPLE_TEST(ASMAtomicAddZ, size_t);
1812}
1813
1814
1815#define TEST_RET_NEW_NV(a_Type, a_Fmt, a_pVar, a_Function, a_VarExpect) do { \
1816 a_Type uNewRet = a_Function(a_pVar); \
1817 if (RT_LIKELY( uNewRet == (a_VarExpect) && *(a_pVar) == (a_VarExpect) )) { } \
1818 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s) -> " a_Fmt " and %s=" a_Fmt ", expected both " a_Fmt "\n", \
1819 __FUNCTION__, __LINE__, #a_Function, #a_pVar, uNewRet, #a_pVar, *(a_pVar), (a_VarExpect)); \
1820 } while (0)
1821
1822
1823DECLINLINE(void) tstASMAtomicDecIncU32Worker(uint32_t volatile *pu32)
1824{
1825 *pu32 = 3;
1826 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 2);
1827 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 1);
1828 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 0);
1829 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, UINT32_MAX);
1830 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, UINT32_MAX - 1);
1831 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, UINT32_MAX - 2);
1832 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, UINT32_MAX - 1);
1833 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, UINT32_MAX);
1834 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 0);
1835 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 1);
1836 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 2);
1837 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 1);
1838 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 2);
1839 *pu32 = _1M;
1840 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, _1M - 1);
1841 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, _1M);
1842 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, _1M + 1);
1843}
1844
1845DECLINLINE(void) tstASMAtomicUoDecIncU32Worker(uint32_t volatile *pu32)
1846{
1847 *pu32 = 3;
1848 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 2);
1849 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 1);
1850 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 0);
1851 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, UINT32_MAX);
1852 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, UINT32_MAX - 1);
1853 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, UINT32_MAX - 2);
1854 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, UINT32_MAX - 1);
1855 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, UINT32_MAX);
1856 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 0);
1857 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 1);
1858 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 2);
1859 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 1);
1860 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 2);
1861 *pu32 = _1M;
1862 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, _1M - 1);
1863 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, _1M);
1864 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, _1M + 1);
1865}
1866
1867
1868DECLINLINE(void) tstASMAtomicDecIncS32Worker(int32_t volatile *pi32)
1869{
1870 *pi32 = 10;
1871 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 9);
1872 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 8);
1873 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 7);
1874 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 6);
1875 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 5);
1876 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 4);
1877 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 3);
1878 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 2);
1879 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 1);
1880 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 0);
1881 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, -1);
1882 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, -2);
1883 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, -1);
1884 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 0);
1885 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 1);
1886 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 2);
1887 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 3);
1888 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 2);
1889 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 3);
1890 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 2);
1891 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 3);
1892 *pi32 = INT32_MAX;
1893 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, INT32_MAX - 1);
1894 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, INT32_MAX);
1895 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, INT32_MIN);
1896}
1897
1898
1899#if 0
1900DECLINLINE(void) tstASMAtomicUoDecIncS32Worker(int32_t volatile *pi32)
1901{
1902 *pi32 = 10;
1903 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 9);
1904 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 8);
1905 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 7);
1906 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 6);
1907 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 5);
1908 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 4);
1909 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 3);
1910 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 2);
1911 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 1);
1912 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 0);
1913 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, -1);
1914 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, -2);
1915 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, -1);
1916 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 0);
1917 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 1);
1918 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 2);
1919 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 3);
1920 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 2);
1921 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 3);
1922 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 2);
1923 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 3);
1924 *pi32 = INT32_MAX;
1925 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, INT32_MAX - 1);
1926 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, INT32_MAX);
1927 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, INT32_MIN);
1928}
1929#endif
1930
1931
1932DECLINLINE(void) tstASMAtomicDecIncU64Worker(uint64_t volatile *pu64)
1933{
1934 *pu64 = 3;
1935 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, 2);
1936 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, 1);
1937 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, 0);
1938 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, UINT64_MAX);
1939 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, UINT64_MAX - 1);
1940 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, UINT64_MAX - 2);
1941 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, UINT64_MAX - 1);
1942 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, UINT64_MAX);
1943 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, 0);
1944 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, 1);
1945 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, 2);
1946 *pu64 = _4G - 1;
1947 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, _4G - 2);
1948 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, _4G - 1);
1949 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, _4G);
1950 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, _4G + 1);
1951 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, _4G);
1952}
1953
1954
1955#if 0
1956DECLINLINE(void) tstASMAtomicUoDecIncU64Worker(uint64_t volatile *pu64)
1957{
1958 *pu64 = 3;
1959 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, 2);
1960 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, 1);
1961 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, 0);
1962 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, UINT64_MAX);
1963 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, UINT64_MAX - 1);
1964 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, UINT64_MAX - 2);
1965 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, UINT64_MAX - 1);
1966 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, UINT64_MAX);
1967 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, 0);
1968 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, 1);
1969 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, 2);
1970 *pu64 = _4G - 1;
1971 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, _4G - 2);
1972 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, _4G - 1);
1973 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, _4G);
1974 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, _4G + 1);
1975 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, _4G);
1976}
1977#endif
1978
1979
1980DECLINLINE(void) tstASMAtomicDecIncS64Worker(int64_t volatile *pi64)
1981{
1982 *pi64 = 10;
1983 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 9);
1984 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 8);
1985 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 7);
1986 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 6);
1987 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 5);
1988 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 4);
1989 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 3);
1990 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 2);
1991 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 1);
1992 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 0);
1993 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, -1);
1994 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, -2);
1995 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, -1);
1996 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 0);
1997 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 1);
1998 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 2);
1999 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 3);
2000 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 2);
2001 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 3);
2002 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 2);
2003 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 3);
2004 *pi64 = INT64_MAX;
2005 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, INT64_MAX - 1);
2006}
2007
2008
2009#if 0
2010DECLINLINE(void) tstASMAtomicUoDecIncS64Worker(int64_t volatile *pi64)
2011{
2012 *pi64 = 10;
2013 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 9);
2014 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 8);
2015 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 7);
2016 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 6);
2017 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 5);
2018 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 4);
2019 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 3);
2020 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 2);
2021 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 1);
2022 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 0);
2023 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, -1);
2024 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, -2);
2025 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, -1);
2026 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 0);
2027 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 1);
2028 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 2);
2029 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 3);
2030 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 2);
2031 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 3);
2032 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 2);
2033 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 3);
2034 *pi64 = INT64_MAX;
2035 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, INT64_MAX - 1);
2036}
2037#endif
2038
2039
2040DECLINLINE(void) tstASMAtomicDecIncZWorker(size_t volatile *pcb)
2041{
2042 size_t const uBaseVal = ~(size_t)0 >> 7;
2043 *pcb = uBaseVal;
2044 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 1);
2045 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 2);
2046 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 3);
2047 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal - 2);
2048 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal - 1);
2049 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal);
2050 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal + 1);
2051 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal);
2052 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 1);
2053 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal);
2054}
2055
2056
2057static void tstASMAtomicDecInc(void)
2058{
2059 DO_SIMPLE_TEST(ASMAtomicDecIncU32, uint32_t);
2060 DO_SIMPLE_TEST(ASMAtomicUoDecIncU32, uint32_t);
2061 DO_SIMPLE_TEST(ASMAtomicDecIncS32, int32_t);
2062 //DO_SIMPLE_TEST(ASMAtomicUoDecIncS32, int32_t);
2063 DO_SIMPLE_TEST(ASMAtomicDecIncU64, uint64_t);
2064 //DO_SIMPLE_TEST(ASMAtomicUoDecIncU64, uint64_t);
2065 DO_SIMPLE_TEST(ASMAtomicDecIncS64, int64_t);
2066 //DO_SIMPLE_TEST(ASMAtomicUoDecIncS64, int64_t);
2067 DO_SIMPLE_TEST(ASMAtomicDecIncZ, size_t);
2068}
2069
2070
2071#define TEST_RET_VOID(a_Type, a_Fmt, a_pVar, a_Function, a_uVal, a_VarExpect) do { \
2072 a_Function(a_pVar, a_uVal); \
2073 if (RT_LIKELY( *(a_pVar) == (a_VarExpect) )) { } \
2074 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s, " a_Fmt ") -> %s=" a_Fmt ", expected " a_Fmt "\n", \
2075 __FUNCTION__, __LINE__, #a_Function, #a_pVar, a_uVal, #a_pVar, *(a_pVar), (a_VarExpect)); \
2076 } while (0)
2077
2078#define TEST_RET_NEW(a_Type, a_Fmt, a_pVar, a_Function, a_uVal, a_VarExpect) do { \
2079 a_Type uNewRet = a_Function(a_pVar, a_uVal); \
2080 if (RT_LIKELY( uNewRet == (a_VarExpect) && *(a_pVar) == (a_VarExpect) )) { } \
2081 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s, " a_Fmt ") -> " a_Fmt " and %s=" a_Fmt ", expected both " a_Fmt "\n", \
2082 __FUNCTION__, __LINE__, #a_Function, #a_pVar, a_uVal, uNewRet, #a_pVar, *(a_pVar), (a_VarExpect)); \
2083 } while (0)
2084
2085
2086DECLINLINE(void) tstASMAtomicAndOrXorU32Worker(uint32_t volatile *pu32)
2087{
2088 *pu32 = UINT32_C(0xffffffff);
2089 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2090 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2091 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0x8f8f8f8f), UINT32_C(0x8f8f8f8f));
2092 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0x70707070), UINT32_C(0xffffffff));
2093 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(1), UINT32_C(1));
2094 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0x80000000), UINT32_C(0x80000001));
2095 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0x80000000), UINT32_C(0x80000000));
2096 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0), UINT32_C(0));
2097 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0x42424242), UINT32_C(0x42424242));
2098 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0x00ff0f00), UINT32_C(0x00420200));
2099 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicXorU32, UINT32_C(0x42004042), UINT32_C(0x42424242));
2100 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicXorU32, UINT32_C(0xff024200), UINT32_C(0xbd400042));
2101 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicXorU32, UINT32_C(0x00000000), UINT32_C(0xbd400042));
2102}
2103
2104
2105DECLINLINE(void) tstASMAtomicUoAndOrXorU32Worker(uint32_t volatile *pu32)
2106{
2107 *pu32 = UINT32_C(0xffffffff);
2108 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2109 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2110 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0x8f8f8f8f), UINT32_C(0x8f8f8f8f));
2111 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0x70707070), UINT32_C(0xffffffff));
2112 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(1), UINT32_C(1));
2113 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0x80000000), UINT32_C(0x80000001));
2114 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0x80000000), UINT32_C(0x80000000));
2115 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0), UINT32_C(0));
2116 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0x42424242), UINT32_C(0x42424242));
2117 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0x00ff0f00), UINT32_C(0x00420200));
2118 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoXorU32, UINT32_C(0x42004042), UINT32_C(0x42424242));
2119 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoXorU32, UINT32_C(0xff024200), UINT32_C(0xbd400042));
2120 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoXorU32, UINT32_C(0x00000000), UINT32_C(0xbd400042));
2121}
2122
2123
2124DECLINLINE(void) tstASMAtomicAndOrXorExU32Worker(uint32_t volatile *pu32)
2125{
2126 *pu32 = UINT32_C(0xffffffff);
2127 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2128 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2129 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0x8f8f8f8f), UINT32_C(0x8f8f8f8f));
2130 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0x70707070), UINT32_C(0xffffffff));
2131 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(1), UINT32_C(1));
2132 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0x80000000), UINT32_C(0x80000001));
2133 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0x80000000), UINT32_C(0x80000000));
2134 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0), UINT32_C(0));
2135 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0x42424242), UINT32_C(0x42424242));
2136 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0x00ff0f00), UINT32_C(0x00420200));
2137 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicXorExU32, UINT32_C(0x42004042), UINT32_C(0x42424242));
2138 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicXorExU32, UINT32_C(0xff024200), UINT32_C(0xbd400042));
2139 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicXorExU32, UINT32_C(0x00000000), UINT32_C(0xbd400042));
2140}
2141
2142
2143DECLINLINE(void) tstASMAtomicAndOrXorU64Worker(uint64_t volatile *pu64)
2144{
2145 *pu64 = UINT64_C(0xffffffff);
2146 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2147 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2148 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x8f8f8f8f), UINT64_C(0x8f8f8f8f));
2149 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x70707070), UINT64_C(0xffffffff));
2150 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(1), UINT64_C(1));
2151 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x80000000), UINT64_C(0x80000001));
2152 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x80000000), UINT64_C(0x80000000));
2153 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0), UINT64_C(0));
2154 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x42424242), UINT64_C(0x42424242));
2155 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x00ff0f00), UINT64_C(0x00420200));
2156 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x42004042), UINT64_C(0x42424242));
2157 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0xff024200), UINT64_C(0xbd400042));
2158 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x00000000), UINT64_C(0xbd400042));
2159
2160 /* full 64-bit */
2161 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
2162 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2163 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2164 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x8f8f8f8f8f8f8f8f), UINT64_C(0x8f8f8f8f8f8f8f8f));
2165 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x7070707070707070), UINT64_C(0xffffffffffffffff));
2166 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001));
2167 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000001));
2168 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000000));
2169 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0), UINT64_C(0));
2170 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x4242424242424242), UINT64_C(0x4242424242424242));
2171 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x00ff0f00ff0f0000), UINT64_C(0x0042020042020000));
2172 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x4200404242040000), UINT64_C(0x4242424242420000));
2173 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0xff02420000ff2127), UINT64_C(0xbd40004242bd2127));
2174 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x0000000000000000), UINT64_C(0xbd40004242bd2127));
2175}
2176
2177
2178DECLINLINE(void) tstASMAtomicUoAndOrXorU64Worker(uint64_t volatile *pu64)
2179{
2180 *pu64 = UINT64_C(0xffffffff);
2181 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2182 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2183 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x8f8f8f8f), UINT64_C(0x8f8f8f8f));
2184 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x70707070), UINT64_C(0xffffffff));
2185 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(1), UINT64_C(1));
2186 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x80000000), UINT64_C(0x80000001));
2187 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x80000000), UINT64_C(0x80000000));
2188 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0), UINT64_C(0));
2189 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x42424242), UINT64_C(0x42424242));
2190 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x00ff0f00), UINT64_C(0x00420200));
2191 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x42004042), UINT64_C(0x42424242));
2192 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0xff024200), UINT64_C(0xbd400042));
2193 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x00000000), UINT64_C(0xbd400042));
2194
2195 /* full 64-bit */
2196 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
2197 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2198 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2199 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x8f8f8f8f8f8f8f8f), UINT64_C(0x8f8f8f8f8f8f8f8f));
2200 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x7070707070707070), UINT64_C(0xffffffffffffffff));
2201 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001));
2202 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000001));
2203 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000000));
2204 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0), UINT64_C(0));
2205 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x4242424242424242), UINT64_C(0x4242424242424242));
2206 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x00ff0f00ff0f0000), UINT64_C(0x0042020042020000));
2207 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x4200404242040000), UINT64_C(0x4242424242420000));
2208 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0xff02420000ff2127), UINT64_C(0xbd40004242bd2127));
2209 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x0000000000000000), UINT64_C(0xbd40004242bd2127));
2210}
2211
2212
2213#if 0
2214DECLINLINE(void) tstASMAtomicAndOrXorExU64Worker(uint64_t volatile *pu64)
2215{
2216 *pu64 = UINT64_C(0xffffffff);
2217 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2218 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2219 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x8f8f8f8f), UINT64_C(0x8f8f8f8f));
2220 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x70707070), UINT64_C(0xffffffff));
2221 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(1), UINT64_C(1));
2222 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x80000000), UINT64_C(0x80000001));
2223 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x80000000), UINT64_C(0x80000000));
2224 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0), UINT64_C(0));
2225 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x42424242), UINT64_C(0x42424242));
2226 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x00ff0f00), UINT64_C(0x00420200));
2227 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x42004042), UINT64_C(0x42424242));
2228 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0xff024200), UINT64_C(0xbd400042));
2229 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x00000000), UINT64_C(0xbd400042));
2230
2231 /* full 64-bit */
2232 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
2233 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2234 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2235 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x8f8f8f8f8f8f8f8f), UINT64_C(0x8f8f8f8f8f8f8f8f));
2236 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x7070707070707070), UINT64_C(0xffffffffffffffff));
2237 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001));
2238 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000001));
2239 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000000));
2240 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0), UINT64_C(0));
2241 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x4242424242424242), UINT64_C(0x4242424242424242));
2242 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x00ff0f00ff0f0000), UINT64_C(0x0042020042020000));
2243 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x4200404242040000), UINT64_C(0x4242424242420000));
2244 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0xff02420000ff2127), UINT64_C(0xbd40004242bd2127));
2245 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x0000000000000000), UINT64_C(0xbd40004242bd2127));
2246}
2247#endif
2248
2249
2250static void tstASMAtomicAndOrXor(void)
2251{
2252 DO_SIMPLE_TEST(ASMAtomicAndOrXorU32, uint32_t);
2253 DO_SIMPLE_TEST(ASMAtomicUoAndOrXorU32, uint32_t);
2254 DO_SIMPLE_TEST(ASMAtomicAndOrXorExU32, uint32_t);
2255 DO_SIMPLE_TEST(ASMAtomicAndOrXorU64, uint64_t);
2256 DO_SIMPLE_TEST(ASMAtomicUoAndOrXorU64, uint64_t);
2257 //DO_SIMPLE_TEST(ASMAtomicAndOrXorExU64, uint64_t);
2258}
2259
2260
2261typedef struct
2262{
2263 uint8_t ab[PAGE_SIZE];
2264} TSTPAGE;
2265
2266
2267DECLINLINE(void) tstASMMemZeroPageWorker(TSTPAGE *pPage)
2268{
2269 for (unsigned j = 0; j < 16; j++)
2270 {
2271 memset(pPage, 0x11 * j, sizeof(*pPage));
2272 ASMMemZeroPage(pPage);
2273 for (unsigned i = 0; i < sizeof(pPage->ab); i++)
2274 if (pPage->ab[i])
2275 RTTestFailed(g_hTest, "ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
2276 if (ASMMemIsZeroPage(pPage) != true)
2277 RTTestFailed(g_hTest, "ASMMemIsZeroPage returns false after ASMMemZeroPage!\n");
2278 if (ASMMemFirstMismatchingU32(pPage, sizeof(pPage), 0) != NULL)
2279 RTTestFailed(g_hTest, "ASMMemFirstMismatchingU32(,,0) returns non-NULL after ASMMemZeroPage!\n");
2280 }
2281}
2282
2283
2284static void tstASMMemZeroPage(void)
2285{
2286 RTTestISub("ASMMemZeroPage");
2287 DO_SIMPLE_TEST_NO_SUB_NO_STACK(tstASMMemZeroPageWorker, TSTPAGE);
2288}
2289
2290
2291void tstASMMemIsZeroPage(RTTEST hTest)
2292{
2293 RTTestSub(hTest, "ASMMemIsZeroPage");
2294
2295 void *pvPage1 = RTTestGuardedAllocHead(hTest, PAGE_SIZE);
2296 void *pvPage2 = RTTestGuardedAllocTail(hTest, PAGE_SIZE);
2297 RTTESTI_CHECK_RETV(pvPage1 && pvPage2);
2298
2299 memset(pvPage1, 0, PAGE_SIZE);
2300 memset(pvPage2, 0, PAGE_SIZE);
2301 RTTESTI_CHECK(ASMMemIsZeroPage(pvPage1));
2302 RTTESTI_CHECK(ASMMemIsZeroPage(pvPage2));
2303
2304 memset(pvPage1, 0xff, PAGE_SIZE);
2305 memset(pvPage2, 0xff, PAGE_SIZE);
2306 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
2307 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
2308
2309 memset(pvPage1, 0, PAGE_SIZE);
2310 memset(pvPage2, 0, PAGE_SIZE);
2311 for (unsigned off = 0; off < PAGE_SIZE; off++)
2312 {
2313 ((uint8_t *)pvPage1)[off] = 1;
2314 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
2315 ((uint8_t *)pvPage1)[off] = 0;
2316
2317 ((uint8_t *)pvPage2)[off] = 0x80;
2318 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
2319 ((uint8_t *)pvPage2)[off] = 0;
2320 }
2321
2322 RTTestSubDone(hTest);
2323}
2324
2325
2326void tstASMMemFirstMismatchingU8(RTTEST hTest)
2327{
2328 RTTestSub(hTest, "ASMMemFirstMismatchingU8");
2329
2330 uint8_t *pbPage1 = (uint8_t *)RTTestGuardedAllocHead(hTest, PAGE_SIZE);
2331 uint8_t *pbPage2 = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
2332 RTTESTI_CHECK_RETV(pbPage1 && pbPage2);
2333
2334 memset(pbPage1, 0, PAGE_SIZE);
2335 memset(pbPage2, 0, PAGE_SIZE);
2336 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0) == NULL);
2337 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0) == NULL);
2338 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 1) == pbPage1);
2339 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 1) == pbPage2);
2340 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0x87) == pbPage1);
2341 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0x87) == pbPage2);
2342 RTTESTI_CHECK(ASMMemIsZero(pbPage1, PAGE_SIZE));
2343 RTTESTI_CHECK(ASMMemIsZero(pbPage2, PAGE_SIZE));
2344 RTTESTI_CHECK(ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0));
2345 RTTESTI_CHECK(ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0));
2346 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0x34));
2347 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0x88));
2348 unsigned cbSub = 32;
2349 while (cbSub-- > 0)
2350 {
2351 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0) == NULL);
2352 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0) == NULL);
2353 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0) == NULL);
2354 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0) == NULL);
2355
2356 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0x34) == &pbPage1[PAGE_SIZE - cbSub] || !cbSub);
2357 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0x99) == &pbPage2[PAGE_SIZE - cbSub] || !cbSub);
2358 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0x42) == pbPage1 || !cbSub);
2359 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0x88) == pbPage2 || !cbSub);
2360 }
2361
2362 memset(pbPage1, 0xff, PAGE_SIZE);
2363 memset(pbPage2, 0xff, PAGE_SIZE);
2364 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0xff) == NULL);
2365 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0xff) == NULL);
2366 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0xfe) == pbPage1);
2367 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0xfe) == pbPage2);
2368 RTTESTI_CHECK(!ASMMemIsZero(pbPage1, PAGE_SIZE));
2369 RTTESTI_CHECK(!ASMMemIsZero(pbPage2, PAGE_SIZE));
2370 RTTESTI_CHECK(ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0xff));
2371 RTTESTI_CHECK(ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0xff));
2372 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0));
2373 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0));
2374 cbSub = 32;
2375 while (cbSub-- > 0)
2376 {
2377 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0xff) == NULL);
2378 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0xff) == NULL);
2379 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0xff) == NULL);
2380 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0xff) == NULL);
2381
2382 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0xfe) == &pbPage1[PAGE_SIZE - cbSub] || !cbSub);
2383 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0xfe) == &pbPage2[PAGE_SIZE - cbSub] || !cbSub);
2384 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0xfe) == pbPage1 || !cbSub);
2385 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0xfe) == pbPage2 || !cbSub);
2386 }
2387
2388
2389 /*
2390 * Various alignments and sizes.
2391 */
2392 uint8_t const bFiller1 = 0x00;
2393 uint8_t const bFiller2 = 0xf6;
2394 size_t const cbBuf = 128;
2395 uint8_t *pbBuf1 = pbPage1;
2396 uint8_t *pbBuf2 = &pbPage2[PAGE_SIZE - cbBuf]; /* Put it up against the tail guard */
2397 memset(pbPage1, ~bFiller1, PAGE_SIZE);
2398 memset(pbPage2, ~bFiller2, PAGE_SIZE);
2399 memset(pbBuf1, bFiller1, cbBuf);
2400 memset(pbBuf2, bFiller2, cbBuf);
2401 for (size_t offNonZero = 0; offNonZero < cbBuf; offNonZero++)
2402 {
2403 uint8_t bRand = (uint8_t)RTRandU32();
2404 pbBuf1[offNonZero] = bRand | 1;
2405 pbBuf2[offNonZero] = (0x80 | bRand) ^ 0xf6;
2406
2407 for (size_t offStart = 0; offStart < 32; offStart++)
2408 {
2409 size_t const cbMax = cbBuf - offStart;
2410 for (size_t cb = 0; cb < cbMax; cb++)
2411 {
2412 size_t const offEnd = offStart + cb;
2413 uint8_t bSaved1, bSaved2;
2414 if (offEnd < PAGE_SIZE)
2415 {
2416 bSaved1 = pbBuf1[offEnd];
2417 bSaved2 = pbBuf2[offEnd];
2418 pbBuf1[offEnd] = 0xff;
2419 pbBuf2[offEnd] = 0xff;
2420 }
2421#ifdef _MSC_VER /* simple stupid compiler warnings */
2422 else
2423 bSaved1 = bSaved2 = 0;
2424#endif
2425
2426 uint8_t *pbRet = (uint8_t *)ASMMemFirstMismatchingU8(pbBuf1 + offStart, cb, bFiller1);
2427 RTTESTI_CHECK(offNonZero - offStart < cb ? pbRet == &pbBuf1[offNonZero] : pbRet == NULL);
2428
2429 pbRet = (uint8_t *)ASMMemFirstMismatchingU8(pbBuf2 + offStart, cb, bFiller2);
2430 RTTESTI_CHECK(offNonZero - offStart < cb ? pbRet == &pbBuf2[offNonZero] : pbRet == NULL);
2431
2432 if (offEnd < PAGE_SIZE)
2433 {
2434 pbBuf1[offEnd] = bSaved1;
2435 pbBuf2[offEnd] = bSaved2;
2436 }
2437 }
2438 }
2439
2440 pbBuf1[offNonZero] = 0;
2441 pbBuf2[offNonZero] = 0xf6;
2442 }
2443
2444 RTTestSubDone(hTest);
2445}
2446
2447
2448typedef struct TSTBUF32 { uint32_t au32[384]; } TSTBUF32;
2449
2450DECLINLINE(void) tstASMMemZero32Worker(TSTBUF32 *pBuf)
2451{
2452 ASMMemZero32(pBuf, sizeof(*pBuf));
2453 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2454 if (pBuf->au32[i])
2455 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear dword at index %#x!\n", i);
2456 if (ASMMemFirstNonZero(pBuf, sizeof(*pBuf)) != NULL)
2457 RTTestFailed(g_hTest, "ASMMemFirstNonZero return non-NULL after ASMMemZero32\n");
2458 if (!ASMMemIsZero(pBuf, sizeof(*pBuf)))
2459 RTTestFailed(g_hTest, "ASMMemIsZero return false after ASMMemZero32\n");
2460
2461 memset(pBuf, 0xfe, sizeof(*pBuf));
2462 ASMMemZero32(pBuf, sizeof(*pBuf));
2463 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2464 if (pBuf->au32[i])
2465 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear dword at index %#x!\n", i);
2466 if (ASMMemFirstNonZero(pBuf, sizeof(*pBuf)) != NULL)
2467 RTTestFailed(g_hTest, "ASMMemFirstNonZero return non-NULL after ASMMemZero32\n");
2468 if (!ASMMemIsZero(pBuf, sizeof(*pBuf)))
2469 RTTestFailed(g_hTest, "ASMMemIsZero return false after ASMMemZero32\n");
2470}
2471
2472
2473void tstASMMemZero32(void)
2474{
2475 RTTestSub(g_hTest, "ASMMemZero32");
2476
2477 struct
2478 {
2479 uint64_t u64Magic1;
2480 uint8_t abPage[PAGE_SIZE - 32];
2481 uint64_t u64Magic2;
2482 } Buf1, Buf2, Buf3;
2483
2484 Buf1.u64Magic1 = UINT64_C(0xffffffffffffffff);
2485 memset(Buf1.abPage, 0x55, sizeof(Buf1.abPage));
2486 Buf1.u64Magic2 = UINT64_C(0xffffffffffffffff);
2487 Buf2.u64Magic1 = UINT64_C(0xffffffffffffffff);
2488 memset(Buf2.abPage, 0x77, sizeof(Buf2.abPage));
2489 Buf2.u64Magic2 = UINT64_C(0xffffffffffffffff);
2490 Buf3.u64Magic1 = UINT64_C(0xffffffffffffffff);
2491 memset(Buf3.abPage, 0x99, sizeof(Buf3.abPage));
2492 Buf3.u64Magic2 = UINT64_C(0xffffffffffffffff);
2493 ASMMemZero32(Buf1.abPage, sizeof(Buf1.abPage));
2494 ASMMemZero32(Buf2.abPage, sizeof(Buf2.abPage));
2495 ASMMemZero32(Buf3.abPage, sizeof(Buf3.abPage));
2496 if ( Buf1.u64Magic1 != UINT64_C(0xffffffffffffffff)
2497 || Buf1.u64Magic2 != UINT64_C(0xffffffffffffffff)
2498 || Buf2.u64Magic1 != UINT64_C(0xffffffffffffffff)
2499 || Buf2.u64Magic2 != UINT64_C(0xffffffffffffffff)
2500 || Buf3.u64Magic1 != UINT64_C(0xffffffffffffffff)
2501 || Buf3.u64Magic2 != UINT64_C(0xffffffffffffffff))
2502 {
2503 RTTestFailed(g_hTest, "ASMMemZero32 violated one/both magic(s)!\n");
2504 }
2505 for (unsigned i = 0; i < RT_ELEMENTS(Buf1.abPage); i++)
2506 if (Buf1.abPage[i])
2507 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear byte at offset %#x!\n", i);
2508 for (unsigned i = 0; i < RT_ELEMENTS(Buf2.abPage); i++)
2509 if (Buf2.abPage[i])
2510 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear byte at offset %#x!\n", i);
2511 for (unsigned i = 0; i < RT_ELEMENTS(Buf3.abPage); i++)
2512 if (Buf3.abPage[i])
2513 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear byte at offset %#x!\n", i);
2514
2515 DO_SIMPLE_TEST_NO_SUB(tstASMMemZero32Worker, TSTBUF32);
2516}
2517
2518
2519DECLINLINE(void) tstASMMemFill32Worker(TSTBUF32 *pBuf)
2520{
2521 ASMMemFill32(pBuf, sizeof(*pBuf), UINT32_C(0xf629bce1));
2522 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2523 if (pBuf->au32[i] != UINT32_C(0xf629bce1))
2524 RTTestFailed(g_hTest, "ASMMemFill32 didn't set dword at index %#x correctly!\n", i);
2525 if (ASMMemFirstMismatchingU32(pBuf, sizeof(*pBuf), UINT32_C(0xf629bce1)) != NULL)
2526 RTTestFailed(g_hTest, "ASMMemFirstMismatchingU32(,,UINT32_C(0xf629bce1)) returns non-NULL after ASMMemFill32!\n");
2527
2528 memset(pBuf, 0xfe, sizeof(*pBuf));
2529 ASMMemFill32(pBuf, sizeof(*pBuf), UINT32_C(0x12345678));
2530 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2531 if (pBuf->au32[i] != UINT32_C(0x12345678))
2532 RTTestFailed(g_hTest, "ASMMemFill32 didn't set dword at index %#x correctly!\n", i);
2533 if (ASMMemFirstMismatchingU32(pBuf, sizeof(*pBuf), UINT32_C(0x12345678)) != NULL)
2534 RTTestFailed(g_hTest, "ASMMemFirstMismatchingU32(,,UINT32_C(0x12345678)) returns non-NULL after ASMMemFill32!\n");
2535}
2536
2537void tstASMMemFill32(void)
2538{
2539 RTTestSub(g_hTest, "ASMMemFill32");
2540
2541 struct
2542 {
2543 uint64_t u64Magic1;
2544 uint32_t au32Page[PAGE_SIZE / 4];
2545 uint64_t u64Magic2;
2546 } Buf1;
2547 struct
2548 {
2549 uint64_t u64Magic1;
2550 uint32_t au32Page[(PAGE_SIZE / 4) - 3];
2551 uint64_t u64Magic2;
2552 } Buf2;
2553 struct
2554 {
2555 uint64_t u64Magic1;
2556 uint32_t au32Page[(PAGE_SIZE / 4) - 1];
2557 uint64_t u64Magic2;
2558 } Buf3;
2559
2560 Buf1.u64Magic1 = UINT64_C(0xffffffffffffffff);
2561 memset(Buf1.au32Page, 0x55, sizeof(Buf1.au32Page));
2562 Buf1.u64Magic2 = UINT64_C(0xffffffffffffffff);
2563 Buf2.u64Magic1 = UINT64_C(0xffffffffffffffff);
2564 memset(Buf2.au32Page, 0x77, sizeof(Buf2.au32Page));
2565 Buf2.u64Magic2 = UINT64_C(0xffffffffffffffff);
2566 Buf3.u64Magic1 = UINT64_C(0xffffffffffffffff);
2567 memset(Buf3.au32Page, 0x99, sizeof(Buf3.au32Page));
2568 Buf3.u64Magic2 = UINT64_C(0xffffffffffffffff);
2569 ASMMemFill32(Buf1.au32Page, sizeof(Buf1.au32Page), 0xdeadbeef);
2570 ASMMemFill32(Buf2.au32Page, sizeof(Buf2.au32Page), 0xcafeff01);
2571 ASMMemFill32(Buf3.au32Page, sizeof(Buf3.au32Page), 0xf00dd00f);
2572 if ( Buf1.u64Magic1 != UINT64_C(0xffffffffffffffff)
2573 || Buf1.u64Magic2 != UINT64_C(0xffffffffffffffff)
2574 || Buf2.u64Magic1 != UINT64_C(0xffffffffffffffff)
2575 || Buf2.u64Magic2 != UINT64_C(0xffffffffffffffff)
2576 || Buf3.u64Magic1 != UINT64_C(0xffffffffffffffff)
2577 || Buf3.u64Magic2 != UINT64_C(0xffffffffffffffff))
2578 RTTestFailed(g_hTest, "ASMMemFill32 violated one/both magic(s)!\n");
2579 for (unsigned i = 0; i < RT_ELEMENTS(Buf1.au32Page); i++)
2580 if (Buf1.au32Page[i] != 0xdeadbeef)
2581 RTTestFailed(g_hTest, "ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf1.au32Page[i], 0xdeadbeef);
2582 for (unsigned i = 0; i < RT_ELEMENTS(Buf2.au32Page); i++)
2583 if (Buf2.au32Page[i] != 0xcafeff01)
2584 RTTestFailed(g_hTest, "ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf2.au32Page[i], 0xcafeff01);
2585 for (unsigned i = 0; i < RT_ELEMENTS(Buf3.au32Page); i++)
2586 if (Buf3.au32Page[i] != 0xf00dd00f)
2587 RTTestFailed(g_hTest, "ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf3.au32Page[i], 0xf00dd00f);
2588
2589 DO_SIMPLE_TEST_NO_SUB(tstASMMemFill32Worker, TSTBUF32);
2590}
2591
2592
2593void tstASMProbe(RTTEST hTest)
2594{
2595 RTTestSub(hTest, "ASMProbeReadByte/Buffer");
2596
2597 uint8_t b = 42;
2598 RTTESTI_CHECK(ASMProbeReadByte(&b) == 42);
2599 ASMProbeReadBuffer(&b, sizeof(b));
2600
2601 for (uint32_t cPages = 1; cPages < 16; cPages++)
2602 {
2603 uint8_t *pbBuf1 = (uint8_t *)RTTestGuardedAllocHead(hTest, cPages * PAGE_SIZE);
2604 uint8_t *pbBuf2 = (uint8_t *)RTTestGuardedAllocTail(hTest, cPages * PAGE_SIZE);
2605 RTTESTI_CHECK_RETV(pbBuf1 && pbBuf2);
2606
2607 memset(pbBuf1, 0xf6, cPages * PAGE_SIZE);
2608 memset(pbBuf2, 0x42, cPages * PAGE_SIZE);
2609
2610 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf1[cPages * PAGE_SIZE - 1]) == 0xf6);
2611 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf2[cPages * PAGE_SIZE - 1]) == 0x42);
2612 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf1[0]) == 0xf6);
2613 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf2[0]) == 0x42);
2614
2615 ASMProbeReadBuffer(pbBuf1, cPages * PAGE_SIZE);
2616 ASMProbeReadBuffer(pbBuf2, cPages * PAGE_SIZE);
2617 }
2618}
2619
2620
2621void tstASMMisc(void)
2622{
2623 RTTestSub(g_hTest, "Misc");
2624 for (uint32_t i = 0; i < 20; i++)
2625 {
2626 ASMWriteFence();
2627 ASMCompilerBarrier();
2628 ASMReadFence();
2629 ASMNopPause();
2630 ASMSerializeInstruction();
2631 ASMMemoryFence();
2632 }
2633}
2634
2635void tstASMMath(void)
2636{
2637 RTTestSub(g_hTest, "Math");
2638
2639 uint64_t u64 = ASMMult2xU32RetU64(UINT32_C(0x80000000), UINT32_C(0x10000000));
2640 CHECKVAL(u64, UINT64_C(0x0800000000000000), "%#018RX64");
2641
2642 uint32_t u32 = ASMDivU64ByU32RetU32(UINT64_C(0x0800000000000000), UINT32_C(0x10000000));
2643 CHECKVAL(u32, UINT32_C(0x80000000), "%#010RX32");
2644
2645 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x00000001), UINT32_C(0x00000001), UINT32_C(0x00000001));
2646 CHECKVAL(u32, UINT32_C(0x00000001), "%#018RX32");
2647 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x10000000), UINT32_C(0x80000000), UINT32_C(0x20000000));
2648 CHECKVAL(u32, UINT32_C(0x40000000), "%#018RX32");
2649 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x76543210), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2650 CHECKVAL(u32, UINT32_C(0x76543210), "%#018RX32");
2651 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0xffffffff), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2652 CHECKVAL(u32, UINT32_C(0xffffffff), "%#018RX32");
2653 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0xffffffff), UINT32_C(0xfffffff0), UINT32_C(0xffffffff));
2654 CHECKVAL(u32, UINT32_C(0xfffffff0), "%#018RX32");
2655 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x10359583), UINT32_C(0x58734981), UINT32_C(0xf8694045));
2656 CHECKVAL(u32, UINT32_C(0x05c584ce), "%#018RX32");
2657 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x10359583), UINT32_C(0xf8694045), UINT32_C(0x58734981));
2658 CHECKVAL(u32, UINT32_C(0x2d860795), "%#018RX32");
2659
2660#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
2661 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x0000000000000001), UINT32_C(0x00000001), UINT32_C(0x00000001));
2662 CHECKVAL(u64, UINT64_C(0x0000000000000001), "%#018RX64");
2663 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x0000000100000000), UINT32_C(0x80000000), UINT32_C(0x00000002));
2664 CHECKVAL(u64, UINT64_C(0x4000000000000000), "%#018RX64");
2665 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xfedcba9876543210), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2666 CHECKVAL(u64, UINT64_C(0xfedcba9876543210), "%#018RX64");
2667 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xffffffffffffffff), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2668 CHECKVAL(u64, UINT64_C(0xffffffffffffffff), "%#018RX64");
2669 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xffffffffffffffff), UINT32_C(0xfffffff0), UINT32_C(0xffffffff));
2670 CHECKVAL(u64, UINT64_C(0xfffffff0fffffff0), "%#018RX64");
2671 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x3415934810359583), UINT32_C(0x58734981), UINT32_C(0xf8694045));
2672 CHECKVAL(u64, UINT64_C(0x128b9c3d43184763), "%#018RX64");
2673 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x3415934810359583), UINT32_C(0xf8694045), UINT32_C(0x58734981));
2674 CHECKVAL(u64, UINT64_C(0x924719355cd35a27), "%#018RX64");
2675
2676# if 0 /* bird: question is whether this should trap or not:
2677 *
2678 * frank: Of course it must trap:
2679 *
2680 * 0xfffffff8 * 0x77d7daf8 = 0x77d7daf441412840
2681 *
2682 * During the following division, the quotient must fit into a 32-bit register.
2683 * Therefore the smallest valid divisor is
2684 *
2685 * (0x77d7daf441412840 >> 32) + 1 = 0x77d7daf5
2686 *
2687 * which is definitely greater than 0x3b9aca00.
2688 *
2689 * bird: No, the C version does *not* crash. So, the question is whether there's any
2690 * code depending on it not crashing.
2691 *
2692 * Of course the assembly versions of the code crash right now for the reasons you've
2693 * given, but the 32-bit MSC version does not crash.
2694 *
2695 * frank: The C version does not crash but delivers incorrect results for this case.
2696 * The reason is
2697 *
2698 * u.s.Hi = (unsigned long)(u64Hi / u32C);
2699 *
2700 * Here the division is actually 64-bit by 64-bit but the 64-bit result is truncated
2701 * to 32 bit. If using this (optimized and fast) function we should just be sure that
2702 * the operands are in a valid range.
2703 */
2704 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xfffffff8c65d6731), UINT32_C(0x77d7daf8), UINT32_C(0x3b9aca00));
2705 CHECKVAL(u64, UINT64_C(0x02b8f9a2aa74e3dc), "%#018RX64");
2706# endif
2707#endif /* AMD64 || X86 */
2708
2709 u32 = ASMModU64ByU32RetU32(UINT64_C(0x0ffffff8c65d6731), UINT32_C(0x77d7daf8));
2710 CHECKVAL(u32, UINT32_C(0x3B642451), "%#010RX32");
2711
2712 int32_t i32;
2713 i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(-2));
2714 CHECKVAL(i32, INT32_C(-1), "%010RI32");
2715 i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(2));
2716 CHECKVAL(i32, INT32_C(-1), "%010RI32");
2717 i32 = ASMModS64ByS32RetS32(INT64_C(11), INT32_C(-2));
2718 CHECKVAL(i32, INT32_C(1), "%010RI32");
2719
2720 i32 = ASMModS64ByS32RetS32(INT64_C(92233720368547758), INT32_C(2147483647));
2721 CHECKVAL(i32, INT32_C(2104533974), "%010RI32");
2722 i32 = ASMModS64ByS32RetS32(INT64_C(-92233720368547758), INT32_C(2147483647));
2723 CHECKVAL(i32, INT32_C(-2104533974), "%010RI32");
2724}
2725
2726
2727void tstASMByteSwap(void)
2728{
2729 RTTestSub(g_hTest, "ASMByteSwap*");
2730
2731 uint64_t u64In = UINT64_C(0x0011223344556677);
2732 uint64_t u64Out = ASMByteSwapU64(u64In);
2733 CHECKVAL(u64In, UINT64_C(0x0011223344556677), "%#018RX64");
2734 CHECKVAL(u64Out, UINT64_C(0x7766554433221100), "%#018RX64");
2735 u64Out = ASMByteSwapU64(u64Out);
2736 CHECKVAL(u64Out, u64In, "%#018RX64");
2737 u64In = UINT64_C(0x0123456789abcdef);
2738 u64Out = ASMByteSwapU64(u64In);
2739 CHECKVAL(u64In, UINT64_C(0x0123456789abcdef), "%#018RX64");
2740 CHECKVAL(u64Out, UINT64_C(0xefcdab8967452301), "%#018RX64");
2741 u64Out = ASMByteSwapU64(u64Out);
2742 CHECKVAL(u64Out, u64In, "%#018RX64");
2743 u64In = 0;
2744 u64Out = ASMByteSwapU64(u64In);
2745 CHECKVAL(u64Out, u64In, "%#018RX64");
2746 u64In = UINT64_MAX;
2747 u64Out = ASMByteSwapU64(u64In);
2748 CHECKVAL(u64Out, u64In, "%#018RX64");
2749
2750 uint32_t u32In = UINT32_C(0x00112233);
2751 uint32_t u32Out = ASMByteSwapU32(u32In);
2752 CHECKVAL(u32In, UINT32_C(0x00112233), "%#010RX32");
2753 CHECKVAL(u32Out, UINT32_C(0x33221100), "%#010RX32");
2754 u32Out = ASMByteSwapU32(u32Out);
2755 CHECKVAL(u32Out, u32In, "%#010RX32");
2756 u32In = UINT32_C(0x12345678);
2757 u32Out = ASMByteSwapU32(u32In);
2758 CHECKVAL(u32In, UINT32_C(0x12345678), "%#010RX32");
2759 CHECKVAL(u32Out, UINT32_C(0x78563412), "%#010RX32");
2760 u32Out = ASMByteSwapU32(u32Out);
2761 CHECKVAL(u32Out, u32In, "%#010RX32");
2762 u32In = 0;
2763 u32Out = ASMByteSwapU32(u32In);
2764 CHECKVAL(u32Out, u32In, "%#010RX32");
2765 u32In = UINT32_MAX;
2766 u32Out = ASMByteSwapU32(u32In);
2767 CHECKVAL(u32Out, u32In, "%#010RX32");
2768
2769 uint16_t u16In = UINT16_C(0x0011);
2770 uint16_t u16Out = ASMByteSwapU16(u16In);
2771 CHECKVAL(u16In, UINT16_C(0x0011), "%#06RX16");
2772 CHECKVAL(u16Out, UINT16_C(0x1100), "%#06RX16");
2773 u16Out = ASMByteSwapU16(u16Out);
2774 CHECKVAL(u16Out, u16In, "%#06RX16");
2775 u16In = UINT16_C(0x1234);
2776 u16Out = ASMByteSwapU16(u16In);
2777 CHECKVAL(u16In, UINT16_C(0x1234), "%#06RX16");
2778 CHECKVAL(u16Out, UINT16_C(0x3412), "%#06RX16");
2779 u16Out = ASMByteSwapU16(u16Out);
2780 CHECKVAL(u16Out, u16In, "%#06RX16");
2781 u16In = 0;
2782 u16Out = ASMByteSwapU16(u16In);
2783 CHECKVAL(u16Out, u16In, "%#06RX16");
2784 u16In = UINT16_MAX;
2785 u16Out = ASMByteSwapU16(u16In);
2786 CHECKVAL(u16Out, u16In, "%#06RX16");
2787}
2788
2789
2790void tstASMBench(void)
2791{
2792 /*
2793 * Make this static. We don't want to have this located on the stack.
2794 */
2795 static uint8_t volatile s_u8;
2796 static int8_t volatile s_i8;
2797 static uint16_t volatile s_u16;
2798 static int16_t volatile s_i16;
2799 static uint32_t volatile s_u32;
2800 static int32_t volatile s_i32;
2801 static uint64_t volatile s_u64;
2802 static int64_t volatile s_i64;
2803 static uint8_t s_u8Old;
2804 static int8_t s_i8Old;
2805 static uint16_t s_u16Old;
2806 static int16_t s_i16Old;
2807 static uint32_t s_u32Old;
2808 static int32_t s_i32Old;
2809 static uint64_t s_u64Old;
2810 static int64_t s_i64Old;
2811 unsigned i;
2812 const unsigned cRounds = _16M; /* Must be multiple of 8 */
2813 uint64_t u64Elapsed;
2814
2815 RTTestSub(g_hTest, "Benchmarking");
2816
2817#if 0 && !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) || defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32))
2818# define BENCH(op, str) \
2819 do { \
2820 RTThreadYield(); \
2821 u64Elapsed = ASMReadTSC(); \
2822 for (i = cRounds; i > 0; i--) \
2823 op; \
2824 u64Elapsed = ASMReadTSC() - u64Elapsed; \
2825 RTTestValue(g_hTest, str, u64Elapsed / cRounds, RTTESTUNIT_TICKS_PER_CALL); \
2826 } while (0)
2827#else
2828# define BENCH(op, str) \
2829 do { \
2830 RTThreadYield(); \
2831 u64Elapsed = RTTimeNanoTS(); \
2832 for (i = cRounds / 8; i > 0; i--) \
2833 { \
2834 op; \
2835 op; \
2836 op; \
2837 op; \
2838 op; \
2839 op; \
2840 op; \
2841 op; \
2842 } \
2843 u64Elapsed = RTTimeNanoTS() - u64Elapsed; \
2844 RTTestValue(g_hTest, str, u64Elapsed * 1000 / cRounds, RTTESTUNIT_PS_PER_CALL); \
2845 } while (0)
2846#endif
2847#if (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) || defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)) && !defined(GCC44_32BIT_PIC)
2848# define BENCH_TSC(op, str) \
2849 do { \
2850 RTThreadYield(); \
2851 u64Elapsed = ASMReadTSC(); \
2852 for (i = cRounds / 8; i > 0; i--) \
2853 { \
2854 op; \
2855 op; \
2856 op; \
2857 op; \
2858 op; \
2859 op; \
2860 op; \
2861 op; \
2862 } \
2863 u64Elapsed = ASMReadTSC() - u64Elapsed; \
2864 RTTestValue(g_hTest, str, u64Elapsed / cRounds, RTTESTUNIT_TICKS_PER_CALL); \
2865 } while (0)
2866#else
2867# define BENCH_TSC(op, str) BENCH(op, str)
2868#endif
2869
2870 BENCH(s_u32 = 0, "s_u32 = 0");
2871 BENCH(ASMAtomicUoReadU8(&s_u8), "ASMAtomicUoReadU8");
2872 BENCH(ASMAtomicUoReadS8(&s_i8), "ASMAtomicUoReadS8");
2873 BENCH(ASMAtomicUoReadU16(&s_u16), "ASMAtomicUoReadU16");
2874 BENCH(ASMAtomicUoReadS16(&s_i16), "ASMAtomicUoReadS16");
2875 BENCH(ASMAtomicUoReadU32(&s_u32), "ASMAtomicUoReadU32");
2876 BENCH(ASMAtomicUoReadS32(&s_i32), "ASMAtomicUoReadS32");
2877 BENCH(ASMAtomicUoReadU64(&s_u64), "ASMAtomicUoReadU64");
2878 BENCH(ASMAtomicUoReadS64(&s_i64), "ASMAtomicUoReadS64");
2879 BENCH(ASMAtomicReadU8(&s_u8), "ASMAtomicReadU8");
2880 BENCH(ASMAtomicReadS8(&s_i8), "ASMAtomicReadS8");
2881 BENCH(ASMAtomicReadU16(&s_u16), "ASMAtomicReadU16");
2882 BENCH(ASMAtomicReadS16(&s_i16), "ASMAtomicReadS16");
2883 BENCH(ASMAtomicReadU32(&s_u32), "ASMAtomicReadU32");
2884 BENCH(ASMAtomicReadS32(&s_i32), "ASMAtomicReadS32");
2885 BENCH(ASMAtomicReadU64(&s_u64), "ASMAtomicReadU64");
2886 BENCH(ASMAtomicReadS64(&s_i64), "ASMAtomicReadS64");
2887 BENCH(ASMAtomicUoWriteU8(&s_u8, 0), "ASMAtomicUoWriteU8");
2888 BENCH(ASMAtomicUoWriteS8(&s_i8, 0), "ASMAtomicUoWriteS8");
2889 BENCH(ASMAtomicUoWriteU16(&s_u16, 0), "ASMAtomicUoWriteU16");
2890 BENCH(ASMAtomicUoWriteS16(&s_i16, 0), "ASMAtomicUoWriteS16");
2891 BENCH(ASMAtomicUoWriteU32(&s_u32, 0), "ASMAtomicUoWriteU32");
2892 BENCH(ASMAtomicUoWriteS32(&s_i32, 0), "ASMAtomicUoWriteS32");
2893 BENCH(ASMAtomicUoWriteU64(&s_u64, 0), "ASMAtomicUoWriteU64");
2894 BENCH(ASMAtomicUoWriteS64(&s_i64, 0), "ASMAtomicUoWriteS64");
2895 BENCH(ASMAtomicWriteU8(&s_u8, 0), "ASMAtomicWriteU8");
2896 BENCH(ASMAtomicWriteS8(&s_i8, 0), "ASMAtomicWriteS8");
2897 BENCH(ASMAtomicWriteU16(&s_u16, 0), "ASMAtomicWriteU16");
2898 BENCH(ASMAtomicWriteS16(&s_i16, 0), "ASMAtomicWriteS16");
2899 BENCH(ASMAtomicWriteU32(&s_u32, 0), "ASMAtomicWriteU32");
2900 BENCH(ASMAtomicWriteS32(&s_i32, 0), "ASMAtomicWriteS32");
2901 BENCH(ASMAtomicWriteU64(&s_u64, 0), "ASMAtomicWriteU64");
2902 BENCH(ASMAtomicWriteS64(&s_i64, 0), "ASMAtomicWriteS64");
2903 BENCH(ASMAtomicXchgU8(&s_u8, 0), "ASMAtomicXchgU8");
2904 BENCH(ASMAtomicXchgS8(&s_i8, 0), "ASMAtomicXchgS8");
2905 BENCH(ASMAtomicXchgU16(&s_u16, 0), "ASMAtomicXchgU16");
2906 BENCH(ASMAtomicXchgS16(&s_i16, 0), "ASMAtomicXchgS16");
2907 BENCH(ASMAtomicXchgU32(&s_u32, 0), "ASMAtomicXchgU32");
2908 BENCH(ASMAtomicXchgS32(&s_i32, 0), "ASMAtomicXchgS32");
2909 BENCH(ASMAtomicXchgU64(&s_u64, 0), "ASMAtomicXchgU64");
2910 BENCH(ASMAtomicXchgS64(&s_i64, 0), "ASMAtomicXchgS64");
2911 BENCH(ASMAtomicCmpXchgU8(&s_u8, 0, 0), "ASMAtomicCmpXchgU8");
2912 BENCH(ASMAtomicCmpXchgS8(&s_i8, 0, 0), "ASMAtomicCmpXchgS8");
2913 //BENCH(ASMAtomicCmpXchgU16(&s_u16, 0, 0), "ASMAtomicCmpXchgU16");
2914 //BENCH(ASMAtomicCmpXchgS16(&s_i16, 0, 0), "ASMAtomicCmpXchgS16");
2915 BENCH(ASMAtomicCmpXchgU32(&s_u32, 0, 0), "ASMAtomicCmpXchgU32");
2916 BENCH(ASMAtomicCmpXchgS32(&s_i32, 0, 0), "ASMAtomicCmpXchgS32");
2917 BENCH(ASMAtomicCmpXchgU64(&s_u64, 0, 0), "ASMAtomicCmpXchgU64");
2918 BENCH(ASMAtomicCmpXchgS64(&s_i64, 0, 0), "ASMAtomicCmpXchgS64");
2919 BENCH(ASMAtomicCmpXchgU8(&s_u8, 0, 1), "ASMAtomicCmpXchgU8/neg");
2920 BENCH(ASMAtomicCmpXchgS8(&s_i8, 0, 1), "ASMAtomicCmpXchgS8/neg");
2921 //BENCH(ASMAtomicCmpXchgU16(&s_u16, 0, 1), "ASMAtomicCmpXchgU16/neg");
2922 //BENCH(ASMAtomicCmpXchgS16(&s_s16, 0, 1), "ASMAtomicCmpXchgS16/neg");
2923 BENCH(ASMAtomicCmpXchgU32(&s_u32, 0, 1), "ASMAtomicCmpXchgU32/neg");
2924 BENCH(ASMAtomicCmpXchgS32(&s_i32, 0, 1), "ASMAtomicCmpXchgS32/neg");
2925 BENCH(ASMAtomicCmpXchgU64(&s_u64, 0, 1), "ASMAtomicCmpXchgU64/neg");
2926 BENCH(ASMAtomicCmpXchgS64(&s_i64, 0, 1), "ASMAtomicCmpXchgS64/neg");
2927 BENCH(ASMAtomicCmpXchgExU8(&s_u8, 0, 0, &s_u8Old), "ASMAtomicCmpXchgExU8");
2928 BENCH(ASMAtomicCmpXchgExS8(&s_i8, 0, 0, &s_i8Old), "ASMAtomicCmpXchgExS8");
2929 BENCH(ASMAtomicCmpXchgExU16(&s_u16, 0, 0, &s_u16Old), "ASMAtomicCmpXchgExU16");
2930 BENCH(ASMAtomicCmpXchgExS16(&s_i16, 0, 0, &s_i16Old), "ASMAtomicCmpXchgExS16");
2931 BENCH(ASMAtomicCmpXchgExU32(&s_u32, 0, 0, &s_u32Old), "ASMAtomicCmpXchgExU32");
2932 BENCH(ASMAtomicCmpXchgExS32(&s_i32, 0, 0, &s_i32Old), "ASMAtomicCmpXchgExS32");
2933 BENCH(ASMAtomicCmpXchgExU64(&s_u64, 0, 0, &s_u64Old), "ASMAtomicCmpXchgExU64");
2934 BENCH(ASMAtomicCmpXchgExS64(&s_i64, 0, 0, &s_i64Old), "ASMAtomicCmpXchgExS64");
2935 BENCH(ASMAtomicCmpXchgExU8(&s_u8, 0, 1, &s_u8Old), "ASMAtomicCmpXchgExU8/neg");
2936 BENCH(ASMAtomicCmpXchgExS8(&s_i8, 0, 1, &s_i8Old), "ASMAtomicCmpXchgExS8/neg");
2937 BENCH(ASMAtomicCmpXchgExU16(&s_u16, 0, 1, &s_u16Old), "ASMAtomicCmpXchgExU16/neg");
2938 BENCH(ASMAtomicCmpXchgExS16(&s_i16, 0, 1, &s_i16Old), "ASMAtomicCmpXchgExS16/neg");
2939 BENCH(ASMAtomicCmpXchgExU32(&s_u32, 0, 1, &s_u32Old), "ASMAtomicCmpXchgExU32/neg");
2940 BENCH(ASMAtomicCmpXchgExS32(&s_i32, 0, 1, &s_i32Old), "ASMAtomicCmpXchgExS32/neg");
2941 BENCH(ASMAtomicCmpXchgExU64(&s_u64, 0, 1, &s_u64Old), "ASMAtomicCmpXchgExU64/neg");
2942 BENCH(ASMAtomicCmpXchgExS64(&s_i64, 0, 1, &s_i64Old), "ASMAtomicCmpXchgExS64/neg");
2943 BENCH(ASMAtomicIncU32(&s_u32), "ASMAtomicIncU32");
2944 BENCH(ASMAtomicIncS32(&s_i32), "ASMAtomicIncS32");
2945 BENCH(ASMAtomicDecU32(&s_u32), "ASMAtomicDecU32");
2946 BENCH(ASMAtomicDecS32(&s_i32), "ASMAtomicDecS32");
2947 BENCH(ASMAtomicAddU32(&s_u32, 5), "ASMAtomicAddU32");
2948 BENCH(ASMAtomicAddS32(&s_i32, 5), "ASMAtomicAddS32");
2949 BENCH(ASMAtomicUoIncU32(&s_u32), "ASMAtomicUoIncU32");
2950 BENCH(ASMAtomicUoDecU32(&s_u32), "ASMAtomicUoDecU32");
2951 BENCH(ASMAtomicUoAndU32(&s_u32, 0xffffffff), "ASMAtomicUoAndU32");
2952 BENCH(ASMAtomicUoOrU32(&s_u32, 0xffffffff), "ASMAtomicUoOrU32");
2953#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
2954 BENCH_TSC(ASMSerializeInstructionCpuId(), "ASMSerializeInstructionCpuId");
2955 BENCH_TSC(ASMSerializeInstructionIRet(), "ASMSerializeInstructionIRet");
2956#endif
2957 BENCH(ASMReadFence(), "ASMReadFence");
2958 BENCH(ASMWriteFence(), "ASMWriteFence");
2959 BENCH(ASMMemoryFence(), "ASMMemoryFence");
2960 BENCH(ASMSerializeInstruction(), "ASMSerializeInstruction");
2961 BENCH(ASMNopPause(), "ASMNopPause");
2962
2963 /* The Darwin gcc does not like this ... */
2964#if !defined(RT_OS_DARWIN) && !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
2965 BENCH(s_u8 = ASMGetApicId(), "ASMGetApicId");
2966 BENCH(s_u32 = ASMGetApicIdExt0B(), "ASMGetApicIdExt0B");
2967 BENCH(s_u32 = ASMGetApicIdExt8000001E(), "ASMGetApicIdExt8000001E");
2968#endif
2969#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) || defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32))
2970 BENCH(s_u64 = ASMReadTSC(), "ASMReadTSC");
2971#endif
2972#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
2973 uint32_t uAux;
2974 if ( ASMHasCpuId()
2975 && RTX86IsValidExtRange(ASMCpuId_EAX(0x80000000))
2976 && (ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_RDTSCP) )
2977 {
2978 BENCH_TSC(ASMSerializeInstructionRdTscp(), "ASMSerializeInstructionRdTscp");
2979 BENCH(s_u64 = ASMReadTscWithAux(&uAux), "ASMReadTscWithAux");
2980 }
2981 union
2982 {
2983 uint64_t u64[2];
2984 RTIDTR Unaligned;
2985 struct
2986 {
2987 uint16_t abPadding[3];
2988 RTIDTR Aligned;
2989 } s;
2990 } uBuf;
2991 Assert(((uintptr_t)&uBuf.Unaligned.pIdt & (sizeof(uintptr_t) - 1)) != 0);
2992 BENCH(ASMGetIDTR(&uBuf.Unaligned), "ASMGetIDTR/unaligned");
2993 Assert(((uintptr_t)&uBuf.s.Aligned.pIdt & (sizeof(uintptr_t) - 1)) == 0);
2994 BENCH(ASMGetIDTR(&uBuf.s.Aligned), "ASMGetIDTR/aligned");
2995#endif
2996
2997#undef BENCH
2998}
2999
3000
3001int main(int argc, char **argv)
3002{
3003 RT_NOREF_PV(argc); RT_NOREF_PV(argv);
3004
3005 int rc = RTTestInitAndCreate("tstRTInlineAsm", &g_hTest);
3006 if (rc)
3007 return rc;
3008 RTTestBanner(g_hTest);
3009
3010 /*
3011 * Execute the tests.
3012 */
3013#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
3014 tstASMCpuId();
3015 //bruteForceCpuId();
3016#endif
3017#if 1
3018 tstASMAtomicRead();
3019 tstASMAtomicWrite();
3020 tstASMAtomicXchg();
3021 tstASMAtomicCmpXchg();
3022 tstASMAtomicCmpXchgEx();
3023
3024 tstASMAtomicAdd();
3025 tstASMAtomicDecInc();
3026 tstASMAtomicAndOrXor();
3027
3028 tstASMMemZeroPage();
3029 tstASMMemIsZeroPage(g_hTest);
3030 tstASMMemFirstMismatchingU8(g_hTest);
3031 tstASMMemZero32();
3032 tstASMMemFill32();
3033 tstASMProbe(g_hTest);
3034
3035 tstASMMisc();
3036
3037 tstASMMath();
3038
3039 tstASMByteSwap();
3040
3041 tstASMBench();
3042#endif
3043
3044 /*
3045 * Show the result.
3046 */
3047 return RTTestSummaryAndDestroy(g_hTest);
3048}
3049
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