Skip to content

Commit

Permalink
Some experiments to remove misaligned accesses
Browse files Browse the repository at this point in the history
Although it seems to me like this would hurt performance on most CPUs.
  • Loading branch information
petabyt committed Jan 4, 2024
1 parent 65cde89 commit c723829
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
4 changes: 3 additions & 1 deletion src/camlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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));
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/data.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
Expand Down
69 changes: 48 additions & 21 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand All @@ -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);
Expand Down

0 comments on commit c723829

Please sign in to comment.