From c723829e7e7c23570033543e4b8a37b2c9862fda Mon Sep 17 00:00:00 2001 From: Daniel C Date: Wed, 3 Jan 2024 20:59:50 -0500 Subject: [PATCH] Some experiments to remove misaligned accesses Although it seems to me like this would hurt performance on most CPUs. --- Makefile | 7 +++--- src/camlib.h | 4 ++- src/data.c | 1 + src/packet.c | 69 ++++++++++++++++++++++++++++++++++++---------------- 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index c3e51ba..c09c4db 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ FILES+=src/libusb.o src/transport.o COMMIT=$(shell git rev-parse HEAD) CFLAGS+='-DCAMLIB_COMMIT="$(COMMIT)"' +# TODO: add LDFLAGS to fix mac builds libcamlib.so: $(FILES) $(CC) -shared $(FILES) -o libcamlib.so @@ -42,7 +43,7 @@ install: libcamlib.so -mkdir /usr/include/camlib cp src/*.h /usr/include/camlib/ -test-ci: test/test.o $(FILES) - $(CC) test/test.o $(FILES) $(LDFLAGS) $(CFLAGS) -o test-ci +test: + bash test/myci.sh -.PHONY: all clean install stringify +.PHONY: all clean install stringify test diff --git a/src/camlib.h b/src/camlib.h index 086d4db..da823c7 100644 --- a/src/camlib.h +++ b/src/camlib.h @@ -258,7 +258,7 @@ void ptp_set_prop_avail_info(struct PtpRuntime *r, int code, int memb_size, int // Duplicate array, return malloc'd buffer // TODO: deprecate this -struct UintArray *ptp_dup_uint_array(struct UintArray *arr); +struct UintArray *ptp_dup_uint_array(struct UintArray *arr) __attribute__ ((deprecated)); void *ptp_dup_payload(struct PtpRuntime *r); @@ -271,6 +271,7 @@ int ptp_dump(struct PtpRuntime *r); #include "cl_enum.h" #include "cl_bind.h" +#if 0 // Deprecated old functions void ptp_generic_reset(struct PtpRuntime *r) __attribute__ ((deprecated)); struct PtpRuntime *ptp_generic_new() __attribute__ ((deprecated)); @@ -280,6 +281,7 @@ int ptp_generic_send(struct PtpRuntime *r, struct PtpCommand *cmd) __attribute__ int ptp_generic_send_data(struct PtpRuntime *r, struct PtpCommand *cmd, void *data, int length) __attribute__ ((deprecated)); int ptp_generic_send(struct PtpRuntime *r, struct PtpCommand *cmd) __attribute__ ((deprecated)); int ptp_generic_send_data(struct PtpRuntime *r, struct PtpCommand *cmd, void *data, int length) __attribute__ ((deprecated)); +#endif // Backwards compatibility (mostly renamed functions) #ifndef CAMLIB_NO_COMPAT diff --git a/src/data.c b/src/data.c index b50b05b..053d0aa 100644 --- a/src/data.c +++ b/src/data.c @@ -1,5 +1,6 @@ // Data structure unpacking and packing functions // Copyright 2022 by Daniel C (https://github.com/petabyt/camlib) +// This code uses unaligned access/writes, will probably be changed in the future #include #include diff --git a/src/packet.c b/src/packet.c index 9ce5f91..00f3784 100644 --- a/src/packet.c +++ b/src/packet.c @@ -30,15 +30,56 @@ uint32_t ptp_read_uint32(void *dat) { return x; } +#if 0 +// This seems slower than misaligned access lol +uint8_t ptp_read_uint8(void *dat) { + uint8_t **p = (uint8_t **)dat; + uint8_t x = (**p); + (*p)++; + return x; +} + +uint16_t ptp_read_uint16(void *dat) { + uint8_t **p = (uint8_t **)dat; + uint16_t x = (p[0][0] << 8) | p[0][0]; + (*p) += 2; + return x; +} + +uint32_t ptp_read_uint32(void *dat) { + uint8_t **p = (uint8_t **)dat; + uint32_t x; + ((uint8_t *)&x)[0] = p[0][0]; + ((uint8_t *)&x)[1] = p[0][1]; + ((uint8_t *)&x)[2] = p[0][2]; + ((uint8_t *)&x)[3] = p[0][3]; + (*p) += 4; + return x; +} +#endif + +void ptp_write_uint8(void *dat, uint8_t b) { + uint8_t **ptr = (uint8_t **)dat; + (**ptr) = b; + (*ptr)++; +} + +int ptp_write_uint32(void *dat, uint32_t b) { + uint32_t **ptr = (uint32_t **)dat; + (**ptr) = b; + (*ptr)++; + + return 4; +} + // Read standard UTF16 string void ptp_read_string(void *dat, char *string, int max) { - uint8_t **p = (uint8_t **)dat; - int length = (int)ptp_read_uint8((void **)p); + int length = (int)ptp_read_uint8(dat); int y = 0; while (y < length) { - string[y] = (char)(**p); - (*p) += 2; + string[y] = (char)ptp_read_uint8(dat); + ptp_read_uint8(dat); // skip \0 y++; if (y >= max) { break; } } @@ -47,33 +88,19 @@ void ptp_read_string(void *dat, char *string, int max) { } int ptp_read_uint16_array(void *dat, uint16_t *buf, int max) { - int n = ptp_read_uint32((void **)dat); + int n = ptp_read_uint32(dat); for (int i = 0; i < n; i++) { if (i >= max) { - (void)ptp_read_uint16((void **)dat); + (void)ptp_read_uint16(dat); } else { - buf[i] = ptp_read_uint16((void **)dat); + buf[i] = ptp_read_uint16(dat); } } return n; } -void ptp_write_uint8(void *dat, uint8_t b) { - uint8_t **ptr = (uint8_t **)dat; - (**ptr) = b; - (*ptr)++; -} - -int ptp_write_uint32(void *dat, uint32_t b) { - uint32_t **ptr = (uint32_t **)dat; - (**ptr) = b; - (*ptr)++; - - return 4; -} - int ptp_write_string(void *dat, char *string) { int length = strlen(string); ptp_write_uint8(dat, length);