Skip to content

Commit 3cc6c40

Browse files
committed
Correct GM.Random* methods.
1 parent 54ed2b3 commit 3cc6c40

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed

jemalloc.Api/GenericMath.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,67 +130,77 @@ public static TData Random()
130130
switch (e)
131131
{
132132
case SByte v:
133-
return JemUtil.ValToGenericStruct<SByte, TData>(checked((sbyte)Rng.Next(0, SByte.MaxValue)));
133+
return Const(checked((sbyte)Rng.Next(0, SByte.MaxValue)));
134134
case Byte v:
135-
return JemUtil.ValToGenericStruct<Byte, TData>(checked((byte)Rng.Next(0, Byte.MaxValue)));
135+
return Const(checked((byte)Rng.Next(0, Byte.MaxValue)));
136136
case Int32 v:
137-
return JemUtil.ValToGenericStruct<Int32, TData>(checked((int)Rng.Next(0, Int32.MaxValue)));
137+
return Const(checked((int)Rng.Next(0, Int32.MaxValue)));
138138
case UInt32 v:
139-
return JemUtil.ValToGenericStruct<UInt32, TData>(checked((uint)Rng.Next(0, Int32.MaxValue)));
139+
return Const(checked((uint)Rng.Next(0, Int32.MaxValue)));
140140
case Int16 v:
141-
return JemUtil.ValToGenericStruct<Int16, TData>(checked((short)Rng.Next(0, Int16.MaxValue)));
141+
return Const(checked((short)Rng.Next(0, Int16.MaxValue)));
142142
case UInt16 v:
143-
return JemUtil.ValToGenericStruct<UInt16, TData>(checked((ushort)Rng.Next(0, UInt16.MaxValue)));
143+
return Const(checked((ushort)Rng.Next(0, UInt16.MaxValue)));
144144
case Int64 v:
145-
return JemUtil.ValToGenericStruct<Int64, TData>(checked((long)Rng.NextDouble() * Int64.MaxValue));
145+
return Const(checked((long)Rng.NextDouble() * Int64.MaxValue));
146146
case UInt64 v:
147-
return JemUtil.ValToGenericStruct<UInt64, TData>(checked((ulong)Rng.NextDouble() * UInt64.MaxValue));
147+
return Const(checked((ulong)Rng.NextDouble() * UInt64.MaxValue));
148+
case Single v:
149+
return Const(checked((Single)Rng.NextDouble() * Int64.MaxValue));
150+
case Double v:
151+
return Const(checked((double)Rng.NextDouble() * Int64.MaxValue));
152+
148153
default:
149154
throw new ArithmeticException();
150155
}
151156
}
152157

153-
public static Tuple<TData, TData> RandomMultiplyFactorAndValue()
158+
public static TData Random(double max)
159+
{
160+
return Const(checked((double)Rng.NextDouble() * max));
161+
}
162+
163+
public static (TData, TData) RandomMultiplyFactorAndValue()
154164
{
155165
TData e = default;
156166
TData max;
157-
int factor = Rng.Next(1, 4);
167+
int factor = Rng.Next(0, 4);
158168
switch (e)
159169
{
160170
case SByte v:
161-
max = JemUtil.ValToGenericStruct<sbyte, TData>( (sbyte.MaxValue / (sbyte) 4));
171+
max = Random((sbyte)(sbyte.MaxValue / 4));
162172
break;
163173
case Byte v:
164-
max = JemUtil.ValToGenericStruct<byte, TData>((byte)(byte.MaxValue / (byte) 4));
165-
break;
166-
case Int32 v:
167-
max = JemUtil.ValToGenericStruct<int, TData>((int)(int.MaxValue / 4));
168-
break;
169-
case UInt32 v:
170-
max = JemUtil.ValToGenericStruct<uint, TData>(uint.MaxValue / 4u);
174+
max = Random((byte)(byte.MaxValue / (byte) 4));
171175
break;
172176
case Int16 v:
173-
max = JemUtil.ValToGenericStruct<short, TData>((short)(short.MaxValue / (short) 4));
177+
max = Random((short)(short.MaxValue / (short)4));
174178
break;
175179
case UInt16 v:
176-
max = JemUtil.ValToGenericStruct<ushort, TData>((ushort)(ushort.MaxValue / (ushort) 4));
180+
max = Random((ushort)(ushort.MaxValue / (ushort)4));
177181
break;
178-
case Int64 v:
179-
max = JemUtil.ValToGenericStruct<long, TData>((long)(long.MaxValue / 4));
182+
case Int32 v:
183+
max = Random((int)(int.MaxValue / 4));
184+
break;
185+
case UInt32 v:
186+
max = Random(uint.MaxValue / 4u);
187+
break;
188+
case Int64 v:
189+
max = Random((long)(long.MaxValue / 4));
180190
break;
181191
case UInt64 v:
182-
max = JemUtil.ValToGenericStruct<ulong, TData>(ulong.MaxValue / 4u);
192+
max = Random(ulong.MaxValue / 4u);
183193
break;
184194
case Double v:
185-
max = JemUtil.ValToGenericStruct<double, TData>((double)(long.MaxValue / 4));
195+
max = Random((double)(long.MaxValue / 4));
186196
break;
187197
case Single v:
188-
max = JemUtil.ValToGenericStruct<Single, TData>((Single)(long.MaxValue / 4));
198+
max = Random((Single)(long.MaxValue / 4));
189199
break;
190200
default:
191201
throw new ArithmeticException();
192202
}
193-
return new Tuple<TData, TData>(JemUtil.ValToGenericStruct<int, TData>(factor), JemUtil.ValToGenericStruct<int, TData>(20));
203+
return (Const(factor), Const(max));
194204

195205
}
196206
#endregion

