void addSum(long long* buf, int max) {
// The sum from 1 to max is going to be the sum of the average values, which is max*(max+1)/2
// Note that the numerator will always be even since either max or max+1 is even.
long long sum = (long long)max*(max+1)/2;
*buf += sum;
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("This program requires a positive number as its second argument.\n");
return 0;
}
long long buffer = 0;
int sum_to = atoi(argv[1]);
addSum(&buffer, sum_to);
printf("%lli\n", buffer);
}
```
---
## Run Times
* We rely upon humans for fundamental optimizations
| O level | O | O2 | O3 |
| version 1 | 2.228s | 0.302s | 0.195s |
| version 4 | 0.759s | 0.294s | 0.180s |
| version 5 | 0.607s | 0.196s | 0.186s |
| version 6 | 0.435s | 0.175s | 0.173s |
| version 7 | 0.532s | 0.076s | 0.188s |
| version 8 | 0.142s | 0.039s | 0.038s |
| version 9 | 0.001s | 0.000s | 0.000s |
---
## Premature Optimization
* So we can, at times, out-think the compiler with a good algorithm
* If we had spent time stressing about that function though, it likely would have been pointless
* Unless we called this function *a lot*
---
## Best Practices
* In reality, you can't stress about evey piece of code
* So first write for correctness
* Then let your compiler optimize
* [https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)
* Then use a tool to profile your code
* `gprof` or similar
* Other options for non-C languges
---
## Example Question
**Q** Which of the following will **not** resolve a read-after-write hazard between two instructions?
a. A no-op bubble in between them.
b. Data forwarding from the first to the second.
c. An out of order executed instruction in between them.
d. Branch prediction.
---
## Example Question
**Q** Consider an fp-8 type with 1 bit sign, 4 bits exponent, and 3 bits significand. Whenever all three exponent bits are set, the value is either NaN or +/- infinity. Exponent bias is -8. What is the maximum value this type can store? You may write the equation rather than reducing it to a single number.
\_\_\_\_\_\_\_\_\_\_\_\_
---
## Example Question Answer
* The exponent range is 0-15, but the bias always reduces it so that there is one more negative value
* So -8 to +7
* The significand is 3 bits, so it is treated as a fraction out of 8
* The maximum is $\frac{7}{8}$
* The answer is $2^7*(1+\frac{7}{8})$, which is 240
---
## Example Question
**Q** Incrementing the largest floating point value less than 1 will:
a. Overflow from the significand and increment the exponent.
b. Result in an exponent value equal to the bias.
c. Leave the value in the significand equal to 0.
d. All of the above.
---
## Example Question
**Q** What is immediate addressing?
a. When an operand loads data from an offset into the memory referred to by a register.
b. When the operand is specified within the instruction itself.
c. When an operand is within one of the general purpose registers.
d. None of the above.
---
## Example Question
**Q** Which of these statments is **false** about the x86-64 instruction set?
a. It supports C-style array access through indirect memory access instructions.
b. Compared to other instruction sets, the x86-64 ISA has a large number of instructions.
c. The x86-64 ISA only supports 64-bit operands.
d. The same registers can be referred to with multiple names to use different amounts of the registers.