CS/ECE 5785/6785: Homework 2
(back to: main 5785 page)
You must solve the following problems on your own (not in groups).
You may discuss the problems with other students but you must not
discuss specific solutions, borrow code from another student, look at
another student's code, look for code on the web, or ask for solutions
on mailing lists or message boards. All of these are cheating.
This question refers to ``Getting the least out of your C compiler.''
When you compile code, compile it using the CodeWarrior compiler for
Coldfire. Turn on as many optimization options as possible. Put all
of the C code that you write for this problem into a single file
Write two C functions, each taking a short argument and
returning a char, that implement the logic in the code
examples from Section 4.2. Answer the following two questions in your
homework. Is the generated code different? If so, is it harmlessly
different, or is one of them actually worse? Why? Also hand in
commented versions of the two assembly language functions output by
the compiler, where you explain the effect of each instruction. Don't
state the obvious (i.e., ``add instruction adds two numbers'') but
rather explain why the instructions together implement the desired
functionality. If the compiler emits the same code for both of your
functions, then of course you only need to explain one of them.
Do the same thing for the second set of code examples in Section 4.9.
The function foo should take a pointer to char as argument
and return an int. You should not write this function, just give the
compiler a prototype.
Do the same thing for the code described in Section 4.10. You should
write two functions, one containing a loop counting up to 100, one
counting down. The loops should be summing up the elements of a
100-element global array. Each function takes no arguments and
returns an int (the sum).
Saturating arithmetic operations are those where overflowing
results "stick" at the maximum or minimum representable result. In
other words, if you are using saturating addition and subtraction
operators, (INT_MAX + 1) == INT_MAX and (INT_MIN - 1) == INT_MIN. See
this header file. It provides the prototypes
for four saturating arithmetic operations that you must implement.
Your functions should live in a file called sat_ops.c that
you create. Some other requirements:
- Your code must compile without warnings using three different
compilers: CodeWarrior for ColdFire and both Clang and GCC for x86-64.
For GCC and Clang, make sure you use the -Wall option to
enable extra warnings.
- You must write C, no assembly language.
- Your code must not execute any math operations with undefined
behavior, such as shift past bitwidth or signed overflow. To help you
test this, we have provided a hacked compiler based on Clang. Mostly
Clang can be invoked just like GCC. To see how this checking works,
try compiling and running this program like
50 lab3-1:~> /home/regehr/llvm-inst/bin/clang -fcatch-undefined-behavior -L/home/regehr/lib -O undef.c -o undef
51 lab3-1:~> ./undef
CLANG UNDEFINED at : Op: +, Reason : Signed Addition Overflow, BINARY OPERATION: left (int32): 2147483647 right (int32): 1
The code you hand in must not produce any of these errors when compiled
using the checking Clang.
You may use the C preprocessor to write macros and to include files,
but you must not use conditional compilation. If you choose to write
macros and your macros are incorrect, points will be deducted.
Your code must be independent of the bitwidth of the operations. In
other words, the same exact C code must correctly implement the
saturating operations no matter which of the typedefs at the top of
sat_ops.h is enabled.
You will not receive credit for incorrect code. In other words,
test your code before handing it in. Be creative and try to test
different combinations of values that exercise various corner cases in
The file you handin should not include a main function
Do not put the text of sat_ops.h into your C file! rather,
include it using the #include directive
Big hint: You do not get a correct sat_signed_add by
implementing it as sat_signed_add (a, -b). I won't explain
this further, it's not hard to work out the reason on your own.
We will total up the number of bytes of x86-64 object code and
ColdFire object code generated by each student's sat_ops.c
file when compiled with gcc's -Os option, and when the
objects being operated on are 32-bit ints. The student with the
lowest byte count in each of these categories will receive a prize.
What to Hand In
This assignment is due BEFORE CLASS on Thursday September 23.
This homework requires electronic handin of two C files, each
answering one of the parts.
The handin directory is "hw2".
See the submission instructions for more details.
This page is maintained by John Regehr, mail me if you find a
mistake or if any content is unclear.