-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add support for complex numbers #355
Comments
I'd be game! I hear though that there are weird ABI implications though we may have to deal with? |
I’d love to help with the implementation, but I’m not sure if I’m competent enough to do it, and I’m not aware of any ABI implications. The way I see it is that each of the two types can be defined using an array or a struct: pub type c_float_complex = [f32; 2];
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct c_float_complex {
pub re: f32,
pub im: f32,
} I don’t think one should implement any arithmetic traits and such. We don’t want to write |
Right yeah we'd just want the type definitions and related functions if any here. I'd want to be 100% sure about the ABI though (with it being equivalent to such a struct) before merging. |
I perfectly understand your concern. I’ve tried to read a bit about the layout of complex numbers in C. According to both C99 (section 6.2.5, paragraph 13) and C11 (section 6.2.5, paragraph 13),
(Note that I give links to drafts as they are the only ones publicly available.) Regarding C++ (not our case, but just to make the picture more complete), the layout is not guaranteed by C++03, but it is by C++1x (section 26.4, paragraph 4), which is done in order to have a better interoperability with C:
Regarding related functions, C99 defines the following: Please let me know what you think about all this. Thank you. |
Ah yeah I'm just specifically thinking of this issue: rust-lang/rfcs#793. If we handle that then seems good to me! |
Thanks for the link! I see now what the problem is. But if it can’t be done conveniently and reliably without additional support from the compiler, there is no point to discuss it here. Probably you don’t want to see those workarounds in |
I see in |
Yeah I'm just not sure if we can do this without compiler support. If we can, great! If not, we may want to wait for that. It may be best to actually prototype this support outside of libc itself and once it's proven we could merge? Right now the CI doesn't actually test the ABI, just that the definitions in C and Rust agree. So it may not be enough to get a green build on this repo :( |
Can you please give an example that cannot be solved using conditional compilation via attributes so that I don’t waste time if it’s really hopeless?
I meant that I would write a couple of such tests first. For instance, one could call some of those functions that I previously mention and check that the output is as expected. |
Oh if it's all solvable with |
cc @emilio how does bindgen handle complex numbers? and what is your opinion about adding these to libc? |
See rust-lang/rust-bindgen#72 and related. Bindgen generates something like |
Hi,
According to the standard (section 6.7.2, page 111), the C language provides the
_Compex
type specifier, which allows one to work with complex numbers using the following types:float _Complex
,double _Complex
, andlong double _Complex
.I’m curious if there are any particular objections against adding the following types to
libc
:c_float_complex
andc_double_complex
.The reason I ask is that there are C APIs that make use of complex numbers, and, in order interact with them from Rust, one has to create complex types him/herself instead of just importing them from
libc
. Thank you!Regards,
Ivan
The text was updated successfully, but these errors were encountered: