main(void) { div_char(); div_short(); div_int(); div_long(); }
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; } }
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; }
Bignum* power(Bignum *base, Bignum *exp, Bignum *r) { Bignum aux1, aux2; int resto; if (exp->len == 1 && exp->digits[0] == '0') return create_int(1,r); if (exp->len == 1 && exp->digits[0] == '1') return copy(r,base); div_int(exp,2,&aux1,&resto); power(base,&aux1,&aux2); if (resto == 0) return multiply(&aux2,&aux2,r); return multiply(multiply(&aux2,&aux2,&aux1),base,r); }
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; } }