Implementing Endian.h little endian functions in IRIX -
Raion - 04-18-2021
So I've been holding off on this for libxg, my attempt to add modern functions to IRIX, due to really not getting the most efficient methods to do this.
As libxg is BSD-licensed, please do not give me GNU examples. I don't want to potentially infringe on GPL code.
Currently, our endian.h only implements htobe functions (which require nothing, as IRIX is already eb).
Here's the current version (derived from NetBSD). Some of these includes are probably not necessary at all, but I didn't want to take chances.
Code:
#ifndef _ENDIAN_H_
#define _ENDIAN_H_
#include <standards.h>
#include <sys/endian.h>
#include <netinet/in.h>
#include <inttypes.h>
/*
* Dummy host-to-be macros. Honestly this shouldn't be a major issue
* to exclude htole functions, but we'll see
*/
#define htobe16(x) (x)
#define htobe32(x) (x)
#define htobe64(x) (x)
#define HTOBE16(x) (void, (x))
#define HTOBE32(x) (void, (x))
#define HTOBE64(x) (void, (x))
#define be16toh(x) htobe16(x)
#define be32toh(x) htobe32(x)
#define be64toh(x) htobe64(x)
#define BE16TOH(x) HTOBE16(x)
#define BE32TOH(x) HTOBE32(x)
#define BE64TOH(x) HTOBE64(x)
#endif
The NetBSD guys have their code to do the bswap functions in sys/bswap.h - and the relevant functions that they correspond to are here:
Code:
#define __byte_swap_u64_constant(x) \
(__CAST(uint64_t, \
((((x) & 0xff00000000000000ull) >> 56) | \
(((x) & 0x00ff000000000000ull) >> 40) | \
(((x) & 0x0000ff0000000000ull) >> 24) | \
(((x) & 0x000000ff00000000ull) >> 8) | \
(((x) & 0x00000000ff000000ull) << 8) | \
(((x) & 0x0000000000ff0000ull) << 24) | \
(((x) & 0x000000000000ff00ull) << 40) | \
(((x) & 0x00000000000000ffull) << 56))))
#define __byte_swap_u32_constant(x) \
(__CAST(uint32_t, \
((((x) & 0xff000000) >> 24) | \
(((x) & 0x00ff0000) >> 8) | \
(((x) & 0x0000ff00) << 8) | \
(((x) & 0x000000ff) << 24))))
#define __byte_swap_u16_constant(x) \
(__CAST(uint16_t, \
((((x) & 0xff00) >> 8) | \
(((x) & 0x00ff) << 8))))
Clearly, these are bitwise math functions. My questions are as follows:
1. Are these functions efficient to port into IRIX? If not, what's a better way to do this?
2. Is this close to how GNULib does it? I can't quite go and /look/ at their code to do this since I'm not wanting to do that.
3. Other than removing things like __CAST from the functions, are there any problematic bits that I could encounter by using these?
RE: Implementing Endian.h little endian functions in IRIX -
jpstewart - 04-19-2021
(04-18-2021, 08:04 PM)Raion Wrote: 2. Is this close to how GNULib does it? I can't quite go and /look/ at their code to do this since I'm not wanting to do that.
It's almost identical. There's really only one way to swap bytes. There's nothing unique or proprietary about the algorithm.
RE: Implementing Endian.h little endian functions in IRIX -
Raion - 04-19-2021
Gotcha. So this is indeed the best way to do this. Thank you.
RE: Implementing Endian.h little endian functions in IRIX -
Raion - 04-19-2021
Implemented. Will make it a point to have someone test this eventually.