bug-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Add arithmetic builtin functions


From: Pete Dietl
Subject: Re: [PATCH] Add arithmetic builtin functions
Date: Fri, 29 Nov 2024 12:31:56 -0800

Gisle Vanem wrote:
> > +#define generic_math_op(a, b, op)                                          
> >    \
> > +  ((void)sizeof (                                                          
> >    \
> > +       char[__builtin_types_compatible_p (typeof (a), typeof (b)) ? 1 : 
> > -1]), \
> > +   _Generic ((a),                                                          
> >    \
> > +       double: generic_math_op_double,                                     
> >    \
> > +       long long int: generic_math_op_ll) (a, b, op))

> this is some GNU-C-ism that wont compile with MSVC.

I will remove the use of __builtin_types_compatible_p

> > +static char *
> > +func_add (char *o, char **argv, const char *funcname)
> > +{
> > +  struct number n = perform_math_op (
> > +      op_add, funcname,
> > +      (struct math_operation_init){ .init_type = t_constant, .constant = 0 
> > },
> > +      argv);

> Not sure about such inline 'structs'.

These are called "compound literals" and have been part of C since C99.

More broadly though, what is the newest version of C that I may rely
on? The use of "_Generic" is a C11 thing. I can remove it if we want
to rely only on C99.

Should I write some unit or integration tests?


On Fri, Nov 29, 2024 at 7:34 AM Gisle Vanem <gvanem@yahoo.no> wrote:
>
> Pete Dietl wrote:
>
> > Here I submit to you a patch that adds the following builtin functions
> > to make: `add` (addition), `sub` (subtraction), `mul`
> > (multiplication), `div` (division), `mod` (modulus), `max` (maximum),
> > `min` (minimum), and `abs` (absolute value). The implementation I
> > provide supports both integers and floats with promotions from integer
> > to float when appropriate (an operation where at least one argument is
> > a float causes the other argument to be promoted to a float and the
> > result to be a float).
>
> This would be nice, but...
>
> > +#define generic_math_op(a, b, op)                                          
> >    \
> > +  ((void)sizeof (                                                          
> >    \
> > +       char[__builtin_types_compatible_p (typeof (a), typeof (b)) ? 1 : 
> > -1]), \
> > +   _Generic ((a),                                                          
> >    \
> > +       double: generic_math_op_double,                                     
> >    \
> > +       long long int: generic_math_op_ll) (a, b, op))
>
> this is some GNU-C-ism that wont compile with MSVC.
>
> > +static char *
> > +func_add (char *o, char **argv, const char *funcname)
> > +{
> > +  struct number n = perform_math_op (
> > +      op_add, funcname,
> > +      (struct math_operation_init){ .init_type = t_constant, .constant = 0 
> > },
> > +      argv);
>
> Not sure about such inline 'structs'.
>
> --
> --gv
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]