Re: tgmath.h

David Brown <[email protected]> Tue, 26 Apr 2022 16:11:21 +0200
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <[email protected]>
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 <tgmath.h> and 
replacing it with <math.h> with explicitly named functions for different 
sized parameters.  Alternatively, use a common C++ subset and drop the 
C99 requirements (again, dropping <tgmath.h>, replacing it now with 
<cmath>).

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 <[email protected] 
> <mailto:[email protected]>> 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 <tgmath.h> has been part of C since C99.  But it is
>     not part of C++, which is what the Arduino tools use.  <tgmath.h> 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, <tgmath.h> 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 <cmath>, not <tgmath.h>, 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.)
>