Skip to content

Instantly share code, notes, and snippets.

@shermaza
Last active October 10, 2015 22:57
Show Gist options
  • Select an option

  • Save shermaza/853140ccc3baee2e082e to your computer and use it in GitHub Desktop.

Select an option

Save shermaza/853140ccc3baee2e082e to your computer and use it in GitHub Desktop.
Finding the 30th bit of the *ecx register
#include "processor.h"
void native_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
/* ecx is often an input as well as an output. */
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));
}
int get_bit(unsigned int k, unsigned int n)
{
int mask = 1 << k;
int masked_n = n & mask;
int the_bit = masked_n >> k;
return the_bit;
}
RDRAND_AVAILABLE = 0;
/* Check for RDRAND support using CPUID */
unsigned eax, ebx, ecx, edx;
eax = 1; /* processor info and feature bits? Took this from elesewhere... */
native_cpuid( &eax, &ebx, &ecx, &edx );
RDRAND_AVAILABLE = get_bit( 30, ecx );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment