Issue 89668
Summary Fixed point integral sqrt (uksqrtui) crashes with LIBC_FAST_MATH and produces an incorrect value without LIBC_FAST_MATH
Labels bug, libc
Assignees lntue
Reporter PiJoules
    ```
#include <stdio.h>
#include <stdint.h>

extern "C" unsigned _Accum uksqrtui(uint32_t);

int main() {
  unsigned _Accum x = uksqrtui(65529);
  printf("%f\n", (float)x);
}
```

This will crash with `integer divide by zero` on an x86-64 linux. It looks like the main reason for this is the `FastFractType` for  `SqrtConfig<unsigned int>` is a 16-bit `unsigned fract`, so when we get to `FracType r = a * x_frac + b;`, we end up with `(0x820c * 0xfff9) // 2**16 + 0x7df8 == 0x10000` but we only retain the lower 16 bits for an unsigned fract. This is with `LIBC_FAST_MATH`.

Without `LIBC_FAST_MATH`, the error seems less clear and this will produce a value of `3.953979`. The final decimal result for either of these modes should be around `255.98632775990205`.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to