admin 管理员组文章数量: 887021
2023年12月21日发(作者:自动局部变量的作用域定义)
/* * Instructions to Students: * * STEP 1: Read the following instructions carefully. *//*You will provide your solution to the Data Lab byediting the collection of functions in this source R CODING RULES:
Replace the "return" statement in each function with one or more lines of C code that implements the function. Your code
must conform to the following style: int Funct(arg1, arg2, ...) { /* brief description of how your implementation works *//* int var1 = Expr1; ... int varM = ExprM; varJ = ExprJ; ... varN = ExprN; return ExprR; }/* Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations ! ~ 4. Binary integer operations & ^ | + << >>
Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line. You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&, ||, -, or ?: 6. Use any form of casting. 7. Use any data type other than int. This implies that you cannot use arrays, structs, or unions.
You may assume that your machine: 1. Uses 2s complement, 32-bit representations of integers. 2. Performs right shifts arithmetically. 3. Has unpredictable behavior when shifting an integer by more than the word ES OF ACCEPTABLE CODING STYLE: /* * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31 */// int pow2plus1(int x) {// /* exploit ability of shifts to compute powers of 2 */// return (1 << x) + 1;// } /* * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
* pow2plus4 - returns 2^x + 4, where 0 <= x <= 31 */// int pow2plus4(int x) {// /* exploit ability of shifts to compute powers of 2 */// int result = (1 << x);// result += 4;// return result;// }/*FLOATING POINT CODING RULESFor the problems that require you to implent floating-point operations,the coding rules are less strict. You are allowed to use looping andconditional control. You are allowed to use both ints and can use arbitrary integer and unsigned are expressly forbidden to: 1. Define or use any macros. 2. Define any additional functions in this file. 3. Call any functions. 4. Use any form of casting. 5. Use any data type other than int or unsigned. This means that you cannot use arrays, structs, or unions. 6. Use any floating point data types, operations, or : 1. Use the dlc (data lab checker) compiler (described in the handout) to
check the legality of your solutions. 2. Each function has a maximum number of operators (! ~ & ^ | + << >>) that you are allowed to use for your implementation of the function.
The max operator count is checked by dlc. Note that '=' is not
counted; you may use as many of these as you want without penalty.
3. Use the btest test harness to check your functions for correctness. 4. Use the BDD checker to formally verify your functions 5. The maximum number of ops for each function is given in the header comment for each function. If there are any inconsistencies
between the maximum ops in the writeup and in this file, consider this file the authoritative source./* * STEP 2: Modify the following functions according the coding rules. *
* IMPORTANT. TO AVOID GRADING SURPRISES:grading surprised * 1. Use the dlc compiler to check that your solutions conform * to the coding rules. * 2. Use the BDD checker to formally verify that your solutions produce
* the correct answers. */#endif/*
* 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
* floating point argument f. 返回浮点型f乘以2的结果 * 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 f = uf; if ((f & 0x7F800000) == 0) //如果阶码全为0
{ f = ((f & 0x007FFFFF) << 1) | (0x80000000 & f);//考虑符号位,左移
} else if ((f & 0x7F800000) != 0x7F800000)//阶码并不是全为1
{ f =f + 0x00800000;//阶码+1
}//其他情况为inf或者nan,不处理直接返回
return f;
}
版权声明:本文标题:深入理解计算机系统实验二datalab 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1703147375h439906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论