1 /*===-- flang/runtime/complex-reduction.c ---------------------------*- C -*-===
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 * ===-----------------------------------------------------------------------===
10 #include "complex-reduction.h"
13 struct CppComplexFloat
{
16 struct CppComplexDouble
{
19 struct CppComplexLongDouble
{
23 /* Not all environments define CMPLXF, CMPLX, CMPLXL. */
26 #if defined(__clang_major__) && (__clang_major__ >= 12)
27 #define CMPLXF __builtin_complex
29 static float_Complex_t
CMPLXF(float r
, float i
) {
31 struct CppComplexFloat x
;
32 float_Complex_t result
;
42 #if defined(__clang_major__) && (__clang_major__ >= 12)
43 #define CMPLX __builtin_complex
45 static double_Complex_t
CMPLX(double r
, double i
) {
47 struct CppComplexDouble x
;
48 double_Complex_t result
;
58 #if defined(__clang_major__) && (__clang_major__ >= 12)
59 #define CMPLXL __builtin_complex
61 static long_double_Complex_t
CMPLXL(long double r
, long double i
) {
63 struct CppComplexLongDouble x
;
64 long_double_Complex_t result
;
73 /* RTNAME(SumComplex4) calls RTNAME(CppSumComplex4) with the same arguments
74 * and converts the members of its C++ complex result to C _Complex.
77 #define CPP_NAME(name) Cpp##name
78 #define ADAPT_REDUCTION(name, cComplex, cpptype, cmplxMacro, ARGS, ARG_NAMES) \
79 struct cpptype RTNAME(CPP_NAME(name))(struct cpptype *, ARGS); \
80 cComplex RTNAME(name)(ARGS) { \
81 struct cpptype result; \
82 RTNAME(CPP_NAME(name))(&result, ARG_NAMES); \
83 return cmplxMacro(result.r, result.i); \
86 /* TODO: COMPLEX(2 & 3) */
89 ADAPT_REDUCTION(SumComplex4
, float_Complex_t
, CppComplexFloat
, CMPLXF
,
90 REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
91 ADAPT_REDUCTION(SumComplex8
, double_Complex_t
, CppComplexDouble
, CMPLX
,
92 REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
93 #if LDBL_MANT_DIG == 64
94 ADAPT_REDUCTION(SumComplex10
, long_double_Complex_t
, CppComplexLongDouble
,
95 CMPLXL
, REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
96 #elif LDBL_MANT_DIG == 113
97 ADAPT_REDUCTION(SumComplex16
, long_double_Complex_t
, CppComplexLongDouble
,
98 CMPLXL
, REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
102 ADAPT_REDUCTION(ProductComplex4
, float_Complex_t
, CppComplexFloat
, CMPLXF
,
103 REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
104 ADAPT_REDUCTION(ProductComplex8
, double_Complex_t
, CppComplexDouble
, CMPLX
,
105 REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
106 #if LDBL_MANT_DIG == 64
107 ADAPT_REDUCTION(ProductComplex10
, long_double_Complex_t
, CppComplexLongDouble
,
108 CMPLXL
, REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
109 #elif LDBL_MANT_DIG == 113
110 ADAPT_REDUCTION(ProductComplex16
, long_double_Complex_t
, CppComplexLongDouble
,
111 CMPLXL
, REDUCTION_ARGS
, REDUCTION_ARG_NAMES
)
115 ADAPT_REDUCTION(DotProductComplex4
, float_Complex_t
, CppComplexFloat
, CMPLXF
,
116 DOT_PRODUCT_ARGS
, DOT_PRODUCT_ARG_NAMES
)
117 ADAPT_REDUCTION(DotProductComplex8
, double_Complex_t
, CppComplexDouble
, CMPLX
,
118 DOT_PRODUCT_ARGS
, DOT_PRODUCT_ARG_NAMES
)
119 #if LDBL_MANT_DIG == 64
120 ADAPT_REDUCTION(DotProductComplex10
, long_double_Complex_t
,
121 CppComplexLongDouble
, CMPLXL
, DOT_PRODUCT_ARGS
, DOT_PRODUCT_ARG_NAMES
)
122 #elif LDBL_MANT_DIG == 113
123 ADAPT_REDUCTION(DotProductComplex16
, long_double_Complex_t
,
124 CppComplexLongDouble
, CMPLXL
, DOT_PRODUCT_ARGS
, DOT_PRODUCT_ARG_NAMES
)