Finish Data Lab

This commit is contained in:
cyp0633 2022-03-25 20:13:16 +08:00
parent e55a744aab
commit 23705e632c
Signed by: cyp0633
GPG Key ID: E1BC508A994A5138
2 changed files with 38 additions and 10 deletions

View File

@ -228,16 +228,11 @@ int tmin(void)
* Max ops: 15
* Rating: 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);
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
}
/*
@ -355,7 +350,40 @@ unsigned float_neg(unsigned uf)
*/
unsigned float_i2f(int x)
{
return 2;
int sign, exp = 0, frac = 0, i = 0;
if (x == 0) // Two special situations, cannot be calculated normally
{
return 0;
}
if (x == 0x80000000)
{
return 0xcf000000;
}
sign = (x >> 31) & 1; // Get the sign bit & abs(x)
if (sign)
{
x = -x;
}
i = 30; // Not 31 coz the top bit is always 0
while (!(x >> i))
{
i--;
}
exp = i + 127; // Get the exponent. i is the E, unbiased
x = x << (31 - i); // Left shift to clean zeroes, right shift to fit 23 bits...
frac = (x >> 8) & 0x7fffff; // ...to get the fraction
x = x & 0xff;
frac += (x > 0x80) || ((x == 0x80) && (frac & 0x01)); // Round up if fraction is greater than 0.5
if (frac >> 23)
{
exp++;
frac = frac & 0x7fffff;
}
return (sign << 31) | (exp << 23) | frac;
}
/*
* float_twice - Return bit-level equivalent of expression 2*f for
@ -385,7 +413,7 @@ unsigned float_twice(unsigned uf)
{
return uf;
}
// if ((exp >> 23) + 1 == 0xff) // exponent overflow
// if ((exp >> 23) + 1 == 0xff)
if (exp == 0x7f000000) // exponent overflow
{
return sign | 0x7f800000;

Binary file not shown.