Пример #1
0
size_t
readtoken (FILE *stream,
           const char *delim,
           size_t n_delim,
           token_buffer *tokenbuffer)
{
  char *p;
  int c;
  size_t i, n;
  word isdelim[(UCHAR_MAX + bits_per_word) / bits_per_word];

  memset (isdelim, 0, sizeof isdelim);
  for (i = 0; i < n_delim; i++)
    {
      unsigned char ch = delim[i];
      set_nth_bit (ch, isdelim);
    }

  /* skip over any leading delimiters */
  for (c = getc (stream); c >= 0 && get_nth_bit (c, isdelim); c = getc (stream))
    {
      /* empty */
    }

  p = tokenbuffer->buffer;
  n = tokenbuffer->size;
  i = 0;
  for (;;)
    {
      if (c < 0 && i == 0)
        return -1;

      if (i == n)
        p = x2nrealloc (p, &n, sizeof *p);

      if (c < 0)
        {
          p[i] = 0;
          break;
        }
      if (get_nth_bit (c, isdelim))
        {
          p[i] = 0;
          break;
        }
      p[i++] = c;
      c = getc (stream);
    }

  tokenbuffer->buffer = p;
  tokenbuffer->size = n;
  return i;
}
Пример #2
0
//prints the hex string representation of the stored/processed bit string    
void BVEC::show_hexstr()
{
    std::string buffer;
    std::string word;
    word.reserve(4);
    for(int bit_index = 0;bit_index < width;)
    {
        word += (get_nth_bit(ulong1_vec[bit_index/ULONG1_SIZE],bit_index % ULONG1_SIZE)) ? "1" : "0";
        bit_index++;
        //if the index is greater than the width,just convert that part to binary and break out of the loop
        if(bit_index >= width)
        {
            std::reverse(word.begin(),word.end());
            buffer += bin_to_hex(word);
            word.erase();
            break;
        }
        if(word.length() == 4)
        {
            std::reverse(word.begin(),word.end());
            buffer += bin_to_hex(word);
            word.erase();
            continue;
        }
    }
    std::reverse(buffer.begin(),buffer.end());
    std::cout<<buffer;
    return;
}
/*
 *  Arithmetic right shift.
 */
typedata *arithmetic_right_shift(uint32_t content, uint32_t shift_factor) {
  typedata *result;
  uint32_t mask = 0xffffffff;
  mask = mask << (BITS_PER_INSTR - shift_factor);
  result = malloc(sizeof(typedata));
  result->result_content = mask | (content >> shift_factor);
  result->carry_out = get_nth_bit(content, shift_factor - 1);
  return result;
}
/*
 *  Logic Right Shift.
 */
typedata *logical_shift_right(uint32_t content, uint32_t shift_factor) {
  typedata *result = malloc(sizeof(typedata));
  if (shift_factor == 0) {
    result->carry_out = 0;
    result->result_content = content;
    return result;
  }
  result->carry_out = get_nth_bit(content, (shift_factor - 1));
  result->result_content = (content >> shift_factor);
  return result;
}
/*
 *  Logic Left Shift.
 */
typedata *logical_left_shift(uint32_t content, uint32_t shift_factor) {
  typedata *result = malloc(sizeof(typedata));
  if (shift_factor == 0) {
      result->carry_out = 0;
      result->result_content = content;
      return result;
  }
  uint32_t nth_bit = (LAST_BIT - shift_factor);
  result->carry_out = get_nth_bit(content, nth_bit);
  result->result_content = (content << shift_factor);
  return result;
}
Пример #6
0
static bool
muffet_coin_toss (const uint8_t prev_digest[16], unsigned int round_count)
{
  unsigned int x, y, a, b, r, v, i;
  for (i = 0, x = 0, y = 0; i < 8; i++)
    {
      a = prev_digest[(i + 0) % 16];
      b = prev_digest[(i + 3) % 16];
      r = a >> (b % 5);
      v = prev_digest[r % 16];
      if (b & (1u << (a % 8)))
        v /= 2;
      x |= ((unsigned int) +get_nth_bit (prev_digest, v)) << i;

      a = prev_digest[(i + 8)  % 16];
      b = prev_digest[(i + 11) % 16];
      r = a >> (b % 5);
      v = prev_digest[r % 16];
      if (b & (1u << (a % 8)))
        v /= 2;
      y |= ((unsigned int) +get_nth_bit (prev_digest, v)) << i;
    }

  if (get_nth_bit (prev_digest, round_count))
    x /= 2;
  if (get_nth_bit (prev_digest, round_count + 64))
    y /= 2;

  return !!(get_nth_bit (prev_digest, x) ^ get_nth_bit (prev_digest, y));
}
Пример #7
0
//prints the bit array representation of the bit string
void BVEC::show()
{
    //the output is stored in a buffer as the final output needs to be reversed
    std::string buffer;
    buffer.reserve(width);
    for(int bit_index = 0;bit_index < width;bit_index++)
    {
        //set each element of the buffer according to the value of the corresponding bit in ulong1_vec
        buffer += get_nth_bit(ulong1_vec[bit_index/ULONG1_SIZE],bit_index % ULONG1_SIZE) ? '1' : '0';
    }
    std::reverse(buffer.begin(),buffer.end());
    std::cout<<buffer.c_str();
    return;
}
/*
 *  Rotates number right by a shift_factor amount.
 */
