Re: tgmath.h

2022-07-22 Thread Matthijs Kooijman
Hi,

A bit of an old thread, but I wanted to add a bit more detail on how
Arduino handles C/C++ code.

> Arduino is a non-standard mix of C and C++, of undocumented (AFAIK) version,
> with an IDE that automates and hides some features (such as some headers).
> They use their own special libraries,

Arduino uses avr-gcc without changes, so anything supported by that
should work in Arduino. The build process has mostly two peculiarities:
 - It uses .ino files, which are merged into a single file (if you use
   multiple .ino files), and have auto-generated function prototypes
   added (so novice users do not have to care about this, though this
   also often breaks for more complex code).
 - For libraries, the build detects which files you #include and
   selects the libraries to compile (and put on the include path) based
   on those.

No system headers should be hidden, and no behavior-changing patches
shoulde be included in the avr-gcc and avr-libc builds.

> and an older version of avr-gcc that has a number of non-conformities.
Arduino is a bit slow to update at times, but is still updating the
toolchain every now and then.

> (Of particular relevance here, it has 32-bit "double" instead of
> 64-bit.)
Isn't this always the case on avr-gcc? Or is this a build-time option or
so?

> You cannot achieve your aims here with a single code base that works on
> Arduino tools and also on, say, a PC platform for simulation, and gives the
> same results.

There are actually some codebases that aim to simulate (or at least
stub) the Arduino API on a PC platform (other than the API, there is
nothing really special about Arduino code).



As for the original question about tgmath.h, it seems that it being
missing on Arduino is not a matter of C vs C++ or Arduino-specific
additions, but it seems that it is just missing from avr-libc entirely
(so will also be missing if you drop Arduino and use plain AVR). It also
seems it *is* present for ARM-based Arduino boards.


Gr.

Matthijs


signature.asc
Description: PGP signature


Re: tgmath.h

2022-04-26 Thread David Brown

Hi,

Arduino is a non-standard mix of C and C++, of undocumented (AFAIK) 
version, with an IDE that automates and hides some features (such as 
some headers).  They use their own special libraries, and an older 
version of avr-gcc that has a number of non-conformities.  (Of 
particular relevance here, it has 32-bit "double" instead of 64-bit.)


You cannot achieve your aims here with a single code base that works on 
Arduino tools and also on, say, a PC platform for simulation, and gives 
the same results.


You can do a /lot/ better if you use modern versions of avr-gcc as 
"normal" tools and drop the Arduino stuff.


You can also do better if you code to a common subset of C99 and C++ 
(whatever version Arduino uses) - that means dropping  and 
replacing it with  with explicitly named functions for different 
sized parameters.  Alternatively, use a common C++ subset and drop the 
C99 requirements (again, dropping , replacing it now with 
).


There's a lot to like with Arduino - its relatively easy to use, and 
there is a lot of sample code and tools that let you get proof of 
concepts working quickly.  But it has its disadvantages, and its 
non-standard language and oversimplification of the underlying compiler 
is a problem for more advanced usage.


David


On 26/04/2022 14:20, Ivan Perez wrote:

Thanks!! This is very helpful!

The hope is to generate the same C99 code regardless of the platform. So 
we need to use a math library that will work across platforms and 
deliver consistent behaviour.


Ivan

On Fri, Apr 22, 2022 at 5:22 AM David Brown > wrote:


On 22/04/2022 02:18, Ivan Perez wrote:
 > Hi,
 >
 > I'm trying to compile a program for arduino that relies on
tgmath.h to
 > pick the right version of a function based on the types (the code is
 > automatically generated by another tool).
 >
 > It's failing to compile with avr because tgmath.h is missing.
 >
 > I thought it was part of the standard C distribution since C99
and thus
 > I'd be able to rely on it, but I'm no C expert. Does anyone know
why it
 > is not included in avr-libc? Any advice?
 >
 > I'm on Ubuntu 20.04, using:
 > avr-libc 2.0.0+Atmel3.6.2-1.1
 > gcc-avr 5.4.0+Atmel3.6.2-1
 > binutils-avr 2.26.20160125+Atmel-3.6.2-2
 >
 > Thanks,
 >
 > Ivan

