Skip to content

Commit 89596c6

Browse files
use 16 byte alignment
1 parent 59bcd37 commit 89596c6

4 files changed

Lines changed: 28 additions & 28 deletions

File tree

‎numpy/_core/include/numpy/ndarraytypes.h‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ typedef struct _PyArray_Descr_fields {
632632
* be two type_numbers with the same type
633633
* object.
634634
*/
635-
NPY_DECL_ALIGNED(8) PyTypeObject *typeobj;
635+
NPY_DECL_ALIGNED(16) PyTypeObject *typeobj;
636636
/* kind for this type */
637637
char kind;
638638
/* unique-character representing this type */
@@ -664,7 +664,7 @@ typedef struct _PyArray_Descr_fields {
664664

665665
typedef struct _PyArray_Descr {
666666
PyObject_HEAD
667-
NPY_DECL_ALIGNED(8) PyTypeObject *typeobj;
667+
NPY_DECL_ALIGNED(16) PyTypeObject *typeobj;
668668
char kind;
669669
char type;
670670
char byteorder;
@@ -675,7 +675,7 @@ typedef struct _PyArray_Descr {
675675
/* To access modified fields, define the full 2.0 struct: */
676676
typedef struct {
677677
PyObject_HEAD
678-
NPY_DECL_ALIGNED(8) PyTypeObject *typeobj;
678+
NPY_DECL_ALIGNED(16) PyTypeObject *typeobj;
679679
char kind;
680680
char type;
681681
char byteorder;
@@ -702,7 +702,7 @@ typedef struct {
702702
#ifndef Py_TARGET_ABI3T
703703
PyObject_HEAD
704704
#endif
705-
NPY_DECL_ALIGNED(8) PyTypeObject *typeobj;
705+
NPY_DECL_ALIGNED(16) PyTypeObject *typeobj;
706706
char kind;
707707
char type;
708708
char byteorder;
@@ -797,7 +797,7 @@ typedef struct tagPyArrayObject_fields {
797797
PyObject_HEAD
798798
#endif
799799
/* Pointer to the raw data buffer */
800-
NPY_DECL_ALIGNED(8) char *data;
800+
NPY_DECL_ALIGNED(16) char *data;
801801
/* The number of dimensions, also called 'ndim' */
802802
int nd;
803803
/* The size in each dimension, also called 'shape' */
@@ -1224,7 +1224,7 @@ typedef struct PyArrayIterObject_fields {
12241224
#ifndef Py_TARGET_ABI3T
12251225
PyObject_HEAD
12261226
#endif
1227-
NPY_DECL_ALIGNED(8) int nd_m1; /* number of dimensions - 1 */
1227+
NPY_DECL_ALIGNED(16) int nd_m1; /* number of dimensions - 1 */
12281228
npy_intp index, size;
12291229
npy_intp coordinates[NPY_MAXDIMS_LEGACY_ITERS];/* N-dimensional loop */
12301230
npy_intp dims_m1[NPY_MAXDIMS_LEGACY_ITERS]; /* ao->dimensions - 1 */
@@ -1250,7 +1250,7 @@ typedef struct {
12501250
#ifndef Py_TARGET_ABI3T
12511251
PyObject_HEAD
12521252
#endif
1253-
NPY_DECL_ALIGNED(8) int numiter; /* number of iters */
1253+
NPY_DECL_ALIGNED(16) int numiter; /* number of iters */
12541254
npy_intp size; /* broadcasted size */
12551255
npy_intp index; /* current index */
12561256
int nd; /* number of dims */
@@ -1297,7 +1297,7 @@ typedef struct {
12971297
/*
12981298
* PyArrayIterObject part: keep this in this exact order
12991299
*/
1300-
NPY_DECL_ALIGNED(8) int nd_m1; /* number of dimensions - 1 */
1300+
NPY_DECL_ALIGNED(16) int nd_m1; /* number of dimensions - 1 */
13011301
npy_intp index, size;
13021302
npy_intp coordinates[NPY_MAXDIMS_LEGACY_ITERS];/* N-dimensional loop */
13031303
npy_intp dims_m1[NPY_MAXDIMS_LEGACY_ITERS]; /* ao->dimensions - 1 */

‎numpy/_core/include/numpy/ufuncobject.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ typedef struct _tagPyUFuncObject {
109109
* nout: Number of outputs
110110
* nargs: Always nin + nout (Why is it stored?)
111111
*/
112-
NPY_DECL_ALIGNED(8) int nin;
112+
NPY_DECL_ALIGNED(16) int nin;
113113
int nout, nargs;
114114

115115
/*

‎numpy/_core/src/multiarray/arrayobject.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,30 +1283,30 @@ NPY_NO_EXPORT PyTypeObject PyArray_Type = {
12831283
but in our case it is not a problem in practice because this is used only in stable ABI
12841284
extensions where the original object layout is opaque.
12851285
1286-
The first member of the structs are aligned to 8 bytes, because on 32 bit free-threaded builds,
1287-
sizeof(PyObject) is not a multiple of 8 so the compiler will add padding to the struct and if we
1288-
don't align the first member to 8 bytes, the offsets of fields structs i.e. `PyArray_Descr_fields`
1289-
will differ from `PyArray_Descr` which is the actual layout. The alignment of 8 bytes is important
1290-
because older abi3 extensions have sizeof(PyObject) which a multiple of 8 so the added alignment of 8
1286+
The first member of the structs are aligned to 16 bytes, because on 32 bit free-threaded builds,
1287+
sizeof(PyObject) is not a multiple of 16 so the compiler will add padding to the struct and if we
1288+
don't align the first member to 16 bytes, the offsets of fields structs i.e. `PyArray_Descr_fields`
1289+
will differ from `PyArray_Descr` which is the actual layout. The alignment of 16 bytes is important
1290+
because older abi3 extensions have sizeof(PyObject) which a multiple of 16 so the added alignment of 16
12911291
does not change the offsets of the fields structs for those extensions.
12921292
12931293
To calculate the correct offset for the fields, we use offsetof with the first member
12941294
of the struct because there can be padding before the first member of the struct and the object header,
12951295
and we want to skip that padding.
12961296
*/
12971297

1298-
static_assert(NPY_ALIGNOF(PyArray_Descr_fields) <= 8,
1299-
"PyArray_Descr must not require more than 8-byte alignment");
1300-
static_assert(NPY_ALIGNOF(_PyArray_LegacyDescr_fields) <= 8,
1301-
"_PyArray_LegacyDescr must not require more than 8-byte alignment");
1302-
static_assert(NPY_ALIGNOF(PyArrayObject_fields) <= 8,
1303-
"PyArrayObject must not require more than 8-byte alignment");
1304-
static_assert(NPY_ALIGNOF(PyArrayMultiIterObject_fields) <= 8,
1305-
"PyArrayMultiIterObject must not require more than 8-byte alignment");
1306-
static_assert(NPY_ALIGNOF(PyArrayIterObject_fields) <= 8,
1307-
"PyArrayIterObject must not require more than 8-byte alignment");
1308-
static_assert(NPY_ALIGNOF(PyArrayNeighborhoodIterObject_fields) <= 8,
1309-
"PyArrayNeighborhoodIterObject must not require more than 8-byte alignment");
1298+
static_assert(NPY_ALIGNOF(PyArray_Descr_fields) <= 16,
1299+
"PyArray_Descr must not require more than 16-byte alignment");
1300+
static_assert(NPY_ALIGNOF(_PyArray_LegacyDescr_fields) <= 16,
1301+
"_PyArray_LegacyDescr must not require more than 16-byte alignment");
1302+
static_assert(NPY_ALIGNOF(PyArrayObject_fields) <= 16,
1303+
"PyArrayObject must not require more than 16-byte alignment");
1304+
static_assert(NPY_ALIGNOF(PyArrayMultiIterObject_fields) <= 16,
1305+
"PyArrayMultiIterObject must not require more than 16-byte alignment");
1306+
static_assert(NPY_ALIGNOF(PyArrayIterObject_fields) <= 16,
1307+
"PyArrayIterObject must not require more than 16-byte alignment");
1308+
static_assert(NPY_ALIGNOF(PyArrayNeighborhoodIterObject_fields) <= 16,
1309+
"PyArrayNeighborhoodIterObject must not require more than 16-byte alignment");
13101310
#undef _PyDataType_GET_ITEM_DATA
13111311
/*NUMPY_API*/
13121312
NPY_NO_EXPORT PyArray_Descr_fields *

‎numpy/_core/src/umath/ufunc_object.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5489,8 +5489,8 @@ PyUFunc_RegisterLoopForType(PyUFuncObject *ufunc,
54895489
#undef _SETCPTR
54905490

54915491
#undef _PyUFuncObject_GET_ITEM_DATA
5492-
static_assert(NPY_ALIGNOF(PyUFuncObject_fields) <= 8,
5493-
"PyUFuncObject must not require more than 8-byte alignment");
5492+
static_assert(NPY_ALIGNOF(PyUFuncObject_fields) <= 16,
5493+
"PyUFuncObject must not require more than 16-byte alignment");
54945494
/*UFUNC_API*/
54955495
NPY_NO_EXPORT PyUFuncObject_fields *
54965496
_PyUFuncObject_GET_ITEM_DATA(const PyUFuncObject *obj)

0 commit comments

Comments
 (0)