bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Collin Funk <collin.funk1@gmail.com>
To: "bug-gnulib@gnu.org" <bug-gnulib@gnu.org>
Subject: byteswap.h behavior
Date: Sun, 12 May 2024 12:23:16 -0700	[thread overview]
Message-ID: <7c6b4360-7607-41ac-ab4d-9607e6c66a11@gmail.com> (raw)

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


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

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-12 19:23 Collin Funk [this message]
2024-05-12 19:38 ` byteswap.h behavior 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 ` byteswap.h behavior Jeffrey Walton

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=7c6b4360-7607-41ac-ab4d-9607e6c66a11@gmail.com \
    --to=collin.funk1@gmail.com \
    --cc=bug-gnulib@gnu.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).