<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/86871>86871</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [builtins] divtc3 accuracy issue
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            compiler-rt:builtins
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          vzakhari
      </td>
    </tr>
</table>

<pre>
    I am getting quite incorrect result for `_Complex __float128` division with clang 17.0.6.  GCC 13.2.0 version returns closer result.

I used libquadmath for printing the results.  Here is a reproducer:
```
#include <complex.h>
#include <quadmath.h>
#include <stdio.h>

#define complex128 _Complex __float128

int main() {
  char buffer[1000];
  complex128 z1 = {4.181214e+4931Q, -1.113838e+4932Q};
  complex128 z2 = {-1.113838e+4932Q, -4.181214e+4931Q};
  quadmath_snprintf(buffer, sizeof buffer, "%.6Qe", __real__ z1);
  printf("z1: %s ", buffer);
 quadmath_snprintf(buffer, sizeof buffer, "%.6Qe", __imag__ z1);
 printf("%s\n", buffer);
  quadmath_snprintf(buffer, sizeof buffer, "%.6Qe", __real__ z2);
  printf("z2: %s ", buffer);
 quadmath_snprintf(buffer, sizeof buffer, "%.6Qe", __imag__ z2);
 printf("%s\n", buffer);
  z1 /= z2;
  quadmath_snprintf(buffer, sizeof buffer, "%.6Qe", __real__ z1);
  printf("z1/z2: %s ", buffer);
  quadmath_snprintf(buffer, sizeof buffer, "%.6Qe", __imag__ z1);
  printf("%s\n", buffer);
  return 0;
}
```

Compilation with `clang testdiv.c -I ${GCC_INSTALL}/lib/gcc/x86_64-pc-linux-gnu/13.2.0/include ${GCC_INSTALL}/lib64/libquadmath.a -lm --gcc-toolchain=${GCC_INSTALL}`:
```
z1: 4.181214e+4931 -1.113838e+4932
z2: -1.113838e+4932 -4.181214e+4931
z1/z2: -0.000000e+00 1.000000e+00
```

This is close to the correct mathematical result: https://www.wolframalpha.com/input?i=%284.181214e%2B4931+-1.113838e%2B4932i%29%2F%28-1.113838e%2B4932+-4.181214e%2B4931i%29

When I link the builtins library with `clang testdiv.c -I ${GCC_INSTALL}/lib/gcc/x86_64-pc-linux-gnu/13.2.0/include ${GCC_INSTALL}/lib64/libquadmath.a -lm ${CLANG_INSTALL}/lib/clang/17/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a --gcc-toolchain=${GCC_INSTALL}` I get this:
```
z1: 4.181214e+4931 -1.113838e+4932
z2: -1.113838e+4932 -4.181214e+4931
z1/z2: 1.680232e-4932 1.680232e-4932
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVl1v4jgX_jXm5iiRfUI-uOCihTfzIlUjVTvSXiLjGOKtkzC2U9r--pWdQGGgo1nN7uwilA_bz3M-cvwcc2vVrpVyTtJ7ki4nvHd1Z-bPb_yp5kZNNl31Ol8Bb2AnnVPtDr72yklQreiMkcKBkbbXDradAZLR9aJr9lq-wHq91R13DAuSUajUs7Kqa-GgXA1C83YHLI9pnMUAnxYLYEmMMYVnacIyI11vWgtCd1aa0UZM6JLQu-G6gt7KCrTafO151XBXBxf2RrXBTVfLEWZjgP9LI0FZ4GDk3nRVL6QhyUhFMjr-h1dMVCt0X0kgyUIM8cQ1Sf53a_5o_cMF1lWqO589rqnkVrUSRgMMC7iVvDOMah00XLUEC4IzIPn9MA4gam5g02-30pD0nlFKSbokyfv8u403BiRZevA0ZgVDNpUE76ezhD0SXEDEYsaSIinGUXwk-UdMeGS6AfJU1wYuqI6ZW9s2fLUtwWKMARdg1ZvstvA-QBAJpnH2KMPTAtZrI7ler-GNEZydEZ_YCOIbI8kdEEwtjLAj4xni5z1RDd9de3LuiHeBpIv2Yy_-xoTgdxKCvzQh-DMJ8bWKpS8y7_UvLxwsfyhZ_1j5_NV0DaIJ9DTk99tNhQtXLzZKc3eSZZLRQZmd9KL1HAuIVkBwSvL7T4vFevX5ty93Dw-eFUutNgTLnRAEy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