Skip to content
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.MathF] Bring support for the single-precission Math operations to Mono #7941

Merged
merged 8 commits into from Apr 5, 2018

Conversation

migueldeicaza
Copy link
Contributor

This pull request brings support to Mono's class libraries for the new System.MathF type which includes operations for single-floating point.

This is needed to run some new applications like:

https://github.com/aras-p/ToyPathTracer

With this change, it is possible to run this unmodified. With this support, and the -O=float32 option, it is possible to improve the performance from 6.3Mrays/sec to 7.8Mrays/sec on a Mac.

Oddly, this seems to be faster than .NET Core 2.1.4, which is at 4.2Mrays/sec.

#if MONO
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte Clamp(byte value, byte min, byte max)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use K&R indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copy pasted this from CoreCLR, I rather not change the style of the copy-pasted code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tentative on this, would much rather see directly used ../external/corefx/src/Common/src/CoreLib/System/Math.cs but we could do it as follow up change

@@ -316,3 +316,142 @@ ves_icall_System_Math_SplitFractionDouble (gdouble *v)
{
return modf (*v, v);
}

gfloat
ves_icall_System_MathF_Acos (gfloat x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be just float, we don't use gfloat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do in that file, just a couple of lines above ;-)

I dont mind updating.

@@ -439,6 +439,31 @@ ICALL(MATH_16, "Sqrt", ves_icall_System_Math_Sqrt)
ICALL(MATH_17, "Tan", ves_icall_System_Math_Tan)
ICALL(MATH_18, "Tanh", ves_icall_System_Math_Tanh)

ICALL_TYPE(MATHF, "System.MathF", MATHF_1)
ICALL(MATHF_1, "Acos", ves_icall_System_MathF_Acos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be HANDLES(..) I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many but not all lines icall-def.h have HANDLES() around them.
This is how icalls get converted to be coop-friendly.

  1. passes/returns coop handles instead of raw pointers
  2. passes an extra, initialized MonoError*
  3. Check the error at the end.
    Eventually every line should either have HANDLES or some comment or other macro explaining why not -- for example, maybe the only one, if no pointers and no errors and performance sensitive, that combination does not want HANDLES.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vargaz wouldn't HANDLE'izing those functions produce a net loss of perf for something they don't need? IE, none raise exceptions or take ref pointers.

ICALL(MATHF_13, "Floor", ves_icall_System_MathF_Floor)
ICALL(MATHF_14, "Log", ves_icall_System_MathF_Log)
ICALL(MATHF_15, "Log10", ves_icall_System_MathF_Log10)
ICALL(MATHF_23, "ModF(float,float&)", ves_icall_System_MathF_ModF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signature doesn't match the managed signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed my mind, I kept the .NET signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short version: done

}

float
ves_icall_System_MathF_ModF (float x, float *d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This icall must null check d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to be ref, do we still need the check in that case?

CoreCLR does not have that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If managed was using a byref instead of a pointer, we won't need it as we emit caller side checks. That doesn't happen with pointers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's it's only used by Truncate and Round with locals, so it's just lousy code on their end but we should be safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check, just for sanity's sake.

@migueldeicaza
Copy link
Contributor Author

Mhm, the build breaks elsewhere with a series of errors like this:

/Users/builder/jenkins/workspace/test-mono-pull-request-amd64-osx/external/corefx/src/System.Numerics.Vectors/src/System/Numerics/Matrix3x2.cs(281,26): error CS0436: The type 'MathF' in '/Users/builder/jenkins/workspace/test-mono-pull-request-amd64-osx/mcs/class/System.Numerics/../../../external/corefx/src/Common/src/System/MathF.netstandard.cs' conflicts with the imported type 'MathF' in 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Using the type defined in '/Users/builder/jenkins/workspace/test-mono-pull-request-amd64-osx/mcs/class/System.Numerics/../../../external/corefx/src/Common/src/System/MathF.netstandard.cs'.

@luhenry
Copy link
Contributor

luhenry commented Apr 3, 2018

@monojenkins build Linux x64

@luhenry
Copy link
Contributor

luhenry commented Apr 3, 2018

@monojenkins build Linux AArch64 Interpreter

@luhenry
Copy link
Contributor

luhenry commented Apr 3, 2018

@akoeplinger @marek-safar can you please check the API Diff

@akoeplinger
Copy link
Member

The API diff looks fine to me.

Copy link
Member

@marek-safar marek-safar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add external/corefx/src/System.Runtime.Extensions/tests/System/MathTests.netcoreapp.cs to corlib_xtest.dll.sources to add tests which test the new APIs

{
public static partial class MathF
{
[MethodImpl(MethodImplOptions.InternalCall)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to follow Mono coding style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather not for code that is copy pasted, makes it simpler to track changes.

@@ -978,6 +978,8 @@ ReferenceSources/Type.cs
../referencesource/mscorlib/system/invalidtimezoneexception.cs
../referencesource/mscorlib/system/Lazy.cs
../referencesource/mscorlib/system/math.cs
../../../external/corert//src/System.Private.CoreLib/shared/System/MathF.cs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use version from ../../../external/corefx/src/Common/src/CoreLib/System/MathF.cs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

#if MONO
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte Clamp(byte value, byte min, byte max)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tentative on this, would much rather see directly used ../external/corefx/src/Common/src/CoreLib/System/Math.cs but we could do it as follow up change

@marek-safar
Copy link
Member

The System.Runtime.Extensions change for a desktop could be problematic but let's see if anyone hits it

@marek-safar
Copy link
Member

@monojenkins commit apidiff

monojenkins added a commit to mono/api-snapshot that referenced this pull request Apr 5, 2018
@akoeplinger akoeplinger merged commit 52df0f2 into mono:master Apr 5, 2018
akoeplinger added a commit to akoeplinger/mono that referenced this pull request Apr 6, 2018
Noticed this while looking at Jenkins logs today:

```
15:00:56 /mnt/jenkins/workspace/test-mono-pull-request-amd64/mcs/class/lib/net_4_x/mscorlib.dll
15:00:56 cant resolve internal call to "System.MathF::ModF(single,single*)" (tested without signature also)
```

We need to use `single` in the icall def instead of `float`.

It was introduced by mono#7941
akoeplinger added a commit that referenced this pull request Apr 6, 2018
Noticed this while looking at Jenkins logs today:

```
15:00:56 /mnt/jenkins/workspace/test-mono-pull-request-amd64/mcs/class/lib/net_4_x/mscorlib.dll
15:00:56 cant resolve internal call to "System.MathF::ModF(single,single*)" (tested without signature also)
```

We need to use `single` in the icall def instead of `float`.

It was introduced by #7941
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…s to Mono (mono/mono#7941)

This pull request brings support to Mono's class libraries for the new `System.MathF` type which includes operations for single-floating point.   

This is needed to run some new applications like:

https://github.com/aras-p/ToyPathTracer

With this change, it is possible to run this unmodified.  With this support, and the `-O=float32` option, it is possible to improve the performance from 6.3Mrays/sec to 7.8Mrays/sec on a Mac.

Oddly, this seems to be faster than .NET Core 2.1.4, which is at 4.2Mrays/sec.

* Bump API snapshot submodule


Commit migrated from mono/mono@52df0f2
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Noticed this while looking at Jenkins logs today:

```
15:00:56 /mnt/jenkins/workspace/test-mono-pull-request-amd64/mcs/class/lib/net_4_x/mscorlib.dll
15:00:56 cant resolve internal call to "System.MathF::ModF(single,single*)" (tested without signature also)
```

We need to use `single` in the icall def instead of `float`.

It was introduced by mono/mono#7941

Commit migrated from mono/mono@d59246b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants