sonjeheon / lz4

Automatically exported from code.google.com/p/lz4
0 stars 0 forks source link

Unaligned access to dwords/words (prohibited on ARM/MIPS cpus) #4

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
1. Compile lz4 for ARM cpu
2. Turn on SIGBUS signals generation in case of unaligned access (on linux can 
be done via "echo 5 > /proc/cpu/alignment")
3. Run lz4 test

I'm trying it on linux 2.4.21.
The core of the problem is that ARM/MIPS cpu does not allow unaligned access, 
so that the following code crashes:
char a[8];
*(int*) &a[1] = 0; // crash because of unaligned write to mem
int b = *(int*) &a[1]; // crash because of unaligned read from mem

In lz4 there is a plenty of such lines:
do { *(U32*)op = *(U32*)anchor; op+=4; anchor+=4; } while (op<l_end) ;
in case of op pointer not aligned to 32-bit boundary that would cause crash 
(SIGBUS).

The possible solution could be using __attribute__ ((packed)) if compiling 
using gcc (that's almost always true for ARM/MIPS).
In case of x86 & x64 that won't affect performance at all, the generated code 
should remain the same. In case of ARM/MIPS that forces compiler to generate 
per-byte access code. Plz take a look at the patch:
http://directnet-drive.net/lz4.20.patch
(result here: http://directnet-drive.net/lz4.c)
That fixes the problem in my case and ARM tests succeed. x86 and x64 code is 
not affected at all.

Although ARM/MIPS cpus are not so widespread now they are becoming more and 
more popular. If you need any kind of my help please do not hesitate to ask for 
it.

Original issue reported on code.google.com by fmot.f...@gmail.com on 21 Sep 2011 at 3:16

GoogleCodeExporter commented 9 years ago
Yes, you are perfectly right.
It is within the objective of LZ4 to become available to ARM.

As a matter of fact, i know of several users which have been successfully 
adapting LZ4 to ARM, but doing it on their own. It would be better to provide a 
generic solution to everyone.
I first need to finalise an ARM testbed so that i can probably test and debug 
this adaptation.

Regards

Original comment by yann.col...@gmail.com on 22 Sep 2011 at 7:59

GoogleCodeExporter commented 9 years ago
I'll handle this one.

Original comment by yann.col...@gmail.com on 26 Sep 2011 at 9:40

GoogleCodeExporter commented 9 years ago
ARM-compatible code added in r33.
Thanks to Vlad Grachov for very detailed code proposition.

Original comment by yann.col...@gmail.com on 27 Sep 2011 at 12:05