# Assignment2: RISC-V Toolchain (placeholder)
Contributed by kc71486
###### tags: `RISC-V`, `jserv`
## Install riscv rv32emu toolchain
Follow the instruction from [this note](https://hackmd.io/@sysprog/rJAufgHYS)<br>
Ignore `Configure $PATH` part, and add riscv-none-elf-gcc and rv32emu into `~/.bashrc` instead.
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/bashrc.png"><br>
Then `source` the .bashrc file
```bash
source .bashrc
```
## Pick a Program
The following question is picked from the Assignment 1
> 唐飴苹 Calculate the Hamming Distance using Counting Leading Zeros
> The Hamming Distance between two integers is defined as the number of differing bits at the same position when comparing the binary representations of the integers. For example, the Hamming Distance between 1011101 and 1001001 is 2.
> In the assignment, I implement the program to calculate the Hamming Distance between the two given 64-bit unsigned integers.
[Source code](https://github.com/yptang5488/Computer-Architecture)
The original implementation of the questions is as follows.
```c=
int32_t HammingDistance_c(uint64_t x0, uint64_t x1) {
int Hdist = 0;
int16_t max_digit = 64 - (int16_t)count_leading_zeros((x0 > x1)? x0 : x1);
while(max_digit > 0){
uint64_t c1 = x0 & 1;
uint64_t c2 = x1 & 1;
if(c1 != c2) Hdist += 1;
x0 = x0 >> 1;
x1 = x1 >> 1;
max_digit -= 1;
}
return Hdist;
}
```
(redacted)
<br>The reason I choose this question is that hamming code is used in error correcting code, which is widely used in main memory, especially in servers, where reliability is very important.
### Compile / excecute the code
(redacted)
#### gcc O0 optimization
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/O0_v0.png" width=100% alt="image of O0 optimization"><br>
#### gcc O1 optimization
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/O1_v0.png" width=100% alt="image of O1 optimization"><br>
#### gcc O2 optimization
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/O2_v0.png" width=100% alt="image of O2 optimization"><br>
#### gcc -Os optimization
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/Os_v0.png" width=100% alt="image of s0 optimization"><br>
#### gcc -Ofast optimization
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/Ofast_v0.png" width=100% alt="image of Ofast optimization"><br>
#### gcc -O3 optimization
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/O3_v0.png" width=100% alt="image of O3 optimization"><br>
#### original assembly
<img src="https://github.com/kc71486/Computer-Architecture/raw/main/hw2/img/as_v0.png" width=100% alt="image of assembly optimization"><br>
## Optimize the program
(redacted)