Forums

Misaligned Memory Exception in ADSP-TS201

Started by deepak February 13, 2006
Hi

I am trying to port some code from C++ to DSP.  I have a packer
unpacker class which reads and initializes data from a buffer.

I am using 64 bit precision for doubles and 32 bit addressing scheme.

when i try to load a 64 bit double from a odd address in the buffer it
gives me this misaligned memory exception.

has someone encountered this problem before ....

i would appreciate any pointers....

( i tried the __builtin_aligned( ptr+3, 4) but it did not help

thanx
deepak

deepak wrote:
> Hi > > I am trying to port some code from C++ to DSP. I have a packer > unpacker class which reads and initializes data from a buffer. > > I am using 64 bit precision for doubles and 32 bit addressing scheme. > > when i try to load a 64 bit double from a odd address in the buffer it > gives me this misaligned memory exception. > > has someone encountered this problem before .... > > i would appreciate any pointers.... > > ( i tried the __builtin_aligned( ptr+3, 4) but it did not help
Patient: It hurts when I turn my head like this... Doctor: So don't do that. Align your data. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
"deepak" <deepak.saxena@gmail.com> wrote in message
news:1139863014.508262.20850@g14g2000cwa.googlegroups.com...
> Hi > > I am trying to port some code from C++ to DSP. I have a packer > unpacker class which reads and initializes data from a buffer. > > I am using 64 bit precision for doubles and 32 bit addressing scheme. > > when i try to load a 64 bit double from a odd address in the buffer it > gives me this misaligned memory exception. > > has someone encountered this problem before .... > > i would appreciate any pointers.... > > ( i tried the __builtin_aligned( ptr+3, 4) but it did not help > > thanx > deepak
I don't have experience with the Tigers but I do with the regular Sharcs. When you use 64 bit doubles (needs 2 memory locations to store 1 data point), my guess is that the data is even aligned. So when you try to read a 64 bit number from an odd location, you are getting flagged. This is probably what happens in the default setup case - I'm not sure if you are doing something special (and didn't tell us).
> I am trying to port some code from C++ to DSP. I have a packer
BTW - I had no idea, DSP is now a language. Are you porting C++ code from a PC to a DSP? Cheers Bhaskar
hi bhaskar

i am not doing anything special. i have buffer which contains 3  32 bit
values followed by a 64 bit double.

the index in the buffer is 0x03 after reading the 32 bit values.  and
thats causing the problem when i try to read a 64 bit value as I try to
cast the address to a double....

> > I am trying to port some code from C++ to DSP. > BTW - I had no idea, DSP is now a language. Are you porting C++ code from a > PC to a DSP?
thanx for pointing that out..... as u must have guessed by now.... i am less than a month old in DSPs thanx deepak
"deepak" <deepak.saxena@gmail.com> wrote in message
news:1139867969.886871.318110@f14g2000cwb.googlegroups.com...
> hi bhaskar > > i am not doing anything special. i have buffer which contains 3 32 bit > values followed by a 64 bit double. > > the index in the buffer is 0x03 after reading the 32 bit values. and > thats causing the problem when i try to read a 64 bit value as I try to > cast the address to a double....
I suspect that if you add a 4th 32 bit value in your buffer before your 64 bit value, you won't get the error anymore...my guess is that it wants to see the 64 bit value even aligned in memory. I'm sure there are several tricks to force the linker to place it such that you don't have these issues - but that's probably beyond where you are right now. Cheers Bhaskar
> > > > I am trying to port some code from C++ to DSP. > > BTW - I had no idea, DSP is now a language. Are you porting C++ code
from a
> > PC to a DSP? > > thanx for pointing that out..... as u must have guessed by now.... i am > less than a month old in DSPs > > thanx > deepak >
On 13 Feb 2006 13:59:29 -0800, "deepak" <deepak.saxena@gmail.com>
wrote in comp.dsp:

> hi bhaskar > > i am not doing anything special. i have buffer which contains 3 32 bit > values followed by a 64 bit double. > > the index in the buffer is 0x03 after reading the 32 bit values. and > thats causing the problem when i try to read a 64 bit value as I try to > cast the address to a double.... > > > > I am trying to port some code from C++ to DSP. > > BTW - I had no idea, DSP is now a language. Are you porting C++ code from a > > PC to a DSP? > > thanx for pointing that out..... as u must have guessed by now.... i am > less than a month old in DSPs > > thanx > deepak
Yes, you have a basic C language problem with packed data. If you violate the alignment requirements of the hardware, C says the behavior is undefined. On some processors you merely get slower access. Some access the wrong data. And some generate a fault. So what you need to do is move the unaligned data into a value of the proper type with the proper alignment. Here are two possible methods: double x; long my_buff[5]; /* double is at 32 bit words my_buff[3] & my_buff[4]; You can do either: memcpy(&x, my_buff + 3, sizeof x); /* must #include <stdlib.h> */ ...or this, which avoids a library call: double x; union { double d; long l[2]; } targ; long my_buff[5]; targ.l[0] = my_buff[3]; targ.l[1] = my_buff[4]; x = targ.d; -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
When you declare your array you need to use for instance:

#pragma aligned 4

have you tried that?

When you say porting from C++ to DSP, do you mean porting to assembly?
or just porting it to use the TS201 C/C++ intrinsics?

DB




deepak wrote:
> Hi > > I am trying to port some code from C++ to DSP. I have a packer > unpacker class which reads and initializes data from a buffer. > > I am using 64 bit precision for doubles and 32 bit addressing scheme. > > when i try to load a 64 bit double from a odd address in the buffer it > gives me this misaligned memory exception. > > has someone encountered this problem before .... > > i would appreciate any pointers.... > > ( i tried the __builtin_aligned( ptr+3, 4) but it did not help > > thanx > deepak
thanx everyone for responding...

the solution to the problem is using memcpy. ( thanx jack klien).

-ds