or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Assignment2: RISC-V Toolchain
contributed by Terry7Wei7
Rewrite Implement priority encoder using CLZ by 倪英智 https://hackmd.io/@NIYINGCHIH/BkvgNE3e6
Motivation: I choose the problem Implement priority encoder using CLZ from 倪英智. One common application of using the CLZ (Count Leading Zeros) instruction is in interrupt handling within computer architectures. During the interrupt handling process, the system needs to determine the priorities of different interrupt requests to decide which one should be processed. The CLZ instruction is helpful in determining the priority of interrupt requests because it efficiently calculates the number of leading zero bits in the input.
In this case, I will attempt to enhance program performance.
Original c code
command line
Optimized by riscv-none-elf-gcc
O0
O1
O2
O3
Os
Ofast
Don't put the screenshots which contain plain text only.
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Optimization c code
Use Bitwise Operations Instead of Multiplication and Division: In the count_leading_zeros function, you use bitwise operations and a series of additions, but when calculating the number of 1s in x, you can use more bitwise operations instead of additions. For instance, you can use x = (x * 0x200040008001ULL & 0x1111111111111111) % 0xf; to replace the final additions.
Use unsigned int Instead of uint16_t: On most platforms, unsigned int might be more efficient than uint16_t because it matches the native word size of the machine.
Reduce Function Call Overhead: In the count_leading_zeros function, you can directly calculate the leading zeros and priority encoding in the main function instead of splitting it into two function calls. This can reduce the overhead of function calls.
Here is a slightly optimized version of your code:
O0
O1
O2
O3
OS
Ofast
Don't put the screenshots which contain plain text only.
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Assembly code
RISC-V
Analysis
The original and optimized C code has both been optimized using riscv-none-elf-gcc. After applying -O2 optimization, it can be observed that the cycle count cannot be further reduced. It has reached its limit.
src
https://github.com/riscv-non-isa/riscv-arch-test https://github.com/sysprog21/rv32emu https://github.com/sysprog21/rv32emu/blob/master/tests/ticks.c https://github.com/sysprog21/rv32emu/blob/master/tests/perfcounter/getcycles.S https://hackmd.io/@sysprog/SJAR5XMmi