VirtualBox

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

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

iprt/asm.h: Added ASMAtomicCmpXchgU128 and friends for AMD64 and ARM64, implemented ASMAtomicCmpWriteU128 et al for ARM64. bugref:9898

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

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