VirtualBox

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

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

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 145.0 KB
Line 
1/* $Id: tstRTInlineAsm.cpp 93115 2022-01-01 11:31:46Z 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 = ASMIsIntelCpuEx(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, ASMGetCpuFamily(s.uEAX),
430 (s.uEAX >> 4) & 0xf, (s.uEAX >> 16) & 0x0f, ASMGetCpuModel(s.uEAX, fIntel),
431 ASMGetCpuStepping(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, ASMGetCpuFamily(s.uEAX),
561 (s.uEAX >> 4) & 0xf, (s.uEAX >> 16) & 0x0f, ASMGetCpuModel(s.uEAX, fIntel),
562 ASMGetCpuStepping(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) tstASMAtomicCmpXchgExU32Worker(uint32_t volatile *pu32)
1573{
1574 *pu32 = UINT32_C(0xffffffff);
1575 uint32_t u32Old = UINT32_C(0x80005111);
1576 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, 0, 0, &u32Old), false, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
1577 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, 0, UINT32_C(0xffffffff), &u32Old), true, 0, UINT32_C(0xffffffff));
1578 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, 0, UINT32_C(0xffffffff), &u32Old), false, 0, UINT32_C(0x00000000));
1579 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, UINT32_C(0x80088efd), 0, &u32Old), true, UINT32_C(0x80088efd), 0);
1580 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#x", pu32, u32Old, ASMAtomicCmpXchgExU32(pu32, UINT32_C(0x80088efd), 0, &u32Old), false, UINT32_C(0x80088efd), UINT32_C(0x80088efd));
1581 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));
1582
1583 int32_t volatile *pi32 = (int32_t volatile *)pu32;
1584 int32_t i32Old = 0;
1585 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, 32, 32, &i32Old), false, -32, -32);
1586 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, 32, -32, &i32Old), true, 32, -32);
1587 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MIN, 32, &i32Old), true, INT32_MIN, 32);
1588 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MIN, 32, &i32Old), false, INT32_MIN, INT32_MIN);
1589 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MAX, INT32_MAX, &i32Old), false, INT32_MIN, INT32_MIN);
1590 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, INT32_MAX, INT32_MIN, &i32Old), true, INT32_MAX, INT32_MIN);
1591 CHECK_OP_AND_VAL_EX2(bool, "%d", "%d", pi32, i32Old, ASMAtomicCmpXchgExS32(pi32, 42, INT32_MAX, &i32Old), true, 42, INT32_MAX);
1592
1593#if ARCH_BITS == 32
1594 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu32;
1595 RTSEMEVENT hEvtOld = (RTSEMEVENT)~(uintptr_t)31;
1596 bool fRc = true;
1597 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)~(uintptr_t)0, fRc, &hEvtOld);
1598 CHECKVAL(fRc, false, "%d");
1599 CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)42, "%p");
1600 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1601
1602 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)(uintptr_t)42, fRc, &hEvtOld);
1603 CHECKVAL(fRc, true, "%d");
1604 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, "%p");
1605 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1606#endif
1607}
1608
1609
1610DECLINLINE(void) tstASMAtomicCmpXchgExU64Worker(uint64_t volatile *pu64)
1611{
1612 *pu64 = UINT64_C(0xffffffffffffffff);
1613 uint64_t u64Old = UINT64_C(0x8000000051111111);
1614 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, 0, 0, &u64Old), false, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
1615 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, 0, UINT64_C(0xffffffffffffffff), &u64Old), true, 0, UINT64_C(0xffffffffffffffff));
1616 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, UINT64_C(0x0080040008008efd), 0x342, &u64Old), false, 0, 0);
1617 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#llx", pu64, u64Old, ASMAtomicCmpXchgExU64(pu64, UINT64_C(0x0080040008008efd), 0, &u64Old), true, UINT64_C(0x0080040008008efd), 0);
1618 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));
1619
1620 int64_t volatile *pi64 = (int64_t volatile *)pu64;
1621 int64_t i64Old = -3;
1622 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 64, 64, &i64Old), false, -64, -64);
1623 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 64, -64, &i64Old), true, 64, -64);
1624 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 64, -64, &i64Old), false, 64, 64);
1625 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, INT64_MIN, -64, &i64Old), false, 64, 64);
1626 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, INT64_MIN, 64, &i64Old), true, INT64_MIN, 64);
1627 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, INT64_MAX, INT64_MIN, &i64Old), true, INT64_MAX, INT64_MIN);
1628 CHECK_OP_AND_VAL_EX2(bool, "%d", "%#lld", pi64, i64Old, ASMAtomicCmpXchgExS64(pi64, 42, INT64_MAX, &i64Old), true, 42, INT64_MAX);
1629
1630#if ARCH_BITS == 64
1631 RTSEMEVENT volatile *phEvt = (RTSEMEVENT volatile *)pu64;
1632 RTSEMEVENT hEvtOld = (RTSEMEVENT)~(uintptr_t)31;
1633 bool fRc = true;
1634 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)~(uintptr_t)0, fRc, &hEvtOld);
1635 CHECKVAL(fRc, false, "%d");
1636 CHECKVAL(*phEvt, (RTSEMEVENT)(uintptr_t)42, "%p");
1637 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1638
1639 ASMAtomicCmpXchgExHandle(phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, (RTSEMEVENT)(uintptr_t)42, fRc, &hEvtOld);
1640 CHECKVAL(fRc, true, "%d");
1641 CHECKVAL(*phEvt, (RTSEMEVENT)~(uintptr_t)0x12380964, "%p");
1642 CHECKVAL(hEvtOld, (RTSEMEVENT)(uintptr_t)42, "%p");
1643
1644 void * volatile *ppv = (void * volatile *)pu64;
1645 void *pvOld;
1646 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);
1647 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);
1648
1649 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);
1650 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);
1651#endif
1652}
1653
1654
1655static void tstASMAtomicCmpXchgEx(void)
1656{
1657 DO_SIMPLE_TEST(ASMAtomicCmpXchgExU32, uint32_t);
1658 DO_SIMPLE_TEST(ASMAtomicCmpXchgExU64, uint64_t);
1659}
1660
1661
1662#define TEST_RET_OLD(a_Type, a_Fmt, a_pVar, a_Function, a_uVal, a_VarExpect) do { \
1663 a_Type const uOldExpect = *(a_pVar); \
1664 a_Type uOldRet = a_Function(a_pVar, a_uVal); \
1665 if (RT_LIKELY( uOldRet == (uOldExpect) && *(a_pVar) == (a_VarExpect) )) { } \
1666 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s," a_Fmt ") -> " a_Fmt ", expected " a_Fmt "; %s=" a_Fmt ", expected " a_Fmt "\n", \
1667 __FUNCTION__, __LINE__, #a_Function, #a_pVar, a_uVal, uOldRet, uOldExpect, #a_pVar, *(a_pVar), (a_VarExpect)); \
1668 } while (0)
1669
1670
1671DECLINLINE(void) tstASMAtomicAddU32Worker(uint32_t *pu32)
1672{
1673 *pu32 = 10;
1674 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 1, 11);
1675 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0xfffffffe), 9);
1676 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0xfffffff7), 0);
1677 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0x7fffffff), UINT32_C(0x7fffffff));
1678 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 1, UINT32_C(0x80000000));
1679 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 1, UINT32_C(0x80000001));
1680 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, UINT32_C(0x7fffffff), 0);
1681 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAddU32, 0, 0);
1682
1683 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, 0, 0);
1684 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, 32, UINT32_C(0xffffffe0));
1685 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, UINT32_C(0x7fffffff), UINT32_C(0x7fffffe1));
1686 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicSubU32, UINT32_C(0x7fffffde), UINT32_C(0x00000003));
1687}
1688
1689
1690DECLINLINE(void) tstASMAtomicAddS32Worker(int32_t *pi32)
1691{
1692 *pi32 = 10;
1693 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 1, 11);
1694 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, -2, 9);
1695 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, -9, 0);
1696 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, -0x7fffffff, -0x7fffffff);
1697 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 0, -0x7fffffff);
1698 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 0x7fffffff, 0);
1699 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicAddS32, 0, 0);
1700
1701 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicSubS32, 0, 0);
1702 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicSubS32, 1, -1);
1703 TEST_RET_OLD(int32_t, "%d", pi32, ASMAtomicSubS32, INT32_MIN, INT32_MAX);
1704}
1705
1706
1707DECLINLINE(void) tstASMAtomicAddU64Worker(uint64_t volatile *pu64)
1708{
1709 *pu64 = 10;
1710 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, 1, 11);
1711 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0xfffffffffffffffe), UINT64_C(0x0000000000000009));
1712 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0xfffffffffffffff7), UINT64_C(0x0000000000000000));
1713 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x7ffffffffffffff0), UINT64_C(0x7ffffffffffffff0));
1714 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x7ffffffffffffff0), UINT64_C(0xffffffffffffffe0));
1715 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x0000000000000000), UINT64_C(0xffffffffffffffe0));
1716 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x000000000000001f), UINT64_C(0xffffffffffffffff));
1717 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicAddU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000000));
1718
1719 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
1720 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x0000000000000020), UINT64_C(0xffffffffffffffe0));
1721 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x7fffffffffffffff), UINT64_C(0x7fffffffffffffe1));
1722 TEST_RET_OLD(uint64_t, "%llx", pu64, ASMAtomicSubU64, UINT64_C(0x7fffffffffffffdd), UINT64_C(0x0000000000000004));
1723}
1724
1725
1726DECLINLINE(void) tstASMAtomicAddS64Worker(int64_t volatile *pi64)
1727{
1728 *pi64 = 10;
1729 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 1, 11);
1730 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -2, 9);
1731 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -9, 0);
1732 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -INT64_MAX, -INT64_MAX);
1733 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 0, -INT64_MAX);
1734 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, -1, INT64_MIN);
1735 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, INT64_MAX, -1);
1736 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 1, 0);
1737 TEST_RET_OLD(int64_t, "%lld", pi64, ASMAtomicAddS64, 0, 0);
1738
1739 TEST_RET_OLD(int64_t, "%d", pi64, ASMAtomicSubS64, 0, 0);
1740 TEST_RET_OLD(int64_t, "%d", pi64, ASMAtomicSubS64, 1, -1);
1741 TEST_RET_OLD(int64_t, "%d", pi64, ASMAtomicSubS64, INT64_MIN, INT64_MAX);
1742}
1743
1744
1745
1746DECLINLINE(void) tstASMAtomicAddZWorker(size_t volatile *pcb)
1747{
1748 *pcb = 10;
1749 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicAddZ, 1, 11);
1750 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicAddZ, ~(size_t)1, 9);
1751 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicAddZ, ~(size_t)8, 0);
1752
1753 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicSubZ, 0, 0);
1754 TEST_RET_OLD(size_t, "%zx", pcb, ASMAtomicSubZ, 10, ~(size_t)9);
1755}
1756
1757static void tstASMAtomicAdd(void)
1758{
1759 DO_SIMPLE_TEST(ASMAtomicAddU32, uint32_t);
1760 DO_SIMPLE_TEST(ASMAtomicAddS32, int32_t);
1761 DO_SIMPLE_TEST(ASMAtomicAddU64, uint64_t);
1762 DO_SIMPLE_TEST(ASMAtomicAddS64, int64_t);
1763 DO_SIMPLE_TEST(ASMAtomicAddZ, size_t);
1764}
1765
1766
1767#define TEST_RET_NEW_NV(a_Type, a_Fmt, a_pVar, a_Function, a_VarExpect) do { \
1768 a_Type uNewRet = a_Function(a_pVar); \
1769 if (RT_LIKELY( uNewRet == (a_VarExpect) && *(a_pVar) == (a_VarExpect) )) { } \
1770 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s) -> " a_Fmt " and %s=" a_Fmt ", expected both " a_Fmt "\n", \
1771 __FUNCTION__, __LINE__, #a_Function, #a_pVar, uNewRet, #a_pVar, *(a_pVar), (a_VarExpect)); \
1772 } while (0)
1773
1774
1775DECLINLINE(void) tstASMAtomicDecIncU32Worker(uint32_t volatile *pu32)
1776{
1777 *pu32 = 3;
1778 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 2);
1779 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 1);
1780 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 0);
1781 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, UINT32_MAX);
1782 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, UINT32_MAX - 1);
1783 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, UINT32_MAX - 2);
1784 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, UINT32_MAX - 1);
1785 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, UINT32_MAX);
1786 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 0);
1787 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 1);
1788 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 2);
1789 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, 1);
1790 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, 2);
1791 *pu32 = _1M;
1792 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicDecU32, _1M - 1);
1793 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, _1M);
1794 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicIncU32, _1M + 1);
1795}
1796
1797DECLINLINE(void) tstASMAtomicUoDecIncU32Worker(uint32_t volatile *pu32)
1798{
1799 *pu32 = 3;
1800 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 2);
1801 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 1);
1802 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 0);
1803 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, UINT32_MAX);
1804 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, UINT32_MAX - 1);
1805 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, UINT32_MAX - 2);
1806 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, UINT32_MAX - 1);
1807 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, UINT32_MAX);
1808 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 0);
1809 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 1);
1810 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 2);
1811 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, 1);
1812 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, 2);
1813 *pu32 = _1M;
1814 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoDecU32, _1M - 1);
1815 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, _1M);
1816 TEST_RET_NEW_NV(uint32_t, "%#x", pu32, ASMAtomicUoIncU32, _1M + 1);
1817}
1818
1819
1820DECLINLINE(void) tstASMAtomicDecIncS32Worker(int32_t volatile *pi32)
1821{
1822 *pi32 = 10;
1823 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 9);
1824 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 8);
1825 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 7);
1826 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 6);
1827 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 5);
1828 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 4);
1829 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 3);
1830 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 2);
1831 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 1);
1832 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 0);
1833 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, -1);
1834 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, -2);
1835 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, -1);
1836 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 0);
1837 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 1);
1838 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 2);
1839 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 3);
1840 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 2);
1841 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 3);
1842 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, 2);
1843 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, 3);
1844 *pi32 = INT32_MAX;
1845 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicDecS32, INT32_MAX - 1);
1846 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, INT32_MAX);
1847 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicIncS32, INT32_MIN);
1848}
1849
1850
1851#if 0
1852DECLINLINE(void) tstASMAtomicUoDecIncS32Worker(int32_t volatile *pi32)
1853{
1854 *pi32 = 10;
1855 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 9);
1856 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 8);
1857 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 7);
1858 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 6);
1859 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 5);
1860 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 4);
1861 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 3);
1862 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 2);
1863 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 1);
1864 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 0);
1865 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, -1);
1866 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, -2);
1867 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, -1);
1868 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 0);
1869 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 1);
1870 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 2);
1871 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 3);
1872 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 2);
1873 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 3);
1874 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, 2);
1875 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, 3);
1876 *pi32 = INT32_MAX;
1877 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoDecS32, INT32_MAX - 1);
1878 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, INT32_MAX);
1879 TEST_RET_NEW_NV(int32_t, "%d", pi32, ASMAtomicUoIncS32, INT32_MIN);
1880}
1881#endif
1882
1883
1884DECLINLINE(void) tstASMAtomicDecIncU64Worker(uint64_t volatile *pu64)
1885{
1886 *pu64 = 3;
1887 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, 2);
1888 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, 1);
1889 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, 0);
1890 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, UINT64_MAX);
1891 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, UINT64_MAX - 1);
1892 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, UINT64_MAX - 2);
1893 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, UINT64_MAX - 1);
1894 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, UINT64_MAX);
1895 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, 0);
1896 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, 1);
1897 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, 2);
1898 *pu64 = _4G - 1;
1899 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, _4G - 2);
1900 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, _4G - 1);
1901 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, _4G);
1902 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicIncU64, _4G + 1);
1903 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicDecU64, _4G);
1904}
1905
1906
1907#if 0
1908DECLINLINE(void) tstASMAtomicUoDecIncU64Worker(uint64_t volatile *pu64)
1909{
1910 *pu64 = 3;
1911 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, 2);
1912 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, 1);
1913 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, 0);
1914 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, UINT64_MAX);
1915 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, UINT64_MAX - 1);
1916 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, UINT64_MAX - 2);
1917 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, UINT64_MAX - 1);
1918 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, UINT64_MAX);
1919 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, 0);
1920 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, 1);
1921 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, 2);
1922 *pu64 = _4G - 1;
1923 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, _4G - 2);
1924 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, _4G - 1);
1925 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, _4G);
1926 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoIncU64, _4G + 1);
1927 TEST_RET_NEW_NV(uint64_t, "%lld", pu64, ASMAtomicUoDecU64, _4G);
1928}
1929#endif
1930
1931
1932DECLINLINE(void) tstASMAtomicDecIncS64Worker(int64_t volatile *pi64)
1933{
1934 *pi64 = 10;
1935 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 9);
1936 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 8);
1937 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 7);
1938 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 6);
1939 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 5);
1940 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 4);
1941 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 3);
1942 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 2);
1943 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 1);
1944 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 0);
1945 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, -1);
1946 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, -2);
1947 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, -1);
1948 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 0);
1949 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 1);
1950 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 2);
1951 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 3);
1952 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 2);
1953 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 3);
1954 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, 2);
1955 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicIncS64, 3);
1956 *pi64 = INT64_MAX;
1957 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicDecS64, INT64_MAX - 1);
1958}
1959
1960
1961#if 0
1962DECLINLINE(void) tstASMAtomicUoDecIncS64Worker(int64_t volatile *pi64)
1963{
1964 *pi64 = 10;
1965 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 9);
1966 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 8);
1967 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 7);
1968 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 6);
1969 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 5);
1970 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 4);
1971 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 3);
1972 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 2);
1973 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 1);
1974 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 0);
1975 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, -1);
1976 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, -2);
1977 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, -1);
1978 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 0);
1979 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 1);
1980 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 2);
1981 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 3);
1982 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 2);
1983 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 3);
1984 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, 2);
1985 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoIncS64, 3);
1986 *pi64 = INT64_MAX;
1987 TEST_RET_NEW_NV(int64_t, "%lld", pi64, ASMAtomicUoDecS64, INT64_MAX - 1);
1988}
1989#endif
1990
1991
1992DECLINLINE(void) tstASMAtomicDecIncZWorker(size_t volatile *pcb)
1993{
1994 size_t const uBaseVal = ~(size_t)0 >> 7;
1995 *pcb = uBaseVal;
1996 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 1);
1997 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 2);
1998 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 3);
1999 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal - 2);
2000 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal - 1);
2001 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal);
2002 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal + 1);
2003 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal);
2004 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicDecZ, uBaseVal - 1);
2005 TEST_RET_NEW_NV(size_t, "%zx", pcb, ASMAtomicIncZ, uBaseVal);
2006}
2007
2008
2009static void tstASMAtomicDecInc(void)
2010{
2011 DO_SIMPLE_TEST(ASMAtomicDecIncU32, uint32_t);
2012 DO_SIMPLE_TEST(ASMAtomicUoDecIncU32, uint32_t);
2013 DO_SIMPLE_TEST(ASMAtomicDecIncS32, int32_t);
2014 //DO_SIMPLE_TEST(ASMAtomicUoDecIncS32, int32_t);
2015 DO_SIMPLE_TEST(ASMAtomicDecIncU64, uint64_t);
2016 //DO_SIMPLE_TEST(ASMAtomicUoDecIncU64, uint64_t);
2017 DO_SIMPLE_TEST(ASMAtomicDecIncS64, int64_t);
2018 //DO_SIMPLE_TEST(ASMAtomicUoDecIncS64, int64_t);
2019 DO_SIMPLE_TEST(ASMAtomicDecIncZ, size_t);
2020}
2021
2022
2023#define TEST_RET_VOID(a_Type, a_Fmt, a_pVar, a_Function, a_uVal, a_VarExpect) do { \
2024 a_Function(a_pVar, a_uVal); \
2025 if (RT_LIKELY( *(a_pVar) == (a_VarExpect) )) { } \
2026 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s, " a_Fmt ") -> %s=" a_Fmt ", expected " a_Fmt "\n", \
2027 __FUNCTION__, __LINE__, #a_Function, #a_pVar, a_uVal, #a_pVar, *(a_pVar), (a_VarExpect)); \
2028 } while (0)
2029
2030#define TEST_RET_NEW(a_Type, a_Fmt, a_pVar, a_Function, a_uVal, a_VarExpect) do { \
2031 a_Type uNewRet = a_Function(a_pVar, a_uVal); \
2032 if (RT_LIKELY( uNewRet == (a_VarExpect) && *(a_pVar) == (a_VarExpect) )) { } \
2033 else RTTestFailed(g_hTest, "%s, %d: FAILURE: %s(%s, " a_Fmt ") -> " a_Fmt " and %s=" a_Fmt ", expected both " a_Fmt "\n", \
2034 __FUNCTION__, __LINE__, #a_Function, #a_pVar, a_uVal, uNewRet, #a_pVar, *(a_pVar), (a_VarExpect)); \
2035 } while (0)
2036
2037
2038DECLINLINE(void) tstASMAtomicAndOrXorU32Worker(uint32_t volatile *pu32)
2039{
2040 *pu32 = UINT32_C(0xffffffff);
2041 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2042 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2043 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0x8f8f8f8f), UINT32_C(0x8f8f8f8f));
2044 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0x70707070), UINT32_C(0xffffffff));
2045 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(1), UINT32_C(1));
2046 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0x80000000), UINT32_C(0x80000001));
2047 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0x80000000), UINT32_C(0x80000000));
2048 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0), UINT32_C(0));
2049 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicOrU32, UINT32_C(0x42424242), UINT32_C(0x42424242));
2050 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicAndU32, UINT32_C(0x00ff0f00), UINT32_C(0x00420200));
2051 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicXorU32, UINT32_C(0x42004042), UINT32_C(0x42424242));
2052 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicXorU32, UINT32_C(0xff024200), UINT32_C(0xbd400042));
2053 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicXorU32, UINT32_C(0x00000000), UINT32_C(0xbd400042));
2054}
2055
2056
2057DECLINLINE(void) tstASMAtomicUoAndOrXorU32Worker(uint32_t volatile *pu32)
2058{
2059 *pu32 = UINT32_C(0xffffffff);
2060 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2061 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2062 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0x8f8f8f8f), UINT32_C(0x8f8f8f8f));
2063 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0x70707070), UINT32_C(0xffffffff));
2064 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(1), UINT32_C(1));
2065 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0x80000000), UINT32_C(0x80000001));
2066 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0x80000000), UINT32_C(0x80000000));
2067 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0), UINT32_C(0));
2068 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoOrU32, UINT32_C(0x42424242), UINT32_C(0x42424242));
2069 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoAndU32, UINT32_C(0x00ff0f00), UINT32_C(0x00420200));
2070 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoXorU32, UINT32_C(0x42004042), UINT32_C(0x42424242));
2071 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoXorU32, UINT32_C(0xff024200), UINT32_C(0xbd400042));
2072 TEST_RET_VOID(uint32_t, "%#x", pu32, ASMAtomicUoXorU32, UINT32_C(0x00000000), UINT32_C(0xbd400042));
2073}
2074
2075
2076DECLINLINE(void) tstASMAtomicAndOrXorExU32Worker(uint32_t volatile *pu32)
2077{
2078 *pu32 = UINT32_C(0xffffffff);
2079 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2080 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2081 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0x8f8f8f8f), UINT32_C(0x8f8f8f8f));
2082 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0x70707070), UINT32_C(0xffffffff));
2083 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(1), UINT32_C(1));
2084 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0x80000000), UINT32_C(0x80000001));
2085 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0x80000000), UINT32_C(0x80000000));
2086 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0), UINT32_C(0));
2087 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicOrExU32, UINT32_C(0x42424242), UINT32_C(0x42424242));
2088 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicAndExU32, UINT32_C(0x00ff0f00), UINT32_C(0x00420200));
2089 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicXorExU32, UINT32_C(0x42004042), UINT32_C(0x42424242));
2090 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicXorExU32, UINT32_C(0xff024200), UINT32_C(0xbd400042));
2091 TEST_RET_OLD(uint32_t, "%#x", pu32, ASMAtomicXorExU32, UINT32_C(0x00000000), UINT32_C(0xbd400042));
2092}
2093
2094
2095DECLINLINE(void) tstASMAtomicAndOrXorU64Worker(uint64_t volatile *pu64)
2096{
2097 *pu64 = UINT64_C(0xffffffff);
2098 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2099 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2100 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x8f8f8f8f), UINT64_C(0x8f8f8f8f));
2101 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x70707070), UINT64_C(0xffffffff));
2102 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(1), UINT64_C(1));
2103 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x80000000), UINT64_C(0x80000001));
2104 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x80000000), UINT64_C(0x80000000));
2105 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0), UINT64_C(0));
2106 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x42424242), UINT64_C(0x42424242));
2107 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x00ff0f00), UINT64_C(0x00420200));
2108 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x42004042), UINT64_C(0x42424242));
2109 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0xff024200), UINT64_C(0xbd400042));
2110 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x00000000), UINT64_C(0xbd400042));
2111
2112 /* full 64-bit */
2113 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
2114 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2115 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2116 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x8f8f8f8f8f8f8f8f), UINT64_C(0x8f8f8f8f8f8f8f8f));
2117 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x7070707070707070), UINT64_C(0xffffffffffffffff));
2118 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001));
2119 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000001));
2120 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000000));
2121 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0), UINT64_C(0));
2122 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicOrU64, UINT64_C(0x4242424242424242), UINT64_C(0x4242424242424242));
2123 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicAndU64, UINT64_C(0x00ff0f00ff0f0000), UINT64_C(0x0042020042020000));
2124 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x4200404242040000), UINT64_C(0x4242424242420000));
2125 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0xff02420000ff2127), UINT64_C(0xbd40004242bd2127));
2126 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicXorU64, UINT64_C(0x0000000000000000), UINT64_C(0xbd40004242bd2127));
2127}
2128
2129
2130DECLINLINE(void) tstASMAtomicUoAndOrXorU64Worker(uint64_t volatile *pu64)
2131{
2132 *pu64 = UINT64_C(0xffffffff);
2133 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2134 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2135 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x8f8f8f8f), UINT64_C(0x8f8f8f8f));
2136 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x70707070), UINT64_C(0xffffffff));
2137 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(1), UINT64_C(1));
2138 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x80000000), UINT64_C(0x80000001));
2139 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x80000000), UINT64_C(0x80000000));
2140 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0), UINT64_C(0));
2141 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x42424242), UINT64_C(0x42424242));
2142 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x00ff0f00), UINT64_C(0x00420200));
2143 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x42004042), UINT64_C(0x42424242));
2144 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0xff024200), UINT64_C(0xbd400042));
2145 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x00000000), UINT64_C(0xbd400042));
2146
2147 /* full 64-bit */
2148 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
2149 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2150 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2151 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x8f8f8f8f8f8f8f8f), UINT64_C(0x8f8f8f8f8f8f8f8f));
2152 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x7070707070707070), UINT64_C(0xffffffffffffffff));
2153 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001));
2154 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000001));
2155 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000000));
2156 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0), UINT64_C(0));
2157 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoOrU64, UINT64_C(0x4242424242424242), UINT64_C(0x4242424242424242));
2158 TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoAndU64, UINT64_C(0x00ff0f00ff0f0000), UINT64_C(0x0042020042020000));
2159 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x4200404242040000), UINT64_C(0x4242424242420000));
2160 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0xff02420000ff2127), UINT64_C(0xbd40004242bd2127));
2161 //TEST_RET_VOID(uint64_t, "%#llx", pu64, ASMAtomicUoXorU64, UINT64_C(0x0000000000000000), UINT64_C(0xbd40004242bd2127));
2162}
2163
2164
2165#if 0
2166DECLINLINE(void) tstASMAtomicAndOrXorExU64Worker(uint64_t volatile *pu64)
2167{
2168 *pu64 = UINT64_C(0xffffffff);
2169 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2170 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0xffffffff), UINT64_C(0xffffffff));
2171 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x8f8f8f8f), UINT64_C(0x8f8f8f8f));
2172 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x70707070), UINT64_C(0xffffffff));
2173 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(1), UINT64_C(1));
2174 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x80000000), UINT64_C(0x80000001));
2175 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x80000000), UINT64_C(0x80000000));
2176 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0), UINT64_C(0));
2177 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x42424242), UINT64_C(0x42424242));
2178 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x00ff0f00), UINT64_C(0x00420200));
2179 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x42004042), UINT64_C(0x42424242));
2180 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0xff024200), UINT64_C(0xbd400042));
2181 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x00000000), UINT64_C(0xbd400042));
2182
2183 /* full 64-bit */
2184 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000));
2185 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2186 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff));
2187 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x8f8f8f8f8f8f8f8f), UINT64_C(0x8f8f8f8f8f8f8f8f));
2188 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x7070707070707070), UINT64_C(0xffffffffffffffff));
2189 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001));
2190 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000001));
2191 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x8000000000000000), UINT64_C(0x8000000000000000));
2192 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0), UINT64_C(0));
2193 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicOrExU64, UINT64_C(0x4242424242424242), UINT64_C(0x4242424242424242));
2194 TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicAndExU64, UINT64_C(0x00ff0f00ff0f0000), UINT64_C(0x0042020042020000));
2195 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x4200404242040000), UINT64_C(0x4242424242420000));
2196 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0xff02420000ff2127), UINT64_C(0xbd40004242bd2127));
2197 //TEST_RET_OLD(uint64_t, "%#llx", pu64, ASMAtomicXorExU64, UINT64_C(0x0000000000000000), UINT64_C(0xbd40004242bd2127));
2198}
2199#endif
2200
2201
2202static void tstASMAtomicAndOrXor(void)
2203{
2204 DO_SIMPLE_TEST(ASMAtomicAndOrXorU32, uint32_t);
2205 DO_SIMPLE_TEST(ASMAtomicUoAndOrXorU32, uint32_t);
2206 DO_SIMPLE_TEST(ASMAtomicAndOrXorExU32, uint32_t);
2207 DO_SIMPLE_TEST(ASMAtomicAndOrXorU64, uint64_t);
2208 DO_SIMPLE_TEST(ASMAtomicUoAndOrXorU64, uint64_t);
2209 //DO_SIMPLE_TEST(ASMAtomicAndOrXorExU64, uint64_t);
2210}
2211
2212
2213typedef struct
2214{
2215 uint8_t ab[PAGE_SIZE];
2216} TSTPAGE;
2217
2218
2219DECLINLINE(void) tstASMMemZeroPageWorker(TSTPAGE *pPage)
2220{
2221 for (unsigned j = 0; j < 16; j++)
2222 {
2223 memset(pPage, 0x11 * j, sizeof(*pPage));
2224 ASMMemZeroPage(pPage);
2225 for (unsigned i = 0; i < sizeof(pPage->ab); i++)
2226 if (pPage->ab[i])
2227 RTTestFailed(g_hTest, "ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
2228 if (ASMMemIsZeroPage(pPage) != true)
2229 RTTestFailed(g_hTest, "ASMMemIsZeroPage returns false after ASMMemZeroPage!\n");
2230 if (ASMMemFirstMismatchingU32(pPage, sizeof(pPage), 0) != NULL)
2231 RTTestFailed(g_hTest, "ASMMemFirstMismatchingU32(,,0) returns non-NULL after ASMMemZeroPage!\n");
2232 }
2233}
2234
2235
2236static void tstASMMemZeroPage(void)
2237{
2238 RTTestISub("ASMMemZeroPage");
2239 DO_SIMPLE_TEST_NO_SUB_NO_STACK(tstASMMemZeroPageWorker, TSTPAGE);
2240}
2241
2242
2243void tstASMMemIsZeroPage(RTTEST hTest)
2244{
2245 RTTestSub(hTest, "ASMMemIsZeroPage");
2246
2247 void *pvPage1 = RTTestGuardedAllocHead(hTest, PAGE_SIZE);
2248 void *pvPage2 = RTTestGuardedAllocTail(hTest, PAGE_SIZE);
2249 RTTESTI_CHECK_RETV(pvPage1 && pvPage2);
2250
2251 memset(pvPage1, 0, PAGE_SIZE);
2252 memset(pvPage2, 0, PAGE_SIZE);
2253 RTTESTI_CHECK(ASMMemIsZeroPage(pvPage1));
2254 RTTESTI_CHECK(ASMMemIsZeroPage(pvPage2));
2255
2256 memset(pvPage1, 0xff, PAGE_SIZE);
2257 memset(pvPage2, 0xff, PAGE_SIZE);
2258 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
2259 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
2260
2261 memset(pvPage1, 0, PAGE_SIZE);
2262 memset(pvPage2, 0, PAGE_SIZE);
2263 for (unsigned off = 0; off < PAGE_SIZE; off++)
2264 {
2265 ((uint8_t *)pvPage1)[off] = 1;
2266 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
2267 ((uint8_t *)pvPage1)[off] = 0;
2268
2269 ((uint8_t *)pvPage2)[off] = 0x80;
2270 RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
2271 ((uint8_t *)pvPage2)[off] = 0;
2272 }
2273
2274 RTTestSubDone(hTest);
2275}
2276
2277
2278void tstASMMemFirstMismatchingU8(RTTEST hTest)
2279{
2280 RTTestSub(hTest, "ASMMemFirstMismatchingU8");
2281
2282 uint8_t *pbPage1 = (uint8_t *)RTTestGuardedAllocHead(hTest, PAGE_SIZE);
2283 uint8_t *pbPage2 = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
2284 RTTESTI_CHECK_RETV(pbPage1 && pbPage2);
2285
2286 memset(pbPage1, 0, PAGE_SIZE);
2287 memset(pbPage2, 0, PAGE_SIZE);
2288 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0) == NULL);
2289 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0) == NULL);
2290 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 1) == pbPage1);
2291 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 1) == pbPage2);
2292 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0x87) == pbPage1);
2293 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0x87) == pbPage2);
2294 RTTESTI_CHECK(ASMMemIsZero(pbPage1, PAGE_SIZE));
2295 RTTESTI_CHECK(ASMMemIsZero(pbPage2, PAGE_SIZE));
2296 RTTESTI_CHECK(ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0));
2297 RTTESTI_CHECK(ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0));
2298 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0x34));
2299 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0x88));
2300 unsigned cbSub = 32;
2301 while (cbSub-- > 0)
2302 {
2303 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0) == NULL);
2304 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0) == NULL);
2305 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0) == NULL);
2306 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0) == NULL);
2307
2308 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0x34) == &pbPage1[PAGE_SIZE - cbSub] || !cbSub);
2309 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0x99) == &pbPage2[PAGE_SIZE - cbSub] || !cbSub);
2310 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0x42) == pbPage1 || !cbSub);
2311 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0x88) == pbPage2 || !cbSub);
2312 }
2313
2314 memset(pbPage1, 0xff, PAGE_SIZE);
2315 memset(pbPage2, 0xff, PAGE_SIZE);
2316 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0xff) == NULL);
2317 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0xff) == NULL);
2318 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, PAGE_SIZE, 0xfe) == pbPage1);
2319 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, PAGE_SIZE, 0xfe) == pbPage2);
2320 RTTESTI_CHECK(!ASMMemIsZero(pbPage1, PAGE_SIZE));
2321 RTTESTI_CHECK(!ASMMemIsZero(pbPage2, PAGE_SIZE));
2322 RTTESTI_CHECK(ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0xff));
2323 RTTESTI_CHECK(ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0xff));
2324 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage1, PAGE_SIZE, 0));
2325 RTTESTI_CHECK(!ASMMemIsAllU8(pbPage2, PAGE_SIZE, 0));
2326 cbSub = 32;
2327 while (cbSub-- > 0)
2328 {
2329 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0xff) == NULL);
2330 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0xff) == NULL);
2331 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0xff) == NULL);
2332 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0xff) == NULL);
2333
2334 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage1[PAGE_SIZE - cbSub], cbSub, 0xfe) == &pbPage1[PAGE_SIZE - cbSub] || !cbSub);
2335 RTTESTI_CHECK(ASMMemFirstMismatchingU8(&pbPage2[PAGE_SIZE - cbSub], cbSub, 0xfe) == &pbPage2[PAGE_SIZE - cbSub] || !cbSub);
2336 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage1, cbSub, 0xfe) == pbPage1 || !cbSub);
2337 RTTESTI_CHECK(ASMMemFirstMismatchingU8(pbPage2, cbSub, 0xfe) == pbPage2 || !cbSub);
2338 }
2339
2340
2341 /*
2342 * Various alignments and sizes.
2343 */
2344 uint8_t const bFiller1 = 0x00;
2345 uint8_t const bFiller2 = 0xf6;
2346 size_t const cbBuf = 128;
2347 uint8_t *pbBuf1 = pbPage1;
2348 uint8_t *pbBuf2 = &pbPage2[PAGE_SIZE - cbBuf]; /* Put it up against the tail guard */
2349 memset(pbPage1, ~bFiller1, PAGE_SIZE);
2350 memset(pbPage2, ~bFiller2, PAGE_SIZE);
2351 memset(pbBuf1, bFiller1, cbBuf);
2352 memset(pbBuf2, bFiller2, cbBuf);
2353 for (size_t offNonZero = 0; offNonZero < cbBuf; offNonZero++)
2354 {
2355 uint8_t bRand = (uint8_t)RTRandU32();
2356 pbBuf1[offNonZero] = bRand | 1;
2357 pbBuf2[offNonZero] = (0x80 | bRand) ^ 0xf6;
2358
2359 for (size_t offStart = 0; offStart < 32; offStart++)
2360 {
2361 size_t const cbMax = cbBuf - offStart;
2362 for (size_t cb = 0; cb < cbMax; cb++)
2363 {
2364 size_t const offEnd = offStart + cb;
2365 uint8_t bSaved1, bSaved2;
2366 if (offEnd < PAGE_SIZE)
2367 {
2368 bSaved1 = pbBuf1[offEnd];
2369 bSaved2 = pbBuf2[offEnd];
2370 pbBuf1[offEnd] = 0xff;
2371 pbBuf2[offEnd] = 0xff;
2372 }
2373#ifdef _MSC_VER /* simple stupid compiler warnings */
2374 else
2375 bSaved1 = bSaved2 = 0;
2376#endif
2377
2378 uint8_t *pbRet = (uint8_t *)ASMMemFirstMismatchingU8(pbBuf1 + offStart, cb, bFiller1);
2379 RTTESTI_CHECK(offNonZero - offStart < cb ? pbRet == &pbBuf1[offNonZero] : pbRet == NULL);
2380
2381 pbRet = (uint8_t *)ASMMemFirstMismatchingU8(pbBuf2 + offStart, cb, bFiller2);
2382 RTTESTI_CHECK(offNonZero - offStart < cb ? pbRet == &pbBuf2[offNonZero] : pbRet == NULL);
2383
2384 if (offEnd < PAGE_SIZE)
2385 {
2386 pbBuf1[offEnd] = bSaved1;
2387 pbBuf2[offEnd] = bSaved2;
2388 }
2389 }
2390 }
2391
2392 pbBuf1[offNonZero] = 0;
2393 pbBuf2[offNonZero] = 0xf6;
2394 }
2395
2396 RTTestSubDone(hTest);
2397}
2398
2399
2400typedef struct TSTBUF32 { uint32_t au32[384]; } TSTBUF32;
2401
2402DECLINLINE(void) tstASMMemZero32Worker(TSTBUF32 *pBuf)
2403{
2404 ASMMemZero32(pBuf, sizeof(*pBuf));
2405 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2406 if (pBuf->au32[i])
2407 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear dword at index %#x!\n", i);
2408 if (ASMMemFirstNonZero(pBuf, sizeof(*pBuf)) != NULL)
2409 RTTestFailed(g_hTest, "ASMMemFirstNonZero return non-NULL after ASMMemZero32\n");
2410 if (!ASMMemIsZero(pBuf, sizeof(*pBuf)))
2411 RTTestFailed(g_hTest, "ASMMemIsZero return false after ASMMemZero32\n");
2412
2413 memset(pBuf, 0xfe, sizeof(*pBuf));
2414 ASMMemZero32(pBuf, sizeof(*pBuf));
2415 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2416 if (pBuf->au32[i])
2417 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear dword at index %#x!\n", i);
2418 if (ASMMemFirstNonZero(pBuf, sizeof(*pBuf)) != NULL)
2419 RTTestFailed(g_hTest, "ASMMemFirstNonZero return non-NULL after ASMMemZero32\n");
2420 if (!ASMMemIsZero(pBuf, sizeof(*pBuf)))
2421 RTTestFailed(g_hTest, "ASMMemIsZero return false after ASMMemZero32\n");
2422}
2423
2424
2425void tstASMMemZero32(void)
2426{
2427 RTTestSub(g_hTest, "ASMMemZero32");
2428
2429 struct
2430 {
2431 uint64_t u64Magic1;
2432 uint8_t abPage[PAGE_SIZE - 32];
2433 uint64_t u64Magic2;
2434 } Buf1, Buf2, Buf3;
2435
2436 Buf1.u64Magic1 = UINT64_C(0xffffffffffffffff);
2437 memset(Buf1.abPage, 0x55, sizeof(Buf1.abPage));
2438 Buf1.u64Magic2 = UINT64_C(0xffffffffffffffff);
2439 Buf2.u64Magic1 = UINT64_C(0xffffffffffffffff);
2440 memset(Buf2.abPage, 0x77, sizeof(Buf2.abPage));
2441 Buf2.u64Magic2 = UINT64_C(0xffffffffffffffff);
2442 Buf3.u64Magic1 = UINT64_C(0xffffffffffffffff);
2443 memset(Buf3.abPage, 0x99, sizeof(Buf3.abPage));
2444 Buf3.u64Magic2 = UINT64_C(0xffffffffffffffff);
2445 ASMMemZero32(Buf1.abPage, sizeof(Buf1.abPage));
2446 ASMMemZero32(Buf2.abPage, sizeof(Buf2.abPage));
2447 ASMMemZero32(Buf3.abPage, sizeof(Buf3.abPage));
2448 if ( Buf1.u64Magic1 != UINT64_C(0xffffffffffffffff)
2449 || Buf1.u64Magic2 != UINT64_C(0xffffffffffffffff)
2450 || Buf2.u64Magic1 != UINT64_C(0xffffffffffffffff)
2451 || Buf2.u64Magic2 != UINT64_C(0xffffffffffffffff)
2452 || Buf3.u64Magic1 != UINT64_C(0xffffffffffffffff)
2453 || Buf3.u64Magic2 != UINT64_C(0xffffffffffffffff))
2454 {
2455 RTTestFailed(g_hTest, "ASMMemZero32 violated one/both magic(s)!\n");
2456 }
2457 for (unsigned i = 0; i < RT_ELEMENTS(Buf1.abPage); i++)
2458 if (Buf1.abPage[i])
2459 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear byte at offset %#x!\n", i);
2460 for (unsigned i = 0; i < RT_ELEMENTS(Buf2.abPage); i++)
2461 if (Buf2.abPage[i])
2462 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear byte at offset %#x!\n", i);
2463 for (unsigned i = 0; i < RT_ELEMENTS(Buf3.abPage); i++)
2464 if (Buf3.abPage[i])
2465 RTTestFailed(g_hTest, "ASMMemZero32 didn't clear byte at offset %#x!\n", i);
2466
2467 DO_SIMPLE_TEST_NO_SUB(tstASMMemZero32Worker, TSTBUF32);
2468}
2469
2470
2471DECLINLINE(void) tstASMMemFill32Worker(TSTBUF32 *pBuf)
2472{
2473 ASMMemFill32(pBuf, sizeof(*pBuf), UINT32_C(0xf629bce1));
2474 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2475 if (pBuf->au32[i] != UINT32_C(0xf629bce1))
2476 RTTestFailed(g_hTest, "ASMMemFill32 didn't set dword at index %#x correctly!\n", i);
2477 if (ASMMemFirstMismatchingU32(pBuf, sizeof(*pBuf), UINT32_C(0xf629bce1)) != NULL)
2478 RTTestFailed(g_hTest, "ASMMemFirstMismatchingU32(,,UINT32_C(0xf629bce1)) returns non-NULL after ASMMemFill32!\n");
2479
2480 memset(pBuf, 0xfe, sizeof(*pBuf));
2481 ASMMemFill32(pBuf, sizeof(*pBuf), UINT32_C(0x12345678));
2482 for (unsigned i = 0; i < RT_ELEMENTS(pBuf->au32); i++)
2483 if (pBuf->au32[i] != UINT32_C(0x12345678))
2484 RTTestFailed(g_hTest, "ASMMemFill32 didn't set dword at index %#x correctly!\n", i);
2485 if (ASMMemFirstMismatchingU32(pBuf, sizeof(*pBuf), UINT32_C(0x12345678)) != NULL)
2486 RTTestFailed(g_hTest, "ASMMemFirstMismatchingU32(,,UINT32_C(0x12345678)) returns non-NULL after ASMMemFill32!\n");
2487}
2488
2489void tstASMMemFill32(void)
2490{
2491 RTTestSub(g_hTest, "ASMMemFill32");
2492
2493 struct
2494 {
2495 uint64_t u64Magic1;
2496 uint32_t au32Page[PAGE_SIZE / 4];
2497 uint64_t u64Magic2;
2498 } Buf1;
2499 struct
2500 {
2501 uint64_t u64Magic1;
2502 uint32_t au32Page[(PAGE_SIZE / 4) - 3];
2503 uint64_t u64Magic2;
2504 } Buf2;
2505 struct
2506 {
2507 uint64_t u64Magic1;
2508 uint32_t au32Page[(PAGE_SIZE / 4) - 1];
2509 uint64_t u64Magic2;
2510 } Buf3;
2511
2512 Buf1.u64Magic1 = UINT64_C(0xffffffffffffffff);
2513 memset(Buf1.au32Page, 0x55, sizeof(Buf1.au32Page));
2514 Buf1.u64Magic2 = UINT64_C(0xffffffffffffffff);
2515 Buf2.u64Magic1 = UINT64_C(0xffffffffffffffff);
2516 memset(Buf2.au32Page, 0x77, sizeof(Buf2.au32Page));
2517 Buf2.u64Magic2 = UINT64_C(0xffffffffffffffff);
2518 Buf3.u64Magic1 = UINT64_C(0xffffffffffffffff);
2519 memset(Buf3.au32Page, 0x99, sizeof(Buf3.au32Page));
2520 Buf3.u64Magic2 = UINT64_C(0xffffffffffffffff);
2521 ASMMemFill32(Buf1.au32Page, sizeof(Buf1.au32Page), 0xdeadbeef);
2522 ASMMemFill32(Buf2.au32Page, sizeof(Buf2.au32Page), 0xcafeff01);
2523 ASMMemFill32(Buf3.au32Page, sizeof(Buf3.au32Page), 0xf00dd00f);
2524 if ( Buf1.u64Magic1 != UINT64_C(0xffffffffffffffff)
2525 || Buf1.u64Magic2 != UINT64_C(0xffffffffffffffff)
2526 || Buf2.u64Magic1 != UINT64_C(0xffffffffffffffff)
2527 || Buf2.u64Magic2 != UINT64_C(0xffffffffffffffff)
2528 || Buf3.u64Magic1 != UINT64_C(0xffffffffffffffff)
2529 || Buf3.u64Magic2 != UINT64_C(0xffffffffffffffff))
2530 RTTestFailed(g_hTest, "ASMMemFill32 violated one/both magic(s)!\n");
2531 for (unsigned i = 0; i < RT_ELEMENTS(Buf1.au32Page); i++)
2532 if (Buf1.au32Page[i] != 0xdeadbeef)
2533 RTTestFailed(g_hTest, "ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf1.au32Page[i], 0xdeadbeef);
2534 for (unsigned i = 0; i < RT_ELEMENTS(Buf2.au32Page); i++)
2535 if (Buf2.au32Page[i] != 0xcafeff01)
2536 RTTestFailed(g_hTest, "ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf2.au32Page[i], 0xcafeff01);
2537 for (unsigned i = 0; i < RT_ELEMENTS(Buf3.au32Page); i++)
2538 if (Buf3.au32Page[i] != 0xf00dd00f)
2539 RTTestFailed(g_hTest, "ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf3.au32Page[i], 0xf00dd00f);
2540
2541 DO_SIMPLE_TEST_NO_SUB(tstASMMemFill32Worker, TSTBUF32);
2542}
2543
2544
2545void tstASMProbe(RTTEST hTest)
2546{
2547 RTTestSub(hTest, "ASMProbeReadByte/Buffer");
2548
2549 uint8_t b = 42;
2550 RTTESTI_CHECK(ASMProbeReadByte(&b) == 42);
2551 ASMProbeReadBuffer(&b, sizeof(b));
2552
2553 for (uint32_t cPages = 1; cPages < 16; cPages++)
2554 {
2555 uint8_t *pbBuf1 = (uint8_t *)RTTestGuardedAllocHead(hTest, cPages * PAGE_SIZE);
2556 uint8_t *pbBuf2 = (uint8_t *)RTTestGuardedAllocTail(hTest, cPages * PAGE_SIZE);
2557 RTTESTI_CHECK_RETV(pbBuf1 && pbBuf2);
2558
2559 memset(pbBuf1, 0xf6, cPages * PAGE_SIZE);
2560 memset(pbBuf2, 0x42, cPages * PAGE_SIZE);
2561
2562 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf1[cPages * PAGE_SIZE - 1]) == 0xf6);
2563 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf2[cPages * PAGE_SIZE - 1]) == 0x42);
2564 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf1[0]) == 0xf6);
2565 RTTESTI_CHECK(ASMProbeReadByte(&pbBuf2[0]) == 0x42);
2566
2567 ASMProbeReadBuffer(pbBuf1, cPages * PAGE_SIZE);
2568 ASMProbeReadBuffer(pbBuf2, cPages * PAGE_SIZE);
2569 }
2570}
2571
2572
2573void tstASMMisc(void)
2574{
2575 RTTestSub(g_hTest, "Misc");
2576 for (uint32_t i = 0; i < 20; i++)
2577 {
2578 ASMWriteFence();
2579 ASMCompilerBarrier();
2580 ASMReadFence();
2581 ASMNopPause();
2582 ASMSerializeInstruction();
2583 ASMMemoryFence();
2584 }
2585}
2586
2587void tstASMMath(void)
2588{
2589 RTTestSub(g_hTest, "Math");
2590
2591 uint64_t u64 = ASMMult2xU32RetU64(UINT32_C(0x80000000), UINT32_C(0x10000000));
2592 CHECKVAL(u64, UINT64_C(0x0800000000000000), "%#018RX64");
2593
2594 uint32_t u32 = ASMDivU64ByU32RetU32(UINT64_C(0x0800000000000000), UINT32_C(0x10000000));
2595 CHECKVAL(u32, UINT32_C(0x80000000), "%#010RX32");
2596
2597 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x00000001), UINT32_C(0x00000001), UINT32_C(0x00000001));
2598 CHECKVAL(u32, UINT32_C(0x00000001), "%#018RX32");
2599 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x10000000), UINT32_C(0x80000000), UINT32_C(0x20000000));
2600 CHECKVAL(u32, UINT32_C(0x40000000), "%#018RX32");
2601 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x76543210), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2602 CHECKVAL(u32, UINT32_C(0x76543210), "%#018RX32");
2603 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0xffffffff), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2604 CHECKVAL(u32, UINT32_C(0xffffffff), "%#018RX32");
2605 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0xffffffff), UINT32_C(0xfffffff0), UINT32_C(0xffffffff));
2606 CHECKVAL(u32, UINT32_C(0xfffffff0), "%#018RX32");
2607 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x10359583), UINT32_C(0x58734981), UINT32_C(0xf8694045));
2608 CHECKVAL(u32, UINT32_C(0x05c584ce), "%#018RX32");
2609 u32 = ASMMultU32ByU32DivByU32(UINT32_C(0x10359583), UINT32_C(0xf8694045), UINT32_C(0x58734981));
2610 CHECKVAL(u32, UINT32_C(0x2d860795), "%#018RX32");
2611
2612#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
2613 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x0000000000000001), UINT32_C(0x00000001), UINT32_C(0x00000001));
2614 CHECKVAL(u64, UINT64_C(0x0000000000000001), "%#018RX64");
2615 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x0000000100000000), UINT32_C(0x80000000), UINT32_C(0x00000002));
2616 CHECKVAL(u64, UINT64_C(0x4000000000000000), "%#018RX64");
2617 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xfedcba9876543210), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2618 CHECKVAL(u64, UINT64_C(0xfedcba9876543210), "%#018RX64");
2619 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xffffffffffffffff), UINT32_C(0xffffffff), UINT32_C(0xffffffff));
2620 CHECKVAL(u64, UINT64_C(0xffffffffffffffff), "%#018RX64");
2621 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xffffffffffffffff), UINT32_C(0xfffffff0), UINT32_C(0xffffffff));
2622 CHECKVAL(u64, UINT64_C(0xfffffff0fffffff0), "%#018RX64");
2623 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x3415934810359583), UINT32_C(0x58734981), UINT32_C(0xf8694045));
2624 CHECKVAL(u64, UINT64_C(0x128b9c3d43184763), "%#018RX64");
2625 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0x3415934810359583), UINT32_C(0xf8694045), UINT32_C(0x58734981));
2626 CHECKVAL(u64, UINT64_C(0x924719355cd35a27), "%#018RX64");
2627
2628# if 0 /* bird: question is whether this should trap or not:
2629 *
2630 * frank: Of course it must trap:
2631 *
2632 * 0xfffffff8 * 0x77d7daf8 = 0x77d7daf441412840
2633 *
2634 * During the following division, the quotient must fit into a 32-bit register.
2635 * Therefore the smallest valid divisor is
2636 *
2637 * (0x77d7daf441412840 >> 32) + 1 = 0x77d7daf5
2638 *
2639 * which is definitely greater than 0x3b9aca00.
2640 *
2641 * bird: No, the C version does *not* crash. So, the question is whether there's any
2642 * code depending on it not crashing.
2643 *
2644 * Of course the assembly versions of the code crash right now for the reasons you've
2645 * given, but the 32-bit MSC version does not crash.
2646 *
2647 * frank: The C version does not crash but delivers incorrect results for this case.
2648 * The reason is
2649 *
2650 * u.s.Hi = (unsigned long)(u64Hi / u32C);
2651 *
2652 * Here the division is actually 64-bit by 64-bit but the 64-bit result is truncated
2653 * to 32 bit. If using this (optimized and fast) function we should just be sure that
2654 * the operands are in a valid range.
2655 */
2656 u64 = ASMMultU64ByU32DivByU32(UINT64_C(0xfffffff8c65d6731), UINT32_C(0x77d7daf8), UINT32_C(0x3b9aca00));
2657 CHECKVAL(u64, UINT64_C(0x02b8f9a2aa74e3dc), "%#018RX64");
2658# endif
2659#endif /* AMD64 || X86 */
2660
2661 u32 = ASMModU64ByU32RetU32(UINT64_C(0x0ffffff8c65d6731), UINT32_C(0x77d7daf8));
2662 CHECKVAL(u32, UINT32_C(0x3B642451), "%#010RX32");
2663
2664 int32_t i32;
2665 i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(-2));
2666 CHECKVAL(i32, INT32_C(-1), "%010RI32");
2667 i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(2));
2668 CHECKVAL(i32, INT32_C(-1), "%010RI32");
2669 i32 = ASMModS64ByS32RetS32(INT64_C(11), INT32_C(-2));
2670 CHECKVAL(i32, INT32_C(1), "%010RI32");
2671
2672 i32 = ASMModS64ByS32RetS32(INT64_C(92233720368547758), INT32_C(2147483647));
2673 CHECKVAL(i32, INT32_C(2104533974), "%010RI32");
2674 i32 = ASMModS64ByS32RetS32(INT64_C(-92233720368547758), INT32_C(2147483647));
2675 CHECKVAL(i32, INT32_C(-2104533974), "%010RI32");
2676}
2677
2678
2679void tstASMByteSwap(void)
2680{
2681 RTTestSub(g_hTest, "ASMByteSwap*");
2682
2683 uint64_t u64In = UINT64_C(0x0011223344556677);
2684 uint64_t u64Out = ASMByteSwapU64(u64In);
2685 CHECKVAL(u64In, UINT64_C(0x0011223344556677), "%#018RX64");
2686 CHECKVAL(u64Out, UINT64_C(0x7766554433221100), "%#018RX64");
2687 u64Out = ASMByteSwapU64(u64Out);
2688 CHECKVAL(u64Out, u64In, "%#018RX64");
2689 u64In = UINT64_C(0x0123456789abcdef);
2690 u64Out = ASMByteSwapU64(u64In);
2691 CHECKVAL(u64In, UINT64_C(0x0123456789abcdef), "%#018RX64");
2692 CHECKVAL(u64Out, UINT64_C(0xefcdab8967452301), "%#018RX64");
2693 u64Out = ASMByteSwapU64(u64Out);
2694 CHECKVAL(u64Out, u64In, "%#018RX64");
2695 u64In = 0;
2696 u64Out = ASMByteSwapU64(u64In);
2697 CHECKVAL(u64Out, u64In, "%#018RX64");
2698 u64In = UINT64_MAX;
2699 u64Out = ASMByteSwapU64(u64In);
2700 CHECKVAL(u64Out, u64In, "%#018RX64");
2701
2702 uint32_t u32In = UINT32_C(0x00112233);
2703 uint32_t u32Out = ASMByteSwapU32(u32In);
2704 CHECKVAL(u32In, UINT32_C(0x00112233), "%#010RX32");
2705 CHECKVAL(u32Out, UINT32_C(0x33221100), "%#010RX32");
2706 u32Out = ASMByteSwapU32(u32Out);
2707 CHECKVAL(u32Out, u32In, "%#010RX32");
2708 u32In = UINT32_C(0x12345678);
2709 u32Out = ASMByteSwapU32(u32In);
2710 CHECKVAL(u32In, UINT32_C(0x12345678), "%#010RX32");
2711 CHECKVAL(u32Out, UINT32_C(0x78563412), "%#010RX32");
2712 u32Out = ASMByteSwapU32(u32Out);
2713 CHECKVAL(u32Out, u32In, "%#010RX32");
2714 u32In = 0;
2715 u32Out = ASMByteSwapU32(u32In);
2716 CHECKVAL(u32Out, u32In, "%#010RX32");
2717 u32In = UINT32_MAX;
2718 u32Out = ASMByteSwapU32(u32In);
2719 CHECKVAL(u32Out, u32In, "%#010RX32");
2720
2721 uint16_t u16In = UINT16_C(0x0011);
2722 uint16_t u16Out = ASMByteSwapU16(u16In);
2723 CHECKVAL(u16In, UINT16_C(0x0011), "%#06RX16");
2724 CHECKVAL(u16Out, UINT16_C(0x1100), "%#06RX16");
2725 u16Out = ASMByteSwapU16(u16Out);
2726 CHECKVAL(u16Out, u16In, "%#06RX16");
2727 u16In = UINT16_C(0x1234);
2728 u16Out = ASMByteSwapU16(u16In);
2729 CHECKVAL(u16In, UINT16_C(0x1234), "%#06RX16");
2730 CHECKVAL(u16Out, UINT16_C(0x3412), "%#06RX16");
2731 u16Out = ASMByteSwapU16(u16Out);
2732 CHECKVAL(u16Out, u16In, "%#06RX16");
2733 u16In = 0;
2734 u16Out = ASMByteSwapU16(u16In);
2735 CHECKVAL(u16Out, u16In, "%#06RX16");
2736 u16In = UINT16_MAX;
2737 u16Out = ASMByteSwapU16(u16In);
2738 CHECKVAL(u16Out, u16In, "%#06RX16");
2739}
2740
2741
2742void tstASMBench(void)
2743{
2744 /*
2745 * Make this static. We don't want to have this located on the stack.
2746 */
2747 static uint8_t volatile s_u8;
2748 static int8_t volatile s_i8;
2749 static uint16_t volatile s_u16;
2750 static int16_t volatile s_i16;
2751 static uint32_t volatile s_u32;
2752 static int32_t volatile s_i32;
2753 static uint64_t volatile s_u64;
2754 static int64_t volatile s_i64;
2755 unsigned i;
2756 const unsigned cRounds = _16M; /* Must be multiple of 8 */
2757 uint64_t u64Elapsed;
2758
2759 RTTestSub(g_hTest, "Benchmarking");
2760
2761#if 0 && !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) || defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32))
2762# define BENCH(op, str) \
2763 do { \
2764 RTThreadYield(); \
2765 u64Elapsed = ASMReadTSC(); \
2766 for (i = cRounds; i > 0; i--) \
2767 op; \
2768 u64Elapsed = ASMReadTSC() - u64Elapsed; \
2769 RTTestValue(g_hTest, str, u64Elapsed / cRounds, RTTESTUNIT_TICKS_PER_CALL); \
2770 } while (0)
2771#else
2772# define BENCH(op, str) \
2773 do { \
2774 RTThreadYield(); \
2775 u64Elapsed = RTTimeNanoTS(); \
2776 for (i = cRounds / 8; i > 0; i--) \
2777 { \
2778 op; \
2779 op; \
2780 op; \
2781 op; \
2782 op; \
2783 op; \
2784 op; \
2785 op; \
2786 } \
2787 u64Elapsed = RTTimeNanoTS() - u64Elapsed; \
2788 RTTestValue(g_hTest, str, u64Elapsed / cRounds, RTTESTUNIT_NS_PER_CALL); \
2789 } while (0)
2790#endif
2791#if (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) || defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)) && !defined(GCC44_32BIT_PIC)
2792# define BENCH_TSC(op, str) \
2793 do { \
2794 RTThreadYield(); \
2795 u64Elapsed = ASMReadTSC(); \
2796 for (i = cRounds / 8; i > 0; i--) \
2797 { \
2798 op; \
2799 op; \
2800 op; \
2801 op; \
2802 op; \
2803 op; \
2804 op; \
2805 op; \
2806 } \
2807 u64Elapsed = ASMReadTSC() - u64Elapsed; \
2808 RTTestValue(g_hTest, str, u64Elapsed / cRounds, /*RTTESTUNIT_TICKS_PER_CALL*/ RTTESTUNIT_NONE); \
2809 } while (0)
2810#else
2811# define BENCH_TSC(op, str) BENCH(op, str)
2812#endif
2813
2814 BENCH(s_u32 = 0, "s_u32 = 0");
2815 BENCH(ASMAtomicUoReadU8(&s_u8), "ASMAtomicUoReadU8");
2816 BENCH(ASMAtomicUoReadS8(&s_i8), "ASMAtomicUoReadS8");
2817 BENCH(ASMAtomicUoReadU16(&s_u16), "ASMAtomicUoReadU16");
2818 BENCH(ASMAtomicUoReadS16(&s_i16), "ASMAtomicUoReadS16");
2819 BENCH(ASMAtomicUoReadU32(&s_u32), "ASMAtomicUoReadU32");
2820 BENCH(ASMAtomicUoReadS32(&s_i32), "ASMAtomicUoReadS32");
2821 BENCH(ASMAtomicUoReadU64(&s_u64), "ASMAtomicUoReadU64");
2822 BENCH(ASMAtomicUoReadS64(&s_i64), "ASMAtomicUoReadS64");
2823 BENCH(ASMAtomicReadU8(&s_u8), "ASMAtomicReadU8");
2824 BENCH(ASMAtomicReadS8(&s_i8), "ASMAtomicReadS8");
2825 BENCH(ASMAtomicReadU16(&s_u16), "ASMAtomicReadU16");
2826 BENCH(ASMAtomicReadS16(&s_i16), "ASMAtomicReadS16");
2827 BENCH(ASMAtomicReadU32(&s_u32), "ASMAtomicReadU32");
2828 BENCH(ASMAtomicReadS32(&s_i32), "ASMAtomicReadS32");
2829 BENCH(ASMAtomicReadU64(&s_u64), "ASMAtomicReadU64");
2830 BENCH(ASMAtomicReadS64(&s_i64), "ASMAtomicReadS64");
2831 BENCH(ASMAtomicUoWriteU8(&s_u8, 0), "ASMAtomicUoWriteU8");
2832 BENCH(ASMAtomicUoWriteS8(&s_i8, 0), "ASMAtomicUoWriteS8");
2833 BENCH(ASMAtomicUoWriteU16(&s_u16, 0), "ASMAtomicUoWriteU16");
2834 BENCH(ASMAtomicUoWriteS16(&s_i16, 0), "ASMAtomicUoWriteS16");
2835 BENCH(ASMAtomicUoWriteU32(&s_u32, 0), "ASMAtomicUoWriteU32");
2836 BENCH(ASMAtomicUoWriteS32(&s_i32, 0), "ASMAtomicUoWriteS32");
2837 BENCH(ASMAtomicUoWriteU64(&s_u64, 0), "ASMAtomicUoWriteU64");
2838 BENCH(ASMAtomicUoWriteS64(&s_i64, 0), "ASMAtomicUoWriteS64");
2839 BENCH(ASMAtomicWriteU8(&s_u8, 0), "ASMAtomicWriteU8");
2840 BENCH(ASMAtomicWriteS8(&s_i8, 0), "ASMAtomicWriteS8");
2841 BENCH(ASMAtomicWriteU16(&s_u16, 0), "ASMAtomicWriteU16");
2842 BENCH(ASMAtomicWriteS16(&s_i16, 0), "ASMAtomicWriteS16");
2843 BENCH(ASMAtomicWriteU32(&s_u32, 0), "ASMAtomicWriteU32");
2844 BENCH(ASMAtomicWriteS32(&s_i32, 0), "ASMAtomicWriteS32");
2845 BENCH(ASMAtomicWriteU64(&s_u64, 0), "ASMAtomicWriteU64");
2846 BENCH(ASMAtomicWriteS64(&s_i64, 0), "ASMAtomicWriteS64");
2847 BENCH(ASMAtomicXchgU8(&s_u8, 0), "ASMAtomicXchgU8");
2848 BENCH(ASMAtomicXchgS8(&s_i8, 0), "ASMAtomicXchgS8");
2849 BENCH(ASMAtomicXchgU16(&s_u16, 0), "ASMAtomicXchgU16");
2850 BENCH(ASMAtomicXchgS16(&s_i16, 0), "ASMAtomicXchgS16");
2851 BENCH(ASMAtomicXchgU32(&s_u32, 0), "ASMAtomicXchgU32");
2852 BENCH(ASMAtomicXchgS32(&s_i32, 0), "ASMAtomicXchgS32");
2853 BENCH(ASMAtomicXchgU64(&s_u64, 0), "ASMAtomicXchgU64");
2854 BENCH(ASMAtomicXchgS64(&s_i64, 0), "ASMAtomicXchgS64");
2855 BENCH(ASMAtomicCmpXchgU32(&s_u32, 0, 0), "ASMAtomicCmpXchgU32");
2856 BENCH(ASMAtomicCmpXchgS32(&s_i32, 0, 0), "ASMAtomicCmpXchgS32");
2857 BENCH(ASMAtomicCmpXchgU64(&s_u64, 0, 0), "ASMAtomicCmpXchgU64");
2858 BENCH(ASMAtomicCmpXchgS64(&s_i64, 0, 0), "ASMAtomicCmpXchgS64");
2859 BENCH(ASMAtomicCmpXchgU32(&s_u32, 0, 1), "ASMAtomicCmpXchgU32/neg");
2860 BENCH(ASMAtomicCmpXchgS32(&s_i32, 0, 1), "ASMAtomicCmpXchgS32/neg");
2861 BENCH(ASMAtomicCmpXchgU64(&s_u64, 0, 1), "ASMAtomicCmpXchgU64/neg");
2862 BENCH(ASMAtomicCmpXchgS64(&s_i64, 0, 1), "ASMAtomicCmpXchgS64/neg");
2863 BENCH(ASMAtomicIncU32(&s_u32), "ASMAtomicIncU32");
2864 BENCH(ASMAtomicIncS32(&s_i32), "ASMAtomicIncS32");
2865 BENCH(ASMAtomicDecU32(&s_u32), "ASMAtomicDecU32");
2866 BENCH(ASMAtomicDecS32(&s_i32), "ASMAtomicDecS32");
2867 BENCH(ASMAtomicAddU32(&s_u32, 5), "ASMAtomicAddU32");
2868 BENCH(ASMAtomicAddS32(&s_i32, 5), "ASMAtomicAddS32");
2869 BENCH(ASMAtomicUoIncU32(&s_u32), "ASMAtomicUoIncU32");
2870 BENCH(ASMAtomicUoDecU32(&s_u32), "ASMAtomicUoDecU32");
2871 BENCH(ASMAtomicUoAndU32(&s_u32, 0xffffffff), "ASMAtomicUoAndU32");
2872 BENCH(ASMAtomicUoOrU32(&s_u32, 0xffffffff), "ASMAtomicUoOrU32");
2873#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
2874 BENCH_TSC(ASMSerializeInstructionCpuId(), "ASMSerializeInstructionCpuId");
2875 BENCH_TSC(ASMSerializeInstructionIRet(), "ASMSerializeInstructionIRet");
2876#endif
2877 BENCH(ASMReadFence(), "ASMReadFence");
2878 BENCH(ASMWriteFence(), "ASMWriteFence");
2879 BENCH(ASMMemoryFence(), "ASMMemoryFence");
2880 BENCH(ASMSerializeInstruction(), "ASMSerializeInstruction");
2881 BENCH(ASMNopPause(), "ASMNopPause");
2882
2883 /* The Darwin gcc does not like this ... */
2884#if !defined(RT_OS_DARWIN) && !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
2885 BENCH(s_u8 = ASMGetApicId(), "ASMGetApicId");
2886 BENCH(s_u32 = ASMGetApicIdExt0B(), "ASMGetApicIdExt0B");
2887 BENCH(s_u32 = ASMGetApicIdExt8000001E(), "ASMGetApicIdExt8000001E");
2888#endif
2889#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) || defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32))
2890 BENCH(s_u64 = ASMReadTSC(), "ASMReadTSC");
2891#endif
2892#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
2893 uint32_t uAux;
2894 if ( ASMHasCpuId()
2895 && ASMIsValidExtRange(ASMCpuId_EAX(0x80000000))
2896 && (ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_RDTSCP) )
2897 {
2898 BENCH_TSC(ASMSerializeInstructionRdTscp(), "ASMSerializeInstructionRdTscp");
2899 BENCH(s_u64 = ASMReadTscWithAux(&uAux), "ASMReadTscWithAux");
2900 }
2901 union
2902 {
2903 uint64_t u64[2];
2904 RTIDTR Unaligned;
2905 struct
2906 {
2907 uint16_t abPadding[3];
2908 RTIDTR Aligned;
2909 } s;
2910 } uBuf;
2911 Assert(((uintptr_t)&uBuf.Unaligned.pIdt & (sizeof(uintptr_t) - 1)) != 0);
2912 BENCH(ASMGetIDTR(&uBuf.Unaligned), "ASMGetIDTR/unaligned");
2913 Assert(((uintptr_t)&uBuf.s.Aligned.pIdt & (sizeof(uintptr_t) - 1)) == 0);
2914 BENCH(ASMGetIDTR(&uBuf.s.Aligned), "ASMGetIDTR/aligned");
2915#endif
2916
2917#undef BENCH
2918}
2919
2920
2921int main(int argc, char **argv)
2922{
2923 RT_NOREF_PV(argc); RT_NOREF_PV(argv);
2924
2925 int rc = RTTestInitAndCreate("tstRTInlineAsm", &g_hTest);
2926 if (rc)
2927 return rc;
2928 RTTestBanner(g_hTest);
2929
2930 /*
2931 * Execute the tests.
2932 */
2933#if !defined(GCC44_32BIT_PIC) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
2934 tstASMCpuId();
2935 //bruteForceCpuId();
2936#endif
2937#if 1
2938 tstASMAtomicRead();
2939 tstASMAtomicWrite();
2940 tstASMAtomicXchg();
2941 tstASMAtomicCmpXchg();
2942 tstASMAtomicCmpXchgEx();
2943
2944 tstASMAtomicAdd();
2945 tstASMAtomicDecInc();
2946 tstASMAtomicAndOrXor();
2947
2948 tstASMMemZeroPage();
2949 tstASMMemIsZeroPage(g_hTest);
2950 tstASMMemFirstMismatchingU8(g_hTest);
2951 tstASMMemZero32();
2952 tstASMMemFill32();
2953 tstASMProbe(g_hTest);
2954
2955 tstASMMisc();
2956
2957 tstASMMath();
2958
2959 tstASMByteSwap();
2960
2961 tstASMBench();
2962#endif
2963
2964 /*
2965 * Show the result.
2966 */
2967 return RTTestSummaryAndDestroy(g_hTest);
2968}
2969
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