uint64_t Bitpack_news(uint64_t word, unsigned width, unsigned lsb, int64_t value){ if(!Bitpack_fitss(value, width)) RAISE(Bitpack_Overflow); assert(Bitpack_fitss(value, width) && 0 < lsb && (width+lsb) < 64); uint64_t placeholders = pow(2, width) - 1; value = value & placeholders; return (Bitpack_newu(word, width, lsb, value)); }
uint64_t Bitpack_news(uint64_t word, unsigned width, unsigned lsb, int64_t value) { if (!Bitpack_fitss(value, width)) RAISE(Bitpack_Overflow); /* thanks to Michael Sackman and Gilad Gray */ return Bitpack_newu(word, width, lsb, Bitpack_getu(value, width, 0)); }
/* * Bitpack_news() - This function takes an unsigned 64 bit word, a field * width to update within that word, the least significant * bit of the field to be updated, and a signed value to * insert into the field being updated. This function * returns the updated word. * * note: if the word being inserted is too large for its * field width, the exception Bitpack_Overflow is * raised. */ uint64_t Bitpack_news(uint64_t word, unsigned width, unsigned lsb, int64_t value) { if (! Bitpack_fitss(value, width)) RAISE(Bitpack_Overflow); return Bitpack_newu(word, width, lsb, Bitpack_getu(value, width, ZERO)); }
int main(int argc, char* argv[]) { (void) argc; (void) argv; if(Bitpack_fitsu(5,3)) printf("5 fits 3 unsigned\n"); if(!Bitpack_fitsu(8,3)) printf("8 doesn't fit 3 unsigned\n"); if(Bitpack_fitss(1, 3)) printf("1 fits 3 signed\n"); if(!Bitpack_fitss(5,3)) printf("5 doesn't fit 3 singed\n"); if(Bitpack_fitss(-8, 4)) printf("-8 fits 4 signed\n"); if(!Bitpack_fitss(-100000000000000, 4)) printf("too big doesn't fit\n"); if(!Bitpack_fitsu(5, 0)) printf("unsigned width 0\n"); if(!Bitpack_fitss(5, 0)) printf("signed width 0\n"); unsigned long temp = ~7; fprintf(stderr, "%lu\n", ~temp); //uint64_t width = atoi(argv[1]); //uint64_t lsb = atoi(argv[2]); //uint64_t value = atoi(argv[3]); /*uint64_t unsignedValue; printf("%lu %lu %lu\n", width, lsb, value); uint64_t i = Bitpack_newu(unsignedValue, width, lsb, value); printf("%lu stored in %lu bit: %lu\n", value, lsb, Bitpack_getu(i, width, lsb)); */ //unsigned u = 20; int a = 275; uint64_t j = Bitpack_newu(j, 9, 23, a); printf("val: %d lsb: %d bit: %ld\n", a, 23, Bitpack_getu(j, 9, 23)); j = Bitpack_news(j, 5, 18, -15); //j = Bitpack_newu(j, 5, 13, 31); printf("val: %d lsb: %d bit: %ld\n", a, 23, Bitpack_getu(j, 9, 23)); return 0; }
/* creaing a new word using the provided value, width and lsb */ uint64_t Bitpack_news(uint64_t word, unsigned width, unsigned lsb, int64_t value) { assert((width <= 64) && ((width + lsb) <= 64)); /* making sure the value fits in the space */ if (!Bitpack_fitss(value, width)) RAISE(Bitpack_Overflow); uint64_t val = value; uint64_t mask = ~0; /* shifting the mask to get the correct position */ mask = mask >> (num_of_bits - (width)); mask = (mask & val); mask = mask << lsb; return (word | mask); }