Re: tgmath.h

Ivan Perez <[email protected]> Tue, 26 Apr 2022 08:20:22 -0400
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <CACZKWEJDRjrnbAP9gFTyxUFZT-TJ=_tqTRgoKDndtfo_j4cSWA@mail.gmail.com>
--0000000000002772fe05dd8db892
Content-Type: text/plain; charset="UTF-8"

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]>
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.)
>

--0000000000002772fe05dd8db892
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">Thanks!! This is very helpful!</div><div dir=3D"auto"><br=
></div><div dir=3D"auto">The hope is to generate the same C99 code regardle=
ss of the platform. So we need to use a math library that will work across =
platforms and deliver consistent behaviour.</div><div dir=3D"auto"><br></di=
v><div dir=3D"auto">Ivan</div><div dir=3D"auto"><br></div><div><div class=
=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, Apr 22, 2022=
 at 5:22 AM David Brown &lt;<a href=3D"mailto:[email protected]">dav=
[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex=
">On 22/04/2022 02:18, Ivan Perez wrote:<br>
&gt; Hi,<br>
&gt; <br>
&gt; I&#39;m trying to compile a program for arduino that relies on tgmath.=
h to <br>
&gt; pick the right version of a function based on the types (the code is <=
br>
&gt; automatically generated by another tool).<br>
&gt; <br>
&gt; It&#39;s failing to compile with avr because tgmath.h is missing.<br>
&gt; <br>
&gt; I thought it was part of the standard C distribution since C99 and thu=
s <br>
&gt; I&#39;d be able to rely on it, but I&#39;m no C expert. Does anyone kn=
ow why it <br>
&gt; is not included in avr-libc? Any advice?<br>
&gt; <br>
&gt; I&#39;m on Ubuntu 20.04, using:<br>
&gt; avr-libc 2.0.0+Atmel3.6.2-1.1<br>
&gt; gcc-avr 5.4.0+Atmel3.6.2-1<br>
&gt; binutils-avr 2.26.20160125+Atmel-3.6.2-2<br>
&gt; <br>
&gt; Thanks,<br>
&gt; <br>
&gt; Ivan<br>
<br>
You are right that &lt;tgmath.h&gt; has been part of C since C99.=C2=A0 But=
 it is <br>
not part of C++, which is what the Arduino tools use.=C2=A0 &lt;tgmath.h&gt=
; has <br>
macros for type-generic maths functions, and was originally implemented <br=
>
with compiler-specific extensions.=C2=A0 With C11, the &quot;_Generic&quot;=
 feature can <br>
be used to make compiler-independent implementations of the functions. <br>
In C++, function overloading has existed from the beginning, and is done <b=
r>
in a completely different way.=C2=A0 (Indeed, &lt;tgmath.h&gt; in C was inv=
ented to <br>
give C programmers the convenience C++ users already enjoyed for their <br>
maths functions, but made in a C-style manner.)<br>
<br>
So for your C++ code, you should use &lt;cmath&gt;, not &lt;tgmath.h&gt;, a=
nd <br>
otherwise the usage will be the same.=C2=A0 (You might need a &quot;using <=
br>
namespace std;&quot;, but the Arduino IDE likes to confusingly hide such de=
tail.)<br>
</blockquote></div></div>

--0000000000002772fe05dd8db892--