README.md

copies-and-fills

SUMMARY

Replacement memcpy and memset functionality for the Raspberry Pi with the intention of gaining greater performance.
Coding with an understanding of single-issue is important.

Tested using a modified https://github.com/ssvb/ssvb-membench, from Siarhei Siamashka.
The testing involves lots of random numbers, iterating through sizes and source/destination alignments.
If you find a bug, please tell me!

To use: define the environment variable, LD_PRELOAD=/full/path/to/libcofi_rpi.so, then run program.

memcpy:
Can be found in memcpy.s.
Compared to the generic libc memcpy, this one reaches performance parity at around ~150 bytes copies with any source/destination alignment and eventually gains 2-3x throughput, especially when the source buffer is uncached.
When taking the libc source and enabling the pld path, it certainly does improve. However the source alignment option appears to do nothing for performance yet greatly increases the code complexity.
In initial testing, some facts were found:

despite the increase in free registers, copies via VFP were slower at peak by ~25%

copying 32 bytes at a time with a single store-multiple gives the highest performance

for misaligned transfers, 32b-aligned stms are the way forward with mov/orr byte shuffling; byte copies give very poor performance

The code deals with the special small sizes, then races to reach 32b alignment of the destination.
We then test for misalignment with the source. If the (source - dest alignment) & 3 != 0 then we use the misaligned path.
For the aligned path, we iterate through the data, 32 bytes at a time. We then handle a word at a time, then a byte.
For the misaligned path, we have to choose how misaligned we are - 1, 2, or 3 bytes. There is a custom path for each that does the appropriate shifts.

The key to this is prefetch of the source array. Prefetch instructions must be far from the load instruction, as it appears the load/store pipe is busy for a while after a large load instruction is issued.

Speeds of up to 680 MB/s have been achieved (effective 339 MB/s copy).

memset:
Can by found in memset.s.
Compared to the generic libc memset, this quickly reaches performance parity at around 100 bytes with any alignment.
On testing,

The code 4-byte aligns the destination with a byte writer, then 32-byte aligns it with a word writer.
We then write two 2*16 bytes of data, then write words, then bytes.
No preload of destination data seems to be required.

Speeds of up to 1390 MB/s have been achieved. This is ~7x faster than the libc version.