Re: [PATCH v2] _Generic.3: New page documenting _Generic()

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

 



On 9/4/22 23:16, Alejandro Colomar wrote:
Hi Florian,

On 8/23/22 09:58, Florian Weimer wrote:
Note that this approach does not really work that well in practice
because macros using _Generic expand all the alternatives (in current
implementations; doing this differently requires deviating from the
layered implementation strategy suggested in the C standard).  This
means that _Generic-using macros can only be nested maybe three or four
levels deep, depending on the number of _Generic alternatives on each
level.  For <tgmath.h>, this is really not enough, so a high-quality
implementation of <tgmath.h> using _Generic is not feasible.  GCC
provides __builtin_tgmath, which is designed in such a way that when
used in a macro, the macro argument is only expanded once.

Maybe mention this under BUGS?

C++ templates do not suffer from this particular problem.


Heh, I don't know how this didn't oocur to me before.  Well, maybe it's because it's non-standard (but the standard might very well benefit from adding this, IMO).

An always_inline function with no extern definition behaves as if it were a macro (trying to take a pointer to it, or something that needs a linker symbol, will result in linker errors, which is not the most readable error, but good enough), in the sense that it doesn't have ABI issues, but has the benefit of not creating code exponentially.


Cheers,

Alex

---

diff --git a/man3/_Generic.3 b/man3/_Generic.3
index f3daf98c1..3bd5f306c 100644
--- a/man3/_Generic.3
+++ b/man3/_Generic.3
@@ -30,7 +30,9 @@ C11 and later.
  The following program demonstrates how to write
  a replacement for the standard
  .BR imaxabs (3)
-function, which being a function can't really provide what it promises:
+function, which being an
+.I extern
+function can't really provide what it promises:
  seamlessly upgrading to the widest available type.
  .PP
  .\" SRC BEGIN (_Generic.c)
@@ -39,11 +41,16 @@ seamlessly upgrading to the widest available type.
  #include <stdio.h>
  #include <stdlib.h>

-#define my_imaxabs(j)  _Generic(INTMAX_C(0),  \e
-    long:           labs(j),                  \e
-    long long:      llabs(j)                  \e
- /* long long long: lllabs(j) */              \e
-)
+[[gnu::always_inline]]
+inline intmax_t
+my_imaxabs(intmax_t j)
+{
+    return _Generic(j,
+        long:           labs(j),
+        long long:      llabs(j)
+     /* long long long: lllabs(j) */
+    )
+}

  int
  main(void)


Huh, although this goes back again to macros, and potentially exponential code, I think it's quite clean and minimal, and has the benefit that it allows one to take pointers:

$ cat gen.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define my_imaxabs  _Generic(INTMAX_C(0),  \
        long:           labs,              \
        long long:      llabs              \
     /* long long long: lllabs */          \
)

int
main(void)
{
        off_t  a;

        a = -42;
        printf("imaxabs(%jd) == %jd\n", (intmax_t) a, my_imaxabs(a));
        printf("&imaxabs == %p\n", &my_imaxabs);
        printf("&labs    == %p\n", &labs);
        printf("&llabs   == %p\n", &llabs);

        exit(EXIT_SUCCESS);
}

$ cc -Wall -Wextra gen.c
$ ./a.out
imaxabs(-42) == 42
&imaxabs == 0x7ff5aa2407a0
&labs    == 0x7ff5aa2407a0
&llabs   == 0x7ff5aa2407b0


Nice experiment :)

Cheers,

Alex


--
<http://www.alejandro-colomar.es/>

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux