-
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
New API for single-precision math #14353
Comments
Why would we not add float overloads directly to System.Math? |
This would break compatibility. If you call Math.Sqrt(2f) would now call the single precision overload with less precision instead of implicitly casting it to double. |
Given the existing API However, if you add This results in different behavior and provides no warning as there isn't a data loss in the view of the compiler (when there is an actual loss of precision as compared to the previous release). |
Ah, makes sense. Having said that, I wonder if there is some compatible approach that would allow us to have these members on the Math class. Maybe we could add methods with some naming scheme (prefix or suffix) or add a nested type to Math. |
Well C uses the
I think a nested type would make it more difficult to access/use the methods. So in a language that doesn't allow A couple of other ideas:
|
Note that Unity already has a Mathf class that does exactly this: https://docs.unity3d.com/ScriptReference/Mathf.html That's an indicator that such an API is useful and can help influence the vote for a good name. |
@tannergooding why don't you create a formal API proposal? We would discuss it at the next API review. I think, in general, the addition is something we should do. It's just a matter of deciding on the right shape/location/naming of the APIs. |
BTW, you can read about the API review process at https://github.com/dotnet/corefx/wiki/API-Review-Process. |
@KrzysztofCwalina, I have updated the first comment to more closely resemble a speclet. Please let me know if any additional changes should be made. |
Updated the speclet to include the full set of proposed API changes and to provided extended details on the changes that would be provided. |
While I definitely support the addition of float-specific / single precision Math functions, I think that moving all math functions out of a centralized static class and into each separate type is not a good idea with regards to developer productivity. Consider the following example code:
"Whoops, I found out I actually need a different type. Let's change this"
While the example is not a real-world one, you can easily see the problem with this. Having a single static Math class is a big plus, because the compiler is able to find out which method overload to use all by itself with no work for the developer. I do not have a great solution for the Math backwards compatibility problem, but personally I'd rather continue to write a few characters of casting operators than having to explicitly use type-specific static methods. |
I wouldn't mind having Also keep in mind that "helper" classes are only helpful if you know about them. Recent arrivals might not be quick to hunt for a Math class. I've had to point it out to several new recruits myself who expected Min/Max methods on the base type. It lends some credence to the practice of putting methods in the base type when all the arguments and the return type match that base type. That said, I don't necessarily think that we need to deprecate System.Math. I'd just dislike the code duplication of having the same method in two places (or the overhead of passing a call to a method on the base type). I don't seem to recall any Method redirecting functionality in .NET, which would be helpful for situations like this where we'd like a matching static signature in two separate places to point to the same IL. We could probably gear up for a great round of bike-shedding about proper architecture with this. I'm not personally opposed to the fleshing out of primitives. As an unrelated example, I've seen argument exceptions occur when calling generic extensions that expected to return integers ( |
@AdamsLair, thanks for the support. I agree that moving them out of a centralized static class is probably not the greatest of options. However, unless the CoreCLR/CoreFX team decides they are willing to break backwards compatibility, it seems that we will end up with at least two distinct locations for any additional math APIs. I think that, logically, it makes sense to provide these methods through the respective primitive types (as @ZigMeowNyan pointed out, many developers assume that they should exist there anyways). However, I also agree that in terms of refactoring, ease of use, etc.. It would make sense to also provide them through a centralized class (one that can be easily updated in the future without breaking backwards compatibility). Example: public struct Double
{
public static double Sqrt(double x);
}
public static class MathHelper
{
public static double Sqrt(double x)
{
return double.Sqrt(x);
}
public static float Sqrt(float x)
{
return float.Sqrt(x);
}
}
public struct Single
{
public static double Sqrt(double x);
} or similar to how public struct Double
{
public static double Sqrt(double x);
}
public static class MathHelper<T> where T : struct
{
[JitIntrinsic]
public static T Sqrt(T x)
{
object value = (object)(x);
if (typeof(T) == typeof(double))
{
value = double.Sqrt((double)(value));
}
else if (typeof(T) == typeof(float))
{
value = float.Sqrt((float)(value));
}
else
{
throw new NotSupportedException();
}
return (T)(value);
}
}
public struct Single
{
public static double Sqrt(double x);
} I have updated the speclet to include the current open question of: Should the new APIs be provided through the respective primitive types, through a centralized static class, or both? |
Also perhaps something like |
Slightly off-topic, but isn't .Net Core the opportunity to break things for once and remove old cruft? As far as I understood, it is a distinct framework similar to .Net, but not interchangeable at all anyway. Anyway! On Topic: With regards to backwards compatibility, I think introducing a generic Math class like you proposed might indeed be a very interesting idea.
... because this could also be used to provide generic operator access:
Which is itself an arguable feature, but as far as I'm concerned we don't currently have any way of using operators generically. If I currently want to define a custom Again, I do not want to propose to add generic operators to the core library - but if they would be to become a thing at some point, having a generic Math class would be a nice synergy and maybe (?) a good spot to add them. I'm a little troubled at the thought of introducing a generic math class though, mostly due to performance concerns. Is there any way to get this to be as fast as directly using Math? Also, should the class itself be generic, or rather its methods? |
It is not a distinct framework. Portable libraries on newer PCL profiles are supposed to work seamlessly on both .Net Framework and .Net Core, so they must be fully compatible at least for what is offered by these profiles, which does include System.Math. |
@AdamsLair The other issue with implementing a generic Math class or adding generic methods to the existing Math class is that, in existing code, calls such such as As for performance, the There doesn't seem to be an optimal solution, but it seems to be between three choices:
The second option (moving to the respective primitive types) still seems like a great idea, provided that option 1 or 3 is also chosen. However, at least in C#/VB, with only option 2 it would be possible to have code such as: using static System.Int32;
// ...
var a = 15;
var b = 42;
var c = Max(a, b); // Resolves to int System.Int32.Max(int, int)
// ... and refactoring would just be: using static System.Single;
// ...
var a = 15;
var b = 42;
var c = Max(a, b); // Resolves to float System.Single.Max(float, float)
// ... This would of course only be supported in a language where |
@GeirGrusom I think |
@tannergooding Very nice writeup of the current discussion state though! 👍 |
I think that functions as real first class citizen (like CLR level) would make it easier to avoid breaking changes in this scenarios. Unfortunately CoreFX is born "old" due to the backward compatibility required. That being said. I really like your proposal. I think that this kind of extensions should be easier to make for developers. I few video games and rendering based code we had to implement also float 16 algebra to be compatible with types on the GPU. Is the same silly things of WPF being built all around double when the underneath DX layer is then using float :) |
@AdamsLair Interestingly enough, the following works correctly: using static System.Double;
using static System.Int32;
using static System.Single;
//...
var x = Max(1, 2); // Resolves to: int System.Int32.Max(int, int)
var y = Max(1d, 2d); // Resolves to: double System.Double.Max(double, double)
var z = Max(1f, 2f); // Resolves to: float System.Single.Max(float, float)
//... It isn't the most optimal solution, and wouldn't work in languages that don't support It's too bad you can't redirect using static MyMath = System.Double;
using static MyMath = System.Int32;
using static MyMath = System.Single;
// Maybe shorten to: using static MyMath = System.Double, System.Int32, System.Single;
//...
var x = MyMath.Max(1, 2); // Resolves to: int System.Int32.Max(int, int)
var y = MyMath.Max(1d, 2d); // Resolves to: double System.Double.Max(double, double)
var z = MyMath.Max(1f, 2f); // Resolves to: float System.Single.Max(float, float)
//... Method redirection of some sort (either syntactic sugar, as above, or direct support by the CLI) would be great and could help with a good bit of these issues. |
@mellinoe, I have provided a pull request to cover these changes (see dotnet/coreclr#710). Please note that it is still a work-in-progress. |
Regarding the open question:
I don't think we'd want to add all these APIs to the core types. In other words, I think having a design that makes the APIs sit on top would be a requirement. |
Is there any chance this can move forward with an API Design Review? At the very least, I would like to expose single-precision equivalents to the existing double-precision functions (the ones in It would be very helpful (and would allow me to submit a PR against both CoreFX and CoreCLR) if I knew where to expose the new APIs. The bare minimum that would need to be exposed (so that the single-precision functions exposed are equivalent to the double-precision functions currently exposed) is: public static class BitConverter
{
public static float Int32BitsToSingle(int value);
public static int SingleToInt32Bits(float value) { return default(int); }
}
public static partial class Mathf
{
public const float PI = 3.14159265f;
public const float E = 2.71828183f;
public static float Abs(float x);
public static float Acos(float x);
public static float Asin(float x);
public static float Atan(float x);
public static float Atan2(float y, float x);
public static float Ceiling(float x);
public static float Cos(float x);
public static float Cosh(float x);
public static float Exp(float x);
public static float Floor(float x);
public static float IEEERemainder(float x, float y);
public static float Log(float x);
public static float Log(float x, float y);
public static float Log10(float x);
public static float Max(float x, float y);
public static float Min(float x, float y);
public static float Pow(float x, float y);
public static float Round(float x);
public static float Round(float x, int digits);
public static float Round(float x, int digits, System.MidpointRounding mode);
public static float Round(float x, System.MidpointRounding mode);
public static int Sign(float x);
public static float Sin(float x);
public static float Sinh(float x);
public static float Sqrt(float x);
public static float Tan(float x);
public static float Tanh(float x);
public static float Truncate(float x);
} |
I love this proposal. |
Please edit the first post with final proposal. If there's agreement, @mellinoe can bring it for API review. |
@karelz, How specific does the 'final' proposal need to be? There are, essentially, four separate pieces to the PR:
Would it help to break the APIs out into four separate categories for the proposal? Additionally, is it sufficient to say that a particular API should be provided, or does it need to have an explicit suggested location (which is where the discussion will likely trend towards, since we can't add to |
I think it would make sense to separate out the first bullet from the rest (a la the issue title). Not to say that the latter 3 aren't important; I think we should do those, too. It will be easier to reason about and deliver if we can focus on the first bullet by itself, though.
It's something we need to decide, but I think we can proceed with a design review if we have a couple of options to choose from. I think the part that is ready for review would be this (copy-pasted from a bit above):
|
@mellinoe, thanks! I'll work on updating the post with a 'final' proposal sometime later tonight. |
@mellinoe, I have updated the original post to be more succinct. It now has:
The proposed API has been trimmed down to only provide feature-parity for the existing double-precision math functions The perf numbers are very basic, only cover a single machine configuration, and do not have the VM layer fully implemented (that is intrinsic support would still need to be properly implemented). Additionally, there is a link to my PR which implements the 'feature-parity' functionality: dotnet/coreclr#5492 (it looks like I have two conflicts which need cleaning up). |
Please let me know if you feel the original post should be modified further. |
Just realized but since the ECMA standard says that the CLR VM only has one internal floating point type (I.12.3.1) I'm not sure how portable this idea might be (since on some platforms it might really just be calling the float64 versions for example) cc: @CarolEidt |
@SamuelEnglard, Even if the target platform/architecture only has a single floating point format (say 80-bits, as was the case with the x87 FPU), the user still gets the benefit of code that is easier to write/maintain. They just won't get the performance benefit that other platforms/architectures will receive. This is really very similar to the |
@tannergooding I'm not saying don't do, just more that it should be noted (just like how it is with |
The methods themselves look fine and the performance improvements seem to be well worth it. Thanks a ton for that! We discussed whether we want to introduce a separate type and if so how we should name it. Due to source compat, we can't introduce the methods on We think the best approach is a new type parallel to Math suffixed with an indicator for the type. We should follow the existing patter for public static class MathF |
Thanks. I will work on updating my PR to match the approved API. The additional work still required in the PR will be to ensure:
|
Writing the performance tests is dependent on the @jkotas mentioned it might be good to break apart the API additions and the intrinsic support into separate PRs. Please let me know if this is not desired and I will start adding this work to the current PR, otherwise I will log a separate issue to track this work and assign it to myself I am currently working on porting the existing There are some unit tests in There are various places in CoreFX and CoreCLR (it looks like |
The core change on the CoreCLR side has gone in, so I think we can close this after the CoreFX side goes in. There are three bugs tracking the finer implementation details still needed (dotnet/coreclr#7689, dotnet/coreclr#7690, and dotnet/coreclr#7691). |
Great, thanks! Keep us posted on the progress. BTW: It seems that you forgot to update the proposal on the top to |
Fixed. |
The CoreFX side has gone in as well now. I will continue working on the finer implementation details in my spare time. The additional work being:
Additionally, it may be useful to review any existing code that currently does |
Instead of adding |
@CodesInChaos I dream of the day a compiler can use a solver to optimize things like trig functions to reduce them through symbolic logic to simpler/faster forms. Like sin(x) * sin(x) - cos(x)_cos(x) = -cos(2_x). |
The optimization I'm talking about is much easier to find than yours. It's essentially a form of common sub-expression elimination, and thus similar to existing optimizations. Apart from the difficulty of finding them, optimizations like yours are often invalid for floats even when they're valid for real numbers. Avoiding degraded precision and handling all special cases correctly (NaN, infinities, signed zeros, etc.). |
@SunnyWar In theory analyzers could do that. Some do things like (though much simpler) |
It doesn't but it would have the advantage of working retroactively. They have had 14 years to implement that seemingly simple optimization though, so I wouldn't hold my breath. An intrinsic is the next best thing, and maybe it has less of a JIT performance impact, but I wouldn't know about that. |
CoreFX fixed in dotnet/corefx#12183, closing. |
Rationale
The .NET framework does not currently provide scalar single-precision floating-point support for many of the trigonometric, logarithmic, and other common mathematical functions.
Single-precision floating-point support should be provided for these mathematical functions in order to better interop with high-performance, scientific, and multimedia-based applications where single-precision floating-points are the only implementation required and/or used.
Since adding these methods to the existing
System.Math
class would break backwards compatibility, they should be provided through a separate class that provides the same and/or similar functionality.Providing these new APIs would:
System.Numerics.Vector
structs)Proposed API
The proposed API here provides feature-parity with the existing double-precision math functions provided by the framework.
System.BitConverter
System.Single
Example Usage
Currently to calculate the Tangent for each member of the
System.Numerics.Vector4
struct, you currently have to call the double-precision version of the method and cast to the result to a single-precision value:With the proposed changes, this would now be simplified to the following:
The
System.Numerics
library itself is filled with similar examples as are various bits of code in the CoreFX and CoreCLR repositories.Perf Numbers
All performance tests are implemented as follows:
Total Time
Average Time
The execution time below is the
Total Time
for all 100,000 iterations, measured in seconds.Hardware: Desktop w/ 3.7GHz Quad-Core A10-7850K (AMD) and 16GB RAM
I believe some extra perf will be squeezed out when the intrinsics (such as
CORINFO_INTRINSIC_Sqrt
) are properly implemented in the VM layer for single-precision values. Without such functionality, it falls back to the double-precision functionality (extra precision, reduced performance) for certain calls.Pull Request
There is a sample pull request covering these changes available: dotnet/coreclr#5492
Additional Details
This will require several changes in the CoreCLR as well to support the new APIs via FCALLs and Intrinsics.
The text was updated successfully, but these errors were encountered: