Skip first iteration check of stop condition

Can unroll the loop, but would look more ugly :)
JIT will probably do it for us.

Fix <= to < for IVs
Inline temp var (start is unused after assigning to low)
This commit is contained in:
Kurt 2022-10-16 20:10:32 -07:00
parent 2a190dd6b3
commit 25b217c53d
2 changed files with 12 additions and 13 deletions

View File

@ -43,16 +43,16 @@ public static int GetSeedsIVs(Span<uint> result, uint hp, uint atk, uint def, ui
public static int GetSeeds(Span<uint> result, uint first, uint second)
{
var diff = (second - (first * LCRNG.Mult)) >> 16;
var start = ((((diff * LCRNG_MOD) + LCRNG_INC) >> 16) * LCRNG_PAT) % LCRNG_MOD;
var low = ((((diff * LCRNG_MOD) + LCRNG_INC) >> 16) * LCRNG_PAT) % LCRNG_MOD;
int ctr = 0;
// at most 3 iterations
for (var low = start; low < 0x1_0000; low += LCRNG_MOD)
do
{
var seed = first | low;
if ((LCRNG.Next(seed) & 0xffff0000) == second)
result[ctr++] = LCRNG.Prev(seed);
}
} while ((low += LCRNG_MOD) < 0x1_0000);
return ctr;
}
@ -75,10 +75,10 @@ public static int GetSeedsIVs(Span<uint> result, uint first, uint second)
return ctr;
}
private static void AddSeeds(Span<uint> result, uint start, uint first, uint second, ref int ctr)
private static void AddSeeds(Span<uint> result, uint low, uint first, uint second, ref int ctr)
{
// at most 3 iterations
for (var low = start; low <= 0x1_0000; low += LCRNG_MOD)
do
{
var test = first | low;
if ((LCRNG.Next(test) & 0x7fff0000) != second)
@ -86,6 +86,6 @@ private static void AddSeeds(Span<uint> result, uint start, uint first, uint sec
var seed = LCRNG.Prev(test);
result[ctr++] = seed;
result[ctr++] = seed ^ 0x80000000;
}
} while ((low += LCRNG_MOD) < 0x1_0000);
}
}

View File

@ -43,16 +43,16 @@ public static int GetSeedsIVs(Span<uint> result, uint hp, uint atk, uint def, ui
public static int GetSeeds(Span<uint> result, uint first, uint third)
{
var diff = (third - LCRNG.Next2(first)) >> 16;
var start = ((((diff * LCRNG_MOD_2) + LCRNG_INC_2) >> 16) * LCRNG_PAT_2) % LCRNG_MOD_2;
var low = ((((diff * LCRNG_MOD_2) + LCRNG_INC_2) >> 16) * LCRNG_PAT_2) % LCRNG_MOD_2;
int ctr = 0;
// at most 5 iterations
for (var low = start; low < 0x1_0000; low += LCRNG_MOD_2)
do
{
var seed = first | low;
if ((LCRNG.Next2(seed) & 0xffff0000) == third)
result[ctr++] = LCRNG.Prev(seed);
}
} while ((low += LCRNG_MOD_2) < 0x1_0000);
return ctr;
}
@ -75,18 +75,17 @@ public static int GetSeedsIVs(Span<uint> result, uint first, uint third)
return ctr;
}
private static void AddSeeds(Span<uint> result, uint start, uint first, uint second, ref int ctr)
private static void AddSeeds(Span<uint> result, uint low, uint first, uint second, ref int ctr)
{
// at most 5 iterations
for (var low = start; low <= 0x1_0000; low += LCRNG_MOD_2)
do
{
var test = first | low;
if ((LCRNG.Next2(test) & 0x7fff0000) != second)
continue;
var seed = LCRNG.Prev(test);
result[ctr++] = seed;
result[ctr++] = seed ^ 0x80000000;
}
} while ((low += LCRNG_MOD_2) < 0x1_0000);
}
}