Exemplo n.º 1
0
/*
 * Bitpack_fitss() - This function takes a signed 64 bit value
 *                   and a field width and returns a bool indicating whether
 *                   or not the given value can be represented in "width" 
 *                   bits.
 */
bool Bitpack_fitss(int64_t n, unsigned width)
{
        if (n >= (int64_t) ZERO)
                return Bitpack_fitsu((uint64_t) n, width - 1);

        return (Bitpack_fitsu((uint64_t)(~n), width - 1));
}
Exemplo n.º 2
0
bool Bitpack_fitss(int64_t n, unsigned width){
    assert( width <= 64);
    uint64_t abs_n = (uint64_t) n;
    if(n < 0)
       abs_n = ~n + ((uint64_t) 1);
    return Bitpack_fitsu(abs_n, width-1);
}
Exemplo n.º 3
0
static inline uint32_t Bitpack_newu(uint32_t word, unsigned width, unsigned lsb, 
                            uint32_t value) {

    /* It should be a checked run-time error if width does not satisfy 
     * 0 ≤ w ≤ 32.
     * It should be a checked run-time error to width and lsb do not 
     * satisfy w + lsb ≤ 32.*/
    assert(width <= MAX_WIDTH);
    assert((width + lsb) <= MAX_WIDTH);

    /* If given unsigned value does not fit in width signed bits, it must 
     * raise
     * the exception Bitpack Overflow. */
    if (Bitpack_fitsu(value, width) != true) {
        RAISE(Bitpack_Overflow);
        assert(0);
    }

    /* Create the mask. */
    uint32_t left_field = shift_leftu(1, MAX_WIDTH - width - lsb) - 1;;
    left_field = shift_leftu(left_field, width + lsb);
    uint32_t right_field = shift_leftu(1, lsb) - 1;
    uint32_t mask = left_field + right_field;
    
    /* Use mask to get the new word. */
    uint32_t result = (word & mask) | shift_leftu(value, lsb);

    return result;              
}
Exemplo n.º 4
0
uint64_t Bitpack_newu(uint64_t word, unsigned width, unsigned lsb,
                    uint64_t value){
   assert((width+lsb) < 64);
   if(!Bitpack_fitsu(value, width))
        RAISE(Bitpack_Overflow); 
   uint64_t temp = pow(2, width) - 1;
   temp = ~(temp << (lsb));
   word = (word & temp )| (value << (lsb));
   return word;
}
Exemplo n.º 5
0
static inline uint64_t Bitpack_newu(uint64_t word, unsigned width, 
                                    unsigned lsb, uint64_t value) {
  unsigned hi = lsb + width; // one beyond the most significant bit
  assert(hi <= 64);
  if (!Bitpack_fitsu(value, width))
    RAISE(Bit_Overflow);
  return shl(shr(word, hi), hi)              // high part
       | shr(shl(word, 64 - lsb), 64 - lsb)  // low part
       | (value << lsb);                     // new part
}
Exemplo n.º 6
0
uint64_t Bitpack_newu(uint64_t word, unsigned width, unsigned lsb,
                      uint64_t value)
{
        unsigned hi = lsb + width; /* one beyond the most significant bit */
        assert(hi <= 64);
        if (!Bitpack_fitsu(value, width))
                RAISE(Bitpack_Overflow);
        return shl(shr(word, hi), hi)                 /* high part */
                | shr(shl(word, 64 - lsb), 64 - lsb)  /* low part  */
                | (value << lsb);                     /* new part  */
}
Exemplo n.º 7
0
bool Bitpack_fitss( int64_t n, unsigned width)
{	
	assert(!(width > 64));
	/*Base case, will be changed by the end of the function*/
	if(n < 0)
	{
		uint64_t result = ~n;
		result++;
		return Bitpack_fitsu(result, width-1);
	}
	else return Bitpack_fitsu((uint64_t)(n), width-1;
}
Exemplo n.º 8
0
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;
}
Exemplo n.º 9
0
uint64_t Bitpack_news(uint64_t word, unsigned width, unsigned lsb,
	int64_t value)
{	
	assert(lsb > 0);
	assert(!((with + lsb) >= 64));
	if(!(Bitpack_fitsu(value, width))) RAISE(Bitpack_Overflow);	
	uint64_t allones = 1;
	for(unsigned i = width; i != 1; i /= 2)
	{
		allones *= 2;
	}
	allones--; /*Converts to all 1's*/
	value = value & allones; /*Converts to unsigned*/
	return Bitpack_newu(word, width, lsb, value);
}
Exemplo n.º 10
0
/* creating a new word using the provided value, width, and lsb */
uint64_t Bitpack_newu(uint64_t word, unsigned width, unsigned lsb, 
                      uint64_t value)
{
        assert((width <= 64) && ((width + lsb) <= 64));
        /* making sure the value fits in the space */
        if (!Bitpack_fitsu(value, width)) RAISE(Bitpack_Overflow);

        uint64_t mask = ~0;
        /* shifting the mask to get the correct position */
        mask = mask >> (num_of_bits - width) << lsb;
        mask = ~mask;

        word = (word & mask);
        return (word | (value << lsb));
}
Exemplo n.º 11
0
/*
 * Bitpack_newu() - 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 an unsigned 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_newu(uint64_t word, unsigned width, unsigned lsb,
                      uint64_t value)
{
        assert((width + lsb) <= 64);

        if (! Bitpack_fitsu(value, width)) RAISE(Bitpack_Overflow);
        
        uint64_t mask;

        mask = ~(make_mask(width, lsb));
        word = (word & mask);
        word = (word | shift_left(value, lsb));

        return word;
}
Exemplo n.º 12
0
uint64_t Bitpack_newu(uint64_t word, unsigned width, unsigned lsb,
	uint64_t value)
{
	assert(!((with + lsb) >= 64));
	if(!(Bitpack_fitsu(value, width))) RAISE(Bitpack_Overflow);
	uint64_t allones = 1;
	for(unsigned i = width; i != 1; i /= 2)
	{
		allones *= 2;
	}
	allones--; /*Converts to all 1's*/
	uint64_t mask = allones << lsb;
	mask = ~mask;
	word = word & mask;
	word = word | (value << lsb);
	return word;
}