Skip to content

Conversation

@sh-mug
Copy link
Contributor

@sh-mug sh-mug commented Apr 25, 2023

stypes.RandXorshift

RandXorshift(reg_initval=0x12345678, dependency=None, enable=None, width=32)

Constructor Arguments

The input arguments of stypes.RandXorshift is as the following.

  • reg_initval: initial value of the register. (optional, default value is 0x12345678)
  • dependency: whether to set dependency between variables. (optional, the default value is None)
  • enable: enable flag. (optional, the default value is None)
  • width: data width (optional, the default value is 32, and 32 or 64 can be set)

The reset input is intentionally omitted to prevent this function from exhibiting deterministic behavior for large unit operations (e.g., iterations of batch learning).

Implementation

The RandXorshift class implements hardware random number generation using the Xorshift algorithm on embedded systems. The Xorshift algorithm is a pseudo-random number generator that utilizes simple bitwise operations to generate a sequence of seemingly random numbers.

To implement the RandXorshift class, I refer to Marsaglia's paper 'Xorshift RNGs' (2003) and update the values according to the data width, as shown in the pseudo-code below. I treat state variables and outputs only as unsigned integers to ensure accurate Xorshift-based pseudo-random number generation implementation, following the paper's method.

Case for 32-bit data width

uint32_t next(uint32_t x) {
  x ^= (x << 13);
  x ^= (x >> 17);
  x ^= (x << 5);
  return x;
}

Case for 64-bit data width

uint64_t next(uint64_t x) {
  x ^= (x << 13);
  x ^= (x >> 7);
  x ^= (x << 17);
  return x;
}

Test

Referring to the test of stypes.Counter, I added stream_rand_xorshift and verified whether the pseudo-random number sequence output by stypes.RandXorshift matches the expectation.

@sh-mug sh-mug changed the title add stypes.Xorshift add stypes.RandXorshift Apr 27, 2023
@sh-mug sh-mug force-pushed the feature_stypes_Xorshift branch from d8d4bfe to 71744ba Compare May 2, 2023 08:50
@shtaxxx shtaxxx merged commit 8d25bf8 into PyHDI:develop Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants