pulp-platform / ri5cy_gnu_toolchain

22 stars 23 forks source link

Floating-point printf output incorrect #409 #45

Open haresh-seenivasagan opened 3 months ago

haresh-seenivasagan commented 3 months ago

I am attempting to run a simple C program on a PULPino RISC-V platform, but I am encountering issues with floating-point printf output. The program performs some basic floating-point operations (addition and subtraction) and then attempts to print the results using printf. However, the output is incorrect, with the floating-point values not being printed as expected.

Here is the C code:

c

include

int main() { float a = 4.2; float b = 3.1; float c; float d;

c = a + b; d = a - b;

printf("Toplam=%f", c); printf("Fark=%f", d);

return 0;

}

I am using the following toolchain and settings:

RISC-V GCC compiler : https://github.com/pulp-platform/ri5cy_gnu_toolchain TARGET_C_FLAGS=" -m32 -g -fsingle-precision-constant -mfpdouble=float "

GCC_MARCH="IMFDXpulpv2 -mhard-float"

When I examine the generated assembly code (before it's converted to the s19 format), I notice that the floating-point values are being loaded into integer registers (e.g., a4) instead of floating-point registers (e.g., fa0) before the printf calls. This seems to be causing the incorrect output.

Furthermore, when I try to compile the code with optimization disabled (-O0), I get the following error:

error: insn does not satisfy its constraints:

}

(insn 54 30 31 2 (set (reg:DF 14 a4) (reg:DF 47 fa5 [orig:80 D.2652 ] [80])) /home/har/Documents/pulpino/sw/apps/helloworld/helloworld.c:21 342 {*movdf_hardfloat_rv32} (nil))

This error suggests that the compiler is trying to generate a movdf (double-precision move) instruction that doesn't satisfy the constraints of the target machine.

However, none of these attempts have resolved the issue.

I suspect that there might be a problem with how the compiler is handling floating-point values and register allocation for the PULPino platform, possibly related to the f extension for single-precision floating-point support.

I would appreciate any insights or suggestions on how to resolve this issue. Please let me know if you need any additional information.

Thank you for your help!

Unlimitosu commented 3 months ago

I think you used F extension for floating point arithmetic but it doesn't work. idk ri5cy toolchain supports floating point arithmetic and if not, I would try with original riscv-gnu-toolchain. It may not be helpful.. because I'm a junior here.

haresh-seenivasagan commented 3 months ago

@Unlimitosu if you have pulpino and modelsim setup could you have a try running the simple float c code on your environment with the same C_FLAGS and MARCH ?