libm: Add CMPLX, CMPLXF and CMPLXL macros

Message ID 20250131211421.969500-1-ykolerov@synopsys.com
State New
Headers
Series libm: Add CMPLX, CMPLXF and CMPLXL macros |

Commit Message

Yuriy Kolerov Jan. 31, 2025, 9:14 p.m. UTC
  This is a backport of the same patch from Picolibc repository
with all fixes:

https://github.com/picolibc/picolibc/commit/197657202913

    libm: Add CMPLX, CMPLXF and CMPLXL macros

    These use __builtin_complex if available, otherwise they open
    code as suggested by the C standard.

    Signed-off-by: Keith Packard <keithp@keithp.com>

https://github.com/picolibc/picolibc/commit/a00c65d42760

    complex.h: Add type casting to CMPLX, CMPLXF and CMPLXL macros

    Adding type casting to CMPLX, CMPLXF and CMPLXL macros to
    ensure type safety constructing complex numbers using
    __builtin_complex function. The real and imaginary parts
    passed to __builtin_complex function are explicity cast to
    double, float and long double respectively, to match the
    types expected by the function.

    This change improves code clarity and prevents unintended
    type conversions when using macros to construct complex
    numbers.

    Signed-off-by: Mostafa Salman <mostafas@synopsys.com>

Signed-off-by: Yuriy Kolerov <ykolerov@synopsys.com>
---
 newlib/libc/include/complex.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
  

Comments

Corinna Vinschen Feb. 3, 2025, 2:15 p.m. UTC | #1
Hi Yuriy,

On Jan 31 23:14, Yuriy Kolerov wrote:
> This is a backport of the same patch from Picolibc repository
> with all fixes:
> 
> https://github.com/picolibc/picolibc/commit/197657202913
> 
>     libm: Add CMPLX, CMPLXF and CMPLXL macros
> 
>     These use __builtin_complex if available, otherwise they open
>     code as suggested by the C standard.
> 
>     Signed-off-by: Keith Packard <keithp@keithp.com>
> 
> https://github.com/picolibc/picolibc/commit/a00c65d42760
> 
>     complex.h: Add type casting to CMPLX, CMPLXF and CMPLXL macros
> 
>     Adding type casting to CMPLX, CMPLXF and CMPLXL macros to
>     ensure type safety constructing complex numbers using
>     __builtin_complex function. The real and imaginary parts
>     passed to __builtin_complex function are explicity cast to
>     double, float and long double respectively, to match the
>     types expected by the function.
> 
>     This change improves code clarity and prevents unintended
>     type conversions when using macros to construct complex
>     numbers.
> 
>     Signed-off-by: Mostafa Salman <mostafas@synopsys.com>
> 
> Signed-off-by: Yuriy Kolerov <ykolerov@synopsys.com>
> ---
>  newlib/libc/include/complex.h | 12 ++++++++++++
>  1 file changed, 12 insertions(+)

The CMPLXx() macros are already in that file, right at the start,
lines 20 to 30.


Corinna
  
Yuriy Kolerov Feb. 3, 2025, 5:27 p.m. UTC | #2
Hi Corinna,

Yes, I see. Sorry, I missed that while rebasing my patches.

Regards,
Yuriy Kolerov

-----Original Message-----
From: Corinna Vinschen <vinschen@redhat.com> 
Sent: Monday, February 3, 2025 4:16 PM
To: Yuriy Kolerov <ykolerov@synopsys.com>
Cc: newlib@sourceware.org
Subject: Re: [PATCH] libm: Add CMPLX, CMPLXF and CMPLXL macros

Hi Yuriy,

On Jan 31 23:14, Yuriy Kolerov wrote:
> This is a backport of the same patch from Picolibc repository with all 
> fixes:
> 
> https://urldefense.com/v3/__https://github.com/picolibc/picolibc/commi
> t/197657202913__;!!A4F2R9G_pg!erpVdhCZzAZUHSMjMkze6yzlGsJZ8D26oU_WOZXv
> 3HSIO3FmG2uS1VS7A3UwIPTHO2MsSjMfgWNzj_oAxgz1eKc$
> 
>     libm: Add CMPLX, CMPLXF and CMPLXL macros
> 
>     These use __builtin_complex if available, otherwise they open
>     code as suggested by the C standard.
> 
>     Signed-off-by: Keith Packard <keithp@keithp.com>
> 
> https://urldefense.com/v3/__https://github.com/picolibc/picolibc/commi
> t/a00c65d42760__;!!A4F2R9G_pg!erpVdhCZzAZUHSMjMkze6yzlGsJZ8D26oU_WOZXv
> 3HSIO3FmG2uS1VS7A3UwIPTHO2MsSjMfgWNzj_oAx12zglc$
> 
>     complex.h: Add type casting to CMPLX, CMPLXF and CMPLXL macros
> 
>     Adding type casting to CMPLX, CMPLXF and CMPLXL macros to
>     ensure type safety constructing complex numbers using
>     __builtin_complex function. The real and imaginary parts
>     passed to __builtin_complex function are explicity cast to
>     double, float and long double respectively, to match the
>     types expected by the function.
> 
>     This change improves code clarity and prevents unintended
>     type conversions when using macros to construct complex
>     numbers.
> 
>     Signed-off-by: Mostafa Salman <mostafas@synopsys.com>
> 
> Signed-off-by: Yuriy Kolerov <ykolerov@synopsys.com>
> ---
>  newlib/libc/include/complex.h | 12 ++++++++++++
>  1 file changed, 12 insertions(+)

The CMPLXx() macros are already in that file, right at the start, lines 20 to 30.


Corinna
  

Patch

diff --git a/newlib/libc/include/complex.h b/newlib/libc/include/complex.h
index dbabdf67a..f06fc2ba6 100644
--- a/newlib/libc/include/complex.h
+++ b/newlib/libc/include/complex.h
@@ -150,6 +150,18 @@  double creal(double complex);
 float crealf(float complex);
 long double creall(long double complex);
 
+#if __ISO_C_VISIBLE >= 2011
+#ifdef _HAVE_BUILTIN_COMPLEX
+#define CMPLX(r,i) __builtin_complex((double)(r), (double)(i))
+#define CMPLXF(r,i) __builtin_complex((float)(r), (float)(i))
+#define CMPLXL(r,i) __builtin_complex((long double)(r), (long double)(i))
+#else
+#define CMPLX(r,i) ((double complex) ((double) (r) + (double complex) I * (double) (i)))
+#define CMPLXF(r,i) ((float complex) ((float) (r) + (float complex) I * (float) (i)))
+#define CMPLXL(r,i) ((long double complex) ((long double) (r) + (long double complex) I * (long double) (i)))
+#endif
+#endif
+
 #if __GNU_VISIBLE
 double complex clog10(double complex);
 float complex clog10f(float complex);