- 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) {
return 2;
}
/*
* 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) {
int offsetValue = 0x1;
int offsetIndex = 0;
int andValue = 0;
int signValue;
while (offsetIndex < 31)
{
signValue = (uf & offsetValue) >> offsetIndex;
if (offsetIndex < 23)
{
andValue = andValue | signValue;
}
else
{
andValue = andValue & signValue;
}
offsetIndex += 1;
offsetValue <<= 1;
}
if (andValue)
{
return uf;//NaN
}
return uf ^ offsetValue;
}
/*
* 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
*/
unsigned float_i2f(int x) {
return 2;
}
/*
* 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) {
int signIndex = 31;
int expIndex = 23;
int offsetValue = 0x1;
int offsetSign = offsetValue << signIndex;
int andValue = 1;
int orValue = 0;
int signValue;
int offsetIndex = expIndex;
while (offsetIndex < signIndex)
{
signValue = (uf & (offsetValue << offsetIndex)) >> offsetIndex;
andValue = andValue & signValue;
orValue = orValue | signValue;
offsetIndex += 1;
}
if (andValue == 1)//exp==255
{
return uf;
}
else if (orValue == 0)//非规格化
{
signValue = !!(uf & offsetSign);
uf <<= 1;
if (signValue == 0)
{
return uf & (~offsetSign);
}
return uf | offsetSign;
}
else
{
signValue = ((uf >> expIndex) + 1) << expIndex;
offsetIndex = expIndex;
while (offsetIndex < signIndex)
{
uf &= ~(offsetValue << offsetIndex);
offsetIndex += 1;
}
return uf | signValue;
}
}
|