shithub: femtolisp

Download patch

ref: 5b0abd4ef6ae0deca52d0d498d73a51d11387b38
parent: a7bae3835d8f73f5ad2a6fbaec4bd650cd0c1dea
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Nov 10 17:53:14 EST 2024

remove arch-dependent bswap uses on non-aligned addresses, use per-byte reads

--- a/flisp.c
+++ b/flisp.c
@@ -818,23 +818,23 @@
 	return nargs;
 }
 
-#if BYTE_ORDER == BIG_ENDIAN
 #define GET_INT32(a) \
 	((int32_t) \
-	((((int32_t)a[0])<<0)  | \
-	 (((int32_t)a[1])<<8)  | \
-	 (((int32_t)a[2])<<16) | \
-	 (((int32_t)a[3])<<24)))
+	((((uint32_t)a[0])<<0)  | \
+	 (((uint32_t)a[1])<<8)  | \
+	 (((uint32_t)a[2])<<16) | \
+	 (((uint32_t)a[3])<<24)))
 #define GET_INT16(a) \
 	((int16_t) \
 	((((int16_t)a[0])<<0)  | \
 	 (((int16_t)a[1])<<8)))
-#define PUT_INT32(a, i) (*(int32_t*)(a) = bswap_32((int32_t)(i)))
-#else
-#define GET_INT32(a) (*(int32_t*)a)
-#define GET_INT16(a) (*(int16_t*)a)
-#define PUT_INT32(a, i) (*(int32_t*)(a) = (int32_t)(i))
-#endif
+#define PUT_INT32(a, i) \
+	do{ \
+		((uint8_t*)(a))[0] = ((uint32_t)(i)>>0)&0xff; \
+		((uint8_t*)(a))[1] = ((uint32_t)(i)>>8)&0xff; \
+		((uint8_t*)(a))[2] = ((uint32_t)(i)>>16)&0xff; \
+		((uint8_t*)(a))[3] = ((uint32_t)(i)>>24)&0xff; \
+	}while(0)
 
 #define OP(x) case x:
 #define NEXT_OP break
@@ -1754,7 +1754,6 @@
 #if BYTE_ORDER == BIG_ENDIAN
 #undef SWAP_INT32
 #undef SWAP_INT16
-#if defined(__sparc__)
 #define SWAP_INT32(a) \
 	do{ \
 		uint8_t *x = (void*)a, y; \
@@ -1766,10 +1765,6 @@
 		uint8_t *x = (void*)a, y; \
 		y = x[0]; x[0] = x[1]; x[1] = y; \
 	}while(0)
-#else
-#define SWAP_INT32(a) (*(int32_t*)(a) = bswap_32(*(int32_t*)(a)))
-#define SWAP_INT16(a) (*(int16_t*)(a) = bswap_16(*(int16_t*)(a)))
-#endif
 #define compute_maxstack compute_maxstack_swap
 #include "maxstack.inc"
 #undef compute_maxstack