typedata *rotate_right(uint32_t content, uint32_t shift_factor) {
  typedata *data1 = malloc(sizeof(typedata));
  if (shift_factor == 0) {
    data1->carry_out = 0;
    data1->result_content = content;
    return data1;
  }
  uint32_t right_hand
            = (logical_shift_right(content, shift_factor))->result_content;
  uint32_t left_hand
            = (logical_left_shift(content, (BITS_PER_INSTR
               - shift_factor)))-> result_content;
  data1->result_content = right_hand | left_hand;
  data1->carry_out = get_nth_bit(content, (shift_factor - 1));
  return data1;
}
Пример #9
0
unsigned long FramePool::get_frame() {

	// Allocate an available frame from the the bitmap,
	// If successful, return its frame number. Otherwise return 0

	// printf("BEGIN: FramePool searching for an available frame\n");
	
	unsigned long i;
	for (i = 0; i < num_frames; i++) {

		if (get_nth_bit(frame_vacancy_bitmap, i) == 0) {
			set_nth_bit(frame_vacancy_bitmap, i);
			// printf("SUCCESS: FramePool found vacancy at frame <#%lu>\n", i);
			return i;
		}
	}


	Console::puts("ERROR: FramePool could not find a vacant frame\n");
	return 0;
}
Пример #10
0
//select bits from hi_index to lo_index(both inclusive) and return the BVEC formed by those bits
//INDEXING STARTS FROM THE LSB 
//INDEX(LSB) = 0    
BVEC BVEC::select(ulong1 hi_index,ulong1 lo_index)
{
    if(hi_index < lo_index || lo_index < 0 || hi_index > (width - 1))
    {
        throw INDEX_ERROR;
    }
    BVEC result;
    result.width = hi_index - lo_index + 1;
    result.ulong1_vec.resize(result.width/ULONG1_SIZE + 1,0);

    for(int index = 0;index < result.width;index++)
    {
        if(get_nth_bit(ulong1_vec[(index + lo_index) / ULONG1_SIZE],(index + lo_index) % ULONG1_SIZE))
        {
            set_nth_bit_1(result.ulong1_vec[index  / ULONG1_SIZE],index % ULONG1_SIZE);
        }
        else
        {
            set_nth_bit_0(result.ulong1_vec[index / ULONG1_SIZE],index % ULONG1_SIZE);
        }
    }
    return result;
}
Пример #11
0
//addition of two BVECs implemented using bitwise operations for efficiency
BVEC BVEC::operator+(BVEC& bv2)
{
    //the final BVEC which stores the sum
    BVEC result;
    //the initial width of the BVEC will be the maximum of the two widths of the operands
    //this width may change by one depending on the presence of a final carry in the sum
    result.width = (width > bv2.width) ? width : bv2.width;
    //initially fill the result with zeros to avoid any garbage values
    result.ulong1_vec.assign(result.width,0); 
    
    //store a copy of the ulong1_vec of the one of the  operands as its value will be padded by zeros to have
    //the width same as the other
    std::vector<ulong1>temp_vec;
    if(width > bv2.width)
    {
        temp_vec = bv2.ulong1_vec;
        bv2.ulong1_vec.resize(ulong1_vec.size(),0);
    }
    else
    {
        temp_vec = ulong1_vec;
        bv2.ulong1_vec.resize(bv2.ulong1_vec.size(),0);
    }

    //the bitwise sum and carry for each bitwise addition
    bool sum;
    bool carry = false;
    ulong1 bit_index = 0;
    while(bit_index < result.width)
    {
        //implementing a full adder for 3 bits
        //cheap bit tricks for addition
        //calculating the sum bit
        sum = get_nth_bit(ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE) ^ get_nth_bit(bv2.ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE)^ carry;
        //calculating the carry bit 
        bool carry1 = get_nth_bit(ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE) & get_nth_bit(bv2.ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE);
        bool carry2 = get_nth_bit(ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE) & carry; 
        bool carry3 = get_nth_bit(bv2.ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE) & carry;
        carry = carry1 | carry2 | carry3;
        
        if(sum)
        {
            set_nth_bit_1(result.ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE);
            bit_index++;
        }
        else
        {
            set_nth_bit_0(result.ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE);
            bit_index++;
        }
    }
    //set the next bit of the result if a final carry is present
    //also increase the width of the result by 1 
    if(carry)
    {
        set_nth_bit_1(result.ulong1_vec[bit_index / ULONG1_SIZE],bit_index % ULONG1_SIZE);
        result.width++;
    }
    //restore the non-padded bit arrays 
    if(width > bv2.width)
    {
        bv2.ulong1_vec = temp_vec;
    }
    else
    {
       ulong1_vec = temp_vec;
    }
    return result;
}
Пример #12
0
/* Initialize. Play out what we need to. Get out. */
int press_play(char *data_filename, char *wave_filename) {
  double *one_audio;
  double *zero_audio;
  void *out_file;
  FILE *in_file;
  char cur_byte;
  int is_pos;
  int (*output_samples)(void *,double *,size_t);

  make_output_audio(&zero_audio, &one_audio,DEFAULT_WAVELENGTH);

  /* If there's no filename, use the standard input */
  if (data_filename == NULL)
    in_file = stdin;
  else
    in_file = fopen(data_filename,"r");

  if (in_file == NULL) {
    cosby_print_err("Couldn't open %s\n",data_filename);
    return -1;
  }
  if (wave_filename == NULL) {
    output_samples = &output_to_speaker;
    if (init_speaker_output(&out_file) < 0)
      return -1;
  } else {
    output_samples = &output_to_file;
    init_file_output(&out_file, wave_filename);
  } 
  /* Output five seconds of 0 */
  for (int c=0;c<DEFAULT_SAMPLE_RATE*5/DEFAULT_WAVELENGTH;c++) {
    output_samples(out_file,zero_audio,DEFAULT_WAVELENGTH);
  }

  /* Output a byte of all 1s */
  for (int n=7;n>=0;n--) {
    output_samples(out_file,one_audio,DEFAULT_WAVELENGTH/2);
  }
  is_pos = 1;

  /* Loop through the data and output the appropriate
     portion of the appropriate wave.

     If you understand this part, you pretty much understand
     playback. You could just take the sine instead of all that fancy
     FFT bull. FFTW is at its most ineffecient when it's generating a
     single wave. It's expensive to make the plan, so it's usually
     used by running the same plan over and over.
  */
  while (fread(&cur_byte,1,1,in_file)) {
    for (int n=7;n>=0;n--) {
      if (get_nth_bit(cur_byte,n)) {
	if (is_pos) {
	  /* positive one */
	  output_samples(out_file,one_audio,DEFAULT_WAVELENGTH/2);
	} else {
	  /* negative one */
	  output_samples(out_file,one_audio+DEFAULT_WAVELENGTH/4,DEFAULT_WAVELENGTH-DEFAULT_WAVELENGTH/2);
	}
      } else {
	if (is_pos) {
	  /* positive zero */
	  output_samples(out_file,zero_audio,DEFAULT_WAVELENGTH/2);
	} else {
	  /* negative zero */
	  output_samples(out_file,zero_audio+DEFAULT_WAVELENGTH/2,DEFAULT_WAVELENGTH-DEFAULT_WAVELENGTH/2);
	}
	is_pos = !is_pos;
      }
    }
  }

  /* and an extra half a wave for padding */
  if (is_pos) {
    /* positive zero */
    output_samples(out_file,zero_audio,DEFAULT_WAVELENGTH/2);
  } else {
    /* negative zero */
    output_samples(out_file,zero_audio+DEFAULT_WAVELENGTH/2,DEFAULT_WAVELENGTH-DEFAULT_WAVELENGTH/2);
  }

  if (wave_filename == NULL) {
    /* FIXME You forgot to close the soundcard on the way out, you jerk! */
  } else {
    sf_close((SNDFILE *)out_file);
  }

  /* Close the input file, free the audio */
  if (data_filename != NULL)
    fclose(in_file);
  free_audio_output(zero_audio, one_audio);
  return 0;
}