|
| 1 | +// https://docs.python.org/3/c-api/complex.html |
| 2 | + |
| 3 | +use std::ffi; |
| 4 | + |
| 5 | +use rustpython_vm::{PyObject, PyObjectRef, builtins::PyComplex}; |
| 6 | + |
| 7 | +#[repr(C)] |
| 8 | +#[derive(Debug, Copy, Clone)] |
| 9 | +pub struct CPyComplex { |
| 10 | + pub real: ffi::c_double, |
| 11 | + pub imag: ffi::c_double, |
| 12 | +} |
| 13 | + |
| 14 | +impl From<CPyComplex> for PyComplex { |
| 15 | + fn from(value: CPyComplex) -> Self { |
| 16 | + PyComplex::new(num_complex::Complex64::new(value.real, value.imag)) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl From<CPyComplex> for num_complex::Complex64 { |
| 21 | + fn from(value: CPyComplex) -> Self { |
| 22 | + num_complex::Complex64::new(value.real, value.imag) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl From<PyComplex> for CPyComplex { |
| 27 | + fn from(value: PyComplex) -> Self { |
| 28 | + let complex = value.to_complex(); |
| 29 | + CPyComplex { |
| 30 | + real: complex.re, |
| 31 | + imag: complex.im, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl From<num_complex::Complex64> for CPyComplex { |
| 37 | + fn from(value: num_complex::Complex64) -> Self { |
| 38 | + CPyComplex { |
| 39 | + real: value.re, |
| 40 | + imag: value.im, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Associated functions for CPyComplex |
| 46 | +// Always convert to PyComplex to do operations |
| 47 | + |
| 48 | +#[unsafe(export_name = "_Py_c_sum")] |
| 49 | +pub unsafe extern "C" fn c_sum(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex { |
| 50 | + let a: PyComplex = unsafe { *a }.into(); |
| 51 | + let b: PyComplex = unsafe { *b }.into(); |
| 52 | + (a.to_complex() + b.to_complex()).into() |
| 53 | +} |
| 54 | + |
| 55 | +#[unsafe(export_name = "_Py_c_diff")] |
| 56 | +pub unsafe extern "C" fn c_diff(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex { |
| 57 | + let a: PyComplex = unsafe { *a }.into(); |
| 58 | + let b: PyComplex = unsafe { *b }.into(); |
| 59 | + (a.to_complex() - b.to_complex()).into() |
| 60 | +} |
| 61 | + |
| 62 | +#[unsafe(export_name = "_Py_c_neg")] |
| 63 | +pub unsafe extern "C" fn c_neg(a: *const CPyComplex) -> CPyComplex { |
| 64 | + let a: PyComplex = unsafe { *a }.into(); |
| 65 | + (-a.to_complex()).into() |
| 66 | +} |
| 67 | + |
| 68 | +#[unsafe(export_name = "_Py_c_prod")] |
| 69 | +pub unsafe extern "C" fn c_prod(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex { |
| 70 | + let a: PyComplex = unsafe { *a }.into(); |
| 71 | + let b: PyComplex = unsafe { *b }.into(); |
| 72 | + (a.to_complex() * b.to_complex()).into() |
| 73 | +} |
| 74 | + |
| 75 | +#[unsafe(export_name = "_Py_c_quot")] |
| 76 | +pub unsafe extern "C" fn c_quot(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex { |
| 77 | + let a: PyComplex = unsafe { *a }.into(); |
| 78 | + let b: PyComplex = unsafe { *b }.into(); |
| 79 | + (a.to_complex() / b.to_complex()).into() |
| 80 | +} |
| 81 | + |
| 82 | +#[unsafe(export_name = "_Py_c_pow")] |
| 83 | +pub unsafe extern "C" fn c_pow(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex { |
| 84 | + let a: PyComplex = unsafe { *a }.into(); |
| 85 | + let b: PyComplex = unsafe { *b }.into(); |
| 86 | + (a.to_complex() * b.to_complex()).into() |
| 87 | +} |
| 88 | + |
| 89 | +#[unsafe(export_name = "PyComplex_FromCComplex")] |
| 90 | +pub unsafe extern "C" fn complex_from_ccomplex(value: CPyComplex) -> *mut PyObject { |
| 91 | + let vm = crate::get_vm(); |
| 92 | + Into::<PyObjectRef>::into(vm.ctx.new_complex(value.into())) |
| 93 | + .into_raw() |
| 94 | + .as_ptr() |
| 95 | +} |
| 96 | + |
| 97 | +#[unsafe(export_name = "PyComplex_FromDoubles")] |
| 98 | +pub unsafe extern "C" fn complex_from_doubles( |
| 99 | + real: ffi::c_double, |
| 100 | + imag: ffi::c_double, |
| 101 | +) -> *mut PyObject { |
| 102 | + let vm = crate::get_vm(); |
| 103 | + Into::<PyObjectRef>::into(vm.ctx.new_complex(num_complex::Complex64::new(real, imag))) |
| 104 | + .into_raw() |
| 105 | + .as_ptr() |
| 106 | +} |
| 107 | + |
| 108 | +#[unsafe(export_name = "PyComplex_RealAsDouble")] |
| 109 | +pub unsafe extern "C" fn complex_real_as_double(value: *mut PyObject) -> ffi::c_double { |
| 110 | + let vm = crate::get_vm(); |
| 111 | + let value = crate::cast_obj_ptr(value).unwrap(); |
| 112 | + let (complex, _) = value.try_complex(&vm).unwrap().unwrap(); |
| 113 | + complex.re |
| 114 | +} |
| 115 | + |
| 116 | +#[unsafe(export_name = "PyComplex_ImagAsDouble")] |
| 117 | +pub unsafe extern "C" fn complex_imag_as_double(value: *mut PyObject) -> ffi::c_double { |
| 118 | + let vm = crate::get_vm(); |
| 119 | + let value = crate::cast_obj_ptr(value).unwrap(); |
| 120 | + let (complex, _) = value.try_complex(&vm).unwrap().unwrap(); |
| 121 | + complex.im |
| 122 | +} |
| 123 | + |
| 124 | +#[unsafe(export_name = "PyComplex_AsCComplex")] |
| 125 | +pub unsafe extern "C" fn complex_as_ccomplex(value: *mut PyObject) -> CPyComplex { |
| 126 | + let vm = crate::get_vm(); |
| 127 | + let value = crate::cast_obj_ptr(value).unwrap(); |
| 128 | + let (complex, _) = value.try_complex(&vm).unwrap().unwrap(); |
| 129 | + complex.into() |
| 130 | +} |
0 commit comments