/*
* 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);
}
/*
* 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) {
int t=0xff;
return t&(x>>(n<<3));
}
/*
* 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 t=(1<<31)&x;
return (x>>n)^((t>>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 sum=0;
int Mask=1|(1<<8);
Mask=Mask|(Mask<<16);
sum=x&Mask;
sum+=(x>>1)&Mask;
sum+=(x>>2)&Mask;
sum+=(x>>3)&Mask;
sum+=(x>>4)&Mask;
sum+=(x>>5)&Mask;
sum+=(x>>6)&Mask;
sum+=(x>>7)&Mask;
sum+=(sum>>16);
sum+=(sum>>8);
return sum&0xff;
}
/*
* 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)&1;
}
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return (1<<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 t=33+(~n);
return !(x^((x<<t)>>t));
}
/*
* 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 bias=(x>>31)&((1<<n)+(~0));
return (x+bias)>>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));
}
/*
* 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 signx=x>>31;
int signy=y>>31;
int signSame=((x+(~y))>>31)&(!(signx^signy));
int signDiffer=signx&(!signy);
return signDiffer|signSame;
}
/*
* 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) {
int sum1,sum2,sum3,sum4,sum5;
sum1=(!!(x>>16))<<4;
x=x>>sum1;
sum2=(!!(x>>8))<<3;
x=x>>sum2;
sum3=(!!(x>>4))<<2;
x=x>>sum3;
sum4=(!!(x>>2))<<1;
x=x>>sum4;
sum5=(!!(x>>1));
return sum1+sum2+sum3+sum4+sum5;
}
/*
* 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=uf^0x80000000;
if((uf&0x7fffffff)>0x7f800000)
result=uf;
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 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) {
unsigned sign=0,shiftleft=1,flag=0;
unsigned absx=x;
if( x==0 ) return 0;
if( x<0 )
{
sign=0x80000000;
absx=(~x+1);
}
while((absx&0x80000000)==0)
{
absx<<=1;
shiftleft++;
}
absx<<=1;
if( (absx & 0x01ff) > 0x0100 ) flag=1;
if( (absx & 0x03ff) == 0x0300 ) flag=1;
return sign+(absx>>9)+((159-shiftleft)<<23)+flag;
}
/*
* 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) {
unsigned e = uf & 0x7F800000;
unsigned sign = uf & 0x80000000;
if (e) {
if ( e != 0x7F800000 ) {
uf = uf + 0x00800000;
}
}
else
uf = ( uf << 1) | sign ;
return uf;
}
ICS datalab总结
最新推荐文章于 2022-07-11 19:09:12 发布