PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_cpu_x86.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_cpu_x86.c
4 * Runtime CPU feature detection for x86
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/port/pg_cpu_x86.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "c.h"
17
18#if defined(USE_SSE2) || defined(__i386__)
19
20#if defined(HAVE__GET_CPUID) || defined(HAVE__GET_CPUID_COUNT)
21#include <cpuid.h>
22#endif
23
24#if defined(HAVE__CPUID) || defined(HAVE__CPUIDEX)
25#include <intrin.h>
26#endif
27
28#ifdef HAVE_XSAVE_INTRINSICS
29#include <immintrin.h>
30#endif
31
32#include "port/pg_cpu.h"
33
34
35/* array indexed by enum X86FeatureId */
37
38/*
39 * Does XGETBV say the ZMM registers are enabled?
40 *
41 * NB: Caller is responsible for verifying that osxsave is available
42 * before calling this.
43 */
44#ifdef HAVE_XSAVE_INTRINSICS
46#endif
47static bool
49{
50#ifdef HAVE_XSAVE_INTRINSICS
51 return (_xgetbv(0) & 0xe6) == 0xe6;
52#else
53 return false;
54#endif
55}
56
57/*
58 * Parse the CPU ID info for runtime checks.
59 */
60void
62{
63 unsigned int exx[4] = {0, 0, 0, 0};
64
65#if defined(HAVE__GET_CPUID)
66 __get_cpuid(1, &exx[0], &exx[1], &exx[2], &exx[3]);
67#elif defined(HAVE__CPUID)
68 __cpuid(exx, 1);
69#else
70#error cpuid instruction not available
71#endif
72
73 X86Features[PG_SSE4_2] = exx[2] >> 20 & 1;
74 X86Features[PG_POPCNT] = exx[2] >> 23 & 1;
75
76 /* All these features depend on OSXSAVE */
77 if (exx[2] & (1 << 27))
78 {
79 /* second cpuid call on leaf 7 to check extended AVX-512 support */
80
81 memset(exx, 0, 4 * sizeof(exx[0]));
82
83#if defined(HAVE__GET_CPUID_COUNT)
84 __get_cpuid_count(7, 0, &exx[0], &exx[1], &exx[2], &exx[3]);
85#elif defined(HAVE__CPUIDEX)
86 __cpuidex(exx, 7, 0);
87#endif
88
90 {
91 X86Features[PG_AVX512_BW] = exx[1] >> 30 & 1;
92 X86Features[PG_AVX512_VL] = exx[1] >> 31 & 1;
93
94 X86Features[PG_AVX512_VPCLMULQDQ] = exx[2] >> 10 & 1;
95 X86Features[PG_AVX512_VPOPCNTDQ] = exx[2] >> 14 & 1;
96 }
97 }
98
100}
101
102#endif /* defined(USE_SSE2) || defined(__i386__) */
#define pg_attribute_target(...)
Definition c.h:224
static int fb(int x)