Skip to content

Commit 2ada50f

Browse files
committed
Added C++23 byteswap
1 parent 38e5363 commit 2ada50f

File tree

10 files changed

+400
-2
lines changed

10 files changed

+400
-2
lines changed

src/crt/i48bswap.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ __i48bswap:
3333
pop de
3434
pop hl
3535
inc sp
36-
ret
36+
ret

src/libc/bswap24.src

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public ___ezcxx_bswap24
6+
7+
___ezcxx_bswap24:
8+
ld iy, 0
9+
add iy, sp
10+
ld h, (iy + 3)
11+
ld l, (iy + 5)
12+
ld (iy + 3), l
13+
ld (iy + 5), h
14+
ld hl, (iy + 3)
15+
ret

src/libc/bswap48.src

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public ___ezcxx_bswap48
6+
7+
; allows __i48bswap to be called from C
8+
___ezcxx_bswap48:
9+
pop bc, hl, de
10+
push de, hl, bc
11+
jp __i48bswap
12+
13+
extern __i48bswap

src/libc/include/byteswap.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
#ifndef _BYTESWAP_H
22
#define _BYTESWAP_H
33

4+
#include <cdefs.h>
45
#include <stdint.h>
56

7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
uint24_t __ezcxx_bswap24(uint24_t) __NOEXCEPT_CONST;
12+
uint48_t __ezcxx_bswap48(uint48_t) __NOEXCEPT_CONST;
13+
614
static inline uint16_t bswap_16(uint16_t x) { return __builtin_bswap16(x); }
15+
static inline uint24_t bswap_24(uint24_t x) { return __ezcxx_bswap24(x); }
716
static inline uint32_t bswap_32(uint32_t x) { return __builtin_bswap32(x); }
17+
static inline uint48_t bswap_48(uint48_t x) { return __ezcxx_bswap48(x); }
818
static inline uint64_t bswap_64(uint64_t x) { return __builtin_bswap64(x); }
919

20+
#ifdef __cplusplus
21+
}
22+
#endif
23+
1024
#endif /* _BYTESWAP_H */

src/libcxx/include/bit

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
#pragma clang system_header
1111

