Ejemplo n.º 1
0
int main() {
  bignum l1 = from_str("99"),
    l2 = from_str("10");

  printf("%s\n", to_str(from_int(666)));
  printf("%d\n", to_int(from_str("666")));

  bignum sum = plus(l1, l2);
  printf("%s\n", to_str(sum));

  bignum diff = minus(l1, l2);
  printf("%s\n", to_str(diff));

  printf("%i\n", lesser(l1, l2));

  bignum muli = mul_int(l1, 2);
  printf("%s\n", to_str(muli));

  printf("%d\n", mod_int(l1, 10));

  bignum divi = div_int(l1, 10);
  printf("%s\n", to_str(divi));

  bignum mul = multiply(l1, l2);
  printf("%s\n", to_str(mul));

  bignum div = divide(l1, l2);
  printf("%s\n", to_str(div));

  bignum mod = modulo(l1, l2);
  printf("%s\n", to_str(mod));

  return 0;
}
Ejemplo n.º 2
0
bignum multiply (bignum x, bignum y) {
  bignum z, zero;
  int len = x.len + y.len;

  zero.len = 1;
  zero.num = calloc(len, sizeof(int));
  zero.num[0] = 0;

  z.len = 1;
  z.num = calloc(len, sizeof(int));
  z.num[0] = 0;

  while (greater(y, zero)) {
    if (mod_int(y, 2) == 1)
      z = plus(z, x);

    x = mul_int(x, 2);
    y = div_int(y, 2);
  }

  free(zero.num);

  if (z.len == len)
    return z;
  else {
    bignum m;
    m.len = z.len;
    m.num = calloc(m.len, sizeof(int));

    memcpy(m.num, z.num, m.len * sizeof(int));
    free(z.num);

    return m;
  }
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
	printf("Testing Int    : %ld\n", mul_int(1, 2));
	printf("Testing Double : %ld\n", mul_double(1, 2));

	return 0;
}
Ejemplo n.º 4
0
void task_application_intersection(uint16_t input) {
   counter++;
   P1OUT ^= 0x02;                                // toggle P1.1 for debug
   P4OUT ^= 0x20;                                // toggle P4.5 for debug
   P1OUT |= 0x04;P1OUT &= ~0x04;                 // pulse P1.2 for debug
   if (counter==0) {
      leds_circular_shift();                     // circular shift LEDs for debug
   }
   
   //get RAM space for packet
   testIntersection = openqueue_getFreePacketBuffer();
   //l1
   testIntersection->l1_channel  = DEFAULTCHANNEL;
   
   P1OUT ^= 0x02;                                // toggle P1.1 for debug
   
   magnetometer_get_measurement(mag_reading);
   mag_X = 0;
   mag_Y = 0;
   mag_Z = 0;
   
   mag_X |= mag_reading[0]<<8;
   mag_X |= mag_reading[1];

   mag_Y |= mag_reading[2]<<8;
   mag_Y |= mag_reading[3];
   
   mag_Z |= mag_reading[4]<<8;
   mag_Z |= mag_reading[5];
   
   //note: in the following I use functions for simple multiplications
   //and divisions for easy replacements in case the number of
   //instruction cycles is too large to be acceptable in this application
   mag_X = div_int(mag_X, 970);
   mag_Y = div_int(mag_Y, 970);
   mag_Z = div_int(mag_Z, 970);//970: look in HMC5843 datasheet

   XX = mul_int(mag_X,mag_X);
   YY = mul_int(mag_Y,mag_Y);
   ZZ = mul_int(mag_Z,mag_Z);
   
   mag_norm = XX + YY + ZZ; //no sqrt for faster execution
   filt_norm = LPF(mag_norm);
   
   //here we enter the state machine
   
   switch (state) {
   case NOCAR:
     if (filt_norm>=threshold){
       FSMcounter = 1;
       state = PERHAPS;
     }
     break; //else you break
     
   case PERHAPS:
     if (filt_norm >= threshold && FSMcounter < maxCount){
       FSMcounter++;
     }
     
     else if (filt_norm < threshold && FSMcounter >minCount)
       FSMcounter--;
     
     else if (filt_norm < threshold && FSMcounter <=minCount){
       state = NOCAR;
       FSMcounter=0;
       if(seenCar){
         seenCar=0;
         packetfunctions_reserveHeaderSize(testIntersection,1);
         testIntersection->payload[0] = seenCar;
         packetfunctions_reserveFooterSize(testIntersection,2);//space for radio to fill in CRC
         //send packet(noCar)
         radio_send(testIntersection);
       }
     }
     
     else if (filt_norm>=threshold && FSMcounter >=maxCount){
       state=CAR;
       if(!seenCar){
         seenCar=1;
         packetfunctions_reserveHeaderSize(testIntersection,1);
         testIntersection->payload[0] = seenCar;
         packetfunctions_reserveFooterSize(testIntersection,2);//space for radio to fill in CRC
         //send packet(Car)
         radio_send(testIntersection);
       }
     }
     break;
     
   case CAR:
     if (filt_norm < threshold){
       FSMcounter--;
       state = PERHAPS;
     }
     break;
   default:
     break;   
   }
   
}
Ejemplo n.º 5
0
bignum divide (bignum x, bignum y) {
  bignum z;
  z.len = x.len - y.len + 1;
  int len = x.len;
  z.num = calloc(len, sizeof(int));

  if (z.len <= 0) {
    z.len = 1;
    z.num[0] = 0;

    return z;
  }

  bignum tmp;
  tmp.len = y.len;
  tmp.num = calloc(tmp.len, sizeof(int));

  int i;
  for (i = 0; i < tmp.len; i++)
    tmp.num[tmp.len - i - 1] = x.num[x.len - i - 1];

  i = x.len - y.len;
  while (i >= 0) {
    int a = 0;
    int b = BASE - 1;

    while (a < b) {
      int c = (a + b) / 2 + 1;

      if (greater(mul_int(y, c), tmp))
        b = c - 1;
      else
        a = c;
    }

    z.num[i] = a;

    tmp = minus(tmp, mul_int(y, a));

    int j;
    for (j = tmp.len - 2; j >= 0; j--)
      tmp.num[j + 1] = tmp.num[j];

    i--;
    if (i >= 0)
      tmp.num[0] = x.num[i];
  }

  free(tmp.num);

  while (z.len > 0 && z.num[z.len - 1] == 0)
    z.len--;

  if (z.len == len)
    return z;
  else {
    bignum d;
    d.len = z.len;
    d.num = calloc(d.len, sizeof(int));

    memcpy(d.num, z.num, d.len * sizeof(int));
    free(z.num);

    return d;
  }
}