CSAPP datalab实验

1、bitAnd

(1)Instructions

  • 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

(2)代码

德·摩根定律:
非(A且B) = (非A) 或(非B)
非(A或B) = (非A) 且(非B)

int bitAnd(int x, int y) 
{
   	int z;
  	z=~((~x)|(~y));
 	return z
}

2、getByte

(1)Instructions

  • 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

(2)代码
int数据有四个字节,一个字节有8位,所以取第n个字节,即取第n<<3位开始的8位。
要获取x中的第n个字节,可以考虑先将要得到的字节移位到第0个字节的位置,然后将其与0xff进行&运算。

int getByte(int x, int n) 
{
	int y,t,z;
	t=x>>(n<<3);
	y=0xff;
	z=t&y;
	return z; 
}

3、logicalShift

(1)Instructions

  • 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

(2)代码
算数右移是在左边补最高位,逻辑右移是是补0。
可以先将int型数据进行算数右移,然后将其与最高n位为0其他位为1的数[~((1<<31)>>(n-1))]进行&运算使得最高的n位变为0。

int logicalShift(int x, int n) 
{
	int z,t,y;
	y=x>>n;
	t=(1<<31)>>n;
	t=~(t<<1);
	z=y&t;
	return z;
}

4、bitCount

(1)Instructions

  • bitCount - returns count of number of 1’s in word
  • Examples: bitCount(5) = 2, bitCount(7) = 3
  • Legal ops: ! ~ & ^ | + << >>
  • Max ops: 40
  • Rating: 4

代码
Divide and Conquer~
计算1的个数就相当于计算x的二进制串的所有位的和。首先将int型数据x的32位分成16组,并进行X31+X30,X29+X28,…,X3+X2,X1+X0的运算;然后将x分成8组,并进行X31+X30+X29+X28,…,X3+X2+X1+X0的运算。依次类推,接着将x分成4组,2组并进行相应的运算。最后只剩下1组,此时将所有的位进行相加即得到了最终结果。

int bitCount(int x) 
{
	int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, y = 0;
	x1 = 0x55 | (0x55 << 8);
	x1 = x1 | (x1 << 16);
	x2 = 0x33 | (0x33 << 8);
	x2 = x2 | (x2 << 16);
	x3 = 0x0f | (0x0f << 8);
	x3 = x3 | (x3 << 16);
	x4 = 0xff | (0xff << 16);
	x5 = 0xff | (0xff << 8);

	y = (x & x1) + ((x >> 1) & x1);
	y = (y & x2) + ((y >> 2) & x2);
	y = (y & x3) + ((y >> 4) & x3);
	y = (y & x4) + ((y >> 8) & x4);
	y = (y & x5) + ((y >> 16) & x5);
	return y;
}

5、bang

(1)Instructions

  • bang - Compute !x without using !
  • Examples: bang(3) = 0, bang(0) = 1
  • Legal ops: ~ & ^ | + << >>
  • Max ops: 12
  • Rating: 4

(2)代码
因为每个非0的int型数据和它的相反数进行 | 运算后,最高位为1;如果x为0,进行上述操作后最高位仍为0。

int bang(int x) 
{
 	int y,z;
	y=x|((~x)+1);
	z=y>>31+1;
	return z;
}

6、tmin

(1)Instructions

  • tmin - return minimum two’s complement integer
  • Legal ops: ! ~ & ^ | + << >>
  • Max ops: 4
  • Rating: 1

(2)代码
最小二进制补码整数即符号位为1,其他位全为0。

int tmin(void) 
{
  	int x;
	x=1<<31;
    return x;
}

7、fitsBits

(1)Instructions

  • 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型数据x可以表示为n位二进制补码整数(其中1 <= n <= 32),则返回1,否则返回0。
n位二进制能表示的最大整数为最高位为0,其他位为1;最小数为最高位为1,其他位为0。我们可以将x右移n-1位(n+(~0))后,再将其与符号位比较,两者相同说明x可以表示位n位二进制数。

int fitsBits(int x, int n) 
{
 	int y,z,t;
	y=x>>31;
	z=x>>(n+(~0));
	t=!(y^z);
	return t;
}

8、divpwr2

(1)Instructions

  • 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

(2)代码
除法运算,对于非负数是默认向0取整;但对负数来说,需要字啊移位之前加一个偏置量(biasing)处理一下,即2^k-1。

int divpwr2(int x, int n) 
{
	int sign = 0, var = 0;
	sign = x >> 31;
	var = (1 << n) + (~0);
	return (x + (sign & var)) >> n;
}

9、negate

(1)Instructions

  • negate - return -x
  • Example: negate(1) = -1.
  • Legal ops: ! ~ & ^ | + << >>
  • Max ops: 5
  • Rating: 2

(2)代码
x相反数就是x取反+1

int negate(int x) 
{
	int z;
	z=(~x)+1;
	return z;
}

10、isPositive

(1)Instructions

  • isPositive - return 1 if x > 0, return 0 otherwise
  • Example: isPositive(-1) = 0.
  • Legal ops: ! ~ & ^ | + << >>
  • Max ops: 8
  • Rating: 3

(2)代码
对于int型数据x,如果x > 0,返回1,否则返回0。
设flag(x)表示x的符号,+为0,-为1;

当x>0时,isPositive(x)=1,flag(x)=0,!x=0;
当x<0时,isPositive(x)=0,flag(x)=1,!x=0;
当x=0时,isPositive(x)=0,flag(x)=0,!x=1;

