Skip to content

Add a fully implementation of endian.h#2

Open
thisisaname1928 wants to merge 14 commits into
BoredOS:mainfrom
thisisaname1928:main
Open

Add a fully implementation of endian.h#2
thisisaname1928 wants to merge 14 commits into
BoredOS:mainfrom
thisisaname1928:main

Conversation

@thisisaname1928

@thisisaname1928 thisisaname1928 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Add a fully implementation of endian.h

Overview

Functions & Macros

#define LITTLE_ENDIAN
#define BIG_ENDIAN 
#define __LIBC_BYTE_ORDER 

uint16_t  be16toh(uint16_t);
uint32_t  be32toh(uint32_t);
uint64_t  be64toh(uint64_t);
uint16_t  htobe16(uint16_t);
uint32_t  htobe32(uint32_t);
uint64_t  htobe64(uint64_t);
uint16_t  htole16(uint16_t);
uint32_t  htole32(uint32_t);
uint64_t  htole64(uint64_t);
uint16_t  le16toh(uint16_t);
uint32_t  le32toh(uint32_t);
uint64_t  le64toh(uint64_t);

@thisisaname1928 thisisaname1928 changed the title Add a fully implement of endian.h Add a fully implementation of endian.h Jun 6, 2026
@BoredDevNL

Copy link
Copy Markdown
Member

Nice work overall - the three-tier compiler detection and manual fallback bswap are solid. A few things are worth addressing before i merge:


[bug] Macro argument not consistently parenthesized in manual bswap fallback

In the #else bswap falback, x isn't wrapped in (x) on every expansion site. This causes operator-precedence bugs when callers pass an expression like a | b or *ptr++:

// e.g. __bswap32(a | b) expands to:
(uint32_t)((((uint32_t)(a | b) >> 24) & 0xff) | ...)
//                      ^^^^^^^ fine here, but not guaranteed safe everywhere

Every occurrence of x inside the macro body should be (x).


[bug] LITTLE_ENDIAN / BIG_ENDIAN defined unconditionally

Defining these without an #ifndef guard will produce redefinition confliicts if any included header (e.g. a compiler runtime header) already defines them. Either guard them:

#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN 1234
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 4321
#endif

or use a private namespace alias (e.g. __LIBC_LITTLE_ENDIAN) and reference that throughout.


[robustness] No fallback #error if __LIBC_BYTE_ORDER is an unrecognised value

If a compiler defines __BYTE_ORDER__ to something unexpected, the header silently generates broken macros with no diagnostic. Add a catch-all:

#if (__LIBC_BYTE_ORDER == LITTLE_ENDIAN)
  // ...
#elif (__LIBC_BYTE_ORDER == BIG_ENDIAN)
  // ...
#else
#error "Unrecognised byte order — please add support for this target"
#endif

@thisisaname1928

Copy link
Copy Markdown
Contributor Author

Thanks for ur reply, btw I have university entrance exam and graduation exam so Ima work on the fix a bit late ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants