| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| 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. */ | ||
|
Comment thread
vstinner marked this conversation as 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 | ||||
|---|---|---|---|---|---|---|
| Expand Up | @@ -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 | ||||||
|
Comment thread
vstinner marked this conversation as resolved.
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityHere I would like to emphasize where new macro create a problem. I.e. name - matter, what it's - actually irrelevant.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityCan we avoid the relative include? They tend to produce problems further down the road.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityHow about Include/internal/pycore_pycomplex.h? There is Include/internal/pycore_pymath.h, that (IMHO) does part of work Modules/_math.h (<math.h> workarounds). There are no relative includes of _math.h (yet), but I think these files could be merged.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI feel that _math.h and _complex.h could be named as Include/internal/pylibc_{math,complex}.h because what they do is patching the C standard library itself rather than adding new functionalities (well, for _complex.h it's not really a patch but rather a way to harmonize the calls).
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality@vstinner: I'd really like to avoid the relative include, but we can fix that in a follow-up PR.
Sorry, something went wrong.
All reactions
|
||||||
| #endif | ||||||
| #include <stdio.h> // printf() | ||||||
| #include <stdlib.h> // qsort() | ||||||
| #include <string.h> // memset() | ||||||
| Expand Down Expand Up | @@ -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); | ||||||
| Expand Down | ||||||
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.