From 18a3f18e5abc6baaf90a3e92eb9c0e50477cba92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Thu, 25 Oct 2018 23:21:00 +0100 Subject: [PATCH] Work extracting System.Math MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace remaining System.Math methods with internal ones - Add friends declaration to make internals available to new system.math Signed-off-by: José Simões --- .../CoreLibrary.nfproj | 3 +- source/nanoFramework.CoreLibrary/Friends.cs | 8 ++ .../nanoFramework.CoreLibrary/System/Math.cs | 122 +----------------- .../System/String.cs | 2 +- .../System/Text/StringBuilder.cs | 14 +- 5 files changed, 22 insertions(+), 127 deletions(-) create mode 100644 source/nanoFramework.CoreLibrary/Friends.cs diff --git a/source/nanoFramework.CoreLibrary/CoreLibrary.nfproj b/source/nanoFramework.CoreLibrary/CoreLibrary.nfproj index 5ba082e4..bc856d7c 100644 --- a/source/nanoFramework.CoreLibrary/CoreLibrary.nfproj +++ b/source/nanoFramework.CoreLibrary/CoreLibrary.nfproj @@ -45,6 +45,7 @@ + @@ -379,4 +380,4 @@ - + \ No newline at end of file diff --git a/source/nanoFramework.CoreLibrary/Friends.cs b/source/nanoFramework.CoreLibrary/Friends.cs new file mode 100644 index 00000000..2f8c5f06 --- /dev/null +++ b/source/nanoFramework.CoreLibrary/Friends.cs @@ -0,0 +1,8 @@ +// +// Copyright (c) 2017 The nanoFramework project contributors +// See LICENSE file in the project root for full license information. +// + +using System.Runtime.CompilerServices; + +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Math, PublicKey=00240000048000009400000006020000002400005253413100040000010001001120aa3e809b3da4f65e1b1f65c0a3a1bf6335c39860ca41acb3c48de278c6b63c5df38239ec1f2e32d58cb897c8c174a5f8e78a9c0b6087d3aef373d7d0f3d9be67700fc2a5a38de1fb71b5b6f6046d841ff35abee2e0b0840a6291a312be184eb311baff5fef0ff6895b9a5f2253aed32fb06b819134f6bb9d531488a87ea2")] diff --git a/source/nanoFramework.CoreLibrary/System/Math.cs b/source/nanoFramework.CoreLibrary/System/Math.cs index bf707f3e..7c38ab2d 100644 --- a/source/nanoFramework.CoreLibrary/System/Math.cs +++ b/source/nanoFramework.CoreLibrary/System/Math.cs @@ -8,129 +8,15 @@ namespace System { using Runtime.CompilerServices; - /// - /// Provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions. - /// - /// - /// Specific for nanoFramework: this class library is split between mscorlib and it's own assembly. - /// If you require any of the other methods that are not available here add the NuGet package nanoFramework.System.Math. - /// - public static partial class Math + internal static class MathInternal { - /// - /// Represents the ratio of the circumference of a circle to its diameter, specified by the constant, π. - /// - /// The value of this field is 3.14159265358979323846. - public const double PI = 3.14159265358979323846; - /// - /// Represents the natural logarithmic base, specified by the constant, e. - /// - /// The value of this field is 2.7182818284590452354. - public const double E = 2.7182818284590452354; - - /// - /// Returns the absolute value of a 32-bit signed integer. - /// - /// A number that is greater than Int32..::..MinValue, but less than or equal to Int32..::..MaxValue. - /// A 32-bit signed integer, x, such that 0 ≤ x ≤ Int32..::..MaxValue. - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern int Abs(int val); - - /// - /// Returns the absolute value of a double-precision floating-point number. - /// - /// A number that is greater than or equal to Double..::..MinValue, but less than or equal to Double..::..MaxValue. - /// A double-precision floating-point number, x, such that 0 ≤ x ≤ Double..::..MaxValue. - /// - /// This method with double-precision floating-point parameter might not be available in all platforms or with firmware images that where build with single point FPU option. - /// - /// - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern double Abs(double val); - - /// - /// Returns the absolute value of a single-precision floating-point number. - /// - /// A number that is greater than or equal to Double..::..MinValue, but less than or equal to Double..::..MaxValue. - /// A single-precision floating-point number, x, such that 0 ≤ x ≤ Double..::..MaxValue. - /// - /// This method with single-precision floating-point parameter is exclusive of nanoFramework. It doesn't exist in the .NET API, only the double-precision floating-point version. - /// It might not be available in all platforms or with firmware images that where build with double point FPU option. - /// - /// - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern float Abs(float val); - - /// - /// Returns the larger of two 32-bit signed integers. - /// - /// The first of two 32-bit signed integers to compare. - /// The second of two 32-bit signed integers to compare. - /// Parameter val1 or val2, whichever is larger. - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern int Max(int val1, int val2); - - /// - /// Returns the larger of two double-precision floating-point numbers. - /// - /// The first of two double-precision floating-point numbers to compare. - /// The second of two double-precision floating-point numbers to compare. - /// Parameter x or y, whichever is larger. If x, y, or both x and y are equal to NaN, NaN is returned. - /// - /// This method with double-precision floating-point parameter might not be available in all platforms or with firmware images that where build with single point FPU option. - /// - /// [MethodImpl(MethodImplOptions.InternalCall)] - public static extern double Max(double x, double y); - - /// - /// Returns the larger of two single-precision floating-point numbers. - /// - /// The first of two single-precision floating-point numbers to compare. - /// The second of two single-precision floating-point numbers to compare. - /// Parameter x or y, whichever is larger. If x, y, or both x and y are equal to NaN, NaN is returned. - /// - /// This method with single-precision floating-point parameter is exclusive of nanoFramework. It doesn't exist in the .NET API, only the double-precision floating-point version. - /// It might not be available in all platforms or with firmware images that where build with double point FPU option. - /// - /// - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern float Max(float x, float y); + internal static extern int Abs(int val); - /// - /// Returns the smaller of two 32-bit signed integers. - /// - /// The first of two 32-bit signed integers to compare. - /// The second of two 32-bit signed integers to compare. - /// Parameter val1 or val2, whichever is smaller. [MethodImpl(MethodImplOptions.InternalCall)] - public static extern int Min(int val1, int val2); + internal static extern int Min(int val1, int val2); - /// - /// Returns the smaller of two double-precision floating-point numbers. - /// - /// The first of two double-precision floating-point numbers to compare. - /// The second of two double-precision floating-point numbers to compare. - /// Parameter x or y, whichever is smaller. If x, y, or both x and y are equal to NaN, NaN is returned. - /// - /// This method with double-precision floating-point parameter might not be available in all platforms or with firmware images that where build with single point FPU option. - /// - /// - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern double Min(double x, double y); - /// - /// Returns the smaller of two single-precision floating-point numbers. - /// - /// The first of two single-precision floating-point numbers to compare. - /// The second of two single-precision floating-point numbers to compare. - /// Parameter x or y, whichever is smaller. If x, y, or both x and y are equal to NaN, NaN is returned. - /// - /// This method with single-precision floating-point parameter is exclusive of nanoFramework. It doesn't exist in the .NET API, only the double-precision floating-point version. - /// It might not be available in all platforms or with firmware images that where build with double point FPU option. - /// - /// [MethodImpl(MethodImplOptions.InternalCall)] - public static extern float Min(float x, float y); - + internal static extern int Max(int val1, int val2); } } diff --git a/source/nanoFramework.CoreLibrary/System/String.cs b/source/nanoFramework.CoreLibrary/System/String.cs index 92b4d212..ab5579ec 100644 --- a/source/nanoFramework.CoreLibrary/System/String.cs +++ b/source/nanoFramework.CoreLibrary/System/String.cs @@ -701,7 +701,7 @@ public static string Format(string format, params object[] args) } else if (alignment < 0) { - output += token.PadRight(Math.Abs(alignment)); + output += token.PadRight(MathInternal.Abs(alignment)); } else { diff --git a/source/nanoFramework.CoreLibrary/System/Text/StringBuilder.cs b/source/nanoFramework.CoreLibrary/System/Text/StringBuilder.cs index 1bd9fdbb..ae4adb71 100644 --- a/source/nanoFramework.CoreLibrary/System/Text/StringBuilder.cs +++ b/source/nanoFramework.CoreLibrary/System/Text/StringBuilder.cs @@ -222,7 +222,7 @@ public StringBuilder(int capacity, int maxCapacity) if (capacity > maxCapacity) throw new ArgumentOutOfRangeException("capacity"); if (maxCapacity < 1) throw new ArgumentOutOfRangeException("maxCapacity"); if (capacity < 0) throw new ArgumentOutOfRangeException("capacity"); - if (capacity == 0) capacity = Math.Min(0x10, maxCapacity); + if (capacity == 0) capacity = MathInternal.Min(0x10, maxCapacity); _maxCapacity = maxCapacity; _chunkChars = new char[capacity]; } @@ -711,8 +711,8 @@ public StringBuilder Replace(char oldChar, char newChar, int startIndex, int cou var num4 = startIndex - chunkPrevious._chunkOffset; if (num3 >= 0) { - var index = Math.Max(num4, 0); - var num6 = Math.Min(chunkPrevious._chunkLength, num3); + var index = MathInternal.Max(num4, 0); + var num6 = MathInternal.Min(chunkPrevious._chunkLength, num3); while (index < num6) { if (chunkPrevious._chunkChars[index] == oldChar) @@ -940,7 +940,7 @@ private void ReplaceInPlaceAtChunk(ref StringBuilder chunk, ref int indexInChunk while (true) { //int num = chunk.m_ChunkLength - indexInChunk; - var length = Math.Min(chunk._chunkLength - indexInChunk, count); + var length = MathInternal.Min(chunk._chunkLength - indexInChunk, count); //ThreadSafeCopy(value, ref valueIndex, chunk.m_ChunkChars, ref indexInChunk, num2); Array.Copy(value, valueIndex, chunk._chunkChars, indexInChunk, length); indexInChunk += length; @@ -977,9 +977,9 @@ internal void MakeRoom(int index, int count, out StringBuilder chunk, out int in } else { - var builder = new StringBuilder(Math.Max(count, 0x10), chunk._maxCapacity, chunk._chunkPrevious); + var builder = new StringBuilder(MathInternal.Max(count, 0x10), chunk._maxCapacity, chunk._chunkPrevious); builder._chunkLength = count; - var length = Math.Min(count, indexInChunk); + var length = MathInternal.Min(count, indexInChunk); if (length > 0) { Array.Copy(chunk._chunkChars, 0, builder._chunkChars, 0, length); @@ -1016,7 +1016,7 @@ internal void AppendHelper(ref string value) internal void ExpandByABlock(int minBlockCharCount) { if (minBlockCharCount + Length > _maxCapacity) throw new ArgumentOutOfRangeException("requiredLength"); - var num = Math.Max(minBlockCharCount, Math.Min(Length, 0x1f40)); + var num = MathInternal.Max(minBlockCharCount, MathInternal.Min(Length, 0x1f40)); _chunkPrevious = new StringBuilder(this); _chunkOffset += _chunkLength; _chunkLength = 0;