似乎无法弄清楚这个头文件mtrand32.h

时间:2018-11-28 08:43:30

标签: c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <mtrand32.h>

#define OPERATOR_ADD '+'
#define OPERATOR_SUBTRACT '-'
#define OPERATOR_MULTIPLY '*'
#define OPERATOR_DIVIDE '/'
#define WORD_BITS 32U
#define STREAMS_N 4U

typedef struct
{
    unsigned int *words;
    unsigned int ones;
}

stream_t;

int random_bit(unsigned int, unsigned int, stream_t *, unsigned int, 
unsigned int);
void add_one(stream_t *, unsigned int, unsigned int);
void print_stream(int, stream_t *, unsigned int, int);
void next_bit(unsigned int, unsigned int *, unsigned int *);

int main(void)
{
    unsigned int w_bits, a_ones, b_ones, oper, s_ones, stream_words_n, 
    *words, stream_idx, q, word_idx, weight, bit;
    stream_t *streams;
    if (scanf("%u", &w_bits) != 1 || w_bits < 1)
    {
        fprintf(stderr, "Invalid w_bits\n");
        fflush(stderr);
        return EXIT_FAILURE;
    }
    if (scanf("%u", &a_ones) != 1 || a_ones > w_bits)
    {
        fprintf(stderr, "Invalid a_ones\n");
        fflush(stderr);
        return EXIT_FAILURE;
    }
    if (scanf("%u", &b_ones) != 1 || b_ones > w_bits)
    {
        fprintf(stderr, "Invalid b_ones\n");
        fflush(stderr);
        return EXIT_FAILURE;
    }
    getchar();
    oper = getchar();
    switch (oper)
    {
         case OPERATOR_ADD:
         case OPERATOR_SUBTRACT:
         case OPERATOR_MULTIPLY:
         case OPERATOR_DIVIDE:
         break;
         default:
         fprintf(stderr, "Invalid operator\n");
         fflush(stderr);
         return EXIT_FAILURE;
   }
   s_ones = w_bits/2;
   stream_words_n = (w_bits-1)/WORD_BITS+1;
   words = (unsigned int*)calloc(stream_words_n*STREAMS_N, sizeof(unsigned 
   int));
   if (!words) {
      fprintf(stderr, "Could not allocate memory for words\n");
      fflush(stderr);
      return EXIT_FAILURE;
   }
   streams = (stream_t*)malloc(sizeof(stream_t)*STREAMS_N);
   if (!streams) {
      fprintf(stderr, "Could not allocate memory for streams\n");
      fflush(stderr);
      free(words);
      return EXIT_FAILURE;
   }
   for (stream_idx = 0; stream_idx < STREAMS_N; stream_idx++) {
      streams[stream_idx].words = words+stream_words_n*stream_idx;
      streams[stream_idx].ones = 0;
  }
  smtrand32((unsigned int)time(NULL));
  q = 0;
  word_idx = 0;
  weight = 1;
  for (bit = 0; bit < w_bits; bit++) {
     int bit_a = random_bit(w_bits, a_ones, streams, word_idx, weight), 
     bit_b = random_bit(w_bits, b_ones, streams+1U, word_idx, weight);
     switch (oper) {
        int bit_s;
        case OPERATOR_ADD:
        bit_s = random_bit(w_bits, s_ones, streams+2, word_idx, weight);
        if ((bit_a && bit_s) || (bit_b && !bit_s)) {
            add_one(streams+3, word_idx, weight);
        }
        break;
        case OPERATOR_SUBTRACT:
        bit_s = random_bit(w_bits, s_ones, streams+2, word_idx, weight);
        if ((bit_a && bit_s) || (!bit_b && !bit_s)) {
            add_one(streams+3, word_idx, weight);
        }
        break;
        case OPERATOR_MULTIPLY:
        if (bit_a && bit_b) {
            add_one(streams+3, word_idx, weight);
        }
        break;
        case OPERATOR_DIVIDE:
        if (bit_a) {
            if (bit_b) {
                q = !q;
                if (q) {
                    add_one(streams+3, word_idx, weight);
                }
            }
            else {
                q = 1;
                add_one(streams+3, word_idx, weight);
            }
        }
        else
        {
            if (bit_b)
            {
                q = 0;
            }
            else {
                if (q)
                {
                    add_one(streams+3, word_idx, weight);
                }
            }
        }
        break;
        default:
        break;
    }
    next_bit(bit, &word_idx, &weight);
}
if (oper == OPERATOR_ADD || oper == OPERATOR_SUBTRACT) {
    print_stream('a', streams, w_bits, 1);
    print_stream('b', streams+1, w_bits, 1);
    print_stream('s', streams+2, w_bits, 0);
    print_stream('y', streams+3, w_bits, 1);
}
else {
    print_stream('a', streams, w_bits, 0);
    print_stream('b', streams+1, w_bits, 0);
    print_stream('y', streams+3, w_bits, 0);
}
fflush(stdout);
free(streams);
free(words);
return EXIT_SUCCESS;
}

int random_bit(unsigned int w_bits, unsigned int x_ones, stream_t *stream, 
unsigned int word_idx, unsigned int weight)
{
    if ((unsigned int)(mtrand32()/(UINT32_MAX+1.0)*w_bits) < x_ones)
    {
        add_one(stream, word_idx, weight);
        return 1;
    }
    return 0;
}

void add_one(stream_t *stream, unsigned int word_idx, unsigned int weight)
{
     stream->words[word_idx] += weight;
     stream->ones++;
}

 void print_stream(int symbol, stream_t *stream, unsigned int w_bits, int 
 bipolar)
 {
      unsigned int word_idx, weight, bit;
       printf("%c = ", symbol);
       word_idx = 0;
        weight = 1;
       for (bit = 0; bit < w_bits; bit++)
      {
           if (stream->words[word_idx] & weight)
           {
                  putchar('1');
           }
           else
           {
                  putchar('0');
           }
           next_bit(bit, &word_idx, &weight);
      }
      printf(" (");
      if (bipolar)
      {
              printf("%d", (int32_t)stream->ones*2-(int32_t)w_bits);
       }
      else
      {
           printf("%u", stream->ones);
       }
      printf("/%u)\n", w_bits);
 }

  void next_bit(unsigned int bit, unsigned int *word, unsigned int *weight)
 {
      if (bit%WORD_BITS == WORD_BITS-1)
     {
            *word += 1;
            *weight = 1;
     }
     else
     {
              *weight *= 2;
     }
 }

我遇到了错误:

  

[错误] G:\ Stochastic Computing Machine \ Stochastic.cpp:5:22:   mtrand32.h:没有这样的文件或目录

     

[错误] G:\ Stochastic Computing Machine \ Stochastic.cpp:82:错误:   未在此范围内声明“ smtrand32”

     

[错误] G:\ Stochastic Computing Machine \ Stochastic.cpp:158:错误:   在此范围内未声明“ mtrand32”

     

[错误] G:\ Stochastic Computing Machine \ Stochastic.cpp:158:错误:   在此范围内未声明“ UINT32_MAX”

我搜索了mtrand32.h,但是找不到

0 个答案:

没有答案