stnolting / neorv32

:desktop_computer: A small, customizable and extensible MCU-class 32-bit RISC-V soft-core CPU and microcontroller-like SoC written in platform-independent VHDL.
https://neorv32.org
BSD 3-Clause "New" or "Revised" License
1.51k stars 209 forks source link

FPU Float to signed integer #942

Open Quma78 opened 2 weeks ago

Quma78 commented 2 weeks ago

Describe the bug Hello, When I replaying the floating_point_test, I came across an error on converting a float to a signed integer. It seems that the rounding of values exceeding 32 bits with the mantissa at zero is not good. To be sure, here's the code I used:

To Reproduce

int main() {

  uint32_t err_cnt = 0;
  uint32_t err_cnt_total = 0;
  uint32_t test_cnt = 0;
  uint32_t i = 0;
  float_conv_t opa;
  float_conv_t opb;
  float_conv_t res_hw;
  float_conv_t res_sw;

  // setup UART at default baud rate, no interrupts
  neorv32_uart0_setup(BAUD_RATE, 0);

  // capture all exceptions and give debug info via UART
  neorv32_rte_setup();

/////////////// test //////////////////////////
neorv32_uart0_printf("\nStart Float to signed integer\n");
for(uint32_t i=0; i < 256; i++){
  uint32_t f_value = 0;
  f_value += (i << 24);
  opa.binary_value = f_value;

  res_hw.binary_value = (uint32_t)riscv_intrinsic_fcvt_ws(opa.float_value);
  res_sw.binary_value = (uint32_t)riscv_emulate_fcvt_ws(opa.float_value);
  err_cnt += verify_result(i, opa.binary_value, 0, res_sw.binary_value, res_hw.binary_value);
}
neorv32_uart0_printf("Float to signed integer finished! \n");
return 0;
///////////////////////////////////////////////
}

Expected behavior The rounding should be 0x80000000 but it is just 0.

Screenshots

Capture

Environment:

Hardware:

Thank you for your help!

stnolting commented 2 weeks ago

Hey @Quma78!

I just tested your code an I can confirm the bug. Thanks for finding this! I'll try to come up with a fix. 🙈

Hardware version: 1.9.4

Btw, you should really update your version of the core. :wink: There were lots of improvements (and FPU bug fixes) since this version (see changelog).