8000 ENH: properly fix unaligned load of complex types · numpy/numpy@c9bf9b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9bf9b0

Browse files
committed
ENH: properly fix unaligned load of complex types
the workaround of setting the aligned to false unconditionally makes copying strided complex data extremely slow as it will always do unaligned elementwise memmoves. Instead set the alignment requirement for complex types correct in the dtype to begin with. Note that on 32 bit gcc complex double will still be aligned to 8 bytes unless compiled with -malign-double. It is possible this will introduce new segfaults on architectures without unaligned loads, but this now indicates a missing alignment check in the affected code as the array description is correct.
1 parent 75cdf3d commit c9bf9b0

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3817,9 +3817,9 @@ NPY_NO_EXPORT PyArray_Descr @from@_Descr = {
38173817
/* type_num */
38183818
NPY_@from@,
38193819
/* elsize */
3820-
@num@*sizeof(@fromtype@),
3820+
@num@ * sizeof(@fromtype@),
38213821
/* alignment */
3822-
_ALIGN(@fromtype@),
3822+
@num@ * _ALIGN(@fromtype@),
38233823
/* subarray */
38243824
NULL,
38253825
/* fields */
@@ -4150,6 +4150,7 @@ set_typeinfo(PyObject *dict)
41504150
* CFLOAT, CDOUBLE, CLONGDOUBLE#
41514151
* #Name = Half, Float, Double, LongDouble,
41524152
* CFloat, CDouble, CLongDouble#
4153+
* #num = 1, 1, 1, 1, 2, 2, 2#
41534154
*/
41544155

41554156
PyDict_SetItemString(infodict, "@name@",
@@ -4160,7 +4161,7 @@ set_typeinfo(PyObject *dict)
41604161
#endif
41614162
NPY_@name@,
41624163
NPY_BITSOF_@name@,
4163-
_ALIGN(@type@),
4164+
@num@ * _ALIGN(@type@),
41644165
(PyObject *) &Py@Name@ArrType_Type));
41654166
Py_DECREF(s);
41664167

numpy/core/src/multiarray/dtype_transfer.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3574,13 +3574,6 @@ PyArray_GetDTypeTransferFunction(int aligned,
35743574
PyArray_ISNBO(dst_dtype->byteorder)) {
35753575

35763576
if (PyArray_EquivTypenums(src_type_num, dst_type_num)) {
3577-
/*
3578-
* For complex numbers, the alignment is smaller than the
3579-
* type size, so we turn off the aligned flag then.
3580-
*/
3581-
if (src_dtype->kind == 'c' || dst_dtype->kind == 'c') {
3582-
aligned = 0;
3583-
}
35843577
*out_stransfer = PyArray_GetStridedCopyFn(aligned,
35853578
src_stride, dst_stride,
35863579
src_itemsize);
@@ -3677,13 +3670,6 @@ PyArray_GetDTypeTransferFunction(int aligned,
36773670
/* This is a straight copy */
36783671
if (src_itemsize == 1 || PyArray_ISNBO(src_dtype->byteorder) ==
36793672
PyArray_ISNBO(dst_dtype->byteorder)) {
3680-
/*
3681-
* For complex numbers, the alignment is smaller than the
3682-
* type size, so we turn off the aligned flag then.
3683-
*/
3684-
if (src_dtype->kind == 'c' || dst_dtype->kind == 'c') {
3685-
aligned = 0;
3686-
}
36873673
*out_stransfer = PyArray_GetStridedCopyFn(aligned,
36883674
src_stride, dst_stride,
36893675
src_itemsize);

0 commit comments

Comments
 (0)
0