1 /* Copyright (C) 2010 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * CPU and memory detection.
25  */
26 
27 #include "precompiled.h"
28 #include "lib/sysdep/cpu.h"
29 
30 static const StatusDefinition cpuStatusDefinitions[] = {
31 	{ ERR::CPU_FEATURE_MISSING, L"This CPU doesn't support a required feature" },
32 	{ ERR::CPU_UNKNOWN_OPCODE, L"Disassembly failed" },
33 	{ ERR::CPU_UNKNOWN_VENDOR, L"CPU vendor unknown" }
34 };
35 STATUS_ADD_DEFINITIONS(cpuStatusDefinitions);
36 
37 
38 // ensure the actual pointer size matches expectations on the most common
39 // architectures (IA-32 and AMD64) - just in case the predefined macros
40 // are wrong or misleading.
41 #if ARCH_IA32
42 cassert(sizeof(void*) == 4);
43 #elif ARCH_AMD64
44 cassert(sizeof(void*) == 8);
45 cassert(sizeof(i64) == sizeof(intptr_t));
46 #endif
47 cassert(sizeof(void*) == sizeof(intptr_t));
48 
49 
50 
TestCAS64()51 static void TestCAS64()
52 {
53 	volatile i64 var = 1;
54 	cpu_CAS64(&var, 1ull, 2ull);
55 	ENSURE(var == 2ull);
56 }
57 
TestAtomicAdd()58 static void TestAtomicAdd()
59 {
60 	volatile intptr_t i1 = 1;
61 	intptr_t prev = cpu_AtomicAdd(&i1, 1);
62 	ENSURE(prev == 1);
63 	ENSURE(i1 == 2);
64 }
65 
cpu_Test()66 void cpu_Test()
67 {
68 	TestCAS64();
69 	TestAtomicAdd();
70 }
71