Skip to content

Commit f3da226

Browse files
authored
Merge pull request #129 from turbolent/fix-big-endian-unaligned-load-store
Support unaligned access on big-endian targets
2 parents 90235e5 + f2362d5 commit f3da226

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ run-tests-%:
4848

4949
run-test-%: test_%$(EXE)
5050
echo ">>>" $<
51-
$(PYTHON3) run.py $(if $(EXECUTE),-e $(EXECUTE),) $(if $(ARCH),-a $(ARCH),) $<
51+
$(PYTHON3) run.py $(if $(EXECUTE),-e "$(EXECUTE)",) $(if $(ARCH),-a $(ARCH),) $<
5252
echo
5353

5454
test_%$(EXE): test_%.o main.o

tests/run.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import difflib
55
import sys
6+
import shlex
67

78
def main():
89
parser = argparse.ArgumentParser()
@@ -14,7 +15,7 @@ def main():
1415
command = []
1516

1617
if args.execute:
17-
command.append(args.execute)
18+
command.extend(shlex.split(args.execute))
1819
command.append(os.path.abspath(args.test))
1920

2021
output = subprocess.run(command, check=True, stderr=subprocess.PIPE).stderr.decode('utf-8').strip()
@@ -43,4 +44,4 @@ def main():
4344
exit(1)
4445

4546
if __name__ == "__main__":
46-
main()
47+
main()

w2c2/w2c2_base.h

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -871,13 +871,38 @@ load_data(
871871

872872
#else
873873

874-
#define readSwapU16(base, offset) swapU16(*(U16*)((base) + (offset)))
875-
#define readSwapU32(base, offset) swapU32(*(U32*)((base) + (offset)))
876-
#define readSwapU64(base, offset) swapU64(*(U64*)((base) + (offset)))
874+
static W2C2_INLINE U16 readSwapU16(const void* address, U32 offset) {
875+
U16 result;
876+
memcpy(&result, address + offset, sizeof(U16));
877+
return swapU16(result);
878+
}
879+
880+
static W2C2_INLINE U32 readSwapU32(const void* address, U32 offset) {
881+
U32 result;
882+
memcpy(&result, address + offset, sizeof(U32));
883+
return swapU32(result);
884+
}
885+
886+
static W2C2_INLINE U64 readSwapU64(const void* address, U32 offset) {
887+
U64 result;
888+
memcpy(&result, address + offset, sizeof(U64));
889+
return swapU64(result);
890+
}
877891

878-
#define writeSwapU16(base, offset, value) (*(U16*)((base) + (offset)) = swapU16(value))
879-
#define writeSwapU32(base, offset, value) (*(U32*)((base) + (offset)) = swapU32(value))
880-
#define writeSwapU64(base, offset, value) (*(U64*)((base) + (offset)) = swapU64(value))
892+
static W2C2_INLINE void writeSwapU16(void* address, U32 offset, U16 v) {
893+
v = swapU16(v);
894+
memcpy(address + offset, &v, sizeof(U16));
895+
}
896+
897+
static W2C2_INLINE void writeSwapU32(void* address, U32 offset, U32 v) {
898+
v = swapU32(v);
899+
memcpy(address + offset, &v, sizeof(U32));
900+
}
901+
902+
static W2C2_INLINE void writeSwapU64(void* address, U32 offset, U64 v) {
903+
v = swapU64(v);
904+
memcpy(address + offset, &v, sizeof(U64));
905+
}
881906

882907
#endif
883908

0 commit comments

Comments
 (0)