8000 MAINT: don't check alignment size=0 arrays (RELAXED_STRIDES) · numpy/numpy@d7ab72d · GitHub
[go: up one dir, main page]

Skip to content

Commit d7ab72d

Browse files
committed
MAINT: don't check alignment size=0 arrays (RELAXED_STRIDES)
Don't check alignment of size-0 arrays in copy-loops, because of RELAXED_STRIDES. Fixes #12503
1 parent 8ee86d5 commit d7ab72d

File tree

2 files changed

+44
-6
line 8000 s changed

2 files changed

+44
-6
lines changed

numpy/core/src/multiarray/lowlevel_strided_loops.c.src

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ static void
121121
{
122122
#if @is_aligned@ && @elsize@ != 16
123123
/* sanity check */
124-
assert(npy_is_aligned(dst, _ALIGN(@type@)));
125-
assert(npy_is_aligned(src, _ALIGN(@type@)));
124+
assert(N == 0 || npy_is_aligned(dst, _ALIGN(@type@)));
125+
assert(N == 0 || npy_is_aligned(src, _ALIGN(@type@)));
126126
#endif
127127
/*printf("fn @prefix@_@oper@_size@elsize@\n");*/
128128
while (N > 0) {
@@ -201,8 +201,8 @@ static NPY_GCC_OPT_3 void
201201
}
202202
#if @is_aligned@ && @elsize@ != 16
203203
/* sanity check */
204-
assert(npy_is_aligned(dst, _ALIGN(@type@)));
205-
assert(npy_is_aligned(src, _ALIGN(@type@)));
204+
assert(N == 0 || npy_is_aligned(dst, _ALIGN(@type@)));
205+
assert(N == 0 || npy_is_aligned(src, _ALIGN(@type@)));
206206
#endif
207207
#if @elsize@ == 1 && @dst_contig@
208208
memset(dst, *src, N);
@@ -809,10 +809,10 @@ static NPY_GCC_OPT_3 void
809809
#if @aligned@
810810
/* sanity check */
811811
# if !@is_complex1@
812-
assert(npy_is_aligned(src, _ALIGN(_TYPE1)));
812+
assert(N == 0 || npy_is_aligned(src, _ALIGN(_TYPE1)));
813813
# endif
814814
# if !@is_complex2@
815-
assert(npy_is_aligned(dst, _ALIGN(_TYPE2)));
815+
assert(N == 0 || npy_is_aligned(dst, _ALIGN(_TYPE2)));
816816
# endif
817817
#endif
818818

numpy/core/tests/test_multiarray.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7923,6 +7923,44 @@ def test_uintalignment_and_alignment():
79237923
dst = np.zeros((2,2), dtype='c8')
79247924
dst[:,1] = src[:,1] # assert in lowlevel_strided_loops fails?
79257925

7926+
class TestAlignment(object):
7927+
# adapted from scipy._lib.tests.test__util.test__aligned_zeros
7928+
# Checks that unusual memory alignments don't trip up numpy.
7929+
# In particular, check RELAXED_STRIDES don't trip alignment assertions in
7930+
# NDEBUG mode for size-0 arrays (gh-12503)
7931+
7932+
def check(self, shape, dtype, order, align):
7933+
err_msg = repr((shape, dtype, order, align))
7934+
x = _aligned_zeros(shape, dtype, order, align=align)
7935+
if align is None:
7936+
align = np.dtype(dtype).alignment
7937+
assert_equal(x.__array_interface__['data'][0] % align, 0)
7938+
if hasattr(shape, '__len__'):
7939+
assert_equal(x.shape, shape, err_msg)
7940+
else:
7941+
assert_equal(x.shape, (shape,), err_msg)
7942+
assert_equal(x.dtype, dtype)
7943+
if order == "C":
7944+
assert_(x.flags.c_contiguous, err_msg)
7945+
elif order == "F":
7946+
if x.size > 0:
7947+
assert_(x.flags.f_contiguous, err_msg)
7948+
elif order is None:
7949+
assert_(x.flags.c_contiguous, err_msg)
7950+
else:
7951+
raise ValueError()
7952+
7953+
def test_various_alignments(self):
7954+
for align in [1, 2, 3, 4, 8, 16, 32, 64, None]:
7955+
for n in [0, 1, 3, 11]:
7956+
for order in ["C", "F", None]:
7957+
for dtype in np.typecodes["All"]:
7958+
if dtype == 'O':
7959+
# object dtype can't be misaligned
7960+
continue
7961+
for shape in [n, (1, 2, 3, n)]:
7962+
self.check(shape, np.dtype(dtype), order, align)
7963+
79267964
def test_getfield():
79277965
a = np.arange(32, dtype='uint16')
79287966
if sys.byteorder == 'little':

0 commit comments

Comments
 (0)
0