# Floating-point to Fixed-point in (Sharc) DSP

Started by June 3, 2010
```Hello,

I am sure this is a pretty simple question, but I am having some ambiguity.
I need to confirm what is happening exactly.  Any information is really
appreciated.

So this is a Sharc DSP I am working with. Question is pretty simple.

Rn = FIX Fx;

FIX description from the manual is as follows:
"Converts the floating-point operand in Fx to a twos-complement 32-bit
fixed-point integer result. If the MODE1 register TRUNC bit=1, the FIX
operation truncates the mantissa towards &ndash;Infinity. If the TRUNC bit=0,
the FIX operation rounds the mantissa towards the nearest integer. The
TRUNC operation always truncates toward 0. Note that the TRUNC bit
does not influence operation of the TRUNC instruction."

Now the problem is, what is the "floating point OPERAND"?
The mantissa is always a fractional value with one digit left to the
decimal point. For example, the floating point will be something like
-2.345*(10-5) or 5.678*(10+8), and the mantissa will be 2.345 or 5.678.
Now, when it says the mantissa towards -inifinity or nearest integer, that
means the fixed-point value will be -2 or 5 (according to the above example
values)? Which definitely cannot be the case since we have 32-bits.

Cheers.
```
```On 6/3/2010 5:07 AM, m26k9 wrote:
> Hello,
>
> I am sure this is a pretty simple question, but I am having some ambiguity.
> I need to confirm what is happening exactly.  Any information is really
> appreciated.
>
> So this is a Sharc DSP I am working with. Question is pretty simple.
>
> Rn = FIX Fx;
>
> FIX description from the manual is as follows:
> "Converts the floating-point operand in Fx to a twos-complement 32-bit
> fixed-point integer result. If the MODE1 register TRUNC bit=1, the FIX
> operation truncates the mantissa towards &ndash;Infinity. If the TRUNC bit=0,
> the FIX operation rounds the mantissa towards the nearest integer. The
> TRUNC operation always truncates toward 0. Note that the TRUNC bit
> does not influence operation of the TRUNC instruction."
>
>
> Now the problem is, what is the "floating point OPERAND"?
> The mantissa is always a fractional value with one digit left to the
> decimal point. For example, the floating point will be something like
> -2.345*(10-5) or 5.678*(10+8), and the mantissa will be 2.345 or 5.678.
> Now, when it says the mantissa towards -inifinity or nearest integer, that
> means the fixed-point value will be -2 or 5 (according to the above example
> values)? Which definitely cannot be the case since we have 32-bits.
>

Your best bet is trying it and examining the result. My guesses are

"operand" is the entire number, which you can consider as a whole in any
base/ Instead of considering the individual bits, Base 10 will make
thinking easier. So 2.644e2 converts to 264.4, which has some binary
representation. (Work it out.) Then apply the truncate/round rules to
the result. The difference between truncating and rounding is either 1 or 0.

Jerry
--
Engineering is the art of making what you want from things you can get.
&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;
```
```>
>Your best bet is trying it and examining the result. My guesses are
>
>"operand" is the entire number, which you can consider as a whole in any
>base/ Instead of considering the individual bits, Base 10 will make
>thinking easier. So 2.644e2 converts to 264.4, which has some binary
>representation. (Work it out.) Then apply the truncate/round rules to
>the result. The difference between truncating and rounding is either 1 or
0.
>
>Jerry

Thank you Jerry.
Unfortunately I'm just starting out with assembly and am given the task of
finding the algorithm of someone else's code. Unfortunately I have
experience nor the time to carry this out. I need to understand how a
floating-point value will be converted to a "signed integer fixed-point"
value to go through the code and report what is happening. I guess it
depends on the DSP I am using too.

Thank you again.
```
```m26k9 <maduranga.liyanage@n_o_s_p_a_m.gmail.com> wrote:
(snip)

> Unfortunately I'm just starting out with assembly and am given the task of
> finding the algorithm of someone else's code. Unfortunately I have
> experience nor the time to carry this out. I need to understand how a
> floating-point value will be converted to a "signed integer fixed-point"
> value to go through the code and report what is happening. I guess it
> depends on the DSP I am using too.

It depends on the DSP and the needed range.  It is somewhat easier
if you don't need all the bits in the floating point value, as
would be the case with 32 bit float and 16 bit int.

For unsigned 16 bit fixed to 32 bit float, store the int into
the low half of a 32 bit word with the appropriate exponent,
then load it as a floating point value.  If there is no
'hidden one', all you need to do is normalize the result,
which adding zero will do on many machines.  If there is
a hidden one, then subtract the value with the same exponent
as use previously with all the other bits zero, which will
then normalize the result.

For twos complement, invert the high bit first, which results
in an unsigned value 32768 higher than the original value.
Convert as above, and subtract 32768.

For 32 bit float to 16 bit int, pretty much reverse the above,
though you should check that the value is in range first.

-- glen
```
```>m26k9 <maduranga.liyanage@n_o_s_p_a_m.gmail.com> wrote:
>(snip)
>
>> Unfortunately I'm just starting out with assembly and am given the task
of
>> finding the algorithm of someone else's code. Unfortunately I have
>> experience nor the time to carry this out. I need to understand how a
>> floating-point value will be converted to a "signed integer
fixed-point"
>> value to go through the code and report what is happening. I guess it
>> depends on the DSP I am using too.
>
>It depends on the DSP and the needed range.  It is somewhat easier
>if you don't need all the bits in the floating point value, as
>would be the case with 32 bit float and 16 bit int.
>
>For unsigned 16 bit fixed to 32 bit float, store the int into
>the low half of a 32 bit word with the appropriate exponent,
>then load it as a floating point value.  If there is no
>'hidden one', all you need to do is normalize the result,
>which adding zero will do on many machines.  If there is
>a hidden one, then subtract the value with the same exponent
>as use previously with all the other bits zero, which will
>then normalize the result.
>
>For twos complement, invert the high bit first, which results
>in an unsigned value 32768 higher than the original value.
>Convert as above, and subtract 32768.
>
>For 32 bit float to 16 bit int, pretty much reverse the above,
>though you should check that the value is in range first.
>
>-- glen
>

Thank you very much Glen.
I think it is what I needed to know.  I will go through your explanation
again and see thourouhly.
And also thank you for that part on inverting the MSB. There was a line
which did that and I wasn't sure what it was supposed to do.

Thank you heaps.
```
```On 06/03/2010 02:07 AM, m26k9 wrote:
> Hello,
>
> I am sure this is a pretty simple question, but I am having some ambiguity.
> I need to confirm what is happening exactly.  Any information is really
> appreciated.
>
> So this is a Sharc DSP I am working with. Question is pretty simple.
>
> Rn = FIX Fx;
>
> FIX description from the manual is as follows:
> "Converts the floating-point operand in Fx to a twos-complement 32-bit
> fixed-point integer result. If the MODE1 register TRUNC bit=1, the FIX
> operation truncates the mantissa towards &ndash;Infinity. If the TRUNC bit=0,
> the FIX operation rounds the mantissa towards the nearest integer. The
> TRUNC operation always truncates toward 0. Note that the TRUNC bit
> does not influence operation of the TRUNC instruction."
>
>
> Now the problem is, what is the "floating point OPERAND"?
> The mantissa is always a fractional value with one digit left to the
> decimal point. For example, the floating point will be something like
> -2.345*(10-5) or 5.678*(10+8), and the mantissa will be 2.345 or 5.678.

Not quite.  It's binary floating point, so the exponent is in powers of
2.  If it's IEEE-like, the mantissa will be unsigned with a sign bit
somewhere in the word.  I _think_ IEEE exponents are signed 2-s
compliment, but they may be sign-magnitude as well.

Learning a bit about the floating point representation of the machine
would be a good thing.

> Now, when it says the mantissa towards -inifinity or nearest integer, that
> means the fixed-point value will be -2 or 5 (according to the above example
> values)? Which definitely cannot be the case since we have 32-bits.
>
>
> Cheers.

Do you have a way to check this on a real machine, preferably with a
debugger?  I can't imagine that's what they mean to do.  It would make
far more sense that the conversion is to first calculate where the radix
point* is in the mantissa, make it into an integer + fractional bits,
then truncate or round.

* It's base two, so it's not a decimal point.

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
```
```On 6/6/10 2:05 PM, Tim Wescott wrote:
> On 06/03/2010 02:07 AM, m26k9 wrote:
>> Hello,
>>
>> I am sure this is a pretty simple question, but I am having some
>> ambiguity.
>> I need to confirm what is happening exactly.  Any information is really
>> appreciated.
>>
>> So this is a Sharc DSP I am working with. Question is pretty simple.
>>
>> Rn = FIX Fx;
>>
>> FIX description from the manual is as follows:
>> "Converts the floating-point operand in Fx to a twos-complement 32-bit
>> fixed-point integer result. If the MODE1 register TRUNC bit=1, the FIX
>> operation truncates the mantissa towards &ndash;Infinity. If the TRUNC bit=0,
>> the FIX operation rounds the mantissa towards the nearest integer. The
>> TRUNC operation always truncates toward 0. Note that the TRUNC bit
>> does not influence operation of the TRUNC instruction."
>>
>>
>> Now the problem is, what is the "floating point OPERAND"?
>> The mantissa is always a fractional value with one digit left to the
>> decimal point. For example, the floating point will be something like
>> -2.345*(10-5) or 5.678*(10+8), and the mantissa will be 2.345 or 5.678.
>
> Not quite.  It's binary floating point, so the exponent is in powers of
> 2.  If it's IEEE-like, the mantissa will be unsigned with a sign bit
> somewhere in the word.  I _think_ IEEE exponents are signed 2-s
> compliment, but they may be sign-magnitude as well.

IEEE exponents are unsigned, but they have a bias so the an exponent
value of the bias represents a true exponent of 0.  The mantissa might
also have a hidden 1 bit, as someone else mentioned.

>
> Learning a bit about the floating point representation of the machine
> would be a good thing.
>
>> Now, when it says the mantissa towards -inifinity or nearest integer,
>> that
>> means the fixed-point value will be -2 or 5 (according to the above
>> example
>> values)? Which definitely cannot be the case since we have 32-bits.
>>
>>
>> Cheers.
>
> Do you have a way to check this on a real machine, preferably with a
> debugger?  I can't imagine that's what they mean to do.  It would make

I agree with that.  If they really meant the mantissa and it's binary
floating point, the only possible values of that instruction are 0, +/-1
and +/- 2 (I think).   Hardly useful.  The conversion surely takes into
account the exponent and probably does what you would think it would do,
so -2.345e-5 becomes 0 and 5.678e8 is 567800000.  (Assuming base 10 for
simplicity).

Ray
```
```On 3 jun, 11:07, "m26k9" <maduranga.liyanage@n_o_s_p_a_m.gmail.com>
wrote:
> Hello,
>
> I am sure this is a pretty simple question, but I am having some ambiguity.
> I need to confirm what is happening exactly. &#2013266080;Any information is really
> appreciated.
>
> So this is a Sharc DSP I am working with. Question is pretty simple.
>
> Rn = FIX Fx;
>
> FIX description from the manual is as follows:
> "Converts the floating-point operand in Fx to a twos-complement 32-bit
> fixed-point integer result. If the MODE1 register TRUNC bit=1, the FIX
> operation truncates the mantissa towards &#2013266070;Infinity. If the TRUNC bit=0,
> the FIX operation rounds the mantissa towards the nearest integer. The
> TRUNC operation always truncates toward 0. Note that the TRUNC bit
> does not influence operation of the TRUNC instruction."
>
> Now the problem is, what is the "floating point OPERAND"?
> The mantissa is always a fractional value with one digit left to the
> decimal point. For example, the floating point will be something like
> -2.345*(10-5) or 5.678*(10+8), and the mantissa will be 2.345 or 5.678.
> Now, when it says the mantissa towards -inifinity or nearest integer, that
> means the fixed-point value will be -2 or 5 (according to the above example
> values)? Which definitely cannot be the case since we have 32-bits.
>
>
> Cheers.

Hi,

The operand is Fx. For instance:

R1 = FIX F0;

Converts the floating point value stored in F0 to a fixed point value
stored in R1. Say F0 stores the value 123.456; aplying the above
instruction will store the value 123 in R1.

Could not be easier, thanks to SHARCs!

JaaC
```
```In article <g7CdnVzpH6pQ7ZrRnZ2dnUVZ_qednZ2d@giganews.com>,
>Hello,
>
>I am sure this is a pretty simple question, but I am having some ambiguity.
>I need to confirm what is happening exactly.  Any information is really
>appreciated.
>
>So this is a Sharc DSP I am working with. Question is pretty simple.
>
>Rn = FIX Fx;
>
>FIX description from the manual is as follows:
>"Converts the floating-point operand in Fx to a twos-complement 32-bit
>fixed-point integer result. If the MODE1 register TRUNC bit=1, the FIX
>operation truncates the mantissa towards -Infinity. If the TRUNC bit=0,
>the FIX operation rounds the mantissa towards the nearest integer. The
>TRUNC operation always truncates toward 0. Note that the TRUNC bit
>does not influence operation of the TRUNC instruction."
>
>
>Now the problem is, what is the "floating point OPERAND"?
>The mantissa is always a fractional value with one digit left to the
>decimal point. For example, the floating point will be something like
>-2.345*(10-5) or 5.678*(10+8), and the mantissa will be 2.345 or 5.678.

Start writing the fp numbers as fixed point.
Write those numbers as
- 0.00002345 and 57800000000.0
Truncating towards 0 means leaving off all behind the decimal point.
Towards -infinity means leaving off a .000 or else the highest number
that is still smaller, in those cases:

-1 57800000000

>Now, when it says the mantissa towards -inifinity or nearest integer, that
>means the fixed-point value will be -2 or 5 (according to the above example
>values)? Which definitely cannot be the case since we have 32-bits.

The second number is too large for 32 bits. This probably results in
the largest 32 bit number.
Write a small test program if the manual leaves that unclear.

>
>
>Cheers.

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

```
```Dear Tim, Ray, Jaac and Albert.

Thank you so much.
I think I got it now.
The SHARC uses IEEE single-precision format.
So what I need to do is to write is as a decimal without an exponent, and
round it to the possible integer?

For example, the value is 2.345e-5, I write it as 0.00002345, which
converted to fixed-point will give zero.
If I had 2.345e+5, then I write 234500, so my fixed-point value is 234500.

Thank you very much for the clarification.

Cheers.
```