[
https://issues.apache.org/jira/browse/RNG-169?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17508078#comment-17508078
]
Alex Herbert commented on RNG-169:
----------------------------------
I have created a set of tests that define the conversion contract for
NativeSeedType.
- Native seed types are passed through with no change
- long to int conversion uses hi ^ lo
- int to long conversion expands the bits.
The Int2Long converter is the reference implementation.
- long to long[] conversion seeds a RNG then expands.
The Long2LongArray converter is the reference implementation.
- long to int[] conversion seeds a RNG then expands.
The Long2IntArray converter is the reference implementation.
- int[] to int conversion uses ^ of all the bits
- long[] to long conversion uses ^ of all the bits
- Array-to-array conversions are little-endian.
- Arrays are converted with no zero fill to expand the length.
- Array conversion may be full length (F),
or truncated (T) to the required bytes for the native type.
Conversions are tested to perform an equivalent to the following operations.
{noformat}
Int
int -> int
long -> int
int[] -> int
long[] -> F int[] -> int
byte[] -> F int[] -> int
Long
int -> long
long -> long
int[] -> F long[] -> long
long[] -> long
byte[] -> F long[] -> int
int[]
int -> long -> int[]
long -> int[]
int[] -> int[]
long[] -> T int[]
byte[] -> T int[]
int[]
int -> long -> long[]
long -> long[]
int[] -> T long[]
long[] -> long[]
byte[] -> T long[]
{noformat}
Notes:
1. The actual implementation may be optimised to avoid redundant steps and
intermediate array allocation.
2. Primitive type native seed use all bits from an array (F).
3. Array type native seed use only the initial n bytes from an array (T)
required to satisfy the native seed length n
As highlighted in a previous post this conversion failed the test:
{noformat}
int[] -> long{noformat}
I have updated the code to correct this. Introducing a behavioural
compatibility change to allow 2^64 possible output seeds instead of 2^32 seeds.
Intermediate arrays are now avoided for all array to primitive conversions.
Changes in commit:
d53f3da3533a38dd3cf6b6b300cc58e17a12070a
h2. Reference implementations
h3. int -> long
Currently uses the equivalent of:
{code:java}
int v;
return ((long) v << 32) | (~v & 0xffff_ffffL){code}
Could be updated to use a mixing function, e.g. the equivalent of seeding a
SplitMix64 and calling nextLong().
h3. long -> int[]
Currently seeds a SplitMix64 and generates a sequence of long values. The bytes
from the sequence are then filled into the int array from the end down to 0.
This is for convenience in loop control.
Could be updated to fill the int[] from 0 to the end.
h3. long -> long[]
Currently seeds a SplitMix64 and generates a sequence of long values.
If the reference implementations are updated as described above then this would
be a consistent approach to expanding the input seed. All primitive seeds too
short are used to seed a SplitMix64. The byte output from this is used in order
to generate the required number of bytes.
> Update array seed conversion to use optimum seed length
> -------------------------------------------------------
>
> Key: RNG-169
> URL: https://issues.apache.org/jira/browse/RNG-169
> Project: Commons RNG
> Issue Type: Improvement
> Components: simple
> Affects Versions: 1.4
> Reporter: Alex Herbert
> Priority: Trivial
> Fix For: 1.5
>
>
> The seed conversion routines in ByteArray2LongArray and ByteArray2IntArray
> can be optimised for memory usage.
> The converters can be updated to implement Seed2ArrayConverter. This allows
> the length of the output seed to be constructed to the correct length. This
> will avoid converting part of the byte[] seed that is not used.
> In addition the input seed is expanded if it is not modulus 8 or 4
> respectively using Arrays.copyOf. This will zero fill the end of the seed.
> The array can then be converted by the NumberFactory without an exception.
> These routines should be updated to use the same method as NumberFactory to
> fill in a long[] and then add any trailing bytes to the final long.
> This avoids any array copy when using arbitrary seed lengths, e.g.
> SecureRandom.getSeed(13).
--
This message was sent by Atlassian Jira
(v8.20.1#820001)