12+
extern "C" {
13+
unsigned int __ezcxx_bswap24(unsigned int) noexcept __attribute__((__const__));
14+
unsigned __int48 __ezcxx_bswap48(unsigned __int48) noexcept __attribute__((__const__));
15+
}
16+
1217
namespace std {
1318

1419
enum class endian {
@@ -144,6 +149,33 @@ int popcount(unsigned long long __t) noexcept {
144149
return __builtin_popcountll(__t);
145150
}
146151

152+
//------------------------------------------------------------------------------
153+
// byteswap
154+
//------------------------------------------------------------------------------
155+
156+
#if __cplusplus >= 201703L
157+
158+
template<class _Tp> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
159+
_Tp byteswap(_Tp __val) noexcept {
160+
if constexpr (sizeof(_Tp) == 1) {
161+
return __val;
162+
} else if constexpr (sizeof(_Tp) == 2) {
163+
return __builtin_bswap16(__val);
164+
} else if constexpr (sizeof(_Tp) == 3) {
165+
return __ezcxx_bswap24(__val);
166+
} else if constexpr (sizeof(_Tp) == 4) {
167+
return __builtin_bswap32(__val);
168+
} else if constexpr (sizeof(_Tp) == 6) {
169+
return __ezcxx_bswap48(__val);
170+
} else if constexpr (sizeof(_Tp) == 8) {
171+
return __builtin_bswap64(__val);
172+
} else {
173+
static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");
174+
}
175+
}
176+
177+
#endif /* __cplusplus >= 201703L */
178+
147179
//------------------------------------------------------------------------------
148180
// has_single_bit
149181
//------------------------------------------------------------------------------

src/libcxx/include/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
// # define __cpp_lib_allocate_at_least 202302L
169169
// # define __cpp_lib_associative_heterogeneous_erasure 202110L
170170
// # define __cpp_lib_bind_back 202202L
171-
// # define __cpp_lib_byteswap 202110L
171+
# define __cpp_lib_byteswap 202110L
172172
// # define __cpp_lib_constexpr_bitset 202207L
173173
// # define __cpp_lib_constexpr_charconv 202207L
174174
// # define __cpp_lib_constexpr_cmath 202202L
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"transfer_files": [
3+
"bin/DEMO.8xp"
4+
],
5+
"target": {
6+
"name": "DEMO",
7+
"isASM": true
8+
},
9+
"sequence": [
10+
"action|launch",
11+
"delay|200",
12+
"hashWait|1",
13+
"key|enter",
14+
"delay|300",
15+
"hashWait|2"
16+
],
17+
"hashes": {
18+
"1": {
19+
"description": "All tests passed",
20+
"timeout": 5000,
21+
"start": "vram_start",
22+
"size": "vram_16_size",
23+
"expected_CRCs": [
24+
"38E2AD5A"
25+
]
26+
},
27+
"2": {
28+
"description": "Exit",
29+
"start": "vram_start",
30+
"size": "vram_16_size",
31+
"expected_CRCs": [
32+
"FFAF89BA",
33+
"101734A5",
34+
"9DA19F44",
35+
"A32840C8",
36+
"349F4775"
37+
]
38+
}
39+
}
40+
}

test/standalone/stdbit/makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
11+
CFLAGS = -Wall -Wextra -Wshadow -Wconversion -Wformat=2 -std=c17 -Oz
12+
CXXFLAGS = -Wall -Wextra -Wshadow -Wconversion -Wformat=2 -std=c++17 -Oz
13+
14+
PREFER_OS_LIBC = NO
15+
16+
# ----------------------------
17+
18+
include $(shell cedev-config --makefile)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
assume adl = 1
2+
3+
section .data
4+
5+
; uint8_t byteswap_u8[4] = {
6+
; 0x00,
7+
; 0xFF,
8+
; 0x12,
9+
; 0x43,
10+
; };
11+
; uint16_t byteswap_u16[4] = {
12+
; 0x0000,
13+
; 0xFFFF,
14+
; 0x1234,
15+
; 0x4321,
16+
; };
17+
; uint24_t byteswap_u24[4] = {
18+
; 0x000000,
19+
; 0xFFFFFF,
20+
; 0x123456,
21+
; 0x654321,
22+
; };
23+
; uint32_t byteswap_u32[4] = {
24+
; 0x00000000,
25+
; 0xFFFFFFFF,
26+
; 0x12345678,
27+
; 0x87654321,
28+
; };
29+
; uint48_t byteswap_u48[4] = {
30+
; 0x000000000000,
31+
; 0xFFFFFFFFFFFF,
32+
; 0x123456789ABC,
33+
; 0xCBA987654321,
34+
; };
35+
; uint64_t byteswap_u64[4] = {
36+
; 0x0000000000000000,
37+
; 0xFFFFFFFFFFFFFFFF,
38+
; 0x0123456789ABCDEF,
39+
; 0xFEDCBA9876543210,
40+
; };
41+
42+
public _byteswap_u8
43+
_byteswap_u8:
44+
db 0
45+
db 255
46+
db 18
47+
db 67
48+
49+
public _byteswap_u16
50+
_byteswap_u16:
51+
dw 0
52+
dw 65535
53+
dw 4660
54+
dw 17185
55+
56+
public _byteswap_u24
57+
_byteswap_u24:
58+
dl 0
59+
dl 16777215
60+
dl 1193046
61+
dl 6636321
62+
63+
public _byteswap_u32
64+
_byteswap_u32:
65+
dd 0
66+
dd 4294967295
67+
dd 305419896
68+
dd 2271560481
69+
70+
public _byteswap_u48
71+
_byteswap_u48:
72+
dd 0
73+
dw 0
74+
dd 4294967295
75+
dw 65535
76+
dd 1450744508
77+
dw 4660
78+
dd 2271560481
79+
dw 52137
80+
81+
public _byteswap_u64
82+
_byteswap_u64:
83+
dq 0
84+
dq -1
85+
dq 81985529216486895
86+
dq -81985529216486896
87+
88+
; uint8_t byteswap_i8[4] = {
89+
; 0x00,
90+
; 0xFF,
91+
; 0x12,
92+
; 0x43,
93+
; };
94+
; uint16_t byteswap_i16[4] = {
95+
; 0x0000,
96+
; 0xFFFF,
97+
; 0x3412,
98+
; 0x2143,
99+
; };
100+
; uint24_t byteswap_i24[4] = {
101+
; 0x000000,
102+
; 0xFFFFFF,
103+
; 0x563412,
104+
; 0x214365,
105+
; };
106+
; uint32_t byteswap_i32[4] = {
107+
; 0x00000000,
108+
; 0xFFFFFFFF,
109+
; 0x78563412,
110+
; 0x21436587,
111+
; };
112+
; uint48_t byteswap_i48[4] = {
113+
; 0x000000000000,
114+
; 0xFFFFFFFFFFFF,
115+
; 0xBC9A78563412,
116+
; 0x21436587A9CB,
117+
; };
118+
; uint64_t byteswap_i64[4] = {
119+
; 0x0000000000000000,
120+
; 0xFFFFFFFFFFFFFFFF,
121+
; 0xEFCDAB8967452301,
122+
; 0x1032547698BADCFE,
123+
; };
124+
125+
public _byteswap_i8
126+
_byteswap_i8:
127+
db 0
128+
db 255
129+
db 18
130+
db 67
131+
132+
public _byteswap_i16
133+
_byteswap_i16:
134+
dw 0
135+
dw 65535
136+
dw 13330
137+
dw 8515
138+
139+
public _byteswap_i24
140+
_byteswap_i24:
141+
dl 0
142+
dl 16777215
143+
dl 5649426
144+
dl 2179941
145+
146+
public _byteswap_i32
147+
_byteswap_i32:
148+
dd 0
149+
dd 4294967295
150+
dd 2018915346
151+
dd 558065031
152+
153+
public _byteswap_i48
154+
_byteswap_i48:
155+
dd 0
156+
dw 0
157+
dd 4294967295
158+
dw 65535
159+
dd 2018915346
160+
dw 48282
161+
dd 1703389643
162+
dw 8515
163+
164+
public _byteswap_i64
165+
_byteswap_i64:
166+
dq 0
167+
dq -1
168+
dq -1167088121787636991
169+
dq 1167088121787636990

0 commit comments

Comments
 (0)