DSPRelated.com
Forums

AW: sign extension in C

Started by Burgwedel, Friedrich June 25, 2003
I don't know how your fifo is connected -- but if it is connected in
parallel, you just need to connect the fifo output lines (FIFO[11..0]) to
the highest portion of the data bus -- that is D[31..20] if you use a Sharc
-- and tie the lower end of the data bus to GND via a read-strobed bus
driver. This will give you the codec value shifted left 20 bits which is no
problem if you convert to floating point anyway. If you want to do the job
with fixed point, you can shift your fifo connection to lower bits as you
like; you just have to connect all free higher bits of the data bus to the
MSB of your fifo output. -- Check your fifo output driver capability...

So long
Friedrich

-----Ursprgliche Nachricht-----
Von: Liyju Janardhan [mailto:]
Gesendet: Mittwoch, 25. Juni 2003 07:37
An:
Betreff: [adsp] sign extension in C Sign extenstion is a real problem for me.
In my software I have to sign extend the adc data
which is stored in a 12 bit hardware FIFO.
As I am using a FOR loop for that, it takes 0.8msec.
Its a very long period to me.

How could I reduce this time. Is there any hardware
solution for that.
Using assembly language is a solution, but as the
whole application is written in C, I feel it
difficult to transfer buffer from C to assembly for
sign ext and then tranfer it back to C.
Is there any easy method.

Regards

Liyju
____________________________________________
Things are difficult before it becomes easy.

_____________________________________
Note: If you do a simple "reply" with your email client, only the author of
this message will receive your answer. You need to do a "reply all" if you
want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join: Send an email to

To Post: Send an email to

To Leave: Send an email to

Archives: http://groups.yahoo.com/group/adsp

Other Groups: http://www.dsprelated.com/groups.php3 ">http://docs.yahoo.com/info/terms/



Since variable 'val' is defined to be signed, the compiler will use the
arithmetic shift operation.
I think this will answer your question...

So long
Friedrich

-----Ursprgliche Nachricht-----
Von: andor_bariska [mailto:]
Gesendet: Mittwoch, 25. Juni 2003 18:54
An:
Betreff: [adsp] Re: sign extension in C Jens Michaelsen wrote:
...
> Here a short example how to sign extend
> a 12 bit value in a 16bit register or variable.
>
> signed short val = MyAdcValue;
> val<<=4; // shift so that sign is in sign bit
> val>>=4; // shift in old place, there you are

How does the C/C++ compiler translate >> and << ? There are two
assembly shift computes, ashift and lshift (arithemtic and logic).
Perhaps there is a way to specify to the compiler which shift is used
(because both are useful).

If the logic shift is used, the above sequence won't sign extend, in
fact, in won't do anything.

Regards,
Andor
_____________________________________
Note: If you do a simple "reply" with your email client, only the author of
this message will receive your answer. You need to do a "reply all" if you
want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join: Send an email to

To Post: Send an email to

To Leave: Send an email to

Archives: http://groups.yahoo.com/group/adsp

Other Groups: http://www.dsprelated.com/groups.php3 ">http://docs.yahoo.com/info/terms/


Thanks all for your valuable suggestions

Friedrich, your hardware solution seems good, I
will consult with my board designer.

I am using asdp21060 processor.
FIFO is 1024 deep, 12 bit 2'complement,It is mapped to

a single location in external memory.
So in my software I reading that particular location
1024 times.

Ealier I was using fifo which stored data in
offset binary format. So after transfering to the
internal memory of the processor, I just have to do
the following:
adc_data[i] = adc_data[i]-2047;
which I used to do during filtering. No additional
loop for that.

Jens, your ways of sign extension is also good.
I could save some time.
Thanks a lot.

Regards

Liyju

