summaryrefslogtreecommitdiff
path: root/Zend/zend_cpuinfo.c
blob: ce92460fb4d09419d9d14c908db2d0f985db05d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 2018-2018 Zend Technologies Ltd. (http://www.zend.com) |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.00 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.zend.com/license/2_00.txt.                                |
   | If you did not receive a copy of the Zend license and are unable to  |
   | obtain it through the world-wide-web, please send a note to          |
   | license@zend.com so we can mail you a copy immediately.              |
   +----------------------------------------------------------------------+
   | Authors: Xinchen Hui <xinchen.h@zend.com>                            |
   +----------------------------------------------------------------------+
*/

#include "zend.h"
#include "zend_cpuinfo.h"

typedef struct _zend_cpu_info {
	uint32_t eax;
	uint32_t ebx;
	uint32_t ecx;
	uint32_t edx;
	uint32_t initialized;
} zend_cpu_info;

static zend_cpu_info cpuinfo = {0};

#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
	__asm__ __volatile__ (
		"cpuid"
		: "=a"(cpuinfo.eax), "=b"(cpuinfo.ebx), "=c"(cpuinfo.ecx), "=d"(cpuinfo.edx)
		: "a"(func), "c"(subfunc)
	);
}
#elif defined(ZEND_WIN32)
# include <intrin.h>
static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
	int regs[4];

	__cpuidex(regs, func, subfunc);

	cpuinfo.eax = regs[0];
	cpuinfo.ebx = regs[1];
	cpuinfo.ecx = regs[2];
	cpuinfo.edx = regs[3];
}
#else
static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
	cpuinfo.eax = 0;
}
#endif

void zend_cpu_startup(void)
{
	if (!cpuinfo.initialized) {
		cpuinfo.initialized = 1;
		__zend_cpuid(0, 0);
		if (cpuinfo.eax == 0) {
			return;
		}
		__zend_cpuid(1, 0);
	}
}

ZEND_API int zend_cpu_supports(zend_cpu_feature feature) {
	if (feature & ZEND_CPU_EDX_MASK) {
		return (cpuinfo.edx & (feature & ~ZEND_CPU_EDX_MASK));
	} else {
		return (cpuinfo.ecx & feature);
	}
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * indent-tabs-mode: t
 * End:
 */