2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Numerics / DLR-0.92-BigIntegerv2.patch
blob8da412a0cd146e41ec58cf5063f06f02590f4610
1 --- ./Runtime/Microsoft.Dynamic/Math/BigIntegerV2.cs 2009-10-19 13:36:38.000000000 -0400
2 +++ /cvs/mcs/class/System.Numerics/System.Numerics/BigInteger.cs 2009-10-31 12:13:55.000000000 -0400
3 @@ -12,7 +12,6 @@
6 * ***************************************************************************/
7 -#if CLR2
9 using System;
10 using System.Collections.Generic;
11 @@ -20,8 +19,6 @@
12 using System.Diagnostics.CodeAnalysis;
13 using System.Globalization;
14 using System.Text;
15 -using Microsoft.Contracts;
16 -using Microsoft.Scripting.Utils;
18 namespace Microsoft.Scripting.Math {
19 /// <summary>
20 @@ -104,7 +101,8 @@
21 /// (inverse of ToByteArray())
22 /// </summary>
23 public static BigInteger Create(byte[] v) {
24 - ContractUtils.RequiresNotNull(v, "v");
25 + if (v == null)
26 + throw new ArgumentNullException ("v");
27 if (v.Length == 0) return Create(0);
29 int byteCount = v.Length;
30 @@ -339,10 +337,13 @@
32 [CLSCompliant(false)]
33 public BigInteger(int sign, params uint[] data) {
34 - ContractUtils.RequiresNotNull(data, "data");
35 - ContractUtils.Requires(sign >= -1 && sign <= +1, "sign");
36 + if (data == null)
37 + throw new ArgumentNullException ("data");
38 + if (!(sign >= -1 && sign <= +1))
39 + throw new ArgumentException ("sign");
40 int length = GetLength(data);
41 - ContractUtils.Requires(length == 0 || sign != 0, "sign");
42 + if (!(length == 0 || sign != 0))
43 + throw new ArgumentException ("sign");
45 this.data = data;
46 this.sign = (short)(length == 0 ? 0 : sign);
47 @@ -507,7 +508,7 @@
50 public bool TryToFloat64(out double result) {
51 - return StringUtils.TryParseDouble(ToString(10),
52 + return double.TryParse(ToString(10),
53 System.Globalization.NumberStyles.Number,
54 System.Globalization.CultureInfo.InvariantCulture.NumberFormat,
55 out result);
56 @@ -1355,17 +1356,92 @@
57 return this * this;
60 - [Confined]
61 public override string ToString() {
62 return ToString(10);
65 - [Confined]
66 - public string ToString(int radix) {
67 - return MathUtils.BigIntegerToString(copy(data), sign, radix);
68 + // generated by scripts/radix_generator.py
69 + private static readonly uint[] maxCharsPerDigit = { 0, 0, 31, 20, 15, 13, 12, 11, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 };
70 + private static readonly uint[] groupRadixValues = { 0, 0, 2147483648, 3486784401, 1073741824, 1220703125, 2176782336, 1977326743, 1073741824, 3486784401, 1000000000, 2357947691, 429981696, 815730721, 1475789056, 2562890625, 268435456, 410338673, 612220032, 893871739, 1280000000, 1801088541, 2494357888, 3404825447, 191102976, 244140625, 308915776, 387420489, 481890304, 594823321, 729000000, 887503681, 1073741824, 1291467969, 1544804416, 1838265625, 2176782336 };
72 + public static ArgumentOutOfRangeException MakeArgumentOutOfRangeException(string paramName, object actualValue, string message) {
73 + return new ArgumentOutOfRangeException(paramName, string.Format("{0} (actual value is '{1}')", message, actualValue));
74 + }
76 + internal static string BigIntegerToString(uint[] d, int sign, int radix) {
77 + if (radix < 2) {
78 + throw MakeArgumentOutOfRangeException("radix", radix, "radix must be >= 2");
79 + }
80 + if (radix > 36) {
81 + throw MakeArgumentOutOfRangeException("radix", radix, "radix must be <= 36");
82 + }
84 + int dl = d.Length;
85 + if (dl == 0) {
86 + return "0";
87 + }
89 + List<uint> digitGroups = new List<uint>();
91 + uint groupRadix = groupRadixValues[radix];
92 + while (dl > 0) {
93 + uint rem = div(d, ref dl, groupRadix);
94 + digitGroups.Add(rem);
95 + }
97 + StringBuilder ret = new StringBuilder();
98 + if (sign == -1) {
99 + ret.Append("-");
102 + int digitIndex = digitGroups.Count - 1;
104 + char[] tmpDigits = new char[maxCharsPerDigit[radix]];
106 + AppendRadix((uint)digitGroups[digitIndex--], (uint)radix, tmpDigits, ret, false);
107 + while (digitIndex >= 0) {
108 + AppendRadix((uint)digitGroups[digitIndex--], (uint)radix, tmpDigits, ret, true);
110 + return ret.Length == 0 ? "0" : ret.ToString();
113 + private static uint div(uint[] n, ref int nl, uint d) {
114 + ulong rem = 0;
115 + int i = nl;
116 + bool seenNonZero = false;
117 + while (--i >= 0) {
118 + rem <<= BitsPerDigit;
119 + rem |= n[i];
120 + uint v = (uint)(rem / d);
121 + n[i] = v;
122 + if (v == 0) {
123 + if (!seenNonZero) nl--;
124 + } else {
125 + seenNonZero = true;
127 + rem %= d;
129 + return (uint)rem;
132 + private static void AppendRadix(uint rem, uint radix, char[] tmp, StringBuilder buf, bool leadingZeros) {
133 + const string symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
135 + int digits = tmp.Length;
136 + int i = digits;
137 + while (i > 0 && (leadingZeros || rem != 0)) {
138 + uint digit = rem % radix;
139 + rem /= radix;
140 + tmp[--i] = symbols[(int)digit];
142 + if (leadingZeros) buf.Append(tmp);
143 + else buf.Append(tmp, i, digits - i);
146 + public string ToString(int radix) {
147 + return BigIntegerToString(copy(data), sign, radix);
150 - [Confined]
151 public override int GetHashCode() {
152 // The Object.GetHashCode function needs to be consistent with the Object.Equals function.
153 // Languages that build on top of this may have a more flexible equality function and
154 @@ -1395,12 +1471,10 @@
158 - [Confined]
159 public override bool Equals(object obj) {
160 return Equals(obj as BigInteger);
163 - [StateIndependent]
164 public bool Equals(BigInteger other) {
165 if (object.ReferenceEquals(other, null)) return false;
166 return this == other;
167 @@ -1492,17 +1566,14 @@
169 #region IConvertible Members
171 - [Confined]
172 public TypeCode GetTypeCode() {
173 return TypeCode.Object;
176 - [Confined]
177 public bool ToBoolean(IFormatProvider provider) {
178 return this != Zero;
181 - [Confined]
182 public byte ToByte(IFormatProvider provider) {
183 uint ret;
184 if (AsUInt32(out ret) && (ret & ~0xFF) == 0) {
185 @@ -1561,7 +1632,6 @@
186 return trimmedBytes;
189 - [Confined]
190 public char ToChar(IFormatProvider provider) {
191 int ret;
192 if (AsInt32(out ret) && (ret <= Char.MaxValue) && (ret >= Char.MinValue)) {
193 @@ -1570,24 +1640,20 @@
194 throw new OverflowException("big integer won't fit into char");
197 - [Confined]
198 public DateTime ToDateTime(IFormatProvider provider) {
199 throw new NotImplementedException();
202 - [Confined]
203 public decimal ToDecimal(IFormatProvider provider) {
204 decimal ret;
205 if (AsDecimal(out ret)) return ret;
206 throw new OverflowException("big integer won't fit into decimal");
209 - [Confined]
210 public double ToDouble(IFormatProvider provider) {
211 return ToFloat64();
214 - [Confined]
215 public short ToInt16(IFormatProvider provider) {
216 int ret;
217 if (AsInt32(out ret) && (ret <= short.MaxValue) && (ret >= short.MinValue)) {
218 @@ -1596,7 +1662,6 @@
219 throw new OverflowException("big integer won't fit into short");
222 - [Confined]
223 public int ToInt32(IFormatProvider provider) {
224 int ret;
225 if (AsInt32(out ret)) {
226 @@ -1605,7 +1670,6 @@
227 throw new OverflowException("big integer won't fit into int");
230 - [Confined]
231 public long ToInt64(IFormatProvider provider) {
232 long ret;
233 if (AsInt64(out ret)) {
234 @@ -1614,7 +1678,7 @@
235 throw new OverflowException("big integer won't fit into long");
238 - [CLSCompliant(false), Confined]
239 + [CLSCompliant(false)]
240 public sbyte ToSByte(IFormatProvider provider) {
241 int ret;
242 if (AsInt32(out ret) && (ret <= sbyte.MaxValue) && (ret >= sbyte.MinValue)) {
243 @@ -1623,17 +1687,14 @@
244 throw new OverflowException("big integer won't fit into sbyte");
247 - [Confined]
248 public float ToSingle(IFormatProvider provider) {
249 return checked((float)ToDouble(provider));
252 - [Confined]
253 public string ToString(IFormatProvider provider) {
254 return ToString();
257 - [Confined]
258 public object ToType(Type conversionType, IFormatProvider provider) {
259 if (conversionType == typeof(BigInteger)) {
260 return this;
261 @@ -1641,7 +1702,7 @@
262 throw new NotImplementedException();
265 - [CLSCompliant(false), Confined]
266 + [CLSCompliant(false)]
267 public ushort ToUInt16(IFormatProvider provider) {
268 uint ret;
269 if (AsUInt32(out ret) && ret <= ushort.MaxValue) {
270 @@ -1650,7 +1711,7 @@
271 throw new OverflowException("big integer won't fit into ushort");
274 - [CLSCompliant(false), Confined]
275 + [CLSCompliant(false)]
276 public uint ToUInt32(IFormatProvider provider) {
277 uint ret;
278 if (AsUInt32(out ret)) {
279 @@ -1659,7 +1720,7 @@
280 throw new OverflowException("big integer won't fit into uint");
283 - [CLSCompliant(false), Confined]
284 + [CLSCompliant(false)]
285 public ulong ToUInt64(IFormatProvider provider) {
286 ulong ret;
287 if (AsUInt64(out ret)) {
288 @@ -1724,4 +1785,3 @@
289 #endregion
292 -#endif