-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-146151: memoryview supports 'F' and 'D' format types (complex) #146241
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
Changes from 1 commit
94ee023
c94450e
43e9b18
a728a1b
f83e88f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| :class:`memoryview` now supports the :c:expr:`float complex` and | ||
| :c:expr:`double complex` C types (formatting characters ``'F'`` and ``'D'`` | ||
| respectively). Patch by Sergey B Kirpichev. | ||
skirpichev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1216,6 +1216,8 @@ get_native_fmtchar(char *result, const char *fmt) | |
| case 'f': size = sizeof(float); break; | ||
| case 'd': size = sizeof(double); break; | ||
| case 'e': size = sizeof(float) / 2; break; | ||
| case 'F': size = 2*sizeof(float); break; | ||
| case 'D': size = 2*sizeof(double); break; | ||
| case '?': size = sizeof(_Bool); break; | ||
| case 'P': size = sizeof(void *); break; | ||
| } | ||
|
|
@@ -1260,6 +1262,8 @@ get_native_fmtstr(const char *fmt) | |
| case 'f': RETURN("f"); | ||
| case 'd': RETURN("d"); | ||
| case 'e': RETURN("e"); | ||
| case 'F': RETURN("F"); | ||
| case 'D': RETURN("D"); | ||
| case '?': RETURN("?"); | ||
| case 'P': RETURN("P"); | ||
| } | ||
|
|
@@ -1785,7 +1789,7 @@ unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt) | |
| long long lld; | ||
| long ld; | ||
| Py_ssize_t zd; | ||
| double d; | ||
| double d[2]; | ||
| unsigned char uc; | ||
| void *p; | ||
|
|
||
|
|
@@ -1823,9 +1827,20 @@ unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt) | |
| case 'N': UNPACK_SINGLE(zu, ptr, size_t); goto convert_zu; | ||
|
|
||
| /* floats */ | ||
| case 'f': UNPACK_SINGLE(d, ptr, float); goto convert_double; | ||
| case 'd': UNPACK_SINGLE(d, ptr, double); goto convert_double; | ||
| case 'e': d = PyFloat_Unpack2(ptr, endian); goto convert_double; | ||
| case 'f': UNPACK_SINGLE(d[0], ptr, float); goto convert_double; | ||
| case 'd': UNPACK_SINGLE(d[0], ptr, double); goto convert_double; | ||
| case 'e': d[0] = PyFloat_Unpack2(ptr, endian); goto convert_double; | ||
|
|
||
| /* complexes */ | ||
| case 'F': | ||
| d[0] = PyFloat_Unpack4(ptr, endian); | ||
| d[1] = PyFloat_Unpack4(ptr + sizeof(float), endian); | ||
| goto convert_double_complex; | ||
|
|
||
| case 'D': | ||
| d[0] = PyFloat_Unpack8(ptr, endian); | ||
| d[1] = PyFloat_Unpack8(ptr + sizeof(double), endian); | ||
| goto convert_double_complex; | ||
|
|
||
| /* bytes object */ | ||
| case 'c': goto convert_bytes; | ||
|
|
@@ -1853,7 +1868,9 @@ unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt) | |
| convert_zu: | ||
| return PyLong_FromSize_t(zu); | ||
| convert_double: | ||
| return PyFloat_FromDouble(d); | ||
| return PyFloat_FromDouble(d[0]); | ||
| convert_double_complex: | ||
| return PyComplex_FromDoubles(d[0], d[1]); | ||
| convert_bool: | ||
| return PyBool_FromLong(ld); | ||
| convert_bytes: | ||
|
|
@@ -1885,6 +1902,7 @@ pack_single(PyMemoryViewObject *self, char *ptr, PyObject *item, const char *fmt | |
| long ld; | ||
| Py_ssize_t zd; | ||
| double d; | ||
| Py_complex c; | ||
| void *p; | ||
|
|
||
| #if PY_LITTLE_ENDIAN | ||
|
|
@@ -1986,6 +2004,25 @@ pack_single(PyMemoryViewObject *self, char *ptr, PyObject *item, const char *fmt | |
| } | ||
| break; | ||
|
|
||
| /* complexes */ | ||
| case 'F': case 'D': | ||
| c = PyComplex_AsCComplex(item); | ||
| if (c.real == -1.0 && PyErr_Occurred()) { | ||
| goto err_occurred; | ||
| } | ||
| CHECK_RELEASED_INT_AGAIN(self); | ||
| if (fmt[0] == 'D') { | ||
| double x[2] = {c.real, c.imag}; | ||
|
|
||
| memcpy(ptr, &x, sizeof(x)); | ||
| } | ||
| else { | ||
| float x[2] = {(float)c.real, (float)c.imag}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure of the behavior on underflow or overflow of the >>> import struct, sys
>>> data=struct.pack('F', 0.0) * 4
>>> m=memoryview(bytearray(data)).cast('F')
>>> m[0]=math.nextafter(0, 1)
>>> m[1]=sys.float_info.min
>>> m[2]=sys.float_info.max
>>> m[3]=float("nan")
>>> m.tolist()
[0j, 0j, (inf+0j), (nan+0j)]I got the values that I expected. I suppose that these conversions are well specified by IEEE 754.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behavior here is same as for 'f' format, just component-wise, see |
||
|
|
||
| memcpy(ptr, &x, sizeof(x)); | ||
| } | ||
| break; | ||
|
|
||
| /* bool */ | ||
| case '?': | ||
| ld = PyObject_IsTrue(item); | ||
|
|
@@ -3023,6 +3060,24 @@ unpack_cmp(const char *p, const char *q, char fmt, | |
| return (u == v); | ||
| } | ||
|
|
||
| /* complexes */ | ||
| case 'F': | ||
| { | ||
| float x[2], y[2]; | ||
|
|
||
| memcpy(&x, p, sizeof(x)); | ||
| memcpy(&y, q, sizeof(y)); | ||
| return (x[0] == y[0]) && (x[1] == y[1]); | ||
| } | ||
| case 'D': | ||
| { | ||
| double x[2], y[2]; | ||
|
|
||
| memcpy(&x, p, sizeof(x)); | ||
| memcpy(&y, q, sizeof(y)); | ||
| return (x[0] == y[0]) && (x[1] == y[1]); | ||
| } | ||
|
|
||
| /* bytes object */ | ||
| case 'c': return *p == *q; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.