datalab-handout代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#if 0
#endif
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
return ~((~x)|(~y)); /*de Morgan*/
}
/*
* getByte - Extract byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByte(0x12345678,1) = 0x56
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int getByte(int x, int n) {
return x>>(n<<3)&0xff;
}
/*
* logicalShift - shift x to the right by n, using a logical shift
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
int y=1;
return (~(((y<<31)>>n)<<1))&(x>>n); /*等效于&(0xffffffff>>n消除负数右移时高位补1产生的影响*/
}
/*
* bitCount - returns count of number of 1's in word
* Examples: bitCount(5) = 2, bitCount(7) = 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 40
* Rating: 4
*/
int bitCount(int x) {
int n=0x1|0x1<<8|0x1<<16|0X1<<24; /*相当于把n定义成1个有四个元素的数组,每个元素都为1*/
int count=0;
count+=x&n; /*对x的每一位与n相与,把结果加到count中,count也相当于一个四个元素的数组*/
count+=x>>1&n;
count+=x>>2&n;
count+=x>>3&n;
count+=x>>4&n;
count+=x>>5&n;
count+=x>>6&n;
count+=x>>7&n;
return (count&0xf)+(count>>8&0xf)+(count>>16&0xf)+(count>>24&0xf);/*把count的每八位移到低八位,作为一个单独的数相加*/
}
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
return (((x|(~x+1))>>31)&0x1)^0x1;/*-x=(~x+1),x如果非0,则(x|(~x+1)的符号位为1,如果为0,反之。最后把符号位与 1 做异或运算*/
}
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return 0x1<<31; /*返回最小二进制整数*/
}
/*
* fitsBits - return 1 if x can be represented as an
* n-bit, two's complement integer.
* 1 <= n <= 32
* Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n) {
int shift=33+~n; /*32+(~n+1)=32-n*/
return !(x^((x<
/*int y=x>>(n+~0);
x=x>>(n+~0);
x=x+(x&1);
return !x;*/
}
/*
* divpwr2 - Compute x/(2^n), for 0 <= n <= 30
* Round toward zero
* Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int divpwr2(int x, int n) {
int flag=x>>31;
int bias=(1<
时需要加大小为2^n-1的偏置值 书p65*/
return ((flag&bias)+x)>>n;
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return (~x+1);
}
/*
* isPositive - return 1 if x > 0, return 0 otherwise
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 3
*/
int isPositive(int x) {
return (~(x>>31))&(!!x); /* !!x表示x等于零时为零,否则为1 */
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
int difference=(~x+1)+y; /*y-x*/
return ((x&~y)|(x&~difference)|(~y&~difference))>>31&0x1; /*通过x,y,difference的卡诺图求解*/
}
/*
* ilog2 - return floor(log base 2 of x), where x > 0
* Example: ilog2(16) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int ilog2(int x) {
x=x>>1; /*通过计算把1右移到的次数来实现ilog2的函数*/
x=x|x>>1;
x=x|x>>2;
x=x|x>>4;
x=x|x>>8;
x=x|x>>16; /* 利用或和右移,把1后的所有0变为1*/
int count=0;
int n=0x1|0x1<<8|0x1<<16|0X1<<24;
count+=x&n;
count+=x>>1&n;
count+=x>>2&n;
count+=x>>3&n;
count+=x>>4&n;
count+=x>>5&n;
count+=x>>6&n;
count+=x>>7&n;
return (count&0xf)+(count>>8&0xf)+(count>>16&0xf)+(count>>24&0xf); /* 即BitCount函数 */
}
/*
* float_neg - Return bit-level equivalent of expression -f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representations of
* single-precision floating point values.
* When argument is NaN, return argument.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_neg(unsigned uf) {
unsigned result;
unsigned temp;
result=uf^0x80000000;/* 求uf的相反数*/
temp=uf&(0x7fffffff);/*去除符号位*/
if(temp>0x7f800000)
result=uf; /*如果uf是NAN,原数返回*/
return result;
}
/*
* float_i2f - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of al
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x) {
unsigned n=0;
unsigned temp;
unsigned flag=0;
unsigned frac=x;
unsigned s=0;
if (x==0) return 0;
if (x<0)
{
s=0x80000000;
frac=-x;
}
while(1) /*循环操作,得到小数字段*/
{
temp=frac;
frac<<=1;
n++;
if (temp & 0x80000000) break;
}
if (((frac&0x01ff)>0x0100)||((frac&0x03ff)==0x0300)) /*对超过float精度表示范围的数的取舍*/
flag=1;
return s+((159-n)<<23)+(frac>>9)+flag; /*159-n=127+32-n*/
}
/*
* float_twice - Return bit-level equivalent of expression 2*f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representation of
* single-precision floating point values.
* When argument is NaN, return argument
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_twice(unsigned uf) {
if ((uf&0x7F800000)==0) /*非规格化数*/
uf=(((uf&0x007FFFFF)<<1)|(0x80000000&uf));
else if((uf&0x7F800000)!=0x7F800000)*/*规格化数*/
uf=uf+0x00800000;
return uf;
}