jemalloc.Benchmarks/Benchmarks/HugeNativeVsManagedArray.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public override void GlobalSetup()
1717
base.GlobalSetup();
1818
managedArray = new T[2146435071];
1919
nativeArray = new HugeArray<T>(ArraySize);
20-
fill = GetArrayFillValue();
21-
mul = GetArrayMulValue();
20+
(mul, fill) = GM<T>.RandomMultiplyFactorAndValue();
2221
Console.WriteLine("Managed array fill value is {0}.", fill);
2322
Console.WriteLine("Multiply factor is {0}", mul);
2423
}
@@ -42,7 +41,7 @@ public void CreateHugeNativeArray()
4241
[BenchmarkCategory("Fiill")]
4342
public void FillManagedArray()
4443
{
45-
T fill = GetArrayFillValue();
44+
T fill = GM<T>.Random();
4645
T[] someData = new T[2146435071];
4746
for (int i = 0; i < someData.Length; i++)
4847
{
@@ -57,7 +56,7 @@ public void FillManagedArray()
5756
[BenchmarkCategory("Fill")]
5857
public void FillHugeNativeArray()
5958
{
60-
T fill = GetArrayFillValue();
59+
T fill = GM<T>.Random();
6160
HugeArray<T> array = new HugeArray<T>(ArraySize);
6261
array.Fill(fill);
6362
T r = array[ArraySize / 2];

jemalloc.Benchmarks/Benchmarks/MallocVsArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void CreateManagedArray()
2323
[BenchmarkCategory("Fill")]
2424
public void FillManagedArray()
2525
{
26-
T fill = GetArrayFillValue();
26+
T fill = GM<T>.Random();
2727
T[] someData = new T[ArraySize];
2828
for (int i = 0; i < someData.Length; i++)
2929
{
@@ -49,7 +49,7 @@ public void CreateSpan()
4949
[BenchmarkCategory("Fill")]
5050
public void FillSpan()
5151
{
52-
T fill = GetArrayFillValue();
52+
T fill = GM<T>.Random();
5353
ulong msize = (ulong)(ArraySize * JemUtil.SizeOfStruct<T>());
5454
IntPtr ptr = Jem.Malloc(msize);
5555
Span<T> s = JemUtil.PtrToSpan<T>(ptr, ArraySize);

jemalloc.Benchmarks/Benchmarks/NativeVsManagedArray.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override void GlobalSetup()
2626
public void FillSetup()
2727
{
2828
InfoThis();
29-
T fill = GetArrayFillValue();
29+
T fill = GM<T>.Random();
3030
Info($"Array fill value is {fill}.");
3131
SetValue("fill", fill);
3232
SetValue("managedArray", new T[ArraySize]);
@@ -107,8 +107,7 @@ public void CleanupFillArray()
107107
public void ArithmeticMutiplyGlobalSetup()
108108
{
109109
InfoThis();
110-
T fill = GM<T>.Const(5);
111-
T mul = GM<T>.Const(20);
110+
(T mul, T fill) = GM<T>.RandomMultiplyFactorAndValue();
112111
SafeArray<T> na = new SafeArray<T>(ArraySize);
113112
T[] ma = new T[ArraySize];
114113
Info($"Array fill value is {fill}.");
@@ -118,7 +117,7 @@ public void ArithmeticMutiplyGlobalSetup()
118117
{
119118
ma[i] = fill;
120119
}
121-
120+
na.Acquire();
122121
SetValue("fill", fill);
123122
SetValue("mul", mul);
124123
SetValue("managedArray", ma);

jemalloc.Benchmarks/JemBenchmark.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ public void RemoveValue(string name, [CallerMemberName] string memberName = "",
123123
public static void InfoThis([CallerMemberName] string memberName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0) => Info("Executing {0}().", memberName);
124124
#endregion
125125

126-
public static TData GetArrayFillValue() => GM<TData>.Random();
127-
128-
public static TData GetArrayMulValue() => GM<TData>.Const(4);
129-
130-
public static TData GetArraySqrFillValue() => GM<TData>.Random();
131126
#endregion
132127
}
133128
}

0 commit comments

Comments
 (0)