mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Jens Gustedt <Jens.Gustedt@inria.fr>
To: musl@lists.openwall.com
Subject: [PATCH 1/4] the CMPLX macros must be usable in initializations of static variables
Date: Wed, 26 Nov 2014 14:07:55 +0100	[thread overview]
Message-ID: <1417007215.28402.107.camel@eris.loria.fr> (raw)

Because of some boundary cases for infinities and negative zeros, doing
this properly is only possible with either support for _Imaginary or some
compiler magic.

For the moment, we only know such magic for clang and gcc. There it is
only available for newer compilers. Therefore we make the CMPLX macros
only available when in C11 mode (or actually even in C1X mode).

Internally for the compilation of the complex functions of the math
library we use such a macro, but that doesn't have the constraint of
being usable for static initializations. So if we are not in C11, we
provide such a macro as __CMPLX_NC in complex.h and map the CMPLX macros
internally to that.

As an effect this reverts commit faea4c9937d36b17e53fdc7d5a254d7e936e1755.
---
 include/complex.h   | 30 ++++++++++++++++++++++++++++--
 src/internal/libm.h |  8 ++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/include/complex.h b/include/complex.h
index 13a45c5..e88cf13 100644
--- a/include/complex.h
+++ b/include/complex.h
@@ -112,12 +112,38 @@ long double creall(long double complex);
 #define cimagf(x) __CIMAG(x, float)
 #define cimagl(x) __CIMAG(x, long double)
 
-#define __CMPLX(x, y, t) \
-	((union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z)
+#ifdef _Imaginary_I
+# define __CMPLX(x, y, t) ((t)(x) + _Imaginary_I*(t)(y))
+#else
+# define __CMPLX_I(x, y, t) ((t)(x) + _Complex_I*(t)(y))
+#endif
+
+#ifndef __CMPLX
+# if defined(__clang__)
+  /* Clang allows initializer lists for complex numbers and compound
+     literals for the initialization of static variables. */
+#  define __CMPLX(x, y, t) (+(_Complex t){ (x), (y) })
+# elif 100*__GNUC__+__GNUC_MINOR__ >= 407
+#  define __CMPLX(x, y, t) __builtin_complex((t)(x), (t)(y))
+# endif
+#endif
 
+#ifndef __CMPLX
+# if __STDC_VERSION__ >= 201000L
+#  warning for this compiler, macros CMPLX, CMPLXF and CMPLXL are not standard
+#  warning conforming for infinities and signed zeros
+#  define __CMPLX(x, y, t) __CMPLX_I(x, y, t)
+# endif
+# define __CMPLX_NC(x, y, t) (+(union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z)
+#else
+# define __CMPLX_NC(x, y, t) __CMPLX(x, y, t)
+#endif
+
+#if __STDC_VERSION__ >= 201000L
 #define CMPLX(x, y) __CMPLX(x, y, double)
 #define CMPLXF(x, y) __CMPLX(x, y, float)
 #define CMPLXL(x, y) __CMPLX(x, y, long double)
+#endif
 
 #ifdef __cplusplus
 }
diff --git a/src/internal/libm.h b/src/internal/libm.h
index ebcd784..f916e2e 100644
--- a/src/internal/libm.h
+++ b/src/internal/libm.h
@@ -155,4 +155,12 @@ long double __tanl(long double, long double, int);
 long double __polevll(long double, const long double *, int);
 long double __p1evll(long double, const long double *, int);
 
+/* complex */
+
+#ifndef CMPLX
+#define CMPLX(x, y) __CMPLX_NC(x, y, double)
+#define CMPLXF(x, y) __CMPLX_NC(x, y, float)
+#define CMPLXL(x, y) __CMPLX_NC(x, y, long double)
+#endif
+
 #endif
-- 
1.9.1



             reply	other threads:[~2014-11-26 13:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-26 13:07 Jens Gustedt [this message]
2014-12-02 17:49 ` Rich Felker
2014-12-02 19:10   ` Jens Gustedt
2014-12-02 19:42     ` Rich Felker
2014-12-02 22:00       ` Jens Gustedt
2014-12-02 22:47         ` Rich Felker
2014-12-03  9:18           ` Jens Gustedt
2014-12-03 14:38             ` Rich Felker
2014-12-03 15:12               ` Jens Gustedt
  -- strict thread matches above, loose matches on Subject: below --
2014-11-25 14:49 Jens Gustedt
2014-11-25 15:25 ` Szabolcs Nagy
2014-11-25 19:21   ` Szabolcs Nagy
2014-11-25 21:23     ` Jens Gustedt
2014-11-25 23:19       ` Szabolcs Nagy
2014-11-26  7:45         ` Jens Gustedt
2014-11-25 20:08   ` Jens Gustedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1417007215.28402.107.camel@eris.loria.fr \
    --to=jens.gustedt@inria.fr \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).