--- "Burgwedel, Friedrich" <>
wrote:
> I don't know how your fifo is connected -- but if it
> is connected in
> parallel, you just need to connect the fifo output
> lines (FIFO[11..0]) to
> the highest portion of the data bus -- that is
> D[31..20] if you use a Sharc
> -- and tie the lower end of the data bus to GND via
> a read-strobed bus
> driver. This will give you the codec value shifted
> left 20 bits which is no
> problem if you convert to floating point anyway. If
> you want to do the job
> with fixed point, you can shift your fifo connection
> to lower bits as you
> like; you just have to connect all free higher bits
> of the data bus to the
> MSB of your fifo output. -- Check your fifo output
> driver capability...
>
> So long
> Friedrich
>
> -----Ursprgliche Nachricht-----
> Von: Liyju Janardhan [mailto:]
> Gesendet: Mittwoch, 25. Juni 2003 07:37
> An:
> Betreff: [adsp] sign extension in C > Sign extenstion is a real problem for me.
> In my software I have to sign extend the adc data
> which is stored in a 12 bit hardware FIFO.
> As I am using a FOR loop for that, it takes 0.8msec.
> Its a very long period to me.
>
> How could I reduce this time. Is there any hardware
> solution for that.
> Using assembly language is a solution, but as the
> whole application is written in C, I feel it
> difficult to transfer buffer from C to assembly for
> sign ext and then tranfer it back to C.
> Is there any easy method.
>
> Regards
>
> Liyju
> ____________________________________________
> Things are difficult before it becomes easy. >
>
> _____________________________________
> Note: If you do a simple "reply" with your email
> client, only the author of
> this message will receive your answer. You need to
> do a "reply all" if you
> want your answer to be distributed to the entire
> group.
>
> _____________________________________
> About this discussion group:
>
> To Join: Send an email to > To Post: Send an email to
>
> To Leave: Send an email to > Archives: http://groups.yahoo.com/group/adsp
>
> Other Groups: http://www.dsprelated.com/groups.php3 > ">http://docs.yahoo.com/info/terms/ >
> _____________________________________
> Note: If you do a simple "reply" with your email
> client, only the author of this message will receive
> your answer. You need to do a "reply all" if you
> want your answer to be distributed to the entire
> group.
>
> _____________________________________
> About this discussion group:
>
> To Join: Send an email to > To Post: Send an email to
>
> To Leave: Send an email to > Archives: http://groups.yahoo.com/group/adsp
>
> Other Groups: http://www.dsprelated.com/groups.php3 > ">http://docs.yahoo.com/info/terms/


__________________________________


Jens that was a typo, I was actually using val- 48.

Anyways, I didn't understand the signed-unsigned
funda.

If I had 4 bit FIFO, -4 will be stored as 1100.
So when I read the fifo I will be reading it as 12.

