bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Jeffrey Walton <noloader@gmail.com>
To: Collin Funk <collin.funk1@gmail.com>
Cc: "bug-gnulib@gnu.org" <bug-gnulib@gnu.org>
Subject: Re: byteswap.h behavior
Date: Sun, 12 May 2024 15:50:04 -0400	[thread overview]
Message-ID: <CAH8yC8=5PO6V1qeDCuEgW0ko3TXKzRYOm2QD9-GjmX-KTffEcQ@mail.gmail.com> (raw)
In-Reply-To: <7c6b4360-7607-41ac-ab4d-9607e6c66a11@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2239 bytes --]

On Sun, May 12, 2024 at 3:23 PM Collin Funk <collin.funk1@gmail.com> wrote:

> A few months ago there was a suggestion on emacs-devel to use
> __builtin_bswap functions when available [1]. While I agree that GCC
> can deal with the optimizations properly, I noted an important
> difference between the macros in byteswap.h.in and inline functions
> provided by glibc.
>
> Using this test program to compare the real glibc header to a macro
> copy-pasted from the replacement header:
>
> ======================================
> #define _GNU_SOURCE 1
> #include <stdio.h>
> #include <inttypes.h>
> #include <byteswap.h>
> #define bswap_32_macro(x) ((((x) & 0x000000FF) << 24) | \
>                            (((x) & 0x0000FF00) << 8) |  \
>                            (((x) & 0x00FF0000) >> 8) |  \
>                            (((x) & 0xFF000000) >> 24))
> int
> main (void)
> {
>   uint32_t value = 0x12345678;
>   uint32_t value_macro = 0x12345678;
>   printf ("1. %#" PRIX32 "\n", bswap_32 (value++));
>   printf ("2. %#" PRIX32 "\n", bswap_32_macro (value_macro++));
>   printf ("3. %#" PRIX32 "\n", value);
>   printf ("4. %#" PRIX32 "\n", value_macro);
>   return 0;
> }
> ======================================
>
> We get the output:
>
>     $ ./a.out
>     1. 0X78563412
>     2. 0X78563412
>     3. 0X12345679
>     4. 0X1234567C
>
> I would like to deal with this concern before I implement the
> replacement for <endian.h>. I think the best decision is to use
> 'extern inline' to match the behavior of glibc. Any other opinions on
> this?
>
> Also if we can agree upon making sure these are defined as functions,
> what is the proper way to test it in a configure script? My instinct
> tells me that assigning a function pointer to bswap_16, etc. would
> fail if they are macros but I am not sure the "standard" way of
> performing that check.
>
> Collin
>
> [1] https://lists.gnu.org/archive/html/emacs-devel/2024-03/msg00736.html
>

I think extern is the least of your worries. Pre- and post- increment are
not safe in a macro. Maybe you should make a local copy of the value in a
do {} while, and then operate on the local value. Or make it a function.

Jeff

[-- Attachment #2: Type: text/html, Size: 3145 bytes --]

      parent reply	other threads:[~2024-05-12 19:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-12 19:23 byteswap.h behavior Collin Funk
2024-05-12 19:38 ` Paul Eggert
2024-05-12 19:47   ` Collin Funk
2024-05-12 20:02     ` Bruno Haible
2024-05-12 20:11       ` Collin Funk
2024-05-12 20:30     ` Paul Eggert
2024-05-12 22:43       ` Collin Funk
2024-05-12 23:10         ` endian.h Bruno Haible
2024-05-12 19:50 ` Jeffrey Walton [this message]

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

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

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

  git send-email \
    --in-reply-to='CAH8yC8=5PO6V1qeDCuEgW0ko3TXKzRYOm2QD9-GjmX-KTffEcQ@mail.gmail.com' \
    --to=noloader@gmail.com \
    --cc=bug-gnulib@gnu.org \
    --cc=collin.funk1@gmail.com \
    /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).