You are right that  has been part of C since C99.  But it is
not part of C++, which is what the Arduino tools use.   has
macros for type-generic maths functions, and was originally implemented
with compiler-specific extensions.  With C11, the "_Generic" feature
can
be used to make compiler-independent implementations of the functions.
In C++, function overloading has existed from the beginning, and is
done
in a completely different way.  (Indeed,  in C was
invented to
give C programmers the convenience C++ users already enjoyed for their
maths functions, but made in a C-style manner.)

So for your C++ code, you should use , not , and
otherwise the usage will be the same.  (You might need a "using
namespace std;", but the Arduino IDE likes to confusingly hide such
detail.)






Re: tgmath.h

2022-04-26 Thread Ivan Perez
Thanks!! This is very helpful!

The hope is to generate the same C99 code regardless of the platform. So we
need to use a math library that will work across platforms and deliver
consistent behaviour.

Ivan

On Fri, Apr 22, 2022 at 5:22 AM David Brown 
wrote:

> On 22/04/2022 02:18, Ivan Perez wrote:
> > Hi,
> >
> > I'm trying to compile a program for arduino that relies on tgmath.h to
> > pick the right version of a function based on the types (the code is
> > automatically generated by another tool).
> >
> > It's failing to compile with avr because tgmath.h is missing.
> >
> > I thought it was part of the standard C distribution since C99 and thus
> > I'd be able to rely on it, but I'm no C expert. Does anyone know why it
> > is not included in avr-libc? Any advice?
> >
> > I'm on Ubuntu 20.04, using:
> > avr-libc 2.0.0+Atmel3.6.2-1.1
> > gcc-avr 5.4.0+Atmel3.6.2-1
> > binutils-avr 2.26.20160125+Atmel-3.6.2-2
> >
> > Thanks,
> >
> > Ivan
>
> You are right that  has been part of C since C99.  But it is
> not part of C++, which is what the Arduino tools use.   has
> macros for type-generic maths functions, and was originally implemented
> with compiler-specific extensions.  With C11, the "_Generic" feature can
> be used to make compiler-independent implementations of the functions.
> In C++, function overloading has existed from the beginning, and is done
> in a completely different way.  (Indeed,  in C was invented to
> give C programmers the convenience C++ users already enjoyed for their
> maths functions, but made in a C-style manner.)
>
> So for your C++ code, you should use , not , and
> otherwise the usage will be the same.  (You might need a "using
> namespace std;", but the Arduino IDE likes to confusingly hide such
> detail.)
>


Re: tgmath.h

2022-04-22 Thread David Brown

On 22/04/2022 02:18, Ivan Perez wrote:

Hi,

I'm trying to compile a program for arduino that relies on tgmath.h to 
pick the right version of a function based on the types (the code is 
automatically generated by another tool).


It's failing to compile with avr because tgmath.h is missing.

I thought it was part of the standard C distribution since C99 and thus 
I'd be able to rely on it, but I'm no C expert. Does anyone know why it 
is not included in avr-libc? Any advice?


I'm on Ubuntu 20.04, using:
avr-libc 2.0.0+Atmel3.6.2-1.1
gcc-avr 5.4.0+Atmel3.6.2-1
binutils-avr 2.26.20160125+Atmel-3.6.2-2

Thanks,

Ivan


You are right that  has been part of C since C99.  But it is 
not part of C++, which is what the Arduino tools use.   has 
macros for type-generic maths functions, and was originally implemented 
with compiler-specific extensions.  With C11, the "_Generic" feature can 
be used to make compiler-independent implementations of the functions. 
In C++, function overloading has existed from the beginning, and is done 
in a completely different way.  (Indeed,  in C was invented to 
give C programmers the convenience C++ users already enjoyed for their 
maths functions, but made in a C-style manner.)


So for your C++ code, you should use , not , and 
otherwise the usage will be the same.  (You might need a "using 
namespace std;", but the Arduino IDE likes to confusingly hide such detail.)