-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitpack.c
More file actions
executable file
·159 lines (146 loc) · 4.66 KB
/
bitpack.c
File metadata and controls
executable file
·159 lines (146 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* bitpack.c
* by Ahmed Aly & Ryan Vasios, 2/28/14
* Assignment 4
*
* Interface for bitpack
*/
#include <bitpack.h>
#include <math.h>
#include <stdbool.h>
#include "stdio.h"
#include <except.h>
#include <assert.h>
Except_T Bitpack_Overflow = { "Overflow packing bits"};
/* Bitpack_fitsu take in a unsigned 64 bit integer and a width
* and returns either true if the integer could fit in a bit
* field of width bits or false if the integer can't fit.
*/
bool Bitpack_fitsu(uint64_t n, unsigned width)
{
uint64_t UpBound = pow(2, width);
if (width >= 1 && n < UpBound)
{
return true;
}
else {
RAISE(Bitpack_Overflow);
return false;
}
}
/* Bitpack_fitss take in a signed 64 bit integer and a width
* and returns either true if the integer could fit in a bit
* field of width bits or false if the integer can't fit.
*/
bool Bitpack_fitss(int64_t n, unsigned width)
{
int64_t LowBound = -1 * pow(2, width-1);
int64_t UpBound = pow(2, width-1) - 1;
if ( n >= LowBound && n <= UpBound)
{
return true;
}
else
{
RAISE(Bitpack_Overflow);
return false;
}
}
/* Bitpack_getu takes in an unsigned 64 bit word and extracts a
* field from a word given the width of the field and the location
* of the field’s least significant bit. The extracted field is
* returned in an unsigned 64 bit integer.
*/
uint64_t Bitpack_getu(uint64_t word, unsigned width, unsigned lsb)
{
if (width==0)
{
return 0;
}
else
{
uint64_t mask = (pow(2, width) - 1);
mask = mask << lsb;
uint64_t output = (mask & word) ;
output = output >> lsb;
return output;
}
}
/* Bitpack_gets takes in a signed 64 bit word and extracts a
* field from a word given the width of the field and the location
* of the field’s least significant bit. The extracted field is
* returned in an signed 64 bit integer.
*/
int64_t Bitpack_gets(uint64_t word, unsigned width, unsigned lsb)
{
if (width==0)
{
return 0;
}
if(Bitpack_getu(word, width, lsb) >= pow(2,width-1))
{
word = word >> lsb;
uint64_t mask = ~0;
mask = mask << width;
int64_t output = (mask | word);
return output;
}
else
return Bitpack_getu(word, width, lsb);
}
/* Bitpack_newu return a new word which is identical to the original
* word, except the field of width width with least significant
* bit at lsb will have been replaced by a width-bit representation of
* value. An exception is raised if any of the algebraic rules for width
* or lsb are not followed.
*/
uint64_t Bitpack_newu(uint64_t word, unsigned width, unsigned lsb,
uint64_t value)
{
TRY
Bitpack_fitsu(value,width);
if (width > (64-lsb))
{RAISE(Bitpack_Overflow);}
if (lsb >64)
{RAISE(Bitpack_Overflow);}
if (width > 64)
{RAISE(Bitpack_Overflow);}
EXCEPT(Bitpack_Overflow)
RAISE(Bitpack_Overflow);
END_TRY;
uint64_t mask = ~0;
mask = mask << (width + lsb);
uint64_t maskmerge = (pow(2, lsb) - 1);
mask = (mask | maskmerge);
word = ( word & mask );
word = ( word | (value << lsb));
return word;
}
/* Bitpack_news return a new word which is identical to the original
* word, except the field of width width with least significant
* bit at lsb will have been replaced by a width-bit representation of
* value. An exception is raised if any of the algebraic rules for width
* or lsb are not followed.
*/
uint64_t Bitpack_news(uint64_t word, unsigned width, unsigned lsb,
int64_t value)
{
TRY
Bitpack_fitss(value,width);
if (width > (64-lsb))
{RAISE(Bitpack_Overflow);}
if (lsb >64)
{RAISE(Bitpack_Overflow);}
if (width > 64)
{RAISE(Bitpack_Overflow);}
EXCEPT(Bitpack_Overflow)
RAISE(Bitpack_Overflow);
END_TRY;
uint64_t mask = ~0;
mask = mask << (width + lsb);
uint64_t maskmerge = (pow(2, lsb) - 1);
mask = (mask | maskmerge);
word = ( word & mask );
word = ( word | (value << lsb));
return word;
}