Permalink
Browse files

Merge pull request #2886 from seberg/complex256-byteswap

BUG: Fix byteswapping for complex scalars
  • Loading branch information...
2 parents 5701c87 + d2ca917 commit 05dde0f733419cc7802cd9ea7d03050db120e429 @charris charris committed Jan 5, 2013
Showing with 28 additions and 25 deletions.
  1. +14 −15 numpy/core/src/multiarray/arraytypes.c.src
  2. +14 −10 numpy/core/tests/test_regression.py
@@ -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
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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']]:
+ z = np.array([2.2-1.1j], dtype)
+ 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())
def test_structured_arrays_with_objects1(self):
"""Ticket #1299"""

0 comments on commit 05dde0f

Please sign in to comment.