rlm@1: /** rlm@1: * @file SFMT.c rlm@1: * @brief SIMD oriented Fast Mersenne Twister(SFMT) rlm@1: * rlm@1: * @author Mutsuo Saito (Hiroshima University) rlm@1: * @author Makoto Matsumoto (Hiroshima University) rlm@1: * rlm@1: * Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima rlm@1: * University. All rights reserved. rlm@1: * rlm@1: * The new BSD License is applied to this software, see LICENSE.txt rlm@1: */ rlm@1: #include rlm@1: #include rlm@1: #include "SFMT.h" rlm@1: #include "SFMT-params.h" rlm@1: rlm@1: #if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64) rlm@1: #define BIG_ENDIAN64 1 rlm@1: #endif rlm@1: #if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64) rlm@1: #define BIG_ENDIAN64 1 rlm@1: #endif rlm@1: #if defined(ONLY64) && !defined(BIG_ENDIAN64) rlm@1: #if defined(__GNUC__) rlm@1: #error "-DONLY64 must be specified with -DBIG_ENDIAN64" rlm@1: #endif rlm@1: #undef ONLY64 rlm@1: #endif rlm@1: /*------------------------------------------------------ rlm@1: 128-bit SIMD data type for Altivec, SSE2 or standard C rlm@1: ------------------------------------------------------*/ rlm@1: #if defined(HAVE_ALTIVEC) rlm@1: #if !defined(__APPLE__) rlm@1: #include rlm@1: #endif rlm@1: /** 128-bit data structure */ rlm@1: union W128_T { rlm@1: vector unsigned int s; rlm@1: uint32_t u[4]; rlm@1: }; rlm@1: /** 128-bit data type */ rlm@1: typedef union W128_T w128_t; rlm@1: rlm@1: #elif defined(HAVE_SSE2) rlm@1: #include rlm@1: rlm@1: /** 128-bit data structure */ rlm@1: union W128_T { rlm@1: __m128i si; rlm@1: uint32_t u[4]; rlm@1: }; rlm@1: /** 128-bit data type */ rlm@1: typedef union W128_T w128_t; rlm@1: rlm@1: #else rlm@1: rlm@1: /** 128-bit data structure */ rlm@1: struct W128_T { rlm@1: uint32_t u[4]; rlm@1: }; rlm@1: /** 128-bit data type */ rlm@1: typedef struct W128_T w128_t; rlm@1: rlm@1: #endif rlm@1: rlm@1: /*-------------------------------------- rlm@1: FILE GLOBAL VARIABLES rlm@1: internal state, index counter and flag rlm@1: --------------------------------------*/ rlm@1: /** the 128-bit internal state array */ rlm@1: static w128_t sfmt[N]; rlm@1: /** the 32bit integer pointer to the 128-bit internal state array */ rlm@1: static uint32_t *psfmt32 = &sfmt[0].u[0]; rlm@1: #if !defined(BIG_ENDIAN64) || defined(ONLY64) rlm@1: /** the 64bit integer pointer to the 128-bit internal state array */ rlm@1: static uint64_t *psfmt64 = (uint64_t *)&sfmt[0].u[0]; rlm@1: #endif rlm@1: /** index counter to the 32-bit internal state array */ rlm@1: static int idx; rlm@1: /** a flag: it is 0 if and only if the internal state is not yet rlm@1: * initialized. */ rlm@1: static int initialized = 0; rlm@1: /** a parity check vector which certificate the period of 2^{MEXP} */ rlm@1: static uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4}; rlm@1: rlm@1: /*---------------- rlm@1: STATIC FUNCTIONS rlm@1: ----------------*/ rlm@1: inline static int idxof(int i); rlm@1: inline static void rshift128(w128_t *out, w128_t const *in, int shift); rlm@1: inline static void lshift128(w128_t *out, w128_t const *in, int shift); rlm@1: inline static void gen_rand_all(void); rlm@1: inline static void gen_rand_array(w128_t *array, int size); rlm@1: inline static uint32_t func1(uint32_t x); rlm@1: inline static uint32_t func2(uint32_t x); rlm@1: static void period_certification(void); rlm@1: #if defined(BIG_ENDIAN64) && !defined(ONLY64) rlm@1: inline static void swap(w128_t *array, int size); rlm@1: #endif rlm@1: rlm@1: #if defined(HAVE_ALTIVEC) rlm@1: #include "SFMT-alti.h" rlm@1: #elif defined(HAVE_SSE2) rlm@1: #include "SFMT-sse2.h" rlm@1: #endif rlm@1: rlm@1: /** rlm@1: * This function simulate a 64-bit index of LITTLE ENDIAN rlm@1: * in BIG ENDIAN machine. rlm@1: */ rlm@1: #ifdef ONLY64 rlm@1: inline static int idxof(int i) { rlm@1: return i ^ 1; rlm@1: } rlm@1: #else rlm@1: inline static int idxof(int i) { rlm@1: return i; rlm@1: } rlm@1: #endif rlm@1: /** rlm@1: * This function simulates SIMD 128-bit right shift by the standard C. rlm@1: * The 128-bit integer given in in is shifted by (shift * 8) bits. rlm@1: * This function simulates the LITTLE ENDIAN SIMD. rlm@1: * @param out the output of this function rlm@1: * @param in the 128-bit data to be shifted rlm@1: * @param shift the shift value rlm@1: */ rlm@1: #ifdef ONLY64 rlm@1: inline static void rshift128(w128_t *out, w128_t const *in, int shift) { rlm@1: uint64_t th, tl, oh, ol; rlm@1: rlm@1: th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]); rlm@1: tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]); rlm@1: rlm@1: oh = th >> (shift * 8); rlm@1: ol = tl >> (shift * 8); rlm@1: ol |= th << (64 - shift * 8); rlm@1: out->u[0] = (uint32_t)(ol >> 32); rlm@1: out->u[1] = (uint32_t)(ol & 0xffffffff); rlm@1: out->u[2] = (uint32_t)(oh >> 32); rlm@1: out->u[3] = (uint32_t)(oh & 0xffffffff); rlm@1: } rlm@1: #else rlm@1: inline static void rshift128(w128_t *out, w128_t const *in, int shift) { rlm@1: uint64_t th, tl, oh, ol; rlm@1: rlm@1: th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]); rlm@1: tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]); rlm@1: rlm@1: oh = th >> (shift * 8); rlm@1: ol = tl >> (shift * 8); rlm@1: ol |= th << (64 - shift * 8); rlm@1: out->u[1] = (uint32_t)(ol >> 32); rlm@1: out->u[0] = (uint32_t)(ol & 0xffffffff); rlm@1: out->u[3] = (uint32_t)(oh >> 32); rlm@1: out->u[2] = (uint32_t)(oh & 0xffffffff); rlm@1: } rlm@1: #endif rlm@1: /** rlm@1: * This function simulates SIMD 128-bit left shift by the standard C. rlm@1: * The 128-bit integer given in in is shifted by (shift * 8) bits. rlm@1: * This function simulates the LITTLE ENDIAN SIMD. rlm@1: * @param out the output of this function rlm@1: * @param in the 128-bit data to be shifted rlm@1: * @param shift the shift value rlm@1: */ rlm@1: #ifdef ONLY64 rlm@1: inline static void lshift128(w128_t *out, w128_t const *in, int shift) { rlm@1: uint64_t th, tl, oh, ol; rlm@1: rlm@1: th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]); rlm@1: tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]); rlm@1: rlm@1: oh = th << (shift * 8); rlm@1: ol = tl << (shift * 8); rlm@1: oh |= tl >> (64 - shift * 8); rlm@1: out->u[0] = (uint32_t)(ol >> 32); rlm@1: out->u[1] = (uint32_t)(ol & 0xffffffff); rlm@1: out->u[2] = (uint32_t)(oh >> 32); rlm@1: out->u[3] = (uint32_t)(oh & 0xffffffff); rlm@1: } rlm@1: #else rlm@1: inline static void lshift128(w128_t *out, w128_t const *in, int shift) { rlm@1: uint64_t th, tl, oh, ol; rlm@1: rlm@1: th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]); rlm@1: tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]); rlm@1: rlm@1: oh = th << (shift * 8); rlm@1: ol = tl << (shift * 8); rlm@1: oh |= tl >> (64 - shift * 8); rlm@1: out->u[1] = (uint32_t)(ol >> 32); rlm@1: out->u[0] = (uint32_t)(ol & 0xffffffff); rlm@1: out->u[3] = (uint32_t)(oh >> 32); rlm@1: out->u[2] = (uint32_t)(oh & 0xffffffff); rlm@1: } rlm@1: #endif rlm@1: rlm@1: /** rlm@1: * This function represents the recursion formula. rlm@1: * @param r output rlm@1: * @param a a 128-bit part of the internal state array rlm@1: * @param b a 128-bit part of the internal state array rlm@1: * @param c a 128-bit part of the internal state array rlm@1: * @param d a 128-bit part of the internal state array rlm@1: */ rlm@1: #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2)) rlm@1: #ifdef ONLY64 rlm@1: inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c, rlm@1: w128_t *d) { rlm@1: w128_t x; rlm@1: w128_t y; rlm@1: rlm@1: lshift128(&x, a, SL2); rlm@1: rshift128(&y, c, SR2); rlm@1: r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0] rlm@1: ^ (d->u[0] << SL1); rlm@1: r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1] rlm@1: ^ (d->u[1] << SL1); rlm@1: r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2] rlm@1: ^ (d->u[2] << SL1); rlm@1: r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3] rlm@1: ^ (d->u[3] << SL1); rlm@1: } rlm@1: #else rlm@1: inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c, rlm@1: w128_t *d) { rlm@1: w128_t x; rlm@1: w128_t y; rlm@1: rlm@1: lshift128(&x, a, SL2); rlm@1: rshift128(&y, c, SR2); rlm@1: r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0] rlm@1: ^ (d->u[0] << SL1); rlm@1: r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1] rlm@1: ^ (d->u[1] << SL1); rlm@1: r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2] rlm@1: ^ (d->u[2] << SL1); rlm@1: r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3] rlm@1: ^ (d->u[3] << SL1); rlm@1: } rlm@1: #endif rlm@1: #endif rlm@1: rlm@1: #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2)) rlm@1: /** rlm@1: * This function fills the internal state array with pseudorandom rlm@1: * integers. rlm@1: */ rlm@1: inline static void gen_rand_all(void) { rlm@1: int i; rlm@1: w128_t *r1, *r2; rlm@1: rlm@1: r1 = &sfmt[N - 2]; rlm@1: r2 = &sfmt[N - 1]; rlm@1: for (i = 0; i < N - POS1; i++) { rlm@1: do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1], r1, r2); rlm@1: r1 = r2; rlm@1: r2 = &sfmt[i]; rlm@1: } rlm@1: for (; i < N; i++) { rlm@1: do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1 - N], r1, r2); rlm@1: r1 = r2; rlm@1: r2 = &sfmt[i]; rlm@1: } rlm@1: } rlm@1: rlm@1: /** rlm@1: * This function fills the user-specified array with pseudorandom rlm@1: * integers. rlm@1: * rlm@1: * @param array an 128-bit array to be filled by pseudorandom numbers. rlm@1: * @param size number of 128-bit pseudorandom numbers to be generated. rlm@1: */ rlm@1: inline static void gen_rand_array(w128_t *array, int size) { rlm@1: int i, j; rlm@1: w128_t *r1, *r2; rlm@1: rlm@1: r1 = &sfmt[N - 2]; rlm@1: r2 = &sfmt[N - 1]; rlm@1: for (i = 0; i < N - POS1; i++) { rlm@1: do_recursion(&array[i], &sfmt[i], &sfmt[i + POS1], r1, r2); rlm@1: r1 = r2; rlm@1: r2 = &array[i]; rlm@1: } rlm@1: for (; i < N; i++) { rlm@1: do_recursion(&array[i], &sfmt[i], &array[i + POS1 - N], r1, r2); rlm@1: r1 = r2; rlm@1: r2 = &array[i]; rlm@1: } rlm@1: for (; i < size - N; i++) { rlm@1: do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); rlm@1: r1 = r2; rlm@1: r2 = &array[i]; rlm@1: } rlm@1: for (j = 0; j < 2 * N - size; j++) { rlm@1: sfmt[j] = array[j + size - N]; rlm@1: } rlm@1: for (; i < size; i++, j++) { rlm@1: do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); rlm@1: r1 = r2; rlm@1: r2 = &array[i]; rlm@1: sfmt[j] = array[i]; rlm@1: } rlm@1: } rlm@1: #endif rlm@1: rlm@1: #if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC) rlm@1: inline static void swap(w128_t *array, int size) { rlm@1: int i; rlm@1: uint32_t x, y; rlm@1: rlm@1: for (i = 0; i < size; i++) { rlm@1: x = array[i].u[0]; rlm@1: y = array[i].u[2]; rlm@1: array[i].u[0] = array[i].u[1]; rlm@1: array[i].u[2] = array[i].u[3]; rlm@1: array[i].u[1] = x; rlm@1: array[i].u[3] = y; rlm@1: } rlm@1: } rlm@1: #endif rlm@1: /** rlm@1: * This function represents a function used in the initialization rlm@1: * by init_by_array rlm@1: * @param x 32-bit integer rlm@1: * @return 32-bit integer rlm@1: */ rlm@1: static uint32_t func1(uint32_t x) { rlm@1: return (x ^ (x >> 27)) * (uint32_t)1664525UL; rlm@1: } rlm@1: rlm@1: /** rlm@1: * This function represents a function used in the initialization rlm@1: * by init_by_array rlm@1: * @param x 32-bit integer rlm@1: * @return 32-bit integer rlm@1: */ rlm@1: static uint32_t func2(uint32_t x) { rlm@1: return (x ^ (x >> 27)) * (uint32_t)1566083941UL; rlm@1: } rlm@1: rlm@1: /** rlm@1: * This function certificate the period of 2^{MEXP} rlm@1: */ rlm@1: static void period_certification(void) { rlm@1: int inner = 0; rlm@1: int i, j; rlm@1: uint32_t work; rlm@1: rlm@1: for (i = 0; i < 4; i++) rlm@1: inner ^= psfmt32[idxof(i)] & parity[i]; rlm@1: for (i = 16; i > 0; i >>= 1) rlm@1: inner ^= inner >> i; rlm@1: inner &= 1; rlm@1: /* check OK */ rlm@1: if (inner == 1) { rlm@1: return; rlm@1: } rlm@1: /* check NG, and modification */ rlm@1: for (i = 0; i < 4; i++) { rlm@1: work = 1; rlm@1: for (j = 0; j < 32; j++) { rlm@1: if ((work & parity[i]) != 0) { rlm@1: psfmt32[idxof(i)] ^= work; rlm@1: return; rlm@1: } rlm@1: work = work << 1; rlm@1: } rlm@1: } rlm@1: } rlm@1: rlm@1: /*---------------- rlm@1: PUBLIC FUNCTIONS rlm@1: ----------------*/ rlm@1: /** rlm@1: * This function returns the identification string. rlm@1: * The string shows the word size, the Mersenne exponent, rlm@1: * and all parameters of this generator. rlm@1: */ rlm@1: const char *get_idstring(void) { rlm@1: return IDSTR; rlm@1: } rlm@1: rlm@1: /** rlm@1: * This function returns the minimum size of array used for \b rlm@1: * fill_array32() function. rlm@1: * @return minimum size of array used for fill_array32() function. rlm@1: */ rlm@1: int get_min_array_size32(void) { rlm@1: return N32; rlm@1: } rlm@1: rlm@1: /** rlm@1: * This function returns the minimum size of array used for \b rlm@1: * fill_array64() function. rlm@1: * @return minimum size of array used for fill_array64() function. rlm@1: */ rlm@1: int get_min_array_size64(void) { rlm@1: return N64; rlm@1: } rlm@1: rlm@1: #ifndef ONLY64 rlm@1: /** rlm@1: * This function generates and returns 32-bit pseudorandom number. rlm@1: * init_gen_rand or init_by_array must be called before this function. rlm@1: * @return 32-bit pseudorandom number rlm@1: */ rlm@1: uint32_t gen_rand32(void) { rlm@1: uint32_t r; rlm@1: rlm@1: assert(initialized); rlm@1: if (idx >= N32) { rlm@1: gen_rand_all(); rlm@1: idx = 0; rlm@1: } rlm@1: r = psfmt32[idx++]; rlm@1: return r; rlm@1: } rlm@1: #endif rlm@1: /** rlm@1: * This function generates and returns 64-bit pseudorandom number. rlm@1: * init_gen_rand or init_by_array must be called before this function. rlm@1: * The function gen_rand64 should not be called after gen_rand32, rlm@1: * unless an initialization is again executed. rlm@1: * @return 64-bit pseudorandom number rlm@1: */ rlm@1: uint64_t gen_rand64(void) { rlm@1: #if defined(BIG_ENDIAN64) && !defined(ONLY64) rlm@1: uint32_t r1, r2; rlm@1: #else rlm@1: uint64_t r; rlm@1: #endif rlm@1: rlm@1: assert(initialized); rlm@1: assert(idx % 2 == 0); rlm@1: rlm@1: if (idx >= N32) { rlm@1: gen_rand_all(); rlm@1: idx = 0; rlm@1: } rlm@1: #if defined(BIG_ENDIAN64) && !defined(ONLY64) rlm@1: r1 = psfmt32[idx]; rlm@1: r2 = psfmt32[idx + 1]; rlm@1: idx += 2; rlm@1: return ((uint64_t)r2 << 32) | r1; rlm@1: #else rlm@1: r = psfmt64[idx / 2]; rlm@1: idx += 2; rlm@1: return r; rlm@1: #endif rlm@1: } rlm@1: rlm@1: #ifndef ONLY64 rlm@1: /** rlm@1: * This function generates pseudorandom 32-bit integers in the rlm@1: * specified array[] by one call. The number of pseudorandom integers rlm@1: * is specified by the argument size, which must be at least 624 and a rlm@1: * multiple of four. The generation by this function is much faster rlm@1: * than the following gen_rand function. rlm@1: * rlm@1: * For initialization, init_gen_rand or init_by_array must be called rlm@1: * before the first call of this function. This function can not be rlm@1: * used after calling gen_rand function, without initialization. rlm@1: * rlm@1: * @param array an array where pseudorandom 32-bit integers are filled rlm@1: * by this function. The pointer to the array must be \b "aligned" rlm@1: * (namely, must be a multiple of 16) in the SIMD version, since it rlm@1: * refers to the address of a 128-bit integer. In the standard C rlm@1: * version, the pointer is arbitrary. rlm@1: * rlm@1: * @param size the number of 32-bit pseudorandom integers to be rlm@1: * generated. size must be a multiple of 4, and greater than or equal rlm@1: * to (MEXP / 128 + 1) * 4. rlm@1: * rlm@1: * @note \b memalign or \b posix_memalign is available to get aligned rlm@1: * memory. Mac OSX doesn't have these functions, but \b malloc of OSX rlm@1: * returns the pointer to the aligned memory block. rlm@1: */ rlm@1: void fill_array32(uint32_t *array, int size) { rlm@1: assert(initialized); rlm@1: assert(idx == N32); rlm@1: assert(size % 4 == 0); rlm@1: assert(size >= N32); rlm@1: rlm@1: gen_rand_array((w128_t *)array, size / 4); rlm@1: idx = N32; rlm@1: } rlm@1: #endif rlm@1: rlm@1: /** rlm@1: * This function generates pseudorandom 64-bit integers in the rlm@1: * specified array[] by one call. The number of pseudorandom integers rlm@1: * is specified by the argument size, which must be at least 312 and a rlm@1: * multiple of two. The generation by this function is much faster rlm@1: * than the following gen_rand function. rlm@1: * rlm@1: * For initialization, init_gen_rand or init_by_array must be called rlm@1: * before the first call of this function. This function can not be rlm@1: * used after calling gen_rand function, without initialization. rlm@1: * rlm@1: * @param array an array where pseudorandom 64-bit integers are filled rlm@1: * by this function. The pointer to the array must be "aligned" rlm@1: * (namely, must be a multiple of 16) in the SIMD version, since it rlm@1: * refers to the address of a 128-bit integer. In the standard C rlm@1: * version, the pointer is arbitrary. rlm@1: * rlm@1: * @param size the number of 64-bit pseudorandom integers to be rlm@1: * generated. size must be a multiple of 2, and greater than or equal rlm@1: * to (MEXP / 128 + 1) * 2 rlm@1: * rlm@1: * @note \b memalign or \b posix_memalign is available to get aligned rlm@1: * memory. Mac OSX doesn't have these functions, but \b malloc of OSX rlm@1: * returns the pointer to the aligned memory block. rlm@1: */ rlm@1: void fill_array64(uint64_t *array, int size) { rlm@1: assert(initialized); rlm@1: assert(idx == N32); rlm@1: assert(size % 2 == 0); rlm@1: assert(size >= N64); rlm@1: rlm@1: gen_rand_array((w128_t *)array, size / 2); rlm@1: idx = N32; rlm@1: rlm@1: #if defined(BIG_ENDIAN64) && !defined(ONLY64) rlm@1: swap((w128_t *)array, size /2); rlm@1: #endif rlm@1: } rlm@1: rlm@1: /** rlm@1: * This function initializes the internal state array with a 32-bit rlm@1: * integer seed. rlm@1: * rlm@1: * @param seed a 32-bit integer used as the seed. rlm@1: */ rlm@1: void init_gen_rand(uint32_t seed) { rlm@1: int i; rlm@1: rlm@1: psfmt32[idxof(0)] = seed; rlm@1: for (i = 1; i < N32; i++) { rlm@1: psfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)] rlm@1: ^ (psfmt32[idxof(i - 1)] >> 30)) rlm@1: + i; rlm@1: } rlm@1: idx = N32; rlm@1: period_certification(); rlm@1: initialized = 1; rlm@1: } rlm@1: rlm@1: /** rlm@1: * This function initializes the internal state array, rlm@1: * with an array of 32-bit integers used as the seeds rlm@1: * @param init_key the array of 32-bit integers, used as a seed. rlm@1: * @param key_length the length of init_key. rlm@1: */ rlm@1: void init_by_array(uint32_t *init_key, int key_length) { rlm@1: int i, j, count; rlm@1: uint32_t r; rlm@1: int lag; rlm@1: int mid; rlm@1: int size = N * 4; rlm@1: rlm@1: if (size >= 623) { rlm@1: lag = 11; rlm@1: } else if (size >= 68) { rlm@1: lag = 7; rlm@1: } else if (size >= 39) { rlm@1: lag = 5; rlm@1: } else { rlm@1: lag = 3; rlm@1: } rlm@1: mid = (size - lag) / 2; rlm@1: rlm@1: memset(sfmt, 0x8b, sizeof(sfmt)); rlm@1: if (key_length + 1 > N32) { rlm@1: count = key_length + 1; rlm@1: } else { rlm@1: count = N32; rlm@1: } rlm@1: r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)] rlm@1: ^ psfmt32[idxof(N32 - 1)]); rlm@1: psfmt32[idxof(mid)] += r; rlm@1: r += key_length; rlm@1: psfmt32[idxof(mid + lag)] += r; rlm@1: psfmt32[idxof(0)] = r; rlm@1: rlm@1: count--; rlm@1: for (i = 1, j = 0; (j < count) && (j < key_length); j++) { rlm@1: r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)] rlm@1: ^ psfmt32[idxof((i + N32 - 1) % N32)]); rlm@1: psfmt32[idxof((i + mid) % N32)] += r; rlm@1: r += init_key[j] + i; rlm@1: psfmt32[idxof((i + mid + lag) % N32)] += r; rlm@1: psfmt32[idxof(i)] = r; rlm@1: i = (i + 1) % N32; rlm@1: } rlm@1: for (; j < count; j++) { rlm@1: r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)] rlm@1: ^ psfmt32[idxof((i + N32 - 1) % N32)]); rlm@1: psfmt32[idxof((i + mid) % N32)] += r; rlm@1: r += i; rlm@1: psfmt32[idxof((i + mid + lag) % N32)] += r; rlm@1: psfmt32[idxof(i)] = r; rlm@1: i = (i + 1) % N32; rlm@1: } rlm@1: for (j = 0; j < N32; j++) { rlm@1: r = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)] rlm@1: + psfmt32[idxof((i + N32 - 1) % N32)]); rlm@1: psfmt32[idxof((i + mid) % N32)] ^= r; rlm@1: r -= i; rlm@1: psfmt32[idxof((i + mid + lag) % N32)] ^= r; rlm@1: psfmt32[idxof(i)] = r; rlm@1: i = (i + 1) % N32; rlm@1: } rlm@1: rlm@1: idx = N32; rlm@1: period_certification(); rlm@1: initialized = 1; rlm@1: }