Skip to content

Commit 54ed2b3

Browse files
committed
Don't overflow when converting types.
1 parent 88c3c2b commit 54ed2b3

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

jemalloc.Api/GenericMath.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace jemalloc
66
{
7-
public static class GM<TData> where TData : struct, IEquatable<TData>
7+
public static class GM<TData> where TData : struct, IEquatable<TData>, IComparable<TData>, IConvertible
88
{
99
#region Constructor
1010
static GM()
@@ -24,7 +24,7 @@ static GM()
2424
#region Methods
2525
public static TData Const<TValue>(TValue v) where TValue : struct, IEquatable<TValue>, IComparable<TValue>
2626
{
27-
return JemUtil.ValToGenericStruct<TValue, TData>((checked(v)));
27+
return (TData) Convert.ChangeType(v, typeof(TData));
2828
}
2929

3030
public static TData Multiply(TData l, TData r)
@@ -33,34 +33,34 @@ public static TData Multiply(TData l, TData r)
3333
switch (value)
3434
{
3535
case Tuple<Byte, Byte> v:
36-
return JemUtil.ValToGenericStruct<Byte, TData>(checked((byte)(v.Item1 * v.Item2)));
36+
return (TData)Convert.ChangeType(checked((byte)(v.Item1 * v.Item2)), typeof(TData));
3737

3838
case Tuple<SByte, SByte> v:
39-
return JemUtil.ValToGenericStruct<SByte, TData>(checked((SByte)(v.Item1 * v.Item2)));
39+
return (TData)Convert.ChangeType(checked((SByte)(v.Item1 * v.Item2)), typeof(TData));
4040

4141
case Tuple<UInt16, UInt16> v:
42-
return JemUtil.ValToGenericStruct<UInt16, TData>(checked((UInt16)(v.Item1 * v.Item2)));
42+
return (TData)Convert.ChangeType((checked((UInt16)(v.Item1 * v.Item2))), typeof(TData));
4343

4444
case Tuple<Int16, Int16> v:
45-
return JemUtil.ValToGenericStruct<Int16, TData>(checked((Int16)(v.Item1 * v.Item2)));
45+
return (TData)Convert.ChangeType(checked((Int16)(v.Item1 * v.Item2)), typeof(TData));
4646

4747
case Tuple<UInt32, UInt32> v:
48-
return JemUtil.ValToGenericStruct<UInt32, TData>(checked(v.Item1 * v.Item2));
48+
return (TData)Convert.ChangeType(checked(v.Item1 * v.Item2), typeof(TData));
4949

5050
case Tuple<Int32, Int32> v:
51-
return JemUtil.ValToGenericStruct<Int32, TData>(checked(v.Item1 * v.Item2));
51+
return (TData)Convert.ChangeType(checked(v.Item1 * v.Item2), typeof(TData));
5252

5353
case Tuple<UInt64, UInt64> v:
54-
return JemUtil.ValToGenericStruct<UInt64, TData>(checked(v.Item1 * v.Item2));
54+
return (TData)Convert.ChangeType(checked(v.Item1 * v.Item2), typeof(TData));
5555

5656
case Tuple<Int64, Int64> v:
57-
return JemUtil.ValToGenericStruct<Int64, TData>(checked(v.Item1 * v.Item2));
57+
return (TData)Convert.ChangeType(checked(v.Item1 * v.Item2), typeof(TData));
5858

5959
case Tuple<Single, Single> v:
60-
return JemUtil.ValToGenericStruct<Single, TData>(checked(v.Item1 * v.Item2));
60+
return (TData)Convert.ChangeType(checked(v.Item1 * v.Item2), typeof(TData));
6161

6262
case Tuple<Double, Double> v:
63-
return JemUtil.ValToGenericStruct<Double, TData>(checked(v.Item1 * v.Item2));
63+
return (TData)Convert.ChangeType(checked(v.Item1 * v.Item2), typeof(TData));
6464
default:
6565
throw new Exception($"Unsupported type: {typeof(TData).Name}");
6666
}

jemalloc.Benchmarks/Benchmarks/HugeNativeVsManagedArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace jemalloc.Benchmarks
88
{
99
[JemBenchmarkJob(BenchmarkDotNet.Engines.RunStrategy.ColdStart, 7)]
10-
public class HugeNativeVsManagedArrayBenchmark<T> : JemBenchmark<T, ulong> where T : struct, IEquatable<T>
10+
public class HugeNativeVsManagedArrayBenchmark<T> : JemBenchmark<T, ulong> where T : struct, IEquatable<T>, IComparable<T>, IConvertible
1111
{
1212
public ulong ArraySize => Parameter;
1313

jemalloc.Benchmarks/Benchmarks/MallocVsArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace jemalloc.Benchmarks
88
{
9-
public class MallocVsArrayBenchmark<T> : JemBenchmark<T, int> where T : struct, IEquatable<T>
9+
public class MallocVsArrayBenchmark<T> : JemBenchmark<T, int> where T : struct, IEquatable<T>, IComparable<T>, IConvertible
1010
{
1111
public int ArraySize => Parameter;
1212

jemalloc.Benchmarks/Benchmarks/NativeVsManagedArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace jemalloc.Benchmarks
1010
{
1111
[OrderProvider(methodOrderPolicy: MethodOrderPolicy.Declared)]
12-
public class NativeVsManagedArrayBenchmark<T> : JemBenchmark<T, int> where T : struct, IEquatable<T>
12+
public class NativeVsManagedArrayBenchmark<T> : JemBenchmark<T, int> where T : struct, IEquatable<T>, IComparable<T>, IConvertible
1313
{
1414
public int ArraySize => Parameter;
1515

jemalloc.Benchmarks/JemBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace jemalloc.Benchmarks
1919
{
2020
[JemBenchmarkJob]
2121
[MemoryDiagnoser]
22-
public abstract class JemBenchmark<TData, TParam> where TData : struct, IEquatable<TData> where TParam : struct
22+
public abstract class JemBenchmark<TData, TParam> where TData : struct, IEquatable<TData>, IComparable<TData>, IConvertible where TParam : struct
2323
{
2424
#region Constructors
2525
static JemBenchmark()

jemalloc.Cli/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static void Benchmark(Options o)
275275
Exit(ExitResult.INVALID_OPTIONS);
276276
}
277277
}
278-
static void Benchmark<T>() where T : struct, IEquatable<T>
278+
static void Benchmark<T>() where T : struct, IEquatable<T>, IComparable<T>, IConvertible
279279
{
280280
Contract.Requires(BenchmarkOptions.ContainsKey("Category"));
281281
Contract.Requires(BenchmarkOptions.ContainsKey("Operation"));

0 commit comments

Comments
 (0)