Skip to content

Commit

Permalink
Merge 94c7e3a into 691f020
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtage committed Jul 2, 2019
2 parents 691f020 + 94c7e3a commit f189807
Show file tree
Hide file tree
Showing 32 changed files with 286 additions and 315 deletions.
4 changes: 2 additions & 2 deletions randomgen/src/aesctr/aesctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int aes_capable(void)
return RANDOMGEN_USE_AESNI;
}

#if (defined(HAVE_SSE2) && HAVE_SSE2)
#if defined(HAVE_IMMINTRIN) && !defined(RANDOMGEN_FORCE_SOFTAES)
#define AES_ROUND(rcon, index) \
do \
{ \
Expand All @@ -38,7 +38,7 @@ void aesctr_seed_r(aesctr_state_t *state, uint64_t *seed)
};*/
if (RANDOMGEN_USE_AESNI)
{
#if (defined(HAVE_SSE2) && HAVE_SSE2)
#if defined(HAVE_IMMINTRIN) && !defined(RANDOMGEN_FORCE_SOFTAES)
__m128i k = _mm_set_epi64x(seed[1], seed[0]);
state->seed[0].m128 = k;
// D. Lemire manually unrolled following loop since
Expand Down
49 changes: 4 additions & 45 deletions randomgen/src/aesctr/aesctr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,18 @@
// #ifdef __AES__
// contributed by Samuel Neves

#if defined(RANDOMGEN_FORCE_SOFTAES)
#define HAVE_SSE2 0
#endif

#undef HAVE_IMMINTRIN
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || \
defined(_M_IX86)
#if defined(_MSC_VER) && defined(_WIN32)
#if _MSC_VER >= 1900
#include <immintrin.h>
#define HAVE_IMMINTRIN 1
#endif
#else
#include <immintrin.h>
#define HAVE_IMMINTRIN 1
#endif
#endif

#ifdef _WIN32
#define ALIGN_WINDOWS __declspec(align(16))
#define ALIGN_GCC_CLANG
#if _MSC_VER == 1500
#include "../common/inttypes.h"
#define INLINE __forceinline
#else
#include <inttypes.h>
#define INLINE __inline __forceinline
#endif
#else
#define ALIGN_WINDOWS
#define ALIGN_GCC_CLANG __attribute__((aligned(16)))
#include <inttypes.h>
#define INLINE inline
#endif
#include "../common/randomgen_config.h"
#include "../common/randomgen_immintrin.h"

#include "softaes.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#define AESCTR_UNROLL 4
#define AESCTR_ROUNDS 10

#ifdef _WIN32
#define UNLIKELY(x) ((x))
#else
#define UNLIKELY(x) (__builtin_expect((x), 0))
#endif

extern int RANDOMGEN_USE_AESNI;

union AES128_T {
#if (defined(HAVE_SSE2) && HAVE_SSE2)
#if defined(HAVE_IMMINTRIN) && !defined(RANDOMGEN_FORCE_SOFTAES)
__m128i m128;
#endif
uint64_t u64[2];
Expand All @@ -87,7 +46,7 @@ static INLINE uint64_t aesctr_r(aesctr_state_t *state) {
uint64_t output;
if (UNLIKELY(state->offset >= 16 * AESCTR_UNROLL)) {
if (RANDOMGEN_USE_AESNI) {
#if (defined(HAVE_SSE2) && HAVE_SSE2)
#if defined(HAVE_IMMINTRIN) && !defined(RANDOMGEN_FORCE_SOFTAES)
__m128i work[AESCTR_UNROLL];
for (int i = 0; i < AESCTR_UNROLL; ++i) {
work[i] = _mm_xor_si128(state->ctr[i].m128, state->seed[0].m128);
Expand Down
23 changes: 4 additions & 19 deletions randomgen/src/chacha/chacha.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,16 @@
#define _RANDOMDGEN__CHACHA_H_


#ifdef _WIN32
#if _MSC_VER == 1500
#include "../common/inttypes.h"
#define INLINE __forceinline
#else
#include <inttypes.h>
#define INLINE __inline __forceinline
#endif
#else
#include <inttypes.h>
#define INLINE inline
#endif
#include "../common/randomgen_config.h"

#ifdef _MSC_VER
#define USE_128BIT_COUNTER 1

#if defined(_WIN32) && defined(_MSC_VER)
#define M128I_CAST
#define ALIGN_WINDOWS __declspec(align(16))
#define ALIGN_GCC_CLANG
#else
#define ALIGN_WINDOWS
#define ALIGN_GCC_CLANG __attribute__((aligned(16)))
#define M128I_CAST (__m128i)
#endif

#define USE_128BIT_COUNTER 1

typedef double * aligned_double_ptr ;

ALIGN_WINDOWS struct CHACHA_STATE_T {
Expand Down
2 changes: 1 addition & 1 deletion randomgen/src/common/cpu_features.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void feature_flags(int flags[])
#elif defined(_MSC_VER) && defined(_WIN32)
int cpu_info[4];
int num_ids, ecx = 0;
__cpuid(&cpu_info, 0);
__cpuid(cpu_info, 0);
num_ids = (int)cpu_info[0];
if (num_ids >= 1)
{
Expand Down
6 changes: 1 addition & 5 deletions randomgen/src/common/cpu_features.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#ifndef _RANDOMGEN_CPU_FEATURES_H_
#define _RANDOMGEN_CPU_FEATURES_H_

#if defined(_WIN32) && defined(_MSC_VER) && _MSC_VER == 1500
#include "../common/inttypes.h"
#else
#include <inttypes.h>
#endif
#include "randomgen_config.h"

#undef HAVE_CPUID
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || \
Expand Down
83 changes: 83 additions & 0 deletions randomgen/src/common/randomgen_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef _RANDOMDGEN__CONFIG_H_
#define _RANDOMDGEN__CONFIG_H_

#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#if defined(_WIN32) && defined(_MSC_VER)

/* windows msvc */

#ifndef MSVCFORCEINLINE
#ifndef _DEBUG
#define MSVCFORCEINLINE __forceinline
#else
#define MSVCFORCEINLINE
#endif
#endif

#ifndef inline
#if _MSC_VER < 1600
/* msvs 2008 and earlier */
#define inline _inline MSVCFORCEINLINE
#else
/* msvs 2010 and later */
#define inline __inline MSVCFORCEINLINE
#endif
#define INLINE inline
#else
#define INLINE __inline MSVCFORCEINLINE
#endif

#if _MSC_VER < 1600

/* msvs 2008 and earlier */
#include "inttypes.h"
#include "stdbool.h"
#include "stdint.h"

#elif _MSC_VER < 1800

/* msvs 2010-2012 */
#include "inttypes.h"
#include "stdbool.h"
#include <stdint.h>

#else

/* msvs 2013 and later */
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>

#endif

#define ALIGN_WINDOWS __declspec(align(16))
#define ALIGN_GCC_CLANG

#else

#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>

#define INLINE inline

#define ALIGN_WINDOWS
#define ALIGN_GCC_CLANG __attribute__((aligned(16)))

#endif

#if defined(__MINGW32__)
#include <x86intrin.h>
#endif

#ifdef _WIN32
#define UNLIKELY(x) ((x))
#else
#define UNLIKELY(x) (__builtin_expect((x), 0))
#endif

#endif
25 changes: 25 additions & 0 deletions randomgen/src/common/randomgen_config_numpy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _RANDOMDGEN__CONFIG_NUMPY_H_
#define _RANDOMDGEN__CONFIG_NUMPY_H_

#ifndef RANDOMGEN_STANDALONE

#include "Python.h"
#include "numpy/npy_common.h"
#include "numpy/npy_math.h"

#else

#include "standalone/aligned_malloc.h"
#include "standalone/npy_common.h"
#include "standalone/npy_math.h"
#include "standalone/python.h"

#endif

#ifndef NPY_MEMALIGN

#define NPY_MEMALIGN 64 /* 16 for SSE2, 32 for AVX, 64 for Xeon Phi */

#endif

#endif
17 changes: 17 additions & 0 deletions randomgen/src/common/randomgen_immintrin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _RANDOMGEN__IMMINTRIN_H_
#define _RANDOMGEN__IMMINTRIN_H_

#undef HAVE_IMMINTRIN
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
#if defined(_MSC_VER) && defined(_WIN32)
#if _MSC_VER >= 1900
#include <immintrin.h>
#define HAVE_IMMINTRIN 1
#endif
#else
#include <immintrin.h>
#define HAVE_IMMINTRIN 1
#endif
#endif

#endif /* _RANDOMGEN__IMMINTRIN_H_ */
9 changes: 9 additions & 0 deletions randomgen/src/common/standalone/Python.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef Py_PYTHON_H
#define Py_PYTHON_H

#include <inttypes.h>

typedef uintptr_t Py_uintptr_t;
typedef intptr_t Py_intptr_t;

#endif
28 changes: 28 additions & 0 deletions randomgen/src/common/standalone/aligned_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef _RANDOMDGEN__ALIGNED_MALLOC_H_
#define _RANDOMDGEN__ALIGNED_MALLOC_H_

#include <stdlib.h>
#include <malloc.h>
#include <string.h>

static inline void *_aligned_calloc(size_t n, size_t size, size_t alignment) {

void *p = 0;

size_t asize = n * size;

p = _aligned_malloc(asize, alignment);

if (p) {
memset(p, 0, asize);
}

return p;

}

#define malloc_aligned(a) _aligned_calloc(1,(a),NPY_MEMALIGN)
#define calloc_aligned(a, b) _aligned_calloc((a),(b),NPY_MEMALIGN)
#define free_aligned(a) _aligned_free(a)

#endif
31 changes: 31 additions & 0 deletions randomgen/src/common/standalone/npy_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _NPY_COMMON_H_
#define _NPY_COMMON_H_

#include "Python.h"

#if defined(_MSC_VER)
#define NPY_INLINE __inline
#elif defined(__GNUC__)
#if defined(__STRICT_ANSI__)
#define NPY_INLINE __inline__
#else
#define NPY_INLINE inline
#endif
#else
#define NPY_INLINE
#endif

#define NPY_SIZEOF_LONG SIZEOF_LONG

typedef double npy_double;

typedef unsigned long npy_uint32;

typedef unsigned char npy_bool;
#define NPY_FALSE 0
#define NPY_TRUE 1

typedef Py_intptr_t npy_intp;
typedef Py_uintptr_t npy_uintp;

#endif
25 changes: 25 additions & 0 deletions randomgen/src/common/standalone/npy_math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __NPY_MATH_C99_H_
#define __NPY_MATH_C99_H_

#include <math.h>

#include "npy_common.h"

#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define npy_isnan(x) _isnan((x))
#else
#define npy_isnan(x) isnan(x)
#endif

NPY_INLINE static float __npy_nanf(void)
{
const union { npy_uint32 __i; float __f;} __bint = {0x7fc00000UL};
return __bint.__f;
}

#define NPY_NANF __npy_nanf()

#define NPY_NAN ((npy_double)NPY_NANF)

#endif

13 changes: 13 additions & 0 deletions randomgen/src/common/stdbool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _RANDOMDGEN__STDBOOL_H
#define _RANDOMDGEN__STDBOOL_H

#if defined(_WIN32) && defined(_MSC_VER)
#ifndef __bool_true_false_are_defined
#define __bool_true_false_are_defined
typedef int bool;
#define false 0
#define true 1
#endif
#endif

#endif

0 comments on commit f189807

Please sign in to comment.