因为当flag(x)和!x均为0时,isPositive(x)返回1;所以可以将flag和!x进行 | 运算,然后将得到的结果取 ! 。

int isPositive(int x) 
{
  	int flag,z;
	flag=(x>>31)&0x1;
	z=!(t|(!x));
	return z;
}

11、isLessOrEqual

(1)Instructions

  • isLessOrEqual - if x <= y then return 1, else return 0
  • Example: isLessOrEqual(4,5) = 1.
  • Legal ops: ! ~ & ^ | + << >>
  • Max ops: 24
  • Rating: 3

(2)代码
对于int型数据x和y,如果x <= y,则返回1,否则返回0。
可以先求出两个数的差值t,即t<=0返回1,t>0返回0;然后参考isPositive函数。

int isLessOrEqual(int x, int y) 
{
  	int m = 0, n = 0, r = 0, result = 0,t = 0;
	m = (x >> 31) & 0x1;
	n = (y >> 31) & 0x1;
	r = !(m ^ n);
	result = x + (~y) + 1;
	t=(r & ((result >> 31) | (!result))) | ((!r) & m);
	return t;
}

12、ilog2函数

(1)Instructions

  • ilog2 - return floor(log base 2 of x), where x > 0
  • Example: ilog2(16) = 4
  • Legal ops: ! ~ & ^ | + << >>
  • Max ops: 90
  • Rating: 4

(2)代码
返回log2x的值,返回结果为int型。
这个函数就是要找到最接近一个数n使得2n最接近x,且满足2n <= x。因此可以先将int型数据x右移16位,并进行两次取反操作,如果得到的值为1,则说明x的高16位中存在至少一个1,那么result应加上16;如果得到的值为0,则说明高16位中不存在1。然后再将x右移(result+8)位,同样进行两次取反操作,如果得到的值为1,则说明在(result+8)和(result+15)这8位中至少有一个1,那么result应加上8;如果得到的值为0,则说明这8位中不存在1。依次类推,继续将x右移(result+4),(result+2),(result+1)位,并进行同样的操作即可得到最终结果。

int ilog2(int x) 
{
	int y = 0;
	y = (!!(x >> 16)) << 4;
	y = y + ((!!(x >> (y + 8))) << 3);
	y = y + ((!!(x >> (y + 4))) << 2);
	y = y + ((!!(x >> (y + 2))) << 1);
	y = y + (!!(x >> (y + 1)));
	return y;
}

13、float_neg

(1)Instructions

  • 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

(2)代码
返回浮点参数f的表达式-f的位等效项。参数和结果都作为无符号int传递,但是它们将被解释为单精度浮点值的位级表示。 当参数为NaN时,返回参数。
函数的参数可能为NaN,所以需要进行判断。如果参数uf的第23位到第30位全为1,而且uf的低23位不为0,这说明uf解释为单精度浮点值的位级表示时是一个NaN,所以应该直接返回,否则应直接将uf的最高位(符号位)取反即可得到-f。

unsigned float_neg(unsigned uf) 
{
	unsigned w,x,y,z,t;
	w=(1<<23)-1;
	x=0xff<<23;
	y=uf&w;
	z=uf&x;
	if ((z==x)&&y)
    {
		return uf;
	}
	t=(1<<31)^uf;
	return t;
}

14、float_i2f

(1)Instructions

  • 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 a single-precision floating point values.
  • Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  • Max ops: 30
  • Rating: 4

(2)代码
返回表达式(浮点数)x的等价位。 结果以unsigned int形式返回,但是将其解释为单精度浮点值的位级表示。

unsigned float_i2f(int x) 
{
	unsigned sign = 0, enow = 0, fnow = 0, absx = x,
	shiftLeft = 0, tail = 0, result = 0;
	unsigned pos = 1 << 31;
	if (x == 0) 
	{
		return 0;
	}
	else if (x < 0) 
	{
		absx = -x;
		sign = pos;
	}
	while ((pos & absx) == 0) 
	{
		absx <<= 1;
		shiftLeft += 1;
	}
	enow = 127 + 31 - shiftLeft;
	tail = absx & 0xff;
	fnow = (~(pos >> 8)) & (absx >> 8);
	result = sign | (enow << 23) | fnow;
	if (tail > 0x80) 
	{
		result += 1;
	} 
	else if (0x80 == tail)
	{
		if (fnow & 1) 
		{
		    result += 1;
		}
	}
	return result;	
}

15、float_twice

(1)Instructions

  • 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

(2)代码
返回浮点参数f的表达式2 * f的位等效项。 参数和结果都作为unsigned int传递,但是它们将被解释为的位级表示。单精度浮点值。当参数为NaN时,返回参数。

unsigned float_twice(unsigned uf) 
{
	unsigned sign = 0, enow = 0, fnow = 0;
	unsigned pos = 1 << 31;
	unsigned frule = (1 << 23) - 1;
	if (uf == 0) 
	{
		return 0;
	}
	if (uf == pos) 
	{
		return uf;
	}
	sign = uf & pos;
	enow = (uf >> 23) & 0xff;
	if (enow == 0xff) 
	{
		return uf;
	}
	fnow = uf & frule;
	if (enow == 0)
	{
		fnow = fnow << 1;
		if (fnow & (1 << 23)) 
		{
			fnow = fnow & frule;
			enow += 1;
		}
	} 
	else 
	{
		enow += 1;
	}
	return sign | (enow << 23) | fnow;
}
  • 23
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值