|
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 | #defineCTYPES_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 | returnPyFloat_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 | staticPyObject* |
769 | 772 | D_set(void*ptr,PyObject*value,Py_ssize_tsize) |
770 | 773 | { |
771 | | -assert(NUM_BITS(size)|| (size==sizeof(doublecomplex))); |
| 774 | +assert(NUM_BITS(size)|| (size==2*sizeof(double))); |
772 | 775 | Py_complexc=PyComplex_AsCComplex(value); |
773 | 776 |
|
774 | 777 | if (c.real==-1&&PyErr_Occurred()) { |
775 | 778 | returnNULL; |
776 | 779 | } |
777 | | -doublecomplexx=CMPLX(c.real,c.imag); |
| 780 | +doublex[2]={c.real,c.imag}; |
778 | 781 | memcpy(ptr,&x,sizeof(x)); |
779 | 782 | _RET(value); |
780 | 783 | } |
781 | 784 |
|
782 | 785 | staticPyObject* |
783 | 786 | D_get(void*ptr,Py_ssize_tsize) |
784 | 787 | { |
785 | | -assert(NUM_BITS(size)|| (size==sizeof(doublecomplex))); |
786 | | -doublecomplexx; |
| 788 | +assert(NUM_BITS(size)|| (size==2*sizeof(double))); |
| 789 | +doublex[2]; |
787 | 790 |
|
788 | 791 | memcpy(&x,ptr,sizeof(x)); |
789 | | -returnPyComplex_FromDoubles(creal(x),cimag(x)); |
| 792 | +returnPyComplex_FromDoubles(x[0],x[1]); |
790 | 793 | } |
791 | 794 |
|
792 | 795 | /* F: float complex */ |
793 | 796 | staticPyObject* |
794 | 797 | F_set(void*ptr,PyObject*value,Py_ssize_tsize) |
795 | 798 | { |
796 | | -assert(NUM_BITS(size)|| (size==sizeof(floatcomplex))); |
| 799 | +assert(NUM_BITS(size)|| (size==2*sizeof(float))); |
797 | 800 | Py_complexc=PyComplex_AsCComplex(value); |
798 | 801 |
|
799 | 802 | if (c.real==-1&&PyErr_Occurred()) { |
800 | 803 | returnNULL; |
801 | 804 | } |
802 | | -floatcomplexx=CMPLXF((float)c.real, (float)c.imag); |
| 805 | +floatx[2]={(float)c.real, (float)c.imag}; |
803 | 806 | memcpy(ptr,&x,sizeof(x)); |
804 | 807 | _RET(value); |
805 | 808 | } |
806 | 809 |
|
807 | 810 | staticPyObject* |
808 | 811 | F_get(void*ptr,Py_ssize_tsize) |
809 | 812 | { |
810 | | -assert(NUM_BITS(size)|| (size==sizeof(floatcomplex))); |
811 | | -floatcomplexx; |
| 813 | +assert(NUM_BITS(size)|| (size==2*sizeof(float))); |
| 814 | +floatx[2]; |
812 | 815 |
|
813 | 816 | memcpy(&x,ptr,sizeof(x)); |
814 | | -returnPyComplex_FromDoubles(crealf(x),cimagf(x)); |
| 817 | +returnPyComplex_FromDoubles(x[0],x[1]); |
815 | 818 | } |
816 | 819 |
|
817 | 820 | /* G: long double complex */ |
818 | 821 | staticPyObject* |
819 | 822 | G_set(void*ptr,PyObject*value,Py_ssize_tsize) |
820 | 823 | { |
821 | | -assert(NUM_BITS(size)|| (size==sizeof(longdoublecomplex))); |
| 824 | +assert(NUM_BITS(size)|| (size==2*sizeof(longdouble))); |
822 | 825 | Py_complexc=PyComplex_AsCComplex(value); |
823 | 826 |
|
824 | 827 | if (c.real==-1&&PyErr_Occurred()) { |
825 | 828 | returnNULL; |
826 | 829 | } |
827 | | -longdoublecomplexx=CMPLXL(c.real,c.imag); |
| 830 | +longdoublex[2]={c.real,c.imag}; |
828 | 831 | memcpy(ptr,&x,sizeof(x)); |
829 | 832 | _RET(value); |
830 | 833 | } |
831 | 834 |
|
832 | 835 | staticPyObject* |
833 | 836 | G_get(void*ptr,Py_ssize_tsize) |
834 | 837 | { |
835 | | -assert(NUM_BITS(size)|| (size==sizeof(longdoublecomplex))); |
836 | | -longdoublecomplexx; |
| 838 | +assert(NUM_BITS(size)|| (size==2*sizeof(longdouble))); |
| 839 | +longdoublex[2]; |
837 | 840 |
|
838 | 841 | memcpy(&x,ptr,sizeof(x)); |
839 | | -returnPyComplex_FromDoubles((double)creall(x), (double)cimagl(x)); |
| 842 | +returnPyComplex_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); |
|