Skip to content

Instantly share code, notes, and snippets.

@kumpera
Created November 3, 2010 21:29
Show Gist options
  • Save kumpera/661752 to your computer and use it in GitHub Desktop.
Save kumpera/661752 to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#define NUM_THREADS 1
enum {
X86_FPCW_INVOPEX_MASK = 0x1,
X86_FPCW_DENOPEX_MASK = 0x2,
X86_FPCW_ZERODIV_MASK = 0x4,
X86_FPCW_OVFEX_MASK = 0x8,
X86_FPCW_UNDFEX_MASK = 0x10,
X86_FPCW_PRECEX_MASK = 0x20,
X86_FPCW_PRECC_MASK = 0x300,
X86_FPCW_ROUNDC_MASK = 0xc00,
/* values for precision control */
X86_FPCW_PREC_SINGLE = 0,
X86_FPCW_PREC_DOUBLE = 0x200,
X86_FPCW_PREC_EXTENDED = 0x300,
/* values for rounding control */
X86_FPCW_ROUND_NEAREST = 0,
X86_FPCW_ROUND_DOWN = 0x400,
X86_FPCW_ROUND_UP = 0x800,
X86_FPCW_ROUND_TOZERO = 0xc00
};
/*
* Initialize the cpu to execute managed code.
*/
void
mono_arch_cpu_init (void)
{
unsigned short fpcw;
__asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
printf ("current fpcw %x\n", fpcw);
fpcw &= ~X86_FPCW_PRECC_MASK;
fpcw |= X86_FPCW_PREC_DOUBLE;
printf ("\twith mask fpcw %x\n", fpcw);
__asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
__asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
}
void *TaskCode(void *argument)
{
int tid;
int i = 0;
double d;
mono_arch_cpu_init ();
for (i = 0; i < 10000000; i++) {
d += i;
d += sin(cos(tan(d)));
}
printf ("d: %f\n", d);
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
int rc, i;
mono_arch_cpu_init ();
if (argc == 1) {
TaskCode (NULL);
} else {
/* create all threads */
for (i=0; i<NUM_THREADS; ++i) {
thread_args[i] = i;
printf("In main: creating thread %d\n", i);
rc = pthread_create(&threads[i], NULL, TaskCode, (void *) &thread_args[i]);
assert(0 == rc);
}
/* wait for all threads to complete */
for (i=0; i<NUM_THREADS; ++i) {
rc = pthread_join(threads[i], NULL);
assert(0 == rc);
}
}
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment