owned this note
owned this note
Published
Linked with GitHub
# Assignment2: RISC-V Toolchain (GCD)
###### tags: `RISC-V`
> Rewrite assignment from [陳柏廷](https://hackmd.io/@Max-Chen/SJ4sTXoBD)
Since `__modsi3` is undefined in compiler. I'll try another algorithm(Binary GCD) for GCD.
```c=
#include <stdio.h>
int GCD (int x, int y) {
int xy_odd_ornot;
int i = 0;
while (x ^ y) {
xy_odd_ornot = (x & 1) & (y & 1);
if (!xy_odd_ornot) {
i += (~x & 1) & (~y & 1);
x >>= ~x & 1;
y >>= ~y & 1;
}
else {
int z = x - y;
x = y;
y = (z ^ (z>>31)) - (z>>31);
}
}
return x << i;
}
void _start()
{
int res = GCD(24, 16);
}
```
## -O3
`riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib GCD.c -o GCD`
```
./emu-rv32i GCD
>>> Execution time: 3300 ns
>>> Instruction count: 99 (IPS=30000000)
>>> Jumps: 8 (8.08%) - 2 forwards, 6 backwards
>>> Branching T=2 (16.67%) F=10 (83.33%)
```
#####
:::spoiler `riscv-none-embed-objdump -d GCD`
```cpp=
00010054 <GCD>:
10054: 00000313 li t1,0
10058: 06b50463 beq a0,a1,100c0 <GCD+0x6c>
1005c: 40b507b3 sub a5,a0,a1
10060: 41f7d613 srai a2,a5,0x1f
10064: 00b56733 or a4,a0,a1
10068: 00b576b3 and a3,a0,a1
1006c: 00f64633 xor a2,a2,a5
10070: fff54893 not a7,a0
10074: fff5c813 not a6,a1
10078: fff74713 not a4,a4
1007c: 01f7d793 srli a5,a5,0x1f
10080: 0016f693 andi a3,a3,1
10084: 0018f893 andi a7,a7,1
10088: 00187813 andi a6,a6,1
1008c: 00177713 andi a4,a4,1
10090: 00f607b3 add a5,a2,a5
10094: 00069e63 bnez a3,100b0 <GCD+0x5c>
10098: 41155533 sra a0,a0,a7
1009c: 4105d5b3 sra a1,a1,a6
100a0: 00e30333 add t1,t1,a4
100a4: fab51ce3 bne a0,a1,1005c <GCD+0x8>
100a8: 00651533 sll a0,a0,t1
100ac: 00008067 ret
100b0: 00058513 mv a0,a1
100b4: feb78ae3 beq a5,a1,100a8 <GCD+0x54>
100b8: 00078593 mv a1,a5
100bc: fa1ff06f j 1005c <GCD+0x8>
100c0: 00008067 ret
000100c4 <_start>:
100c4: 01000693 li a3,16
100c8: 01800713 li a4,24
100cc: 40d70633 sub a2,a4,a3
100d0: fff6c793 not a5,a3
100d4: 00e6f533 and a0,a3,a4
100d8: 41f65813 srai a6,a2,0x1f
100dc: fff74593 not a1,a4
100e0: 0017f793 andi a5,a5,1
100e4: 00157513 andi a0,a0,1
100e8: 00c84833 xor a6,a6,a2
100ec: 0015f593 andi a1,a1,1
100f0: 01f65613 srli a2,a2,0x1f
100f4: 40f6d7b3 sra a5,a3,a5
100f8: 00051c63 bnez a0,10110 <_start+0x4c>
100fc: 40b75733 sra a4,a4,a1
10100: 00e78663 beq a5,a4,1010c <_start+0x48>
10104: 00078693 mv a3,a5
10108: fc5ff06f j 100cc <_start+0x8>
1010c: 00008067 ret
10110: 00c807b3 add a5,a6,a2
10114: 00d78863 beq a5,a3,10124 <_start+0x60>
10118: 00068713 mv a4,a3
1011c: 00078693 mv a3,a5
10120: fadff06f j 100cc <_start+0x8>
10124: 00008067 ret
```
:::
#####
:::spoiler `riscv-none-embed-readelf -h GCD`
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x100c4
Start of program headers: 52 (bytes into file)
Start of section headers: 696 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 1
Size of section headers: 40 (bytes)
Number of section headers: 6
Section header string table index: 5
```
:::
#####
`riscv-none-embed-size GCD`
```
text data bss dec hex filename
212 0 0 212 d4 GCD
```
## -Os
`riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -Os -nostdlib GCD.c -o GCD`
```
./emu-rv32i GCD
>>> Execution time: 5200 ns
>>> Instruction count: 106 (IPS=20384615)
>>> Jumps: 16 (15.09%) - 7 forwards, 9 backwards
>>> Branching T=7 (53.85%) F=6 (46.15%)
```
#####
:::spoiler `riscv-none-embed-objdump -d GCD`
```cpp=
00010054 <GCD>:
10054: 00000713 li a4,0
10058: 00b51663 bne a0,a1,10064 <GCD+0x10>
1005c: 00e51533 sll a0,a0,a4
10060: 00008067 ret
10064: 00b577b3 and a5,a0,a1
10068: 0017f793 andi a5,a5,1
1006c: 02079c63 bnez a5,100a4 <GCD+0x50>
10070: 00b567b3 or a5,a0,a1
10074: fff7c793 not a5,a5
10078: 0017f793 andi a5,a5,1
1007c: 00f70733 add a4,a4,a5
10080: fff54693 not a3,a0
10084: fff5c793 not a5,a1
10088: 0016f693 andi a3,a3,1
1008c: 0017f793 andi a5,a5,1
10090: 40f5d7b3 sra a5,a1,a5
10094: 40d555b3 sra a1,a0,a3
10098: 00058513 mv a0,a1
1009c: 00078593 mv a1,a5
100a0: fb9ff06f j 10058 <GCD+0x4>
100a4: 40b50533 sub a0,a0,a1
100a8: 41f55793 srai a5,a0,0x1f
100ac: 00a7c7b3 xor a5,a5,a0
100b0: 01f55513 srli a0,a0,0x1f
100b4: 00a787b3 add a5,a5,a0
100b8: fe1ff06f j 10098 <GCD+0x44>
000100bc <_start>:
100bc: 01000593 li a1,16
100c0: 01800513 li a0,24
100c4: f91ff06f j 10054 <GCD>
```
:::
#####
#####
:::spoiler `riscv-none-embed-readelf -h GCD`
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x100bc
Start of program headers: 52 (bytes into file)
Start of section headers: 600 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 1
Size of section headers: 40 (bytes)
Number of section headers: 6
Section header string table index: 5
```
:::
#####
`riscv-none-embed-size GCD`
```
text data bss dec hex filename
116 0 0 116 74 GCD
```
| | O0 | O1 | O2 | O3 | Os |
| -------- | -------- | -------- | -------- | -------- | -------- |
| **Ex. time** | 10200ns | 4500ns | 3800ns | 3300ns | 4900ns |
| **IC** | 240 | 94 | 115 | 99 | 106 |
| **Jumps** | 16 (6.67%) 8for, 8back | 8 (6.96%) 1for, 7back | 8 (6.96%) 1for, 7back | 8 (8.08%) 2for, 6back | 16 (15.09%) 7for, 9back |
| **Branching** | T=7(53.85%) F=6(46.15%) | T=2(15.38%) F=11(84.62%) | T=6(46.15%) F=7(53.85%) | T=2(16.67%) F=10(83.33%) | T=7(53.85%) F=6(46.15%) |
> The fluctuating range of Ex. time is violent, it won't be used for reference.