[clang] [Clang][Headers] Fix tgmath.h conj() of a real argument (PR #244854)
Brad Smith via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 21 01:02:10 PST 2026
https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/244854
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
>From 113cbecba2bdc0258390c53a5116653019be9586 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
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 <noreply at anthropic.com>
---
clang/lib/Headers/tgmath.h | 6 +++---
clang/test/Headers/tgmath-conj.c | 20 ++++++++++++++++++++
2 files changed, 23 insertions(+), 3 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..866dcca6834106 100644
--- a/clang/lib/Headers/tgmath.h
+++ b/clang/lib/Headers/tgmath.h
@@ -1277,15 +1277,15 @@ static long double
static float _Complex
_TG_ATTRS
- __tg_conj(float __x) {return __x;}
+ __tg_conj(float __x) {return conjf(__x);}
static double _Complex
_TG_ATTRS
- __tg_conj(double __x) {return __x;}
+ __tg_conj(double __x) {return conj(__x);}
static long double _Complex
_TG_ATTRS
- __tg_conj(long double __x) {return __x;}
+ __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 {{%.*}}
More information about the cfe-commits
mailing list