https://github.com/brad0 updated 
https://github.com/llvm/llvm-project/pull/244854

>From 1af36685f0a68b48fd3e5168a0a053b8c6e9c636 Mon Sep 17 00:00:00 2001
From: Brad Smith <[email protected]>
Date: Sat, 21 Nov 2026 02:27:27 -0500
Subject: [PATCH] [Clang][Headers] Fix tgmath.h conj() of a real argument

For a real argument, the type-generic conj() returned the argument
converted to complex, giving x + 0i. C11 7.25p5 requires the complex
function to be called, so the result should be conj(x + 0i), which
is x - 0i. Call conjf/conj/conjl for the real overloads, the same as
cproj() already does.

Fixes #244639

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 clang/lib/Headers/tgmath.h       | 14 +++++---------
 clang/test/Headers/tgmath-conj.c | 20 ++++++++++++++++++++
 2 files changed, 25 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/Headers/tgmath-conj.c

diff --git a/clang/lib/Headers/tgmath.h b/clang/lib/Headers/tgmath.h
index 7acf18b9dd3578..d8db7802d7bfee 100644
--- a/clang/lib/Headers/tgmath.h
+++ b/clang/lib/Headers/tgmath.h
@@ -1275,17 +1275,13 @@ static long double
 
 // conj
 
-static float _Complex
-    _TG_ATTRS
-    __tg_conj(float __x) {return __x;}
+static float _Complex _TG_ATTRS __tg_conj(float __x) { return conjf(__x); }
 
-static double _Complex
-    _TG_ATTRS
-    __tg_conj(double __x) {return __x;}
+static double _Complex _TG_ATTRS __tg_conj(double __x) { return conj(__x); }
 
-static long double _Complex
-    _TG_ATTRS
-    __tg_conj(long double __x) {return __x;}
+static long double _Complex _TG_ATTRS __tg_conj(long double __x) {
+  return conjl(__x);
+}
 
 static float _Complex
     _TG_ATTRS
diff --git a/clang/test/Headers/tgmath-conj.c b/clang/test/Headers/tgmath-conj.c
new file mode 100644
index 00000000000000..9e3573b1c9dc13
--- /dev/null
+++ b/clang/test/Headers/tgmath-conj.c
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -isystem %S/Inputs/include 
\
+// RUN:   -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+
+#include <tgmath.h>
+
+// C11 7.25p5: conj() of a real argument calls the complex function, so the
+// result is x - 0i, not x + 0i.
+
+float complex test_conj_float(float x) { return conj(x); }
+double complex test_conj_double(double x) { return conj(x); }
+long double complex test_conj_long_double(long double x) { return conj(x); }
+
+// CHECK-LABEL: define internal <2 x float> @_ZL9__tg_conjf(
+// CHECK:         store float -0.000000e+00, ptr {{%.*}}
+
+// CHECK-LABEL: define internal { double, double } @_ZL9__tg_conjd(
+// CHECK:         store double -0.000000e+00, ptr {{%.*}}
+
+// CHECK-LABEL: define internal { x86_fp80, x86_fp80 } @_ZL9__tg_conje(
+// CHECK:         store x86_fp80 -0.000000e+00, ptr {{%.*}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to