Skip to content

Commit

Permalink
Fixed missing preprocessor macro in prng.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Knogle committed Sep 12, 2024
1 parent 7ccd95a commit 29863ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 23 additions & 5 deletions src/prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ nwipe_prng_t nwipe_xoroshiro256_prng = { "XORoshiro-256", nwipe_xoroshiro256_prn
/* RC4 PRNG Structure */
nwipe_prng_t nwipe_rc4_prng = { "RC4", nwipe_rc4_prng_init, nwipe_rc4_prng_read };

// Function to check CPUID and test if SSE4.1 is supported
#if defined( _MSC_VER )
#include <intrin.h> // For MSVC compilers to use __cpuid
#else
#include <cpuid.h> // For GCC/Clang compilers to use __cpuid
#if defined( __AVX2__ ) || defined( __SSE4_2__ )
#include <cpuid.h>
#if defined( __AVX2__ )
#include <immintrin.h> // For _xgetbv and AVX intrinsics
#endif

// Function to check if SSE4.2 is supported
Expand All @@ -67,6 +66,7 @@ int check_sse42_support()
// Function to check if AVX2 is supported
int check_avx2_support()
{
#if defined( __AVX2__ )
uint32_t eax, ebx, ecx, edx;

// First check if OS supports XGETBV and AVX
Expand All @@ -88,8 +88,26 @@ int check_avx2_support()
// Check if AVX2 is supported (bit 5 of EBX from CPUID leaf 7)
__cpuid_count( 7, 0, eax, ebx, ecx, edx );
return ( ebx & ( 1 << 5 ) ) != 0;
#else
return 0; // AVX2 not supported by this compiler or platform
#endif
}

#else

// Fallback if neither AVX2 nor SSE4.2 is available or supported by the compiler/platform
int check_sse42_support()
{
return 0; // SSE4.2 is not supported
}

int check_avx2_support()
{
return 0; // AVX2 is not supported
}

#endif

/* Print given number of bytes from unsigned integer number to a byte stream buffer starting with low-endian. */
static inline void u32_to_buffer( u8* restrict buffer, u32 val, const int len )
{
Expand Down
4 changes: 1 addition & 3 deletions src/rc4/rc4_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ void rc4_genrand_4096_to_buf( rc4_state_t* state, unsigned char* bufpos )
*/
for( n = 0; n < OUTPUT_DATA_LENGTH; n += 4 )
{
/* Prefetch the next S-Box values into the cache to optimize memory access */
_mm_prefetch( (const char*) &state->S[state->i + 16], _MM_HINT_T0 );

// Increment the counter (CTR mode)
state->counter++;

Expand Down Expand Up @@ -220,6 +217,7 @@ void rc4_genrand_4096_to_buf( rc4_state_t* state, unsigned char* bufpos )
memcpy( bufpos, temp_buffer, OUTPUT_DATA_LENGTH );
}


/*
* Function: rc4_genrand_4096_to_buf_sse42
* ----------------------------
Expand Down

0 comments on commit 29863ec

Please sign in to comment.