[libc-commits] [libc] [libc][complex] implement different flavors of `creal` and `cimag` functions (PR #113300)
via libc-commits
libc-commits at lists.llvm.org
Sun Oct 27 21:49:28 PDT 2024
================
@@ -0,0 +1,21 @@
+//===-- Implementation of cimag function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/complex/cimag.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, cimag, (_Complex double x)) {
+ double *xCmplxPtr = reinterpret_cast<double *>(&x);
----------------
lntue wrote:
Probably we should introduce a separate number-pair type for direct access to complex numbers inside `libc/src/__support/complex_type.h`:
```
template <typename T>
struct Complex {
T real = T(0);
T imag = T(0);
};
```
Then we can implement this without `reinterpret_cast`:
```
Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
return x_c.imag;
```
https://github.com/llvm/llvm-project/pull/113300
More information about the libc-commits
mailing list