Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aba6adb

Browse files
committed
Merge pull request #4854 from juliantaylor/i386-alignment
BUG: add missing elementsize alignment check for simd reductions
2 parents 251acc0 + 7cf7d21 commit aba6adb

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

numpy/core/src/umath/simd.inc.src

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
((abs(args[1] - args[0]) >= (vsize)) || ((abs(args[1] - args[0]) == 0))))
3838

3939
#define IS_BLOCKABLE_REDUCE(esize, vsize) \
40-
(steps[1] == (esize) && abs(args[1] - args[0]) >= (vsize))
40+
(steps[1] == (esize) && abs(args[1] - args[0]) >= (vsize) && \
41+
npy_is_aligned(args[1], (esize)) && \
42+
npy_is_aligned(args[0], (esize)))
4143

4244
#define IS_BLOCKABLE_BINARY(esize, vsize) \
4345
(steps[0] == steps[1] && steps[1] == steps[2] && steps[2] == (esize) && \

numpy/core/tests/test_scalarmath.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ def test_blocked(self):
8383
np.add(1, inp2, out=out)
8484
assert_almost_equal(out, exp1, err_msg=msg)
8585

86+
def test_lower_align(self):
87+
# check data that is not aligned to element size
88+
# i.e doubles are aligned to 4 bytes on i386
89+
d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
90+
o = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
91+
assert_almost_equal(d + d, d * 2)
92+
np.add(d, d, out=o)
93+
np.add(np.ones_like(d), d, out=o)
94+
np.add(d, np.ones_like(d), out=o)
95+
np.add(np.ones_like(d), d)
96+
np.add(d, np.ones_like(d))
97+
8698

8799
class TestPower(TestCase):
88100
def test_small_types(self):

numpy/core/tests/test_umath.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,13 @@ def test_minmax_blocked(self):
753753
inp[i] = -1e10
754754
assert_equal(inp.min(), -1e10, err_msg=msg)
755755

756+
def test_lower_align(self):
757+
# check data that is not aligned to element size
758+
# i.e doubles are aligned to 4 bytes on i386
759+
d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
760+
assert_equal(d.max(), d[0])
761+
assert_equal(d.min(), d[0])
762+
756763

757764
class TestAbsoluteNegative(TestCase):
758765
def test_abs_neg_blocked(self):
@@ -785,6 +792,17 @@ def test_abs_neg_blocked(self):
785792
np.negative(inp, out=out)
786793
assert_array_equal(out, -1*inp, err_msg=msg)
787794

795+
def test_lower_align(self):
796+
# check data that is not aligned to element size
797+
# i.e doubles are aligned to 4 bytes on i386
798+
d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
799+
assert_equal(np.abs(d), d)
800+
assert_equal(np.negative(d), -d)
801+
np.negative(d, out=d)
802+
np.negative(np.ones_like(d), out=d)
803+
np.abs(d, out=d)
804+
np.abs(np.ones_like(d), out=d)
805+
788806

789807
class TestSpecialMethods(TestCase):
790808
def test_wrap(self):

0 commit comments

Comments
 (0)