1  #include <immintrin.h>
       2  #include <stdint.h>
       3  #include <pthread.h>
       4  #include <string.h>
       5  #ifdef DEBUG
       6  #include <stdio.h>
       7  #endif
       8  #include "cpuid.h"
       9  
      10  typedef struct {
      11    uint32_t id; /* filled in by launch_threads. */
      12  } state_t;
      13  
      14  static pthread_t* threads = 0;
      15  static state_t* thread_state = 0;
      16  static const unsigned int num_threads = 4;
      17  
      18  static void* threads_worker (state_t *tstate);
      19  
      20  void launch_threads (uint32_t nthreads,
      21                       void* (*worker)(state_t*),
      22                       state_t* tstate_proto)
      23  {
      24    int i;
      25    thread_state = malloc (sizeof (state_t) *nthreads);
      26    threads = malloc (sizeof (pthread_t) *nthreads);
      27    memset (threads, 0, sizeof (pthread_t) *nthreads);
      28    for(i = 0; i < nthreads; i++)
      29      {
      30        memcpy (thread_state + i, tstate_proto, sizeof (state_t));
      31        thread_state[i].id = i;
      32        pthread_create (threads+i, NULL,
      33                        (void* (*)(void*))worker,  
      34  		      (void*) (thread_state+i));
      35      }
      36  }
      37  
      38  void wait()
      39  {
      40    int i;
      41    for(i = 0; i < num_threads; i++)
      42      pthread_join (threads[i], 0);
      43    free (threads);
      44    threads = 0;
      45    free (thread_state);
      46    thread_state  = 0;
      47  }
      48  
      49  #ifndef DO_TEST
      50  #define DO_TEST do_test
      51  static void rao_test (void);
      52  __attribute__ ((noinline))
      53  static void
      54  do_test (void)
      55  {
      56    state_t tstate_proto;
      57    launch_threads(num_threads, threads_worker, &tstate_proto);
      58    wait();
      59    rao_test ();
      60  }
      61  #endif
      62  
      63  int
      64  main()
      65  {
      66    if (__builtin_cpu_supports ("raoint"))
      67      {
      68        DO_TEST ();
      69  #ifdef DEBUG
      70        printf ("PASSED\n");
      71  #endif
      72      }
      73  #ifdef DEBUG
      74    else
      75      printf ("SKIPPED\n");
      76  #endif
      77  
      78    return 0;
      79  }