黃書堯
This sqrtf RISC-V code originates from Quiz3 - Problem C.
Below is an explanation of the code logic.
We have a floating point number and we want to calculate its square root.
A floating-point number can be represented as : .
where is the exponent part of and is the mantissa part of .
Since
The square root of the exponent part can be easily calculated by right-shifting the exponent by 1 bit.
We aim to compute , without using division instructions.
Instead, we can achieve this using multiplication operations only with:
From the above, we realize that we only need to calculate .
The formula of the Newton-Raphson method is as follows:
Assuming the function is:
The iterative form becomes:
The B extension part shows as follows:
This line sets the hidden leading bit of the mantissa. To remove the B extension dependency, we replace it with lui and or.
To emulate the code, follow these steps:
Initially, an error might occur:
To resolve this, change the system language to English (United States) via:
Select Setting -> Region & Language -> Language English(United States).
After changing the language, rerun:
This should pass all tests.
To evaluate the performance of the sqrt.S implementation, we can utilize the performance counter example available in rv32emu/tests/perfcounter.
Makefile:main.c:Initially, the compilation might result in an error:
To resolve this, rename the function and file from sqrtf to my_sqrt.
After removing the B extension, the test output is: