-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-61103: support double complex (_Complex) type in ctypes #120894
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 all commits
c9b872f
b402300
ad39e6d
fd9135b
c296fb3
7453ca5
e444c9c
2e90643
32ffacf
9163950
bf81682
af2fdce
6d9d5ab
f9c709c
ccd418d
eb313dc
693c04e
0ee7049
de91bbe
b79200e
be6a685
04e89c2
0bc87da
6347412
e757fb6
1eb989c
b44aae2
2153608
af42aa8
e57198e
36732bc
892b241
ee953c3
875cbb8
f8e3a47
f447919
7b1c366
2ea028a
837add4
1b8f37e
0bd1ebe
6eba445
a6ac463
615e8de
f7d9973
3d170bd
4dd044c
4cf7045
264fa7d
f54b964
23db200
5a7e366
388e2ca
e888565
56b524c
e9cab4a
6d5bf66
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 @@ | ||
Support :c:expr:`double complex` C type in :mod:`ctypes` via | ||
:class:`~ctypes.c_double_complex` if compiler has C11 complex | ||
arithmetic. Patch by Sergey B Kirpichev. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* Workarounds for buggy complex number arithmetic implementations. */ | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#ifndef Py_HAVE_C_COMPLEX | ||
# error "this header file should only be included if Py_HAVE_C_COMPLEX is defined" | ||
#endif | ||
|
||
#include <complex.h> | ||
|
||
/* Other compilers (than clang), that claims to | ||
implement C11 *and* define __STDC_IEC_559_COMPLEX__ - don't have | ||
issue with CMPLX(). This is specific to glibc & clang combination: | ||
https://sourceware.org/bugzilla/show_bug.cgi?id=26287 | ||
|
||
Here we fallback to using __builtin_complex(), available in clang | ||
v12+. Else CMPLX implemented following C11 6.2.5p13: "Each complex type | ||
has the same representation and alignment requirements as an array | ||
type containing exactly two elements of the corresponding real type; | ||
the first element is equal to the real part, and the second element | ||
to the imaginary part, of the complex number. | ||
*/ | ||
#if !defined(CMPLX) | ||
# if defined(__clang__) && __has_builtin(__builtin_complex) | ||
# define CMPLX(x, y) __builtin_complex ((double) (x), (double) (y)) | ||
# else | ||
static inline double complex | ||
CMPLX(double real, double imag) | ||
{ | ||
double complex z; | ||
((double *)(&z))[0] = real; | ||
((double *)(&z))[1] = imag; | ||
return z; | ||
} | ||
# endif | ||
#endif |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,6 +13,12 @@ | |||||
|
||||||
#include <Python.h> | ||||||
|
||||||
#include <ffi.h> // FFI_TARGET_HAS_COMPLEX_TYPE | ||||||
|
||||||
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE) | ||||||
# include "../_complex.h" // csqrt() | ||||||
# undef I // for _ctypes_test_generated.c.h | ||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
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. Here I would like to emphasize where new macro create a problem. I.e. name - matter, what it's - actually irrelevant. 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. Can we avoid the relative include? They tend to produce problems further down the road. 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. How about There is 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 feel that 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. @vstinner: I'd really like to avoid the relative include, but we can fix that in a follow-up PR. |
||||||
#endif | ||||||
#include <stdio.h> // printf() | ||||||
#include <stdlib.h> // qsort() | ||||||
#include <string.h> // memset() | ||||||
|
@@ -443,6 +449,13 @@ EXPORT(double) my_sqrt(double a) | |||||
return sqrt(a); | ||||||
} | ||||||
|
||||||
#if defined(Py_HAVE_C_COMPLEX) && defined(FFI_TARGET_HAS_COMPLEX_TYPE) | ||||||
EXPORT(double complex) my_csqrt(double complex a) | ||||||
{ | ||||||
return csqrt(a); | ||||||
} | ||||||
#endif | ||||||
|
||||||
EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*)) | ||||||
{ | ||||||
qsort(base, num, width, compare); | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.