This commit is contained in:
cyp0633 2022-03-23 10:50:12 +08:00
parent bf5acaaeb8
commit 1dad907eed
170 changed files with 3508 additions and 3440 deletions

View File

@ -129,7 +129,6 @@ NOTES:
* the correct answers.
*/
#endif
/*
* bitAnd - x&y using only ~ and |
@ -138,9 +137,10 @@ NOTES:
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
int bitAnd(int x, int y)
{
// Just discrete mathematics. XY=\overline{X+Y}.
return ~(x|y);
return ~((~x) | (~y));
}
/*
* getByte - Extract byte n from word x
@ -150,16 +150,10 @@ int bitAnd(int x, int y) {
* Max ops: 6
* Rating: 2
*/
int getByte(int x, int n) {
return 2;
int getByte(int x, int n)
{
int mask = 0xff; // Leave only the last 2 bytes
return (x >> (n << 3) & mask); // Right move 8n bits, i.e. n bytes
}
/*
* logicalShift - shift x to the right by n, using a logical shift
@ -169,8 +163,11 @@ int getByte(int x, int n) {
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
return 2;
int logicalShift(int x, int n)
{
int topMask = 0x1 << (32 + ~n); // 32+~n=31-n, to get the top mask
int lowerMask = (0x1 << (32 + ~n)) + ~0;
return (x >> n) & (topMask | lowerMask);
}
/*
* bitCount - returns count of number of 1's in word
@ -179,8 +176,22 @@ int logicalShift(int x, int n) {
* Max ops: 40
* Rating: 4
*/
int bitCount(int x) {
return 2;
int bitCount(int x)
{
int mask = (0x55) | ((0x55) << 8); // 0x00005555
mask = mask | (mask << 16); // 0x55555555
x = (x & mask) + (x >> 1 & mask); // 2-bit sum
mask = (0x33) | ((0x33) << 8); // 0x00003333
mask = mask | (mask << 16); // 0x33333333
x = (x & mask) + (x >> 2 & mask); // 4-bit sum
mask = (0x0f) | (0x0f << 8); // 0x00000f0f
mask = mask | (mask << 16); // 0x0f0f0f0f
x = (x & mask) + (x >> 4 & mask); // 8-bit sum
mask = (0xff) | (0xff << 16); // 0x00ff00ff
x = (x & mask) + (x >> 8 & mask); // 16-bit sum
mask = (0xff) | (0xff << 8); // 0x0000ffff
x = (x & mask) + (x >> 16 & mask); // 32-bit sum
return x;
}
/*
* bang - Compute !x without using !
@ -189,8 +200,14 @@ int bitCount(int x) {
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
return 2;
int bang(int x)
{
x = (x >> 16) | x;
x = (x >> 8) | x;
x = (x >> 4) | x;
x = (x >> 2) | x;
x = (x >> 1) | x;
return ~x & 0x1;
}
/*
* tmin - return minimum two's complement integer
@ -198,8 +215,9 @@ int bang(int x) {
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return 2;
int tmin(void)
{
return 1 << 31;
}
/*
* fitsBits - return 1 if x can be represented as an
@ -210,9 +228,18 @@ int tmin(void) {
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n) {
return 2;
// int fitsBits(int x, int n)
// {
// int moveBits = 33 + ~n; // Equals to 32-n
// int result = (x << moveBits) >> moveBits; // Left move, and then right move
// return !(x ^ result); // If okay, result should be equal to x
// }
int fitsBits(int x, int n)
{
n = n + 31;
return !(((x >> n) + 1) >> 1);
}
/*
* divpwr2 - Compute x/(2^n), for 0 <= n <= 30
* Round toward zero
@ -221,8 +248,10 @@ int fitsBits(int x, int n) {
* Max ops: 15
* Rating: 2
*/
int divpwr2(int x, int n) {
return 2;
int divpwr2(int x, int n)
{
int bias = (x >> 31) & ((1 << n) + ~0);
return (x + bias) >> n;
}
/*
* negate - return -x
@ -231,8 +260,9 @@ int divpwr2(int x, int n) {
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return 2;
int negate(int x)
{
return ~x + 1;
}
/*
* isPositive - return 1 if x > 0, return 0 otherwise
@ -241,8 +271,9 @@ int negate(int x) {
* Max ops: 8
* Rating: 3
*/
int isPositive(int x) {
return 2;
int isPositive(int x)
{
return !(!(x)) & !((x >> 31) & 1);
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
@ -251,8 +282,12 @@ int isPositive(int x) {
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
return 2;
int isLessOrEqual(int x, int y)
{
int val = ((x + ~y) >> 31)&1;
x = x >> 31;
y = y >> 31;
return ((x & 1) | !y) & (((x & 1) & !y) | (val));
}
/*
* ilog2 - return floor(log base 2 of x), where x > 0
@ -261,8 +296,30 @@ int isLessOrEqual(int x, int y) {
* Max ops: 90
* Rating: 4
*/
int ilog2(int x) {
return 2;
int ilog2(int x)
{
int mask;
x = (x >> 1) | x;
x = (x >> 2) | x;
x = (x >> 4) | x;
x = (x >> 8) | x;
x = (x >> 16) | x;
mask = (0x55) | ((0x55) << 8); // 0x00005555
mask = mask | (mask << 16); // 0x55555555
x = (x & mask) + (x >> 1 & mask); // 2-bit sum
mask = (0x33) | ((0x33) << 8); // 0x00003333
mask = mask | (mask << 16); // 0x33333333
x = (x & mask) + (x >> 2 & mask); // 4-bit sum
mask = (0x0f) | (0x0f << 8); // 0x00000f0f
mask = mask | (mask << 16); // 0x0f0f0f0f
x = (x & mask) + (x >> 4 & mask); // 8-bit sum
mask = (0xff) | (0xff << 16); // 0x00ff00ff
x = (x & mask) + (x >> 8 & mask); // 16-bit sum
mask = (0xff) | (0xff << 8); // 0x0000ffff
x = (x & mask) + (x >> 16 & mask); // 32-bit sum
return x + ~0;
}
/*
* float_neg - Return bit-level equivalent of expression -f for
@ -275,8 +332,17 @@ int ilog2(int x) {
* Max ops: 10
* Rating: 2
*/
unsigned float_neg(unsigned uf) {
return 2;
unsigned float_neg(unsigned uf)
{
int c = 0x00ffffff;
if ((~(uf << 1)) < c)
{
return uf;
}
else
{
return uf ^ (0x80000000);
} // 01110000 11110000
}
/*
* float_i2f - Return bit-level equivalent of expression (float) x
@ -287,7 +353,8 @@ unsigned float_neg(unsigned uf) {
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x) {
unsigned float_i2f(int x)
{
return 2;
}
/*
@ -301,6 +368,7 @@ unsigned float_i2f(int x) {
* Max ops: 30
* Rating: 4
*/
unsigned float_twice(unsigned uf) {
unsigned float_twice(unsigned uf)
{
return 2;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.