8000 BUG: Fix byteswapping for complex scalars by seberg · Pull Request #2886 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix byteswapping for complex scalars #2886

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 1 commit into from
Jan 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 14 additions & 15 deletions numpy/core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -946,9 +946,9 @@ HALF_to_@TYPE@(npy_half *ip, @type@ *op, npy_intp n,
}

/**end repeat**/
#if SIZEOF_SHORT == 2
#if NPY_SIZEOF_SHORT == 2
#define HALF_to_HALF SHORT_to_SHORT
#elif SIZEOF_INT == 2
#elif NPY_SIZEOF_INT == 2
#define HALF_to_HALF INT_to_INT
#endif

Expand Down Expand Up @@ -1612,35 +1612,35 @@ static void
char *a, *b, c;

a = (char *)dst;
#if SIZEOF_@fsize@ == 2
#if NPY_SIZEOF_@fsize@ == 2
b = a + 1;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 4
#elif NPY_SIZEOF_@fsize@ == 4
b = a + 3;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 8
#elif NPY_SIZEOF_@fsize@ == 8
b = a + 7;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 10
#elif NPY_SIZEOF_@fsize@ == 10
b = a + 9;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 12
#elif NPY_SIZEOF_@fsize@ == 12
b = a + 11;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 16
#elif NPY_SIZEOF_@fsize@ == 16
b = a + 15;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
Expand Down Expand Up @@ -1744,15 +1744,15 @@ static void
if (swap) {
char *a, *b, c;
a = (char *)dst;
#if SIZEOF_@fsize@ == 4
#if NPY_SIZEOF_@fsize@ == 4
b = a + 3;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
a += 2;
b = a + 3;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 8
#elif NPY_SIZEOF_@fsize@ == 8
b = a + 7;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
Expand All @@ -1764,7 +1764,7 @@ static void
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 10
#elif NPY_SIZEOF_@fsize@ == 10
b = a + 9;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
Expand All @@ -1778,7 +1778,7 @@ static void
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 12
#elif NPY_SIZEOF_@fsize@ == 12
b = a + 11;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
Expand All @@ -1794,7 +1794,7 @@ static void
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b = c;
#elif SIZEOF_@fsize@ == 16
#elif NPY_SIZEOF_@fsize@ == 16
b = a + 15;
c = *a; *a++ = *b; *b-- = c;
c = *a; *a++ = *b; *b-- = c;
Expand Down Expand Up @@ -1825,9 +1825,8 @@ static void
*a++ = *b;
*b-- = c;
}
a += nn / 2;
a += nn;
b = a + (NPY_SIZEOF_@fsize@ - 1);
nn = NPY_SIZEOF_@fsize@ / 2;
for (i = 0; i < nn; i++) {
c = *a;
*a++ = *b;
Expand Down
24 changes: 14 additions & 10 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,16 +1296,20 @@ def test_misaligned_dot_product_objects(self):
np.dot(a['f0'], b['f0'])

def test_byteswap_complex_scalar(self):
"""Ticket #1259"""
z = np.array([-1j], '<c8')
x = z[0] # always native-endian
y = x.byteswap()
if x.dtype.byteorder == z.dtype.byteorder:
# little-endian machine
assert_equal(x, np.fromstring(y.tostring(), dtype='>c8'))
else:
# big-endian machine
assert_equal(x, np.fromstring(y.tostring(), dtype='<c8'))
"""Ticket #1259 and gh-441"""
for dtype in [np.dtype('<'+t) for t in np.typecodes['Complex']]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, t is one of:

In [2]: np.typecodes['Complex']                                                
Out[2]: 'FDG'

but the original test was testing with t="c8" (effectively). So why was testing for "c8" removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not removed np.dtype('F') == np.dtype('c8').

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. All is ok then. Thanks!

z = np.array([2.2-1.1j], dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you changed -1j to 2.2-1.1j, which seems ok to me.

x = z[0] # always native-endian
y = x.byteswap()
if x.dtype.byteorder == z.dtype.byteorder:
# little-endian machine
assert_equal(x, np.fromstring(y.tostring(), dtype=dtype.newbyteorder()))
else:
# big-endian machine
assert_equal(x, np.fromstring(y.tostring(), dtype=dtype))
# double check real and imaginary parts:
assert_equal(x.real, y.real.byteswap())
assert_equal(x.imag, y.imag.byteswap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the changes here look ok to me.


def test_structured_arrays_with_objects1(self):
"""Ticket #1299"""
Expand Down
0