|
14 | 14 | #include <ffi.h> |
15 | 15 | #include "ctypes.h" |
16 | 16 |
|
17 | | -#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX) |
18 | | -# include "../_complex.h" // complex |
19 | | -#endif |
20 | | - |
21 | 17 | #define CTYPES_CFIELD_CAPSULE_NAME_PYMEM "_ctypes/cfield.c pymem" |
22 | 18 |
|
23 | 19 | /*[clinic input] |
@@ -763,80 +759,87 @@ d_get(void *ptr, Py_ssize_t size) |
763 | 759 | return PyFloat_FromDouble(val); |
764 | 760 | } |
765 | 761 |
|
766 | | -#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX) |
| 762 | +#if defined(Py_FFI_SUPPORT_C_COMPLEX) |
| 763 | + |
| 764 | +/* We don't use Annex G complex types here, using arrays instead, as the C17+ |
| 765 | + standard says: "Each complex type has the same representation and alignment |
| 766 | + requirements as an array type containing exactly two elements of the |
| 767 | + corresponding real type; the first element is equal to the real part, and |
| 768 | + the second element to the imaginary part, of the complex number." */ |
| 769 | + |
767 | 770 | /* D: double complex */ |
768 | 771 | static PyObject * |
769 | 772 | D_set(void *ptr, PyObject *value, Py_ssize_t size) |
770 | 773 | { |
771 | | - assert(NUM_BITS(size) || (size == sizeof(double complex))); |
| 774 | + assert(NUM_BITS(size) || (size == 2*sizeof(double))); |
772 | 775 | Py_complex c = PyComplex_AsCComplex(value); |
773 | 776 |
|
774 | 777 | if (c.real == -1 && PyErr_Occurred()) { |
775 | 778 | return NULL; |
776 | 779 | } |
777 | | - double complex x = CMPLX(c.real, c.imag); |
| 780 | + double x[2] = {c.real, c.imag}; |
778 | 781 | memcpy(ptr, &x, sizeof(x)); |
779 | 782 | _RET(value); |
780 | 783 | } |
781 | 784 |
|
782 | 785 | static PyObject * |
783 | 786 | D_get(void *ptr, Py_ssize_t size) |
784 | 787 | { |
785 | | - assert(NUM_BITS(size) || (size == sizeof(double complex))); |
786 | | - double complex x; |
| 788 | + assert(NUM_BITS(size) || (size == 2*sizeof(double))); |
| 789 | + double x[2]; |
787 | 790 |
|
788 | 791 | memcpy(&x, ptr, sizeof(x)); |
789 | | - return PyComplex_FromDoubles(creal(x), cimag(x)); |
| 792 | + return PyComplex_FromDoubles(x[0], x[1]); |
790 | 793 | } |
791 | 794 |
|
792 | 795 | /* F: float complex */ |
793 | 796 | static PyObject * |
794 | 797 | F_set(void *ptr, PyObject *value, Py_ssize_t size) |
795 | 798 | { |
796 | | - assert(NUM_BITS(size) || (size == sizeof(float complex))); |
| 799 | + assert(NUM_BITS(size) || (size == 2*sizeof(float))); |
797 | 800 | Py_complex c = PyComplex_AsCComplex(value); |
798 | 801 |
|
799 | 802 | if (c.real == -1 && PyErr_Occurred()) { |
800 | 803 | return NULL; |
801 | 804 | } |
802 | | - float complex x = CMPLXF((float)c.real, (float)c.imag); |
| 805 | + float x[2] = {(float)c.real, (float)c.imag}; |
803 | 806 | memcpy(ptr, &x, sizeof(x)); |
804 | 807 | _RET(value); |
805 | 808 | } |
806 | 809 |
|
807 | 810 | static PyObject * |
808 | 811 | F_get(void *ptr, Py_ssize_t size) |
809 | 812 | { |
810 | | - assert(NUM_BITS(size) || (size == sizeof(float complex))); |
811 | | - float complex x; |
| 813 | + assert(NUM_BITS(size) || (size == 2*sizeof(float))); |
| 814 | + float x[2]; |
812 | 815 |
|
813 | 816 | memcpy(&x, ptr, sizeof(x)); |
814 | | - return PyComplex_FromDoubles(crealf(x), cimagf(x)); |
| 817 | + return PyComplex_FromDoubles(x[0], x[1]); |
815 | 818 | } |
816 | 819 |
|
817 | 820 | /* G: long double complex */ |
818 | 821 | static PyObject * |
819 | 822 | G_set(void *ptr, PyObject *value, Py_ssize_t size) |
820 | 823 | { |
821 | | - assert(NUM_BITS(size) || (size == sizeof(long double complex))); |
| 824 | + assert(NUM_BITS(size) || (size == 2*sizeof(long double))); |
822 | 825 | Py_complex c = PyComplex_AsCComplex(value); |
823 | 826 |
|
824 | 827 | if (c.real == -1 && PyErr_Occurred()) { |
825 | 828 | return NULL; |
826 | 829 | } |
827 | | - long double complex x = CMPLXL(c.real, c.imag); |
| 830 | + long double x[2] = {c.real, c.imag}; |
828 | 831 | memcpy(ptr, &x, sizeof(x)); |
829 | 832 | _RET(value); |
830 | 833 | } |
831 | 834 |
|
832 | 835 | static PyObject * |
833 | 836 | G_get(void *ptr, Py_ssize_t size) |
834 | 837 | { |
835 | | - assert(NUM_BITS(size) || (size == sizeof(long double complex))); |
836 | | - long double complex x; |
| 838 | + assert(NUM_BITS(size) || (size == 2*sizeof(long double))); |
| 839 | + long double x[2]; |
837 | 840 |
|
838 | 841 | memcpy(&x, ptr, sizeof(x)); |
839 | | - return PyComplex_FromDoubles((double)creall(x), (double)cimagl(x)); |
| 842 | + return PyComplex_FromDoubles((double)x[0], (double)x[1]); |
840 | 843 | } |
841 | 844 | #endif |
842 | 845 |
|
@@ -1596,7 +1599,7 @@ for base_code, base_c_type in [ |
1596 | 1599 | /////////////////////////////////////////////////////////////////////////// |
1597 | 1600 |
|
1598 | 1601 | TABLE_ENTRY_SW(d, &ffi_type_double); |
1599 | | -#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX) |
| 1602 | +#if defined(Py_FFI_SUPPORT_C_COMPLEX) |
1600 | 1603 | if (Py_FFI_COMPLEX_AVAILABLE) { |
1601 | 1604 | TABLE_ENTRY(D, &ffi_type_complex_double); |
1602 | 1605 | TABLE_ENTRY(F, &ffi_type_complex_float); |
|
0 commit comments