add creal/cimag macros in complex.h (and use them in the functions defs)
authorRich Felker <dalias@aerifal.cx>
Thu, 22 Mar 2012 19:54:55 +0000 (15:54 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 22 Mar 2012 19:54:55 +0000 (15:54 -0400)
include/complex.h
src/complex/cimag.c
src/complex/cimagf.c
src/complex/cimagl.c
src/complex/creal.c
src/complex/crealf.c
src/complex/creall.c
src/internal/libm.h

index 8ee7057..90496bd 100644 (file)
@@ -97,6 +97,17 @@ double creal(double complex);
 float crealf(float complex);
 long double creall(long double complex);
 
 float crealf(float complex);
 long double creall(long double complex);
 
+#define __CREALIMAG(x, t, i) \
+       ((union { _Complex t __z; t __xy[2]; }){(_Complex t)(x)}.__xy[i])
+
+#define creal(x) __CREALIMAG(x, double, 0)
+#define crealf(x) __CREALIMAG(x, float, 0)
+#define creall(x) __CREALIMAG(x, long double, 0)
+
+#define cimag(x) __CREALIMAG(x, double, 1)
+#define cimagf(x) __CREALIMAG(x, float, 1)
+#define cimagl(x) __CREALIMAG(x, long double, 1)
+
 #ifdef __cplusplus
 }
 #endif
 #ifdef __cplusplus
 }
 #endif
index 5e1ad46..0095564 100644 (file)
@@ -2,6 +2,5 @@
 
 double (cimag)(double complex z)
 {
 
 double (cimag)(double complex z)
 {
-       union dcomplex u = {z};
-       return u.a[1];
+       return cimag(z);
 }
 }
index 99fffc5..f7bcd76 100644 (file)
@@ -2,6 +2,5 @@
 
 float (cimagf)(float complex z)
 {
 
 float (cimagf)(float complex z)
 {
-       union fcomplex u = {z};
-       return u.a[1];
+       return cimagf(z);
 }
 }
index d9a0780..9ec24ee 100644 (file)
@@ -2,6 +2,5 @@
 
 long double (cimagl)(long double complex z)
 {
 
 long double (cimagl)(long double complex z)
 {
-       union lcomplex u = {z};
-       return u.a[1];
+       return cimagl(z);
 }
 }
index 2bb9181..f670304 100644 (file)
@@ -1,6 +1,6 @@
 #include <complex.h>
 
 #include <complex.h>
 
-double creal(double complex z)
+double (creal)(double complex z)
 {
 {
-       return z;
+       return creal(z);
 }
 }
index 078232f..5dc3ff1 100644 (file)
@@ -1,6 +1,6 @@
 #include <complex.h>
 
 #include <complex.h>
 
-float crealf(float complex z)
+float (crealf)(float complex z)
 {
 {
-       return z;
+       return crealf(z);
 }
 }
index 56e6409..fd9dc34 100644 (file)
@@ -1,6 +1,6 @@
 #include <complex.h>
 
 #include <complex.h>
 
-long double creall(long double complex z)
+long double (creall)(long double complex z)
 {
 {
-       return z;
+       return creall(z);
 }
 }
index 67c42b9..8c5474a 100644 (file)
@@ -173,14 +173,6 @@ union lcomplex {
        long double a[2];
 };
 
        long double a[2];
 };
 
-// FIXME: move to complex.h ?
-#define creal(z) ((double)(z))
-#define crealf(z) ((float)(z))
-#define creall(z) ((long double)(z))
-#define cimag(z) ((union dcomplex){(z)}.a[1])
-#define cimagf(z) ((union fcomplex){(z)}.a[1])
-#define cimagl(z) ((union lcomplex){(z)}.a[1])
-
 /* x + y*I is not supported properly by gcc */
 #define cpack(x,y) ((union dcomplex){.a={(x),(y)}}.z)
 #define cpackf(x,y) ((union fcomplex){.a={(x),(y)}}.z)
 /* x + y*I is not supported properly by gcc */
 #define cpack(x,y) ((union dcomplex){.a={(x),(y)}}.z)
 #define cpackf(x,y) ((union fcomplex){.a={(x),(y)}}.z)