public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] inttypes.h: imaxabs(3): Implement as a macro
@ 2022-09-13 15:18 Alex Colomar
  2022-09-13 15:28 ` Alejandro Colomar
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Alex Colomar @ 2022-09-13 15:18 UTC (permalink / raw)
  To: libc-alpha; +Cc: Alex Colomar, Florian Weimer, JeanHeyd Meneide

Functions using the [u]intmax_t types have a problem: ABI.

The [u]intmax_t types were designed so that users could blindly
use them to work with whatever is the biggest size for a given
architecture.  But this is problematic to implement.  These days,
ABI stability is very important, and programs compiled a specific
version of glibc should continue working with newer glibc
releases.  Of course, that means that future glibc versions can't
change the types of functions, or the functions would be
incompatible.

So, we cannot really change the (linker) signature of functions;
not even of those using [u]intmax_t.

A solution to that problem is to use macros, and not real
functions.  That way, the program really links against whatever
function works with the compile-time maximum width type, be it
long or long long, and the program will be bonded to the ABI of
long or long long, but will know nothing about intmax_t.

We still need a linker symbol with the name imaxabs(), for old
programs linked against old glibc versions, and also for
compatibility with an interpretation of the ISO C standard that
says that imaxabs(3) is a function.  But the standard knows
nothing about ABI or a linker, so a macro that evaluates to a
differently-named function is not a straight violation, but rather
stepping through the line without crossing it, AFAIK.

This implementation with C11 _Generic() is one that I used for the
new _Generic(3) manual page.  It allows treating the macro as a
function, and to take pointers to it.  And it allows to more
easily understand the definition of the macro, without needing
to parse the ifdefs.  It makes it easier to parse with tools like
grepc(1):

$ grepc -k __PRI64_PREFIX inttypes.h
inttypes.h:44:#  define __PRI64_PREFIX	"l"
inttypes.h:47:#  define __PRI64_PREFIX	"ll"
$ grepc -k imaxabs inttypes.h
inttypes.h:290:#define imaxabs  _Generic(INTMAX_C(0), long: labs, long long: llabs)

If glibc can't use C11 features in public headers, then usual
ifdefs could be used.

Cc: Florian Weimer <fweimer@redhat.com>
Cc: JeanHeyd Meneide <wg14@soasis.org>
Signed-off-by: Alex Colomar <alx.manpages@gmail.com>
---

Hi Florian,

What do you think about using this implementation of imaxabs(3) in
glibc?  Is it valid according to ISO C and/or POSIX?

It's been a long time since I last built and tested glibc from
so I haven't tested this patch yet.  I'll try more seriously when
it has some chances of being accepted.

Cheers,

Alex

 stdlib/inttypes.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/stdlib/inttypes.h b/stdlib/inttypes.h
index d550769f2a..eb20e416ac 100644
--- a/stdlib/inttypes.h
+++ b/stdlib/inttypes.h
@@ -287,7 +287,7 @@ typedef struct
 
 
 /* Compute absolute value of N.  */
-extern intmax_t imaxabs (intmax_t __n) __THROW __attribute__ ((__const__));
+#define imaxabs  _Generic(INTMAX_C(0), long: labs, long long: llabs)
 
 /* Return the `imaxdiv_t' representation of the value of NUMER over DENOM. */
 extern imaxdiv_t imaxdiv (intmax_t __numer, intmax_t __denom)
-- 
2.37.2


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-09-15 12:33 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-13 15:18 [PATCH] inttypes.h: imaxabs(3): Implement as a macro Alex Colomar
2022-09-13 15:28 ` Alejandro Colomar
2022-09-13 15:34 ` Alejandro Colomar
2022-09-13 18:27 ` Joseph Myers
2022-09-13 18:47   ` Paul Eggert
2022-09-13 19:30     ` Joseph Myers
2022-09-13 20:59       ` Paul Eggert
2022-09-13 22:43   ` Alejandro Colomar
2022-09-13 22:56     ` Joseph Myers
2022-09-13 23:43       ` Alejandro Colomar
2022-09-14 16:41         ` Joseph Myers
2022-09-14 19:03           ` JeanHeyd Meneide
2022-09-15 12:33             ` Alejandro (Alex) Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).