Skip to content

Commit 026d3f4

Browse files
authored
druntime, posix: Add bindings for POSIX endian.h (#21644)
This is part of the IEEE Std 1003.1 Issue 8 https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html On a spot check of OS's, amongst Linux, OSX and *BSDs, many already had these in `endian.h` prior to it becoming standard. Of those which have omissions to the spec: - OSX 12 doesn't define the `hto*`, `be*`, and `le*` macros - Solaris 11.4 doesn't have the header at all. But that doesn't matter as this is meant to be a macro-only header, and the implementation is obvious for all platforms.
1 parent 5563182 commit 026d3f4

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

druntime/mak/COPY

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ COPY=\
312312
$(IMPDIR)\core\sys\posix\config.d \
313313
$(IMPDIR)\core\sys\posix\dirent.d \
314314
$(IMPDIR)\core\sys\posix\dlfcn.d \
315+
$(IMPDIR)\core\sys\posix\endian.d \
315316
$(IMPDIR)\core\sys\posix\fcntl.d \
316317
$(IMPDIR)\core\sys\posix\grp.d \
317318
$(IMPDIR)\core\sys\posix\iconv.d \

druntime/mak/SRCS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ SRCS=\
307307
src\core\sys\posix\config.d \
308308
src\core\sys\posix\dirent.d \
309309
src\core\sys\posix\dlfcn.d \
310+
src\core\sys\posix\endian.d \
310311
src\core\sys\posix\fcntl.d \
311312
src\core\sys\posix\grp.d \
312313
src\core\sys\posix\iconv.d \
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* D header file for POSIX.
3+
*
4+
* $(LINK2 https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html posix endian.h)
5+
*
6+
* Standards: The Open Group Base Specifications Issue 8, IEEE Std 1003.1, 2024 Edition
7+
*/
8+
module core.sys.posix.endian;
9+
10+
version (Posix):
11+
nothrow:
12+
@safe:
13+
@nogc:
14+
15+
import core.bitop;
16+
17+
enum LITTLE_ENDIAN = 1234;
18+
enum BIG_ENDIAN = 4321;
19+
enum PDP_ENDIAN = 3412;
20+
21+
version (LittleEndian)
22+
{
23+
enum BYTE_ORDER = LITTLE_ENDIAN;
24+
25+
pragma(inline, true):
26+
ushort htobe16(ushort x) => core.bitop.byteswap(x);
27+
ushort htole16(ushort x) => x;
28+
ushort be16toh(ushort x) => core.bitop.byteswap(x);
29+
ushort le16toh(ushort x) => x;
30+
31+
uint htobe32(uint x) => core.bitop.bswap(x);
32+
uint htole32(uint x) => x;
33+
uint be32toh(uint x) => core.bitop.bswap(x);
34+
uint le32toh(uint x) => x;
35+
36+
ulong htobe64(ulong x) => core.bitop.bswap(x);
37+
ulong htole64(ulong x) => x;
38+
ulong be64toh(ulong x) => core.bitop.bswap(x);
39+
ulong le64toh(ulong x) => x;
40+
}
41+
else
42+
{
43+
enum BYTE_ORDER = BIG_ENDIAN;
44+
45+
pragma(inline, true):
46+
ushort htobe16(ushort x) => x;
47+
ushort htole16(ushort x) => core.bitop.byteswap(x);
48+
ushort be16toh(ushort x) => x;
49+
ushort le16toh(ushort x) => core.bitop.byteswap(x);
50+
51+
uint htobe32(uint x) => x;
52+
uint htole32(uint x) => core.bitop.bswap(x);
53+
uint be32toh(uint x) => x;
54+
uint le32toh(uint x) => core.bitop.bswap(x);
55+
56+
ulong htobe64(ulong x) => x;
57+
ulong htole64(ulong x) => core.bitop.bswap(x);
58+
ulong be64toh(ulong x) => x;
59+
ulong le64toh(ulong x) => core.bitop.bswap(x);
60+
}

0 commit comments

Comments
 (0)