Skip to content

Commit

Permalink
implement Ivy Bridge RDRAND support
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Noonan <steven@uplinklabs.net>
  • Loading branch information
tycho committed May 16, 2013
1 parent 6e66967 commit 7a9894c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/rnd.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,46 @@ extern int NDECL(rand);
# endif
#endif /* LINT */

#ifdef __RDRND__
static int
has_rdrand(void)
{
const unsigned int RDRAND_BIT = 0x40000000;
unsigned int eax, ebx, ecx, edx;

static int cache = -1;

if (cache != -1)
return cache;

eax = 1;
ecx = 0;
ebx = edx = 0;
asm volatile(
"cpuid"
: "=a" (eax),
"=b" (ebx),
"=c" (ecx),
"=d" (edx)
: "0" (eax), "2" (ecx));

cache = (ecx & RDRAND_BIT) ? 1 : 0;
return cache;
}

#include <immintrin.h>
static unsigned int rdrand()
{
unsigned int x;
if (has_rdrand() && _rdrand32_step(&x))
return x;
else
return rand();
}
#undef RND
#define RND(x) (int)(rdrand() % (long)(x))
#endif

#ifdef OVL0

int
Expand Down
5 changes: 5 additions & 0 deletions sys/unix/Makefile.src
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ endif

LFLAGS =

ifeq ($(RNG),rdrand)
CFLAGS += -mrdrnd
LFLAGS += -mrdrnd
endif

# The Qt and Be window systems are written in C++, while the rest of
# NetHack is standard C. If using Qt, uncomment the LINK line here to get
# the C++ libraries linked in.
Expand Down
5 changes: 5 additions & 0 deletions sys/unix/Makefile.utl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ endif

LFLAGS =

ifeq ($(RNG),rdrand)
CFLAGS += -mrdrnd
LFLAGS += -mrdrnd
endif

LIBS =

# If you are cross-compiling, you must use this:
Expand Down

0 comments on commit 7a9894c

Please sign in to comment.