val = fifodata; //here val is 12
val<<(;
val>>(; //here val becomes -4

I guess the 32 bit value in hex would be FFFFFFFC.
This is only if val is defined signed.
But if val was defined unsigned, the 32 bit value
would be 0x000000C. > The only difference to signed is
> the toggled MSB or sign bit.

So this is not the only difference.

>
> Take a look at the binary (Hex) 12bit values
> signed: low0 middle0 highf
> unsigned: low0 middle0 highf
> As you see only the msb changes when
> switching from signed to unsigned value range.

I didn't understand the above thing.
Thanks anyway.

Regards

Liyju
>
> ----- Original Message -----
> From: "Liyju Janardhan" <>
> To: <>
> Sent: Thursday, June 26, 2003 4:10 PM
> Subject: Re: AW: [adsp] sign extension in C > > Thanks all for your valuable suggestions
> >
> > Friedrich, your hardware solution seems good, I
> > will consult with my board designer.
> >
> > I am using asdp21060 processor.
> > FIFO is 1024 deep, 12 bit 2'complement,It is
> mapped to
> >
> > a single location in external memory.
> > So in my software I reading that particular
> location
> > 1024 times.
> >
> > Ealier I was using fifo which stored data in
> > offset binary format. So after transfering to the
> > internal memory of the processor, I just have to
> do
> > the following:
> > adc_data[i] = adc_data[i]-2047;
> > which I used to do during filtering. No additional
> > loop for that.
> >
> > Jens, your ways of sign extension is also good.
> > I could save some time.
> > Thanks a lot.
> >
> > Regards
> >
> > Liyju
> >
> > --- "Burgwedel, Friedrich"
> <>
> > wrote:
> > > I don't know how your fifo is connected -- but
> if it
> > > is connected in
> > > parallel, you just need to connect the fifo
> output
> > > lines (FIFO[11..0]) to
> > > the highest portion of the data bus -- that is
> > > D[31..20] if you use a Sharc
> > > -- and tie the lower end of the data bus to GND
> via
> > > a read-strobed bus
> > > driver. This will give you the codec value
> shifted
> > > left 20 bits which is no
> > > problem if you convert to floating point anyway.
> If
> > > you want to do the job
> > > with fixed point, you can shift your fifo
> connection
> > > to lower bits as you
> > > like; you just have to connect all free higher
> bits
> > > of the data bus to the
> > > MSB of your fifo output. -- Check your fifo
> output
> > > driver capability...
> > >
> > > So long
> > > Friedrich
> > >
> > > -----Ursprgliche Nachricht-----
> > > Von: Liyju Janardhan [mailto:]
> > > Gesendet: Mittwoch, 25. Juni 2003 07:37
> > > An:
> > > Betreff: [adsp] sign extension in C
> > >
> > >
> > > Sign extenstion is a real problem for me.
> > > In my software I have to sign extend the adc
> data
> > > which is stored in a 12 bit hardware FIFO.
> > > As I am using a FOR loop for that, it takes
> 0.8msec.
> > > Its a very long period to me.
> > >
> > > How could I reduce this time. Is there any
> hardware
> > > solution for that.
> > > Using assembly language is a solution, but as
> the
> > > whole application is written in C, I feel it
> > > difficult to transfer buffer from C to assembly
> for
> > > sign ext and then tranfer it back to C.
> > > Is there any easy method.
> > >
> > > Regards
> > >
> > > Liyju
> > > ____________________________________________
> > > Things are difficult before it becomes easy.
> > >
> > >
> > >
> > >
> > > _____________________________________
> > > Note: If you do a simple "reply" with your email
> > > client, only the author of
> > > this message will receive your answer. You need
> to
> > > do a "reply all" if you
> > > want your answer to be distributed to the entire
> > > group.
> > >
> > > _____________________________________
> > > About this discussion group:
> > >
> > > To Join: Send an email to
> > >
> > >
> > > To Post: Send an email to
> > >
> > > To Leave: Send an email to
> > >
> > >
> > > Archives: http://groups.yahoo.com/group/adsp
> > >
> > > Other Groups:
> http://www.dsprelated.com/groups.php3
> > >
> > >
> > > ">http://docs.yahoo.com/info/terms/
> > >
> > >
> > >
> > > _____________________________________
> > > Note: If you do a simple "reply" with your email
> > > client, only the author of this message will
> receive
> > > your answer. You need to do a "reply all" if
> you
> > > want your answer to be distributed to the entire
> > > group.
> > >
> > > _____________________________________
> > > About this discussion group:
> > >
> > > To Join: Send an email to
> > >
> > >
> > > To Post: Send an email to
> > >
> > > To Leave: Send an email to
> > >
> > >
> > > Archives: http://groups.yahoo.com/group/adsp
> > >
> > > Other Groups:
> http://www.dsprelated.com/groups.php3
> > >
> > >
> > > ">http://docs.yahoo.com/info/terms/
> > >
> > >
> >
> >
> > __________________________________
> >
> >
> > _____________________________________
> > Note: If you do a simple "reply" with your email
> client, only the author
> of this message will receive your answer. You need
> to do a "reply all" if
> you want your answer to be distributed to the entire
> group.
> >
> > _____________________________________
> > About this discussion group:
> >
> > To Join: Send an email to
>
> >
> > To Post: Send an email to
> >
> > To Leave: Send an email to
>
> >
> > Archives: http://groups.yahoo.com/group/adsp
> >
> > Other Groups:
> http://www.dsprelated.com/groups.php3
> >
> >
> > ">http://docs.yahoo.com/info/terms/
> >
> >
>


__________________________________



ADCs and Binary coding is for beginner (not to blaim)
Starting in real world you have for
example 0 to +10 Volts of input signal.
Convert it 8 bit wise $00 to $FF and use it unsigned.
0Volt=$00 5Volt=$80 10Volt=$FF

Use the same ADC and scale it input -5 to +5 Volts
Again it will convert from $00 to $FF unsigned.
-5Volt=$00 0Volt=$80 +5Volt=$FF
when you now toggle the MSB you get signed values
-5Volt=$80 0Volt=$00 +5Volt=$7F

This is only true for MSBs!!!
If you have a 4 bit value and place it in a 32bit register
you have your MSB of by 28 bit so it can not work. > If I had 4 bit FIFO, -4 will be stored as 1100.
> So when I read the fifo I will be reading it as 12.
>
> val = fifodata; //here val is 12
> val<<(;
> val>>(; //here val becomes -4
>
> I guess the 32 bit value in hex would be FFFFFFFC.
> This is only if val is defined signed.
> But if val was defined unsigned, the 32 bit value
> would be 0x000000C.
>

YES dam right, signed values only work if used in signed valiables.
But where is the problem, use signed variables.
I'm a bit confused about your complain, to me it says:
"If I use the wrong kind of variable your example won't work."
It's hard to help someone with this point of view.

>
> > The only difference to signed is
> > the toggled MSB or sign bit.
>
> So this is not the only difference.
>
> >
> > Take a look at the binary (Hex) 12bit values
> > signed: low0 middle0 highf
> > unsigned: low0 middle0 highf
> > As you see only the msb changes when
> > switching from signed to unsigned value range.
>
> I didn't understand the above thing.
> Thanks anyway.
>
> Regards
>
> Liyju





hi,

Thanks Jens for your help.
I have gained 0.2ms by using bit-wise shifting method
of sign extension.

Earlier I was using the following:
if(val[i]<2047)
val[i]=val[i]-4096;

Regards
Liyju --- Jens Michaelsen <> wrote:
>
> ADCs and Binary coding is for beginner (not to
> blaim)
> Starting in real world you have for
> example 0 to +10 Volts of input signal.
> Convert it 8 bit wise $00 to $FF and use it
> unsigned.
> 0Volt=$00 5Volt=$80 10Volt=$FF
>
> Use the same ADC and scale it input -5 to +5 Volts
> Again it will convert from $00 to $FF unsigned.
> -5Volt=$00 0Volt=$80 +5Volt=$FF
> when you now toggle the MSB you get signed values
> -5Volt=$80 0Volt=$00 +5Volt=$7F
>
> This is only true for MSBs!!!
> If you have a 4 bit value and place it in a 32bit
> register
> you have your MSB of by 28 bit so it can not work. > > If I had 4 bit FIFO, -4 will be stored as 1100.
> > So when I read the fifo I will be reading it as
> 12.
> >
> > val = fifodata; //here val is 12
> > val<<(;
> > val>>(; //here val becomes -4
> >
> > I guess the 32 bit value in hex would be FFFFFFFC.
> > This is only if val is defined signed.
> > But if val was defined unsigned, the 32 bit value
> > would be 0x000000C.
> >
>
> YES dam right, signed values only work if used in
> signed valiables.
> But where is the problem, use signed variables.
> I'm a bit confused about your complain, to me it
> says:
> "If I use the wrong kind of variable your example
> won't work."
> It's hard to help someone with this point of view.
>
> >
> > > The only difference to signed is
> > > the toggled MSB or sign bit.
> >
> > So this is not the only difference.
> >
> > >
> > > Take a look at the binary (Hex) 12bit values
> > > signed: low0 middle0 highf
> > > unsigned: low0 middle0 highf
> > > As you see only the msb changes when
> > > switching from signed to unsigned value range.
> >
> > I didn't understand the above thing.
> > Thanks anyway.
> >
> > Regards
> >
> > Liyju
> >
> >


__________________________________