Created
September 27, 2016 13:28
-
-
Save andr1972/abc66f4c861950cf87275523b1262bd9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string.h> | |
| #include "RCarry2.h" | |
| static const unsigned long int mask_lo = 0x00ffffffUL; /* 2^24 - 1 */ | |
| static const unsigned long int mask_hi = ~0x00ffffffUL; | |
| static const unsigned long int two24 = 16777216; /* 2^24 */ | |
| RCarry2::RCarry2(int32_t seed) | |
| { | |
| /* This is the initialization algorithm of F. James, widely in use | |
| for RANLUX. */ | |
| for (int i = 0; i < 24; i++) | |
| { | |
| unsigned long int k = seed / 53668; | |
| seed = 40014 * (seed - k * 53668) - k * 12211; | |
| if (seed < 0) | |
| { | |
| seed += 2147483563; | |
| } | |
| state0.u[i] = seed % two24; | |
| } | |
| state0.i = 23; | |
| state0.j = 9; | |
| if (state0.u[23] & mask_hi) | |
| state0.carry = 1; | |
| else | |
| state0.carry = 0; | |
| memcpy(&state1, &state0, sizeof(state0)); | |
| } | |
| uint32_t RCarry2::increment(rcarry2_state_t * state) | |
| { | |
| unsigned int i = state->i; | |
| unsigned int j = state->j; | |
| long int unew = state->u[j] - state->u[i] - state->carry; | |
| if (unew & mask_hi) | |
| { | |
| state->carry = 1; | |
| unew &= mask_lo; | |
| } | |
| else | |
| state->carry = 0; | |
| state->u[i] = unew; | |
| if (i == 0) | |
| i = 23; | |
| else | |
| i--; | |
| state->i = i; | |
| if (j == 0) | |
| j = 23; | |
| else | |
| j--; | |
| state->j = j; | |
| return unew; | |
| } | |
| int32_t RCarry2::get() //46.9 ticks | |
| { | |
| if (pass == 0) | |
| { | |
| pass = (increment(&state1) & 31) + 1; | |
| } | |
| unsigned long int r = increment(&state0); | |
| n++; | |
| if (n == pass) | |
| { | |
| unsigned int skip = (increment(&state1) & 31) + 1; | |
| n = 0; | |
| for (unsigned int i = 0; i < skip; i++) | |
| increment(&state0); | |
| pass = 0; | |
| } | |
| return r; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <stdint.h> | |
| class RCarry2 | |
| { | |
| private: | |
| typedef struct | |
| { | |
| unsigned int i; | |
| unsigned int j; | |
| unsigned int carry; | |
| unsigned long int u[24]; | |
| } | |
| rcarry2_state_t; | |
| int n = 0; | |
| int pass = 0; | |
| rcarry2_state_t state0, state1; | |
| uint32_t increment(rcarry2_state_t * state); | |
| public: | |
| RCarry2(int32_t seed); | |
| int32_t get(); | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment