public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alex Colomar <alx.manpages@gmail.com>
To: libc-alpha@sourceware.org
Cc: Alex Colomar <alx.manpages@gmail.com>,
	Florian Weimer <fweimer@redhat.com>,
	JeanHeyd Meneide <wg14@soasis.org>
Subject: [PATCH] inttypes.h: imaxabs(3): Implement as a macro
Date: Tue, 13 Sep 2022 17:18:54 +0200	[thread overview]
Message-ID: <20220913151853.153311-1-alx.manpages@gmail.com> (raw)

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


             reply	other threads:[~2022-09-13 15:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-13 15:18 Alex Colomar [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220913151853.153311-1-alx.manpages@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=wg14@soasis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).