Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in UnalignedCopy64 #7

Open
StefanMenne opened this issue Sep 2, 2014 · 2 comments
Open

Exception in UnalignedCopy64 #7

StefanMenne opened this issue Sep 2, 2014 · 2 comments

Comments

@StefanMenne
Copy link

The following Code results in an Exception during decompression in Utilities.UnalignedCopy64:

byte[] uncompressed = new byte[100];
byte[] compressed = new byte[100];

Snappy.Sharp.SnappyCompressor snappyCompressor = new Snappy.Sharp.SnappyCompressor();
int countBytes = snappyCompressor.Compress( uncompressed, 0, 2, compressed, 0 );

Snappy.Sharp.SnappyDecompressor snappyDecompressor = new Snappy.Sharp.SnappyDecompressor();

// this call works
byte[] uncompressed2 = snappyDecompressor.Decompress( compressed, 0, countBytes );

// this call results in an Exception
snappyDecompressor.Decompress( compressed, 0, countBytes, uncompressed, 0, 100 );

The Exception occured with the Native-Implementation.

@Salty-Sailor
Copy link

I have met a similar problem. It occured when run UnalignedCopy64 function on ARM cpu .See this:How does the ARM Compiler support unaligned accesses?(http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html)

My Solution is :

        public unsafe static void UnalignedCopy64(byte[] source, int sourceIndex, byte[] dest, int destIndex)
        {
            Debug.Assert(sourceIndex > -1);
            Debug.Assert(destIndex > -1);
            Debug.Assert(sourceIndex + 7 < source.Length);
            Debug.Assert(destIndex + 7 < dest.Length);

#if ARM //any macro you predefine to distinguish with x86 cpu
            for (var i = 0; i < 8; i++)
            {
                dest[destIndex + i] = source[sourceIndex + i];
            }
#else
            fixed (byte* src = &source[sourceIndex], dst = &dest[destIndex])
            {
                *((long*)dst) = *((long*)src);
            }
#endif
        }

        public unsafe static uint GetFourBytes(byte[] source, int index)
        {
            Debug.Assert(index > -1);
            Debug.Assert(index + 3 < source.Length);
#if ARM
            return (uint)source[index] |
                (uint)source[index + 1] << 8 |
                (uint)source[index + 2] << 16 |
                (uint)source[index + 3] << 24;
#else
            fixed (byte* src = &source[index])
            {
                return *((uint*)src);
            }
#endif
        }

        public unsafe static ulong GetEightBytes(byte[] source, int index)
        {
            Debug.Assert(index > -1);
            Debug.Assert(index + 7 < source.Length);
#if ARM
            return (ulong)source[index] |
                    (ulong)source[index + 1] << 8 |
                    (ulong)source[index + 2] << 16 |
                    (ulong)source[index + 3] << 24 |
                    (ulong)source[index + 4] << 32 |
                    (ulong)source[index + 5] << 40 |
                    (ulong)source[index + 6] << 48 |
                    (ulong)source[index + 7] << 56;
#else
            
            fixed (byte* src = &source[index])
            {
                return *((ulong*)src);
            }
#endif

@mic89
Copy link

mic89 commented Sep 21, 2018

the same issule that I encountered in Unity3D with Android platform

I have met a similar problem. It occured when run UnalignedCopy64 function on ARM cpu .See this:How does the ARM Compiler support unaligned accesses?(http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html)

My Solution is :

        public unsafe static void UnalignedCopy64(byte[] source, int sourceIndex, byte[] dest, int destIndex)
        {
            Debug.Assert(sourceIndex > -1);
            Debug.Assert(destIndex > -1);
            Debug.Assert(sourceIndex + 7 < source.Length);
            Debug.Assert(destIndex + 7 < dest.Length);

#if ARM //any macro you predefine to distinguish with x86 cpu
            for (var i = 0; i < 8; i++)
            {
                dest[destIndex + i] = source[sourceIndex + i];
            }
#else
            fixed (byte* src = &source[sourceIndex], dst = &dest[destIndex])
            {
                *((long*)dst) = *((long*)src);
            }
#endif
        }

        public unsafe static uint GetFourBytes(byte[] source, int index)
        {
            Debug.Assert(index > -1);
            Debug.Assert(index + 3 < source.Length);
#if ARM
            return (uint)source[index] |
                (uint)source[index + 1] << 8 |
                (uint)source[index + 2] << 16 |
                (uint)source[index + 3] << 24;
#else
            fixed (byte* src = &source[index])
            {
                return *((uint*)src);
            }
#endif
        }

        public unsafe static ulong GetEightBytes(byte[] source, int index)
        {
            Debug.Assert(index > -1);
            Debug.Assert(index + 7 < source.Length);
#if ARM
            return (ulong)source[index] |
                    (ulong)source[index + 1] << 8 |
                    (ulong)source[index + 2] << 16 |
                    (ulong)source[index + 3] << 24 |
                    (ulong)source[index + 4] << 32 |
                    (ulong)source[index + 5] << 40 |
                    (ulong)source[index + 6] << 48 |
                    (ulong)source[index + 7] << 56;
#else
            
            fixed (byte* src = &source[index])
            {
                return *((ulong*)src);
            }
#endif

Useful, THANKYOU

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants