Re: header file substitutes

2024-05-05 Thread Bruno Haible
Hi Collin,

> IIRC in  there is:
> 
>  #define __STDC_ENDIAN_LITTLE__ /* Unique constant */
>  #define __STDC_ENDIAN_BIG__ /* Unique constant */
>  #define __STDC_ENDIAN_NATIVE__ /* __STDC_ENDIAN_LITTLE__ or 
> __STDC_ENDIAN_BIG__ */

You can work on this, once Paul has created an 'stdbit' module with the
3 sets of functions, as mentioned in the other mail, I would say.

> But, I think the next POSIX revision has  like Glibc which I
> prefer.

Preference or not — both ISO C and POSIX are relevant for us. If ISO C
has endianness macros in  and POSIX has endianness macros in
, we will implement both.

> So these defines:
> 
> #define LITTLE_ENDIAN /* Unique constant */
> #define BIG_ENDIAN /* Unique constant */
> #define BYTE_ORDER /* LITTLE_ENDIAN or BIG_ENDIAN */
> 
> plus functions or macros:
> 
>  uint16_t be16toh (uint16_t);
>  uint16_t htobe16 (uint16_t);
> 
> I could try to work on that if it seems useful to anyone else.

Indeed, I see that this is scheduled for inclusion in the next POSIX
revision: https://www.austingroupbugs.net/view.php?id=162

It is useful to do this already now, before the next POSIX revision is
official, because when it becomes official it will contain *many* new
functionalities. (And this new functionality is unlikely to change.
It's been sitting in the waiting line since 2011 (!).)

Bruno






Re: header file substitutes

2024-05-05 Thread Collin Funk
On 5/4/24 11:07 PM, Paul Eggert wrote:
>> Since  seems resonably portable,
> 
> I assume you mean ? There's no  on my Ubuntu system.

No, sorry maybe I worded my original email awkwardly. :)

I think all of the BSDs have  which define:

#define LITTLE_ENDIAN 1234
#define BIG_ENDIAN 4321
#define BYTE_ORDER /* One of thsoe macros.  */

>From what I can tell, they also had ntohl and friends there. I think
that glibc added the be32toh macros and extended that header, while
also moving it to .

My history might be a bit wrong there. The headers predate me by a few
years. :)

I was trying to say that a macro sequence like this could do most of
the heavy lifting:

#if HAVE_ENDIAN_H
#  include 
#elif HAVE_SYS_ENDIAN_H
#  include 
#endif

Since the BSDs and Solaris should define the same stuff in
.

> Although that's a start, we'll need more of course. Here's what I have so far 
> for my prototype stdbit.in.h, but it needs more work. This sort of thing used 
> to be even trickier (see gl_BIGENDIAN and gl_MULTIARCH) but I hope we can 
> dispense with that complexity nowadays (by using something like the following 
> complexity instead :-).

Looks good to me. You got more of the compiler stuff than I would
have.

Collin



Re: header file substitutes

2024-05-05 Thread Paul Eggert

On 2024-05-04 15:33, Collin Funk wrote:


But I don't think C23 has the conversion macros:

 /* big endian 32 to host.  */
 uint32_t be32toh (uint32_t);
 /* little endian 32 to host.  */
 uint32_t le32toh (uint32_t);


Yes, those might be a good reason for a Gnulib endian module, to support 
endian.h GNU-style. Ideally it would be implemented by appealing to 
stdbit.h when that's helpful.




Since  seems resonably portable,


I assume you mean ? There's no  on my Ubuntu system.


 $ echo | gcc -dM -E - | grep 'ENDIAN'
 #define __ORDER_LITTLE_ENDIAN__ 1234
 #define __FLOAT_WORD_ORDER__ __ORDER_LITTLE_ENDIAN__
 #define __ORDER_PDP_ENDIAN__ 3412
 #define __ORDER_BIG_ENDIAN__ 4321
 #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__


Although that's a start, we'll need more of course. Here's what I have 
so far for my prototype stdbit.in.h, but it needs more work. This sort 
of thing used to be even trickier (see gl_BIGENDIAN and gl_MULTIARCH) 
but I hope we can dispense with that complexity nowadays (by using 
something like the following complexity instead :-).


  /* Define the native endianness.  Prefer predefined macros to #include
 directives, to avoid namespace pollution.  */

  /* GCC and Clang define __BYTE_ORDER__ etc.
 ARM compilers define __BIG_ENDIAN etc.
 Oracle Studio defines __SUNPRO_C etc.
 Some platforms work only on little-endian platforms.  */
  #if (defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__ \
   && defined __ORDER_LITTLE_ENDIAN__)
  # define __STDC_ENDIAN_BIG__ __ORDER_BIG_ENDIAN__
  # define __STDC_ENDIAN_LITTLE__ __ORDER_LITTLE_ENDIAN__
  # define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER__
  #elif (defined __SUNPRO_C ? defined __sparc \
 : 0)
  # define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__
  #elif ((defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__ \
  && defined __ORDER_BIG_ENDIAN__) \
 ? __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ \
 : defined __SUNPRO_C ? !defined __sparc \
 : (defined _WIN32 || defined __CYGWIN__ || defined __EMX__ \
|| defined __MSDOS__ || defined __DJGPP__) ? 1 \
 : 0)
  # define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__
  #else
  # ifdef __has_include
  #  if __has_include ()
  #   include 
  #  endif
  # endif
  # if defined __BIG_ENDIAN && defined __LITTLE_ENDIAN && defined 
__BYTE_ORDER

  #  define __STDC_ENDIAN_BIG__ __BIG_ENDIAN
  #  define __STDC_ENDIAN_LITTLE__ __LITTLE_ENDIAN
  #  define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER
  # endif
  # if defined BIG_ENDIAN && defined LITTLE_ENDIAN && defined BYTE_ORDER
  #  define __STDC_ENDIAN_BIG__ BIG_ENDIAN
  #  define __STDC_ENDIAN_LITTLE__ LITTLE_ENDIAN
  #  define __STDC_ENDIAN_NATIVE__ BYTE_ORDER
  # endif
  #endif
  #ifndef __STDC_ENDIAN_BIG__
  # define __STDC_ENDIAN_BIG__ 4321
  #endif
  #ifndef __STDC_ENDIAN_LITTLE__
  # define __STDC_ENDIAN_LITTLE__ 1234
  #endif
  #ifndef __STDC_ENDIAN_NATIVE__
  /* __STDC_ENDIAN_NATIVE__ is not defined on this platform.
 If this doesn't suffice for you, please email a fix
 to .  */
  #endif




Re: header file substitutes

2024-05-04 Thread Collin Funk
On 5/4/24 2:30 PM, Paul Eggert wrote:
>> But, I think the next POSIX revision has  like Glibc which I
>> prefer.
> 
> If the past is any guide, an advantage of the C23 version is that in
> the long run it is likely to be more portable. POSIX is an extension
> of C.

True. But I don't think C23 has the conversion macros:

/* big endian 32 to host.  */
uint32_t be32toh (uint32_t);
/* little endian 32 to host.  */
uint32_t le32toh (uint32_t);

like the next POSIX revision has and I am a fan of.

I assume the addition of  was inspired by glibc's
implementation. glibc seems to have extended  from the
BSDs by adding those conversion macros.

Since  seems resonably portable, that could do the bulk
of the work for non glibc systems.

GCC even has these:

$ echo | gcc -dM -E - | grep 'ENDIAN'
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __FLOAT_WORD_ORDER__ __ORDER_LITTLE_ENDIAN__
#define __ORDER_PDP_ENDIAN__ 3412
#define __ORDER_BIG_ENDIAN__ 4321
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__

Collin



Re: header file substitutes

2024-05-04 Thread Paul Eggert

On 2024-05-04 13:54, Collin Funk wrote:

IIRC in  there is:

  #define __STDC_ENDIAN_LITTLE__ /* Unique constant */
  #define __STDC_ENDIAN_BIG__ /* Unique constant */
  #define __STDC_ENDIAN_NATIVE__ /* __STDC_ENDIAN_LITTLE__ or 
__STDC_ENDIAN_BIG__ */

probably with values taken from Glibc's  or BSD's
.

But, I think the next POSIX revision has  like Glibc which I
prefer.


If the past is any guide, an advantage of the C23 version is that in the 
long run it is likely to be more portable. POSIX is an extension of C.




Re: header file substitutes

2024-05-04 Thread Collin Funk
Hi Bruno,

On 5/4/24 7:52 AM, Bruno Haible wrote:
> And we're not yet done with the header file substitutes. One is still missing
> from Gnulib:  [1]. The only thing that has changed since last year
> is that glibc 2.39 now has this header file [2]. But who will want to use it,
> as long as it's not portable?

C23 and the upcoming POSIX release seem to have a lot of useful stuff.

IIRC in  there is:

 #define __STDC_ENDIAN_LITTLE__ /* Unique constant */
 #define __STDC_ENDIAN_BIG__ /* Unique constant */
 #define __STDC_ENDIAN_NATIVE__ /* __STDC_ENDIAN_LITTLE__ or 
__STDC_ENDIAN_BIG__ */

probably with values taken from Glibc's  or BSD's
.

But, I think the next POSIX revision has  like Glibc which I
prefer. So these defines:

#define LITTLE_ENDIAN /* Unique constant */
#define BIG_ENDIAN /* Unique constant */
#define BYTE_ORDER /* LITTLE_ENDIAN or BIG_ENDIAN */

plus functions or macros:

 uint16_t be16toh (uint16_t);
 uint16_t htobe16 (uint16_t);

I could try to work on that if it seems useful to anyone else.

Collin



Re: header file substitutes

2024-05-04 Thread Paul Eggert

On 2024-05-04 07:52, Bruno Haible wrote:

But who will want to use it,
as long as it's not portable?


I'm working on a Gnulib  that will work on pre-C23 platforms.

Eventually this should replace Gnulib's count-leading-zeros, 
count-trailing-zeros, and count-one-bits modules, which should be marked 
as obsolescent once we have a standard (and nicer) way to get that 
information.




Re: header file substitutes

2024-05-04 Thread Bruno Haible
Collin Funk wrote:
> I didn't realize the header replacements started this early. I'm a big
> fan of them.

And we're not yet done with the header file substitutes. One is still missing
from Gnulib:  [1]. The only thing that has changed since last year
is that glibc 2.39 now has this header file [2]. But who will want to use it,
as long as it's not portable?

Bruno

[1] https://lists.gnu.org/archive/html/bug-gnulib/2023-03/msg00142.html
[2] https://lists.gnu.org/archive/html/info-gnu/2024-01/msg00017.html