-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
System.IntPtr
and System.UIntPtr
Operators
#27614
Comments
This is related to https://github.com/dotnet/corefx/issues/20256 |
Today we already expose: public static unsafe bool operator ==(IntPtr value1, IntPtr value2);
public static unsafe bool operator !=(IntPtr value1, IntPtr value2);
public static unsafe IntPtr operator +(IntPtr pointer, int offset)
public static unsafe IntPtr operator -(IntPtr pointer, int offset) It may be worth discussing whether we also want to expose
|
Today we also expose "friendly named" static methods: public static IntPtr Add(IntPtr pointer, int offset);
public static IntPtr Subtract(IntPtr pointer, int offset); Should we also expose these for the other operators. |
CC. @joperezr; since it looks like you are the area owners and you also marked the previous one as |
As per https://github.com/dotnet/corefx/issues/20256#issuecomment-428764392 and https://github.com/dotnet/corefx/issues/20256#issuecomment-428764666. This should not be blocked by the corresponding language change that is also being discussed (but which keeeps getting delayed). This would also allow use to simplify a bit of code in CoreCLR/CoreFX and ML.NET |
Older C# compilers can generate code to call these operators which would then be inlined. Newer C# compiler versions could emit slim IL directly. Any chance to change the currently broken |
@jkotas, this sounds similar to your thoughts on The reason not to have math operators on Having a different type that represents platform-sized integers would avoid the safety concern (and have a better name). |
I don't think this is valid at all. If you actually have a pointer you are free to do arithmetic with it (outside of
|
Right, this is an alternative for the first class nint/nuint support in the language that we do not seem to be able to make progress on. |
@tannergooding, you've described good reasons to have a platform-native integer type with arithmetic operators. That would certainly justify moving forward with |
Regardless of whether or not someone thinks that While having the language support this directly would be ideal, there has not been much progress made on that front. Supporting this via an API change would improve a lot of code in the framework and elsewhere, while still not being a blocker for the language; As per @jaredpar https://github.com/dotnet/corefx/issues/20256#issuecomment-428765687, this should not be a blocker unless they wanted to take a breaking change (since it would make such a break more likely to be impactful). The language would still be free to support this via partial erasure (in which case they would have a new keyword, |
This isn't true. Detecting the alignment of a pointer can be very important for performance oriented code. Many vectorized algorithms will perform at most 2 unaligned operations (in order to become aligned) and then operate on the rest of the data as aligned, in order to achieve "optimal" performance. We are already doing the above in ML.NET and are having to work around the fact that you can't easily do |
You can also find scattering of |
I can confirm JIT doesnt inline casting operators e.g. (void*)ptr. im fed up c# blocking my way when working with native int's and producing inefficient code. unsafe code is usually used in high performance scenarios and its not good. im trying to use byte* void* or void** instead and that intptr comes in the way somehow and cant avoid it. If you ease working with native ints and optimize produced code it would deserve a major version such as C# 10.0 :) |
@crowo can you share an example? If there are JIT issues blocking you I'd like to make sure we know about them. |
Closing in favor of the language feature: dotnet/csharplang#435 |
Rationale
The .NET framework provides the
System.IntPtr
andSystem.UIntPtr
types which wrap thenative int
IL primitive (as per ECMA-335).These types are frequently used to represent both handles/pointers and integers whose size depend on the underlying platform.
When being used to represent the latter (platform sized integer) type, users may find a difficult time performing some basic mathematical operations on these types.
As such, these types should be modified to expose a the basic set of operators defined in ECMA-335.
Proposed API
The proposed API here only shows the members that would be new to the types.
System.IntPtr
System.UIntPtr
The text was updated successfully, but these errors were encountered: