Skip to content

Commit 8b7ecfb

Browse files
committed
use _Py prefix to prevent public namespace pollution
1 parent d00da3e commit 8b7ecfb

9 files changed

Lines changed: 308 additions & 229 deletions

File tree

Include/internal/pycore_cpuinfo.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ extern "C" {
2727
#include "pycore_cpuinfo_cpuid_features.h"
2828
#include "pycore_cpuinfo_xsave_features.h"
2929

30-
typedef struct py_cpuid_features {
30+
typedef struct _Py_cpuid_features_s {
3131
uint32_t maxleaf;
3232
/*
33-
* Macro to declare a member flag of 'py_cpuid_features' as a uint8_t.
33+
* Macro to declare a member flag of '_Py_cpuid_features' as a uint8_t.
3434
* Whenever this macro is used, do not forget to update the number of
3535
* fields and the bitsize of the 'ready' member (see structure end).
3636
*/
@@ -116,7 +116,7 @@ typedef struct py_cpuid_features {
116116
// number of fields (40) and adjust the bitsize of 'ready'
117117
// so that the size of this structure is a multiple of 8.
118118
uint8_t ready; // set if the structure is ready for usage
119-
} py_cpuid_features;
119+
} _Py_cpuid_features;
120120

121121
/*
122122
* Explicitly initialize all members to zero to guarantee that
@@ -128,7 +128,7 @@ typedef struct py_cpuid_features {
128128
* Note: This function does not set any exception and thus never fails.
129129
*/
130130
PyAPI_FUNC(void)
131-
_Py_cpuid_disable_features(py_cpuid_features *flags);
131+
_Py_cpuid_disable_features(_Py_cpuid_features *flags);
132132

133133
/*
134134
* Check whether the structure is ready and flags are inter-compatible,
@@ -140,7 +140,7 @@ _Py_cpuid_disable_features(py_cpuid_features *flags);
140140
* Note: This function does not set any exception and thus never fails.
141141
*/
142142
PyAPI_FUNC(int)
143-
_Py_cpuid_check_features(const py_cpuid_features *flags);
143+
_Py_cpuid_check_features(const _Py_cpuid_features *flags);
144144

145145
/*
146146
* Return 1 if all expected flags are set in 'actual', 0 otherwise.
@@ -150,8 +150,8 @@ _Py_cpuid_check_features(const py_cpuid_features *flags);
150150
* Note: This function does not set any exception and thus never fails.
151151
*/
152152
PyAPI_FUNC(int)
153-
_Py_cpuid_has_features(const py_cpuid_features *actual,
154-
const py_cpuid_features *expect);
153+
_Py_cpuid_has_features(const _Py_cpuid_features *actual,
154+
const _Py_cpuid_features *expect);
155155

156156
/*
157157
* Return 1 if 'actual' and 'expect' are identical, 0 otherwise.
@@ -161,16 +161,16 @@ _Py_cpuid_has_features(const py_cpuid_features *actual,
161161
* Note: This function does not set any exception and thus never fails.
162162
*/
163163
PyAPI_FUNC(int)
164-
_Py_cpuid_match_features(const py_cpuid_features *actual,
165-
const py_cpuid_features *expect);
164+
_Py_cpuid_match_features(const _Py_cpuid_features *actual,
165+
const _Py_cpuid_features *expect);
166166

167167
/*
168-
* Detect the available features on this machine, storing the result in 'flags'.
168+
* Detect the available host features, storing the result in 'flags'.
169169
*
170170
* Note: This function does not set any exception and thus never fails.
171171
*/
172172
PyAPI_FUNC(void)
173-
_Py_cpuid_detect_features(py_cpuid_features *flags);
173+
_Py_cpuid_detect_features(_Py_cpuid_features *flags);
174174

175175
#ifdef __cplusplus
176176
}
Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
1+
/**
22
* @author Bénédikt Tran
3-
* @seealso Tools/cpuinfo/cpuid_features_gen.py
3+
* @seealso @file Tools/cpuinfo/cpuid_features_gen.py
44
*
55
* The enumeration describes masks to apply on CPUID output registers.
66
*
7-
* Member names are Py_CPUID_MASK_<REGISTER>_L<LEAF>[S<SUBLEAF>]_<FEATURE>,
7+
* Member names are _Py_CPUID_MASK_<REGISTER>_L<LEAF>[S<SUBLEAF>]_<FEATURE>,
88
* where <> (resp. []) denotes a required (resp. optional) group and:
99
*
1010
* - REGISTER is EAX, EBX, ECX or EDX,
@@ -35,68 +35,59 @@ extern "C" {
3535

3636
#include "Python.h"
3737

38-
// fmt: off
3938
/*[python input]
40-
import importlib
41-
import os
42-
import sys
43-
44-
ROOT = os.getcwd()
45-
TOOL = os.path.join(ROOT, "Tools/cpuinfo/cpuid_features_gen.py")
46-
TOOL = os.path.realpath(TOOL)
47-
48-
if not os.path.exists(TOOL):
49-
raise FileNotFoundError(TOOL)
50-
51-
sys.path.insert(0, os.path.dirname(os.path.dirname(TOOL)))
52-
module = importlib.import_module("cpuinfo.cpuid_features_gen")
53-
print(module.generate_cpuid_features_enum("py_cpuid_feature_mask"))
39+
import os, sys
40+
sys.path.insert(0, os.path.realpath(os.path.join(os.getcwd(), "Tools")))
41+
from cpuinfo.cpuid_features_gen import generate_cpuid_features_enum
42+
print(generate_cpuid_features_enum("_Py_cpuid_feature_mask"))
5443
[python start generated code]*/
55-
typedef enum py_cpuid_feature_mask {
44+
// fmt: off
45+
/** Enumeration for CPUID features */
46+
enum _Py_cpuid_feature_mask_e {
5647
/* CPUID (LEAF=1, SUBLEAF=0) [ECX] */
57-
Py_CPUID_MASK_ECX_L1_SSE3 = 0x00000001, // bit = 0
58-
Py_CPUID_MASK_ECX_L1_PCLMULQDQ = 0x00000002, // bit = 1
59-
Py_CPUID_MASK_ECX_L1_SSSE3 = 0x00000200, // bit = 9
60-
Py_CPUID_MASK_ECX_L1_FMA = 0x00001000, // bit = 12
61-
Py_CPUID_MASK_ECX_L1_SSE4_1 = 0x00080000, // bit = 19
62-
Py_CPUID_MASK_ECX_L1_SSE4_2 = 0x00100000, // bit = 20
63-
Py_CPUID_MASK_ECX_L1_POPCNT = 0x00800000, // bit = 23
64-
Py_CPUID_MASK_ECX_L1_XSAVE = 0x04000000, // bit = 26
65-
Py_CPUID_MASK_ECX_L1_OSXSAVE = 0x08000000, // bit = 27
66-
Py_CPUID_MASK_ECX_L1_AVX = 0x10000000, // bit = 28
48+
_Py_CPUID_MASK_ECX_L1_SSE3 = 0x00000001, // bit = 0
49+
_Py_CPUID_MASK_ECX_L1_PCLMULQDQ = 0x00000002, // bit = 1
50+
_Py_CPUID_MASK_ECX_L1_SSSE3 = 0x00000200, // bit = 9
51+
_Py_CPUID_MASK_ECX_L1_FMA = 0x00001000, // bit = 12
52+
_Py_CPUID_MASK_ECX_L1_SSE4_1 = 0x00080000, // bit = 19
53+
_Py_CPUID_MASK_ECX_L1_SSE4_2 = 0x00100000, // bit = 20
54+
_Py_CPUID_MASK_ECX_L1_POPCNT = 0x00800000, // bit = 23
55+
_Py_CPUID_MASK_ECX_L1_XSAVE = 0x04000000, // bit = 26
56+
_Py_CPUID_MASK_ECX_L1_OSXSAVE = 0x08000000, // bit = 27
57+
_Py_CPUID_MASK_ECX_L1_AVX = 0x10000000, // bit = 28
6758
/* CPUID (LEAF=1, SUBLEAF=0) [EDX] */
68-
Py_CPUID_MASK_EDX_L1_CMOV = 0x00008000, // bit = 15
69-
Py_CPUID_MASK_EDX_L1_SSE = 0x02000000, // bit = 25
70-
Py_CPUID_MASK_EDX_L1_SSE2 = 0x04000000, // bit = 26
59+
_Py_CPUID_MASK_EDX_L1_CMOV = 0x00008000, // bit = 15
60+
_Py_CPUID_MASK_EDX_L1_SSE = 0x02000000, // bit = 25
61+
_Py_CPUID_MASK_EDX_L1_SSE2 = 0x04000000, // bit = 26
7162
/* CPUID (LEAF=7, SUBLEAF=0) [EBX] */
72-
Py_CPUID_MASK_EBX_L7_AVX2 = 0x00000020, // bit = 5
73-
Py_CPUID_MASK_EBX_L7_AVX512_F = 0x00010000, // bit = 16
74-
Py_CPUID_MASK_EBX_L7_AVX512_DQ = 0x00020000, // bit = 17
75-
Py_CPUID_MASK_EBX_L7_AVX512_IFMA = 0x00200000, // bit = 21
76-
Py_CPUID_MASK_EBX_L7_AVX512_PF = 0x04000000, // bit = 26
77-
Py_CPUID_MASK_EBX_L7_AVX512_ER = 0x08000000, // bit = 27
78-
Py_CPUID_MASK_EBX_L7_AVX512_CD = 0x10000000, // bit = 28
79-
Py_CPUID_MASK_EBX_L7_AVX512_BW = 0x40000000, // bit = 30
80-
Py_CPUID_MASK_EBX_L7_AVX512_VL = 0x80000000, // bit = 31
63+
_Py_CPUID_MASK_EBX_L7_AVX2 = 0x00000020, // bit = 5
64+
_Py_CPUID_MASK_EBX_L7_AVX512_F = 0x00010000, // bit = 16
65+
_Py_CPUID_MASK_EBX_L7_AVX512_DQ = 0x00020000, // bit = 17
66+
_Py_CPUID_MASK_EBX_L7_AVX512_IFMA = 0x00200000, // bit = 21
67+
_Py_CPUID_MASK_EBX_L7_AVX512_PF = 0x04000000, // bit = 26
68+
_Py_CPUID_MASK_EBX_L7_AVX512_ER = 0x08000000, // bit = 27
69+
_Py_CPUID_MASK_EBX_L7_AVX512_CD = 0x10000000, // bit = 28
70+
_Py_CPUID_MASK_EBX_L7_AVX512_BW = 0x40000000, // bit = 30
71+
_Py_CPUID_MASK_EBX_L7_AVX512_VL = 0x80000000, // bit = 31
8172
/* CPUID (LEAF=7, SUBLEAF=0) [ECX] */
82-
Py_CPUID_MASK_ECX_L7_AVX512_VBMI = 0x00000002, // bit = 1
83-
Py_CPUID_MASK_ECX_L7_AVX512_VBMI2 = 0x00000040, // bit = 6
84-
Py_CPUID_MASK_ECX_L7_AVX512_VNNI = 0x00000800, // bit = 11
85-
Py_CPUID_MASK_ECX_L7_AVX512_BITALG = 0x00001000, // bit = 12
86-
Py_CPUID_MASK_ECX_L7_AVX512_VPOPCNTDQ = 0x00004000, // bit = 14
73+
_Py_CPUID_MASK_ECX_L7_AVX512_VBMI = 0x00000002, // bit = 1
74+
_Py_CPUID_MASK_ECX_L7_AVX512_VBMI2 = 0x00000040, // bit = 6
75+
_Py_CPUID_MASK_ECX_L7_AVX512_VNNI = 0x00000800, // bit = 11
76+
_Py_CPUID_MASK_ECX_L7_AVX512_BITALG = 0x00001000, // bit = 12
77+
_Py_CPUID_MASK_ECX_L7_AVX512_VPOPCNTDQ = 0x00004000, // bit = 14
8778
/* CPUID (LEAF=7, SUBLEAF=0) [EDX] */
88-
Py_CPUID_MASK_EDX_L7_AVX512_4VNNIW = 0x00000004, // bit = 2
89-
Py_CPUID_MASK_EDX_L7_AVX512_4FMAPS = 0x00000008, // bit = 3
90-
Py_CPUID_MASK_EDX_L7_AVX512_VP2INTERSECT = 0x00000100, // bit = 8
79+
_Py_CPUID_MASK_EDX_L7_AVX512_4VNNIW = 0x00000004, // bit = 2
80+
_Py_CPUID_MASK_EDX_L7_AVX512_4FMAPS = 0x00000008, // bit = 3
81+
_Py_CPUID_MASK_EDX_L7_AVX512_VP2INTERSECT = 0x00000100, // bit = 8
9182
/* CPUID (LEAF=7, SUBLEAF=1) [EAX] */
92-
Py_CPUID_MASK_EAX_L7S1_AVX_VNNI = 0x00000010, // bit = 4
93-
Py_CPUID_MASK_EAX_L7S1_AVX_IFMA = 0x00800000, // bit = 23
83+
_Py_CPUID_MASK_EAX_L7S1_AVX_VNNI = 0x00000010, // bit = 4
84+
_Py_CPUID_MASK_EAX_L7S1_AVX_IFMA = 0x00800000, // bit = 23
9485
/* CPUID (LEAF=7, SUBLEAF=1) [EDX] */
95-
Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT8 = 0x00000010, // bit = 4
96-
Py_CPUID_MASK_EDX_L7S1_AVX_NE_CONVERT = 0x00000020, // bit = 5
97-
Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT16 = 0x00000400, // bit = 10
98-
} py_cpuid_feature_mask;
99-
/*[python end generated code: output=c4460242e465fa91 input=61d2b5f1bc368b94]*/
86+
_Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT8 = 0x00000010, // bit = 4
87+
_Py_CPUID_MASK_EDX_L7S1_AVX_NE_CONVERT = 0x00000020, // bit = 5
88+
_Py_CPUID_MASK_EDX_L7S1_AVX_VNNI_INT16 = 0x00000400, // bit = 10
89+
};
10090
// fmt: on
91+
/*[python end generated code: output=8e58b0997d69bbf8 input=fce00935f64021f9]*/
10192

10293
#endif // !Py_INTERNAL_CPUINFO_CPUID_FEATURES_H
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/*
1+
/**
22
* @author Bénédikt Tran
3-
* @seealso Tools/cpuinfo/xsave_features_gen.py
3+
* @seealso @file Tools/cpuinfo/xsave_features_gen.py
44
*
55
* XSAVE state components (XCR0 control register).
66
*
77
* See https://en.wikipedia.org/wiki/Control_register#XCR0_and_XSS.
88
*/
9+
910
#ifndef Py_INTERNAL_CPUINFO_XSAVE_FEATURES_H
1011
#define Py_INTERNAL_CPUINFO_XSAVE_FEATURES_H
1112

@@ -21,29 +22,21 @@ extern "C" {
2122

2223
// fmt: off
2324
/*[python input]
24-
import importlib
25-
import os
26-
import sys
27-
28-
ROOT = os.getcwd()
29-
TOOL = os.path.join(ROOT, "Tools/cpuinfo/xsave_features_gen.py")
30-
TOOL = os.path.realpath(TOOL)
31-
32-
if not os.path.exists(TOOL):
33-
raise FileNotFoundError(TOOL)
34-
35-
sys.path.insert(0, os.path.dirname(os.path.dirname(TOOL)))
36-
module = importlib.import_module("cpuinfo.xsave_features_gen")
37-
print(module.generate_xsave_features_enum("py_xsave_feature_mask"))
25+
import os, sys
26+
sys.path.insert(0, os.path.realpath(os.path.join(os.getcwd(), "Tools")))
27+
from cpuinfo.xsave_features_gen import generate_xsave_features_enum
28+
print(generate_xsave_features_enum("_Py_xsave_feature_mask"))
3829
[python start generated code]*/
39-
typedef enum py_xsave_feature_mask {
40-
Py_XSAVE_MASK_XCR0_SSE = 0x00000002, // bit = 1
41-
Py_XSAVE_MASK_XCR0_AVX = 0x00000004, // bit = 2
42-
Py_XSAVE_MASK_XCR0_AVX512_OPMASK = 0x00000020, // bit = 5
43-
Py_XSAVE_MASK_XCR0_AVX512_ZMM_HI256 = 0x00000040, // bit = 6
44-
Py_XSAVE_MASK_XCR0_AVX512_HI16_ZMM = 0x00000080, // bit = 7
45-
} py_xsave_feature_mask;
46-
/*[python end generated code: output=9a476ed0abbc617b input=41f35058299c0118]*/
30+
// fmt: off
31+
/** Enumeration for XSAVE components */
32+
enum _Py_xsave_feature_mask_e {
33+
_Py_XSAVE_MASK_XCR0_SSE = 0x00000002, // bit = 1
34+
_Py_XSAVE_MASK_XCR0_AVX = 0x00000004, // bit = 2
35+
_Py_XSAVE_MASK_XCR0_AVX512_OPMASK = 0x00000020, // bit = 5
36+
_Py_XSAVE_MASK_XCR0_AVX512_ZMM_HI256 = 0x00000040, // bit = 6
37+
_Py_XSAVE_MASK_XCR0_AVX512_HI16_ZMM = 0x00000080, // bit = 7
38+
};
4739
// fmt: on
40+
/*[python end generated code: output=35ea9a165938f8ef input=336793a305515376]*/
4841

4942
#endif // !Py_INTERNAL_CPUINFO_XSAVE_FEATURES_H

Modules/blake2module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "Python.h"
1818
#include "hashlib.h"
19-
#include "pycore_cpuinfo.h" // py_cpuid_features
19+
#include "pycore_cpuinfo.h" // _Py_cpuid_features
2020
#include "pycore_strhex.h" // _Py_strhex()
2121
#include "pycore_typeobject.h"
2222
#include "pycore_moduleobject.h"
@@ -111,7 +111,7 @@ _blake2_free(void *module)
111111
static void
112112
blake2module_init_cpu_features(Blake2State *state)
113113
{
114-
py_cpuid_features flags;
114+
_Py_cpuid_features flags;
115115
_Py_cpuid_detect_features(&flags);
116116
#if _Py_HACL_CAN_COMPILE_VEC128
117117
state->can_run_simd128 = flags.sse && flags.sse2 && flags.sse3

Modules/hmacmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#endif
1818

1919
#include "Python.h"
20-
#include "pycore_cpuinfo.h" // py_cpuid_features
20+
#include "pycore_cpuinfo.h" // _Py_cpuid_features
2121
#include "pycore_hashtable.h"
2222
#include "pycore_strhex.h" // _Py_strhex()
2323

@@ -1553,7 +1553,7 @@ hmacmodule_init_globals(PyObject *module, hmacmodule_state *state)
15531553
static void
15541554
hmacmodule_init_cpu_features(hmacmodule_state *state)
15551555
{
1556-
py_cpuid_features flags;
1556+
_Py_cpuid_features flags;
15571557
_Py_cpuid_detect_features(&flags);
15581558
#if _Py_HACL_CAN_COMPILE_VEC128
15591559
state->can_run_simd128 = flags.sse && flags.sse2 && flags.sse3

0 commit comments

Comments
 (0)