Skip to content

Commit 9c3d3a1

Browse files
committed
Renaming readAligned to load32Aligned.
1 parent 0c961e8 commit 9c3d3a1

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/mips/common/util/util.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,33 @@ SOFTWARE.
2828

2929
#include <stdint.h>
3030

31-
static __inline__ uint32_t readUnaligned(const void *in, int pos) {
31+
#ifdef __cplusplus
32+
33+
#include <concepts>
34+
35+
namespace Utilities {
36+
37+
template <std::integral T>
38+
T loadUnaligned(const uint8_t *ptr, unsigned size = (sizeof(T) + 7) / 8) {
39+
T ret = 0;
40+
for (unsigned i = 0; i < size; i++) {
41+
ret |= (ptr[i] << (i * 8));
42+
}
43+
return ret;
44+
}
45+
46+
template <std::integral T>
47+
void storeUnaligned(uint8_t *ptr, T value, unsigned size = (sizeof(T) + 7) / 8) {
48+
for (unsigned i = 0; i < size; i++) {
49+
ptr[i] = value >> (i * 8);
50+
}
51+
}
52+
53+
} // namespace Utilities
54+
55+
#endif
56+
57+
static __inline__ uint32_t load32Unaligned(const void *in, int pos) {
3258
const uint8_t *buffer = (const uint8_t *)in;
3359
uint32_t r;
3460
__builtin_memcpy(&r, buffer + pos, sizeof(uint32_t));

src/mips/openbios/cdrom/filesystem.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int cdromReadPathTable() {
7070

7171
const uint32_t *const buffer = (uint32_t *)g_readBuffer;
7272
s_pathTableSize = buffer[132 / 4];
73-
s_rootDirectorySector = readUnaligned(buffer, 158);
73+
s_rootDirectorySector = load32Unaligned(buffer, 158);
7474
s_currentDiscHash = buffer[80 / 4];
7575
uint32_t lba = s_pathTableLocation = buffer[140 / 4];
7676

@@ -83,7 +83,7 @@ int cdromReadPathTable() {
8383
int entryID = 1;
8484
while ((ptr < g_readBuffer + sizeof(g_readBuffer)) && (entryID <= sizeof(s_pathTable) / sizeof(s_pathTable[0]))) {
8585
if (!ptr[0]) break;
86-
entry->LBA = readUnaligned(ptr, 2);
86+
entry->LBA = load32Unaligned(ptr, 2);
8787
// ...why? it's literally the array entry number.
8888
entry->ID = entryID;
8989
// Yes. I can't even.
@@ -122,8 +122,8 @@ static int readDirectory(int entryID) {
122122
while ((ptr < (g_readBuffer + sizeof(g_readBuffer))) &&
123123
(entry < s_cachedDirectoryEntry + sizeof(s_cachedDirectoryEntry) / sizeof(s_cachedDirectoryEntry[0])) &&
124124
ptr[0]) {
125-
entry->LBA = readUnaligned(ptr, 2);
126-
entry->size = readUnaligned(ptr, 10);
125+
entry->LBA = load32Unaligned(ptr, 2);
126+
entry->size = load32Unaligned(ptr, 10);
127127
uint8_t nameSize = ptr[32];
128128
memcpy(entry->name, ptr + 33, nameSize);
129129
entry->name[nameSize] = 0;

src/mips/shell/cdrom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static void complete() {
188188
s_wait = 1;
189189
break;
190190
}
191-
uint32_t lba = readUnaligned(s_sector, 158) + 150;
191+
uint32_t lba = load32Unaligned(s_sector, 158) + 150;
192192
unsigned minutes = lba / 4500;
193193
lba %= 4500;
194194
uint8_t msf[3] = {(minutes % 10) + (minutes / 10) * 0x10, ((lba / 75) % 10) + ((lba / 75) / 10) * 0x10,

0 commit comments

Comments
 (0)