2013-04-20 17:56:34 +00:00
|
|
|
// Copyright (c) 2013, Peter Pettersson
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
// list of conditions and the following disclaimer.
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// The views and conclusions contained in the software and documentation are those
|
|
|
|
// of the authors and should not be interpreted as representing official policies,
|
|
|
|
// either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
|
|
#ifndef RANDOM_WELL512A_SSE2_H
|
|
|
|
#define RANDOM_WELL512A_SSE2_H
|
|
|
|
|
|
|
|
#include <emmintrin.h> // SSE2 instrinsics.
|
2013-04-22 18:42:45 +00:00
|
|
|
#include <stdlib.h> // Needed for rand().
|
2013-04-20 17:56:34 +00:00
|
|
|
|
|
|
|
class RandomWELL512a_SSE2
|
|
|
|
{
|
|
|
|
public:
|
2013-04-22 18:58:39 +00:00
|
|
|
RandomWELL512a_SSE2(int seed);
|
|
|
|
RandomWELL512a_SSE2(unsigned *seed);
|
2013-04-20 17:56:34 +00:00
|
|
|
|
2013-04-22 18:58:39 +00:00
|
|
|
unsigned GetUnsigned();
|
|
|
|
double GetDouble();
|
2013-04-20 17:56:34 +00:00
|
|
|
|
2013-04-22 18:58:39 +00:00
|
|
|
void GetUnsigned4(unsigned *result4);
|
|
|
|
void GetDouble4(double *result4);
|
2013-04-20 17:56:34 +00:00
|
|
|
|
|
|
|
private:
|
2013-04-22 19:09:34 +00:00
|
|
|
__m128i state[16];
|
|
|
|
unsigned result[4];
|
2013-04-22 19:21:14 +00:00
|
|
|
unsigned index;
|
2013-04-22 19:09:34 +00:00
|
|
|
unsigned result_index;
|
2013-04-20 17:56:34 +00:00
|
|
|
};
|
|
|
|
|
2013-04-22 18:42:45 +00:00
|
|
|
|
|
|
|
inline RandomWELL512a_SSE2::RandomWELL512a_SSE2(int seed)
|
2013-04-22 18:58:39 +00:00
|
|
|
: index(0)
|
2013-04-22 19:09:34 +00:00
|
|
|
, result_index(4)
|
2013-04-22 18:42:45 +00:00
|
|
|
{
|
2013-04-22 18:58:39 +00:00
|
|
|
srand(seed);
|
|
|
|
for (int i = 0; i < 16; ++i)
|
2013-04-22 19:09:34 +00:00
|
|
|
state[i] = _mm_set_epi32(rand(), rand(), rand(), rand());
|
2013-04-22 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline RandomWELL512a_SSE2::RandomWELL512a_SSE2(unsigned *seed)
|
2013-04-22 18:58:39 +00:00
|
|
|
: index(0)
|
2013-04-22 19:09:34 +00:00
|
|
|
, result_index(4)
|
2013-04-22 18:42:45 +00:00
|
|
|
{
|
2013-04-22 18:58:39 +00:00
|
|
|
for (int i = 0; i < 16; ++i)
|
2013-04-22 19:09:34 +00:00
|
|
|
state[i] = _mm_set_epi32(seed[i + 48], seed[i + 32], seed[i + 16], seed[i]);
|
2013-04-22 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned RandomWELL512a_SSE2::GetUnsigned()
|
|
|
|
{
|
2013-04-22 19:09:34 +00:00
|
|
|
if (result_index >= 4)
|
2013-04-22 18:58:39 +00:00
|
|
|
{
|
|
|
|
GetUnsigned4(result);
|
2013-04-22 19:09:34 +00:00
|
|
|
result_index = 0;
|
2013-04-22 18:58:39 +00:00
|
|
|
}
|
2013-04-22 19:09:34 +00:00
|
|
|
return result[result_index++];
|
2013-04-22 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline double RandomWELL512a_SSE2::GetDouble()
|
|
|
|
{
|
2013-04-22 18:58:39 +00:00
|
|
|
const double kToFloat = 2.32830643653869628906e-10;
|
|
|
|
return GetUnsigned() * kToFloat;
|
2013-04-22 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void RandomWELL512a_SSE2::GetUnsigned4(unsigned *result4)
|
|
|
|
{
|
2013-04-22 18:58:39 +00:00
|
|
|
unsigned index_15 = (index + 15) & 15;
|
2013-04-22 19:09:34 +00:00
|
|
|
__m128i state_index = state[index];
|
|
|
|
__m128i state_index_9 = state[(index + 9) & 15];
|
|
|
|
__m128i state_index_13 = state[(index + 13) & 15];
|
|
|
|
__m128i state_index_15 = state[index_15];
|
2013-04-22 18:58:39 +00:00
|
|
|
const __m128i kMix = _mm_set1_epi32(0xda442d24);
|
|
|
|
|
2013-04-22 19:09:34 +00:00
|
|
|
__m128i left = _mm_xor_si128(state_index, _mm_slli_epi32(state_index, 16));
|
|
|
|
__m128i right = _mm_xor_si128(state_index_13, _mm_slli_epi32(state_index_13, 15));
|
|
|
|
__m128i z1 = _mm_xor_si128(left, right);
|
|
|
|
__m128i z2 = _mm_xor_si128(state_index_9, _mm_srli_epi32(state_index_9, 11));
|
2013-04-22 18:58:39 +00:00
|
|
|
__m128i result0 = _mm_xor_si128(z1, z2);
|
2013-04-22 19:09:34 +00:00
|
|
|
state[index] = result0;
|
2013-04-22 18:58:39 +00:00
|
|
|
|
|
|
|
__m128i result1 = _mm_xor_si128(state_index_15, _mm_slli_epi32(state_index_15, 2));
|
2013-04-22 19:09:34 +00:00
|
|
|
result1 = _mm_xor_si128(result1, _mm_xor_si128(z1, _mm_slli_epi32(z1, 18)));
|
|
|
|
result1 = _mm_xor_si128(result1, _mm_slli_epi32(z2, 28));
|
|
|
|
result1 = _mm_xor_si128(result1, _mm_xor_si128(result0, _mm_and_si128(_mm_slli_epi32(result0, 5), kMix)));
|
|
|
|
index = index_15;
|
|
|
|
state[index] = result1;
|
2013-04-22 18:58:39 +00:00
|
|
|
|
|
|
|
_mm_storeu_si128((__m128i *)result4, result1);
|
2013-04-22 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void RandomWELL512a_SSE2::GetDouble4(double *result4)
|
|
|
|
{
|
2013-04-22 19:09:34 +00:00
|
|
|
unsigned unsigned_result[4];
|
|
|
|
GetUnsigned4(unsigned_result);
|
2013-04-22 18:42:45 +00:00
|
|
|
|
2013-04-22 18:58:39 +00:00
|
|
|
const double kToFloat = 2.32830643653869628906e-10;
|
|
|
|
for (unsigned loop = 0; loop < 4; ++loop)
|
2013-04-22 19:09:34 +00:00
|
|
|
result4[loop] = unsigned_result[loop] * kToFloat;
|
2013-04-22 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 17:56:34 +00:00
|
|
|
#endif // RANDOM_WELL512A_SSE2_H
|