#ifndef SHISHUA_HALF_SSE2_H
#define SHISHUA_HALF_SSE2_H
#include <stdint.h>
#include <stddef.h>
#include <assert.h>
#if defined(__SSSE3__) || defined(__AVX__)
# include <tmmintrin.h>
# define SHISHUA_ALIGNR_EPI8(hi, lo, amt) \
_mm_alignr_epi8(hi, lo, amt)
#else
# include <emmintrin.h>
# define SHISHUA_ALIGNR_EPI8(hi, lo, amt) \
_mm_or_si128( \
_mm_slli_si128(hi, 16 - (amt)), \
_mm_srli_si128(lo, amt) \
)
#endif
typedef struct prng_state {
__m128i state[4];
__m128i output[2];
__m128i counter[2];
} prng_state;
#if defined(__x86_64__) || defined(_M_X64)
# define SHISHUA_SET_EPI64X(b, a) _mm_set_epi64x(b, a)
# define SHISHUA_CVTSI64_SI128(x) _mm_cvtsi64_si128(x)
#else
# define SHISHUA_SET_EPI64X(b, a) \
_mm_set_epi32( \
(int)(((uint64_t)(b)) >> 32), \
(int)(b), \
(int)(((uint64_t)(a)) >> 32), \
(int)(a) \
)
# define SHISHUA_CVTSI64_SI128(x) SHISHUA_SET_EPI64X(0, x)
#endif
#if defined(__GNUC__) || defined(_MSC_VER)
# define SHISHUA_RESTRICT __restrict
#else
# define SHISHUA_RESTRICT
#endif
static inline void prng_gen(prng_state *SHISHUA_RESTRICT s, uint8_t *SHISHUA_RESTRICT buf, size_t size) {
__m128i s0_lo = s->state[0], s0_hi = s->state[1],
s1_lo = s->state[2], s1_hi = s->state[3],
t0_lo, t0_hi, t1_lo, t1_hi,
u_lo, u_hi,
counter_lo = s->counter[0], counter_hi = s->counter[1];
const __m128i increment_lo = SHISHUA_SET_EPI64X(5, 7);
const __m128i increment_hi = SHISHUA_SET_EPI64X(1, 3);
assert((size % 32 == 0) && "buf's size must be a multiple of 32 bytes.");
for (size_t i = 0; i < size; i += 32) {
if (buf != NULL) {
_mm_storeu_si128((__m128i*)&buf[i+ 0], s->output[0]);
_mm_storeu_si128((__m128i*)&buf[i+16], s->output[1]);
}
t0_lo = SHISHUA_ALIGNR_EPI8(s0_lo, s0_hi, 4);
t0_hi = SHISHUA_ALIGNR_EPI8(s0_hi, s0_lo, 4);
u_lo = _mm_srli_epi64(s0_lo, 1);
u_hi = _mm_srli_epi64(s0_hi, 1);
s0_lo = _mm_add_epi64(u_lo, t0_lo);
s0_hi = _mm_add_epi64(u_hi, t0_hi);
s1_lo = _mm_add_epi64(s1_lo, counter_lo);
s1_hi = _mm_add_epi64(s1_hi, counter_hi);
counter_lo = _mm_add_epi64(counter_lo, increment_lo);
counter_hi = _mm_add_epi64(counter_hi, increment_hi);
t1_lo = SHISHUA_ALIGNR_EPI8(s1_hi, s1_lo, 12);
t1_hi = SHISHUA_ALIGNR_EPI8(s1_lo, s1_hi, 12);
s1_lo = _mm_srli_epi64(s1_lo, 3);
s1_hi = _mm_srli_epi64(s1_hi, 3);
s1_lo = _mm_add_epi64(s1_lo, t1_lo);
s1_hi = _mm_add_epi64(s1_hi, t1_hi);
s->output[0] = _mm_xor_si128(u_lo, t1_lo);
s->output[1] = _mm_xor_si128(u_hi, t1_hi);
}
s->state[0] = s0_lo; s->state[1] = s0_hi;
s->state[2] = s1_lo; s->state[3] = s1_hi;
s->counter[0] = counter_lo;
s->counter[1] = counter_hi;
}
static uint64_t phi[8] = {
0x9E3779B97F4A7C15, 0xF39CC0605CEDC834, 0x1082276BF3A27251, 0xF86C6A11D0C18E95,
0x2767F0B153D27B7F, 0x0347045B5BF1827F, 0x01886F0928403002, 0xC1D64BA40F335E36,
};
void prng_init(prng_state *s, uint64_t seed[4]) {
s->counter[0] = _mm_setzero_si128();
s->counter[1] = _mm_setzero_si128();
# define STEPS 5
# define ROUNDS 4
__m128i seed_0 = SHISHUA_CVTSI64_SI128(seed[0]);
__m128i seed_1 = SHISHUA_CVTSI64_SI128(seed[1]);
__m128i seed_2 = SHISHUA_CVTSI64_SI128(seed[2]);
__m128i seed_3 = SHISHUA_CVTSI64_SI128(seed[3]);
s->state[0] = _mm_xor_si128(seed_0, _mm_loadu_si128((__m128i *)&phi[0]));
s->state[1] = _mm_xor_si128(seed_1, _mm_loadu_si128((__m128i *)&phi[2]));
s->state[2] = _mm_xor_si128(seed_2, _mm_loadu_si128((__m128i *)&phi[4]));
s->state[3] = _mm_xor_si128(seed_3, _mm_loadu_si128((__m128i *)&phi[6]));
for (int i = 0; i < ROUNDS; i++) {
prng_gen(s, NULL, 32 * STEPS);
s->state[0] = s->state[2]; s->state[1] = s->state[3];
s->state[2] = s->output[0]; s->state[3] = s->output[1];
}
# undef STEPS
# undef ROUNDS
}
#undef SHISHUA_ALIGNR_EPI8
#undef SHISHUA_CVTSI64_SI128
#undef SHISHUA_SET_EPI64X
#undef SHISHUA_RESTRICT
#endif