8000 Fix 1.9 alignment by charris · Pull Request #5478 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Fix 1.9 alignment #5478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BUG: fix string arrays not being aligned
If itemsize is a power of two use that as the required alignment up to
the maximum provided by the platform. Power of two sizes may be accessed
via larger moves than bytes.
Non-power of two sizes are accessed bytewise and can thus always be
considered aligned.
Closes gh-5293
  • Loading branch information
juliantaylor authored and charris committed Jan 22, 2015
commit e0ef28b22a04d1d470e381f1b0cee56aec4349ed
11 changes: 10 additions & 1 deletion numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,16 @@ _IsAligned(PyArrayObject *ap)

/* alignment 1 types should have a efficient alignment for copy loops */
if (PyArray_ISFLEXIBLE(ap) || PyArray_ISSTRING(ap)) {
alignment = NPY_MAX_COPY_ALIGNMENT;
npy_intp itemsize = PyArray_ITEMSIZE(ap);
/* power of two sizes may be loaded in larger moves */
if (((itemsize & (itemsize - 1)) == 0)) {
alignment = itemsize > NPY_MAX_COPY_ALIGNMENT ?
NPY_MAX_COPY_ALIGNMENT : itemsize;
}
else {
/* if not power of two it will be accessed bytewise */
alignment = 1;
}
}

if (alignment == 1) {
Expand Down
11 changes: 11 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ def test_otherflags(self):
assert_equal(self.a.flags.aligned, True)
assert_equal(self.a.flags.updateifcopy, False)

def test_string_align(self):
a = np.zeros(4, dtype=np.dtype('|S4'))
assert_(a.flags.aligned)
# not power of two are accessed bytewise and thus considered aligned
a = np.zeros(5, dtype=np.dtype('|S4'))
assert_(a.flags.aligned)

def test_void_align(self):
a = np.zeros(4, dtype=np.dtype([("a", "i4"), ("b", "i4")]))
assert_(a.flags.aligned)

class TestHash(TestCase):
# see #3793
def test_int(self):
Expand Down
0