Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #23524 > unrolled thread

Integer/Fixedpoint to 32 bit float

Started byVincent vB <embedded@spam.com>
First post2016-04-06 15:17 +0200
Last post2016-04-08 03:39 +0100
Articles 20 on this page of 34 — 14 participants

Back to article view | Back to comp.arch.embedded


Contents

  Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-06 15:17 +0200
    Re: Integer/Fixedpoint to 32 bit float Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-06 17:16 +0300
      Re: Integer/Fixedpoint to 32 bit float Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-06 17:21 +0300
      Re: Integer/Fixedpoint to 32 bit float Grant Edwards <invalid@invalid.invalid> - 2016-04-06 14:40 +0000
        Re: Integer/Fixedpoint to 32 bit float Dave Nadler <drn@nadler.com> - 2016-04-06 08:21 -0700
        Re: Integer/Fixedpoint to 32 bit float Robert Wessel <robertwessel2@yahoo.com> - 2016-04-06 12:33 -0500
          Re: Integer/Fixedpoint to 32 bit float Robert Wessel <robertwessel2@yahoo.com> - 2016-04-06 12:36 -0500
            Re: Integer/Fixedpoint to 32 bit float Grant Edwards <invalid@invalid.invalid> - 2016-04-06 17:56 +0000
              Re: Integer/Fixedpoint to 32 bit float Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-06 22:45 +0300
          Re: Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-07 09:06 +0200
          Re: Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-07 11:26 +0200
            Re: Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-07 11:29 +0200
    Re: Integer/Fixedpoint to 32 bit float Wouter van Ooijen <wouter@voti.nl> - 2016-04-06 18:00 +0200
      Re: Integer/Fixedpoint to 32 bit float Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-06 21:35 +0300
        Re: Integer/Fixedpoint to 32 bit float Grant Edwards <invalid@invalid.invalid> - 2016-04-06 18:47 +0000
          Re: Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-07 08:52 +0200
    Re: Integer/Fixedpoint to 32 bit float Tim Wescott <seemywebsite@myfooter.really> - 2016-04-06 16:05 -0500
      Re: Integer/Fixedpoint to 32 bit float Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-07 00:23 +0300
        Re: Integer/Fixedpoint to 32 bit float Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-06 21:30 +0000
          Re: Integer/Fixedpoint to 32 bit float Clifford Heath <no.spam@please.net> - 2016-04-07 09:23 +1000
            Re: Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-07 09:07 +0200
              Re: Integer/Fixedpoint to 32 bit float Tim Wescott <tim@seemywebsite.com> - 2016-04-07 11:14 -0500
        Re: Integer/Fixedpoint to 32 bit float Tim Wescott <seemywebsite@myfooter.really> - 2016-04-06 18:36 -0500
    Re: Integer/Fixedpoint to 32 bit float lasselangwadtchristensen@gmail.com - 2016-04-06 17:03 -0700
      Re: Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-07 09:04 +0200
    Re: Integer/Fixedpoint to 32 bit float David Brown <david.brown@hesbynett.no> - 2016-04-07 10:21 +0200
      Re: Integer/Fixedpoint to 32 bit float Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-07 11:28 +0300
        Re: Integer/Fixedpoint to 32 bit float David Brown <david.brown@hesbynett.no> - 2016-04-07 10:41 +0200
          Re: Integer/Fixedpoint to 32 bit float Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-07 11:47 +0300
            Re: Integer/Fixedpoint to 32 bit float David Brown <david.brown@hesbynett.no> - 2016-04-07 11:40 +0200
            Re: Integer/Fixedpoint to 32 bit float Vincent vB <embedded@spam.com> - 2016-04-07 12:50 +0200
              Re: Integer/Fixedpoint to 32 bit float David Brown <david.brown@hesbynett.no> - 2016-04-07 13:52 +0200
              Re: Integer/Fixedpoint to 32 bit float Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-07 22:14 +0300
    Re: Integer/Fixedpoint to 32 bit float Nobody <nobody@nowhere.invalid> - 2016-04-08 03:39 +0100

Page 1 of 2  [1] 2  Next page →


#23524 — Integer/Fixedpoint to 32 bit float

FromVincent vB <embedded@spam.com>
Date2016-04-06 15:17 +0200
SubjectInteger/Fixedpoint to 32 bit float
Message-ID<ne3289$f5a$1@gioia.aioe.org>
Currently I'm in the process of replacing a custom compass / 
accelerometer with an ST LSM303D. The 'old' custom device produced 
single precision floats. Without parsing the values there where just 
passed inside a UDP packet.

Unfortunately the LSM303D produces 16 bit signed integers. So, the 
embedded system needs to convert these values to floats. The scaling it 
self is quite simple: Values -32768..32767 need to be scaled to [-2,2).

Now, my hardware has no floating point support. However doing the following:

   float output = ( (float)input ) / 16384f;

will require quite a bit of FP magic. I would imagine that this:

  const float scale = 1f / 16384f;
  float output = ( (float)input ) * scale;

may be faster, but still requires FP multiply support.

Is there a simple and fast way which I can use to convert these integers 
to floats without the aid of an FP library? I have not found much code 
in this respect.

Vincent

[toc] | [next] | [standalone]


#23525

FromDimiter_Popoff <dp@tgi-sci.com>
Date2016-04-06 17:16 +0300
Message-ID<ne35hv$kaq$1@dont-email.me>
In reply to#23524
On 06.4.2016 г. 16:17, Vincent vB wrote:
> Currently I'm in the process of replacing a custom compass /
> accelerometer with an ST LSM303D. The 'old' custom device produced
> single precision floats. Without parsing the values there where just
> passed inside a UDP packet.
>
> Unfortunately the LSM303D produces 16 bit signed integers. So, the
> embedded system needs to convert these values to floats. The scaling it
> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>
> Now, my hardware has no floating point support. However doing the
> following:
>
>    float output = ( (float)input ) / 16384f;
>
> will require quite a bit of FP magic. I would imagine that this:
>
>   const float scale = 1f / 16384f;
>   float output = ( (float)input ) * scale;
>
> may be faster, but still requires FP multiply support.
>
> Is there a simple and fast way which I can use to convert these integers
> to floats without the aid of an FP library? I have not found much code
> in this respect.
>
> Vincent
>

Of course there is, will cost a few operations. You can look up the
IEEE 32 bit floating point format and figure out how to do it.
Basically it will take several shifts left (you will have to count
these) of your 16 bit integer until its MSB gets positioned as bit
23, subtract the count of shifts it took to get there from 150 ($96) and 
insert this result into bits 23 to 32 (this will overwrite the
MSB which got to position 23).
For negative input numbers, negate first, convert as above and
set bit 31 of the final result to 1.

Disclaimer: this is what I just made up at the moment by memory,
I can't guarantee its correctness. It is meant to show you the
direction, not to be a 1:1 algorithm you can use. You should
understand the FP format yourself and figure out how to do it.

Dimiter

------------------------------------------------------
Dimiter Popoff, TGI             http://www.tgi-sci.com
------------------------------------------------------
http://www.flickr.com/photos/didi_tgi/


[toc] | [prev] | [next] | [standalone]


#23526

FromDimiter_Popoff <dp@tgi-sci.com>
Date2016-04-06 17:21 +0300
Message-ID<ne35qh$lcf$1@dont-email.me>
In reply to#23525
On 06.4.2016 г. 17:16, Dimiter_Popoff wrote:
> On 06.4.2016 г. 16:17, Vincent vB wrote:
>> Currently I'm in the process of replacing a custom compass /
>> accelerometer with an ST LSM303D. The 'old' custom device produced
>> single precision floats. Without parsing the values there where just
>> passed inside a UDP packet.
>>
>> Unfortunately the LSM303D produces 16 bit signed integers. So, the
>> embedded system needs to convert these values to floats. The scaling it
>> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>>
>> Now, my hardware has no floating point support. However doing the
>> following:
>>
>>    float output = ( (float)input ) / 16384f;
>>
>> will require quite a bit of FP magic. I would imagine that this:
>>
>>   const float scale = 1f / 16384f;
>>   float output = ( (float)input ) * scale;
>>
>> may be faster, but still requires FP multiply support.
>>
>> Is there a simple and fast way which I can use to convert these integers
>> to floats without the aid of an FP library? I have not found much code
>> in this respect.
>>
>> Vincent
>>
>
> Of course there is, will cost a few operations. You can look up the
> IEEE 32 bit floating point format and figure out how to do it.
> Basically it will take several shifts left (you will have to count
> these) of your 16 bit integer until its MSB gets positioned as bit
> 23, subtract the count of shifts it took to get there from 150 ($96) and
> insert this result into bits 23 to 32 (this will overwrite the
> MSB which got to position 23).
> For negative input numbers, negate first, convert as above and
> set bit 31 of the final result to 1.
>
> Disclaimer: this is what I just made up at the moment by memory,
> I can't guarantee its correctness. It is meant to show you the
> direction, not to be a 1:1 algorithm you can use. You should
> understand the FP format yourself and figure out how to do it.
>
> Dimiter
>
> ------------------------------------------------------
> Dimiter Popoff, TGI             http://www.tgi-sci.com
> ------------------------------------------------------
> http://www.flickr.com/photos/didi_tgi/
>
>
>
Oops, sorry for the brain damaged "bits 23 to 32", it should read
"bits 23 to 30" of course (my fingers here _again_ typed in 32
and I had to correct it.....).

[toc] | [prev] | [next] | [standalone]


#23527

FromGrant Edwards <invalid@invalid.invalid>
Date2016-04-06 14:40 +0000
Message-ID<ne375g$huo$2@reader1.panix.com>
In reply to#23525
On 2016-04-06, Dimiter_Popoff <dp@tgi-sci.com> wrote:
> On 06.4.2016 г. 16:17, Vincent vB wrote:
>> Currently I'm in the process of replacing a custom compass /
>> accelerometer with an ST LSM303D. The 'old' custom device produced
>> single precision floats. Without parsing the values there where just
>> passed inside a UDP packet.
>>
>> Unfortunately the LSM303D produces 16 bit signed integers. So, the
>> embedded system needs to convert these values to floats. The scaling it
>> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>>
>> Now, my hardware has no floating point support. However doing the
>> following:
>>
>>    float output = ( (float)input ) / 16384f;
>>
>> will require quite a bit of FP magic. I would imagine that this:
>>
>>   const float scale = 1f / 16384f;
>>   float output = ( (float)input ) * scale;
>>
>> may be faster, but still requires FP multiply support.
>>
>> Is there a simple and fast way which I can use to convert these integers
>> to floats without the aid of an FP library? I have not found much code
>> in this respect.
>>
>> Vincent
>
> Of course there is, will cost a few operations. You can look up the
> IEEE 32 bit floating point format and figure out how to do it.
> Basically it will take several shifts left (you will have to count
> these) of your 16 bit integer until its MSB gets positioned as bit
> 23, subtract the count of shifts it took to get there from 150 ($96) and 
> insert this result into bits 23 to 32 (this will overwrite the
> MSB which got to position 23).
> For negative input numbers, negate first, convert as above and
> set bit 31 of the final result to 1.

[Remember to check for a negative input value, convert it to positive,
and then later set the sign bit in the FP result]

That converts the value to FP.

To scale it by factor of 2/32768 you should be able to simply subtract
14 from the exponent.

Don't forget that the exponent is stored in an excess-127
representation (not 2's compliment or sign-magnitude).

-- 
Grant Edwards               grant.b.edwards        Yow! I guess it was all a
                                  at               DREAM ... or an episode of
                              gmail.com            HAWAII FIVE-O ...

[toc] | [prev] | [next] | [standalone]


#23528

FromDave Nadler <drn@nadler.com>
Date2016-04-06 08:21 -0700
Message-ID<25b38d28-2443-4158-a16f-a9652cfc558c@googlegroups.com>
In reply to#23527
... and start by writing a C test program to debug/test on your desktop,
where you have the float library available to compare against...

[toc] | [prev] | [next] | [standalone]


#23543

FromRobert Wessel <robertwessel2@yahoo.com>
Date2016-04-06 12:33 -0500
Message-ID<orgagbtnru3kiugf47goslh5mirfiqn944@4ax.com>
In reply to#23527
On Wed, 6 Apr 2016 14:40:49 +0000 (UTC), Grant Edwards
<invalid@invalid.invalid> wrote:

>On 2016-04-06, Dimiter_Popoff <dp@tgi-sci.com> wrote:
>> On 06.4.2016 ?. 16:17, Vincent vB wrote:
>>> Currently I'm in the process of replacing a custom compass /
>>> accelerometer with an ST LSM303D. The 'old' custom device produced
>>> single precision floats. Without parsing the values there where just
>>> passed inside a UDP packet.
>>>
>>> Unfortunately the LSM303D produces 16 bit signed integers. So, the
>>> embedded system needs to convert these values to floats. The scaling it
>>> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>>>
>>> Now, my hardware has no floating point support. However doing the
>>> following:
>>>
>>>    float output = ( (float)input ) / 16384f;
>>>
>>> will require quite a bit of FP magic. I would imagine that this:
>>>
>>>   const float scale = 1f / 16384f;
>>>   float output = ( (float)input ) * scale;
>>>
>>> may be faster, but still requires FP multiply support.
>>>
>>> Is there a simple and fast way which I can use to convert these integers
>>> to floats without the aid of an FP library? I have not found much code
>>> in this respect.
>>>
>>> Vincent
>>
>> Of course there is, will cost a few operations. You can look up the
>> IEEE 32 bit floating point format and figure out how to do it.
>> Basically it will take several shifts left (you will have to count
>> these) of your 16 bit integer until its MSB gets positioned as bit
>> 23, subtract the count of shifts it took to get there from 150 ($96) and 
>> insert this result into bits 23 to 32 (this will overwrite the
>> MSB which got to position 23).
>> For negative input numbers, negate first, convert as above and
>> set bit 31 of the final result to 1.
>
>[Remember to check for a negative input value, convert it to positive,
>and then later set the sign bit in the FP result]
>
>That converts the value to FP.
>
>To scale it by factor of 2/32768 you should be able to simply subtract
>14 from the exponent.
>
>Don't forget that the exponent is stored in an excess-127
>representation (not 2's compliment or sign-magnitude).


Assuming you're starting with an IEEE float containing an integer
-32768..32767, you can just subtract 14 from the exponent, *but* you
have to handle zero as a special case.

OTOH, that sounds like extra work, just convert it correctly in the
first place.  Starting with a 16 bit signed integer:

1. handle zero as a special case
2. consider handling -32768 as a special case
3. remove (and save) sign  (IOW, take the absolute value)
4. count the number of leading zeros (lz)
  4a. the result would be 1-15, 0-15 if you didn't special case -32768
5. put the result in a 32 bit unsigned integer
6. shift left (23-lz) places
7. set the 8 exponent bits (30-22) to (127+(15-lz))
  7a. note that this overlays one bit from step 6
  7b. approximately:
     ui32 &= 0x007fffff;
     ui32 |= (127-(15-lz)) << 23;
8. set the sign bit (31) as appropriate

Modulo bugs (I did the above from memory, but it should be close),
that should then have a single precision float in the 32 bit unsigned
integer, cast as needed.  If you want to convert to a double, it's
basically the same procedure, but some of the constants change.

[toc] | [prev] | [next] | [standalone]


#23544

FromRobert Wessel <robertwessel2@yahoo.com>
Date2016-04-06 12:36 -0500
Message-ID<27iagb5s0l5um0dhlrum0g46bmgfiufb53@4ax.com>
In reply to#23543
On Wed, 06 Apr 2016 12:33:35 -0500, Robert Wessel
<robertwessel2@yahoo.com> wrote:

>On Wed, 6 Apr 2016 14:40:49 +0000 (UTC), Grant Edwards
><invalid@invalid.invalid> wrote:
>
>>On 2016-04-06, Dimiter_Popoff <dp@tgi-sci.com> wrote:
>>> On 06.4.2016 ?. 16:17, Vincent vB wrote:
>>>> Currently I'm in the process of replacing a custom compass /
>>>> accelerometer with an ST LSM303D. The 'old' custom device produced
>>>> single precision floats. Without parsing the values there where just
>>>> passed inside a UDP packet.
>>>>
>>>> Unfortunately the LSM303D produces 16 bit signed integers. So, the
>>>> embedded system needs to convert these values to floats. The scaling it
>>>> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>>>>
>>>> Now, my hardware has no floating point support. However doing the
>>>> following:
>>>>
>>>>    float output = ( (float)input ) / 16384f;
>>>>
>>>> will require quite a bit of FP magic. I would imagine that this:
>>>>
>>>>   const float scale = 1f / 16384f;
>>>>   float output = ( (float)input ) * scale;
>>>>
>>>> may be faster, but still requires FP multiply support.
>>>>
>>>> Is there a simple and fast way which I can use to convert these integers
>>>> to floats without the aid of an FP library? I have not found much code
>>>> in this respect.
>>>>
>>>> Vincent
>>>
>>> Of course there is, will cost a few operations. You can look up the
>>> IEEE 32 bit floating point format and figure out how to do it.
>>> Basically it will take several shifts left (you will have to count
>>> these) of your 16 bit integer until its MSB gets positioned as bit
>>> 23, subtract the count of shifts it took to get there from 150 ($96) and 
>>> insert this result into bits 23 to 32 (this will overwrite the
>>> MSB which got to position 23).
>>> For negative input numbers, negate first, convert as above and
>>> set bit 31 of the final result to 1.
>>
>>[Remember to check for a negative input value, convert it to positive,
>>and then later set the sign bit in the FP result]
>>
>>That converts the value to FP.
>>
>>To scale it by factor of 2/32768 you should be able to simply subtract
>>14 from the exponent.
>>
>>Don't forget that the exponent is stored in an excess-127
>>representation (not 2's compliment or sign-magnitude).
>
>
>Assuming you're starting with an IEEE float containing an integer
>-32768..32767, you can just subtract 14 from the exponent, *but* you
>have to handle zero as a special case.
>
>OTOH, that sounds like extra work, just convert it correctly in the
>first place.  Starting with a 16 bit signed integer:
>
>1. handle zero as a special case
>2. consider handling -32768 as a special case
>3. remove (and save) sign  (IOW, take the absolute value)
>4. count the number of leading zeros (lz)
>  4a. the result would be 1-15, 0-15 if you didn't special case -32768
>5. put the result in a 32 bit unsigned integer
>6. shift left (23-lz) places
>7. set the 8 exponent bits (30-22) to (127+(15-lz))
>  7a. note that this overlays one bit from step 6
>  7b. approximately:
>     ui32 &= 0x007fffff;
>     ui32 |= (127-(15-lz)) << 23;
>8. set the sign bit (31) as appropriate
>
>Modulo bugs (I did the above from memory, but it should be close),
>that should then have a single precision float in the 32 bit unsigned
>integer, cast as needed.  If you want to convert to a double, it's
>basically the same procedure, but some of the constants change.


And duh, subtract the 14 in step 7 above...

[toc] | [prev] | [next] | [standalone]


#23545

FromGrant Edwards <invalid@invalid.invalid>
Date2016-04-06 17:56 +0000
Message-ID<ne3ikf$bec$1@reader1.panix.com>
In reply to#23544
On 2016-04-06, Robert Wessel <robertwessel2@yahoo.com> wrote:

> [16-bit signed to IEEE single conversion algorithm]

And then write a test program that tests and verifies the conversion
function for all 65536 possible input values.

You could try to figure out a minimal set of inputs that provides 100%
test coverage, but

  a) you'll be wrong[*]

  b) with only a 16-bit input space, it'll be faster and easier to
     just test all possible inputs

[*] At least that's been my experience with stuff like this.

-- 
Grant Edwards               grant.b.edwards        Yow! Now that I have my
                                  at               "APPLE", I comprehend COST
                              gmail.com            ACCOUNTING!!

[toc] | [prev] | [next] | [standalone]


#23548

FromDimiter_Popoff <dp@tgi-sci.com>
Date2016-04-06 22:45 +0300
Message-ID<ne3opv$2ve$1@dont-email.me>
In reply to#23545
On 06.4.2016 г. 20:56, Grant Edwards wrote:
> On 2016-04-06, Robert Wessel <robertwessel2@yahoo.com> wrote:
>
>> [16-bit signed to IEEE single conversion algorithm]
>
> And then write a test program that tests and verifies the conversion
> function for all 65536 possible input values.
>
> You could try to figure out a minimal set of inputs that provides 100%
> test coverage, but
>
>    a) you'll be wrong[*]
>
>    b) with only a 16-bit input space, it'll be faster and easier to
>       just test all possible inputs
>
> [*] At least that's been my experience with stuff like this.
>

Oh come on, the only special case with a 16 bit integer -> 32 bit
IEEE FP is the case of 0 which is obvious enough (or will make itself
obvious if one tries to shift left and count until finding the 1....).

The scenario when you can be wrong is possible if we throw in all the
NaNs and tiny and rounding etc. but these just do not apply to this
case, it is straight forward.

Dimiter

[toc] | [prev] | [next] | [standalone]


#23560

FromVincent vB <embedded@spam.com>
Date2016-04-07 09:06 +0200
Message-ID<ne50t5$1glj$4@gioia.aioe.org>
In reply to#23543
On 6-4-2016 at 19:33, Robert Wessel wrote:
>
> Assuming you're starting with an IEEE float containing an integer
> -32768..32767, you can just subtract 14 from the exponent, *but* you
> have to handle zero as a special case.
>
> OTOH, that sounds like extra work, just convert it correctly in the
> first place.  Starting with a 16 bit signed integer:
>
> 1. handle zero as a special case
> 2. consider handling -32768 as a special case
> 3. remove (and save) sign  (IOW, take the absolute value)
> 4. count the number of leading zeros (lz)
>    4a. the result would be 1-15, 0-15 if you didn't special case -32768
> 5. put the result in a 32 bit unsigned integer
> 6. shift left (23-lz) places
> 7. set the 8 exponent bits (30-22) to (127+(15-lz))
>    7a. note that this overlays one bit from step 6
>    7b. approximately:
>       ui32 &= 0x007fffff;
>       ui32 |= (127-(15-lz)) << 23;
> 8. set the sign bit (31) as appropriate
>
> Modulo bugs (I did the above from memory, but it should be close),
> that should then have a single precision float in the 32 bit unsigned
> integer, cast as needed.  If you want to convert to a double, it's
> basically the same procedure, but some of the constants change.
>
I'll start with a 16 bit signed integer. Thanks for your step-by-step 
explanation!

[toc] | [prev] | [next] | [standalone]


#23569

FromVincent vB <embedded@spam.com>
Date2016-04-07 11:26 +0200
Message-ID<ne593s$ou$1@gioia.aioe.org>
In reply to#23543
On 6-4-2016 at 19:33, Robert Wessel wrote:
> On Wed, 6 Apr 2016 14:40:49 +0000 (UTC), Grant Edwards
> <invalid@invalid.invalid> wrote:
>
> 1. handle zero as a special case
> 2. consider handling -32768 as a special case
> 3. remove (and save) sign  (IOW, take the absolute value)
> 4. count the number of leading zeros (lz)
>    4a. the result would be 1-15, 0-15 if you didn't special case -32768
> 5. put the result in a 32 bit unsigned integer
> 6. shift left (23-lz) places
> 7. set the 8 exponent bits (30-22) to (127+(15-lz))
>    7a. note that this overlays one bit from step 6
>    7b. approximately:
>       ui32 &= 0x007fffff;
>       ui32 |= (127-(15-lz)) << 23;
> 8. set the sign bit (31) as appropriate
>
> Modulo bugs (I did the above from memory, but it should be close),
> that should then have a single precision float in the 32 bit unsigned
> integer, cast as needed.  If you want to convert to a double, it's
> basically the same procedure, but some of the constants change.
>

I finally came to this code:

===
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

int clz(uint32_t val)
{
	int t = 0;
	if ((val & 0xFFFF0000) == 0) t += 16; else val >>= 16;
	if ((val & 0x0000FF00) == 0) t += 8;  else val >>= 8;
	if ((val & 0x000000F0) == 0) t += 4;  else val >>= 4;
	if ((val & 0x0000000C) == 0) t += 2;  else val >>= 2;
	if ((val & 0x00000002) == 0) t += 1;
	return t;
}

static inline float castU32ToFloat(uint32_t f) {
	void * v = &f;
	return *((float *)v);
}

float fltFromI16(int16_t val, int fBits)
{
	bool sign;
	uint32_t ival;
	int zeros;

	if (val == 0) return 0.0f;

	if (val < 0) { ival = -val; sign = true;  }
	else         { ival = val;	sign = false; }

	zeros = clz(ival) - 16;

	ival  = ival << (zeros + 8) & 0x007fffff;
	ival |= (142 - (zeros + fBits)) << 23;
	if (sign) ival |= 0x80000000;

	return castU32ToFloat(ival);
}


int main(int argc, char **argv)
{

	for (int i = -32768; i < 32767; i += 1)
	{
		float b = fltFromI16(i, 14);
		float f = ((float)i) / 16384;

		if (b != f) printf("Value : %f != %f\n", f, b);
	}

}
===
It may not be optimal, but it seems to produce the correct results.

Vincent

[toc] | [prev] | [next] | [standalone]


#23570

FromVincent vB <embedded@spam.com>
Date2016-04-07 11:29 +0200
Message-ID<ne59aa$1c3$1@gioia.aioe.org>
In reply to#23569
There is an error in this line:
 > ival = ival << (zeros + 8) & 0x007fffff;
It should be:
 > ival = (ival << (zeros + 8)) & 0x007fffff;

Sorry

[toc] | [prev] | [next] | [standalone]


#23533

FromWouter van Ooijen <wouter@voti.nl>
Date2016-04-06 18:00 +0200
Message-ID<5705329d$0$22298$e4fe514c@newszilla.xs4all.nl>
In reply to#23524
Op 06-Apr-16 om 3:17 PM schreef Vincent vB:
> Currently I'm in the process of replacing a custom compass /
> accelerometer with an ST LSM303D. The 'old' custom device produced
> single precision floats. Without parsing the values there where just
> passed inside a UDP packet.
>
> Unfortunately the LSM303D produces 16 bit signed integers. So, the
> embedded system needs to convert these values to floats. The scaling it
> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>
> Now, my hardware has no floating point support. However doing the
> following:
>
>    float output = ( (float)input ) / 16384f;
>
> will require quite a bit of FP magic. I would imagine that this:
>
>   const float scale = 1f / 16384f;
>   float output = ( (float)input ) * scale;
>
> may be faster, but still requires FP multiply support.
>
> Is there a simple and fast way which I can use to convert these integers
> to floats without the aid of an FP library? I have not found much code
> in this respect.

Reality check: what is the use of having the values in floating point 
format, if you want to avoid using an FP library? What are you going to 
do with those values?

Wouter van Ooijen

[toc] | [prev] | [next] | [standalone]


#23546

FromTauno Voipio <tauno.voipio@notused.fi.invalid>
Date2016-04-06 21:35 +0300
Message-ID<ne3kno$h49$1@dont-email.me>
In reply to#23533
On 6.4.16 19:00, Wouter van Ooijen wrote:
> Op 06-Apr-16 om 3:17 PM schreef Vincent vB:
>> Currently I'm in the process of replacing a custom compass /
>> accelerometer with an ST LSM303D. The 'old' custom device produced
>> single precision floats. Without parsing the values there where just
>> passed inside a UDP packet.
>>
>> Unfortunately the LSM303D produces 16 bit signed integers. So, the
>> embedded system needs to convert these values to floats. The scaling it
>> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>>
>> Now, my hardware has no floating point support. However doing the
>> following:
>>
>>    float output = ( (float)input ) / 16384f;
>>
>> will require quite a bit of FP magic. I would imagine that this:
>>
>>   const float scale = 1f / 16384f;
>>   float output = ( (float)input ) * scale;
>>
>> may be faster, but still requires FP multiply support.
>>
>> Is there a simple and fast way which I can use to convert these integers
>> to floats without the aid of an FP library? I have not found much code
>> in this respect.
>
> Reality check: what is the use of having the values in floating point
> format, if you want to avoid using an FP library? What are you going to
> do with those values?
>
> Wouter van Ooijen


The OP said that the rest of the system expects the direction values
in single-precision floating point format.

-- 

-TV

[toc] | [prev] | [next] | [standalone]


#23547

FromGrant Edwards <invalid@invalid.invalid>
Date2016-04-06 18:47 +0000
Message-ID<ne3lkv$ags$1@reader1.panix.com>
In reply to#23546
On 2016-04-06, Tauno Voipio <tauno.voipio@notused.fi.invalid> wrote:
> On 6.4.16 19:00, Wouter van Ooijen wrote:

>> Reality check: what is the use of having the values in floating point
>> format, if you want to avoid using an FP library? What are you going to
>> do with those values?
>
> The OP said that the rest of the system expects the direction values
> in single-precision floating point format.

Presumably the OP is just shipping the FP values out via some
interface to another piece of the system that does have FP support of
some sort.  That's pretty common for embeded firmware in "sensor" type
applicaions: you do all the of the data acquisition and signal
conditioning using fixed point, and then convert it to IEEE FP before
shipping it out the door.  On devices like that, often the only FP
operation that's available is a custom-written "convert from fixed
point to floating point with some hard-wired scaling" function.

-- 
Grant Edwards               grant.b.edwards        Yow! !  Now I understand
                                  at               advanced MICROBIOLOGY and
                              gmail.com            th' new TAX REFORM laws!!

[toc] | [prev] | [next] | [standalone]


#23558

FromVincent vB <embedded@spam.com>
Date2016-04-07 08:52 +0200
Message-ID<ne5034$1fb0$1@gioia.aioe.org>
In reply to#23547
On 6-4-2016 at 20:47, Grant Edwards wrote:
> On 2016-04-06, Tauno Voipio <tauno.voipio@notused.fi.invalid> wrote:
>> On 6.4.16 19:00, Wouter van Ooijen wrote:
>
>>> Reality check: what is the use of having the values in floating point
>>> format, if you want to avoid using an FP library? What are you going to
>>> do with those values?
>>
>> The OP said that the rest of the system expects the direction values
>> in single-precision floating point format.
>
> Presumably the OP is just shipping the FP values out via some
> interface to another piece of the system that does have FP support of
> some sort.  That's pretty common for embeded firmware in "sensor" type
> applicaions: you do all the of the data acquisition and signal
> conditioning using fixed point, and then convert it to IEEE FP before
> shipping it out the door.  On devices like that, often the only FP
> operation that's available is a custom-written "convert from fixed
> point to floating point with some hard-wired scaling" function.
>
Indeed, I'm afraid I can not change the output format. The older modules 
also need to be supported.

[toc] | [prev] | [next] | [standalone]


#23550

FromTim Wescott <seemywebsite@myfooter.really>
Date2016-04-06 16:05 -0500
Message-ID<NPCdneAzmdhj5JjKnZ2dnUU7-amdnZ2d@giganews.com>
In reply to#23524
On Wed, 06 Apr 2016 15:17:05 +0200, Vincent vB wrote:

> Currently I'm in the process of replacing a custom compass /
> accelerometer with an ST LSM303D. The 'old' custom device produced
> single precision floats. Without parsing the values there where just
> passed inside a UDP packet.
> 
> Unfortunately the LSM303D produces 16 bit signed integers. So, the
> embedded system needs to convert these values to floats. The scaling it
> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
> 
> Now, my hardware has no floating point support. However doing the
> following:
> 
>    float output = ( (float)input ) / 16384f;
> 
> will require quite a bit of FP magic. I would imagine that this:
> 
>   const float scale = 1f / 16384f;
>   float output = ( (float)input ) * scale;
> 
> may be faster, but still requires FP multiply support.
> 
> Is there a simple and fast way which I can use to convert these integers
> to floats without the aid of an FP library? I have not found much code
> in this respect.

What processor are you using that does not have easy to find floating 
point software?  I would expect that to come with the tools.

IIRC, the IEEE floating point standard has a whole bunch of stuff that 
you need to do to handle exceptions, but doing your conversion should be 
pretty direct.  You'll need to find the leading significant digit (which 
will probably be what takes the longest), shift, and then shove mantissa, 
sign, and exponent into floating point format.

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


#23551

FromDimiter_Popoff <dp@tgi-sci.com>
Date2016-04-07 00:23 +0300
Message-ID<ne3uit$po1$1@dont-email.me>
In reply to#23550
On 07.4.2016 г. 00:05, Tim Wescott wrote:
> On Wed, 06 Apr 2016 15:17:05 +0200, Vincent vB wrote:
>
>> Currently I'm in the process of replacing a custom compass /
>> accelerometer with an ST LSM303D. The 'old' custom device produced
>> single precision floats. Without parsing the values there where just
>> passed inside a UDP packet.
>>
>> Unfortunately the LSM303D produces 16 bit signed integers. So, the
>> embedded system needs to convert these values to floats. The scaling it
>> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>>
>> Now, my hardware has no floating point support. However doing the
>> following:
>>
>>     float output = ( (float)input ) / 16384f;
>>
>> will require quite a bit of FP magic. I would imagine that this:
>>
>>    const float scale = 1f / 16384f;
>>    float output = ( (float)input ) * scale;
>>
>> may be faster, but still requires FP multiply support.
>>
>> Is there a simple and fast way which I can use to convert these integers
>> to floats without the aid of an FP library? I have not found much code
>> in this respect.
>
> What processor are you using that does not have easy to find floating
> point software?  I would expect that to come with the tools.
>
> IIRC, the IEEE floating point standard has a whole bunch of stuff that
> you need to do to handle exceptions, but doing your conversion should be
> pretty direct.  You'll need to find the leading significant digit (which
> will probably be what takes the longest), shift, and then shove mantissa,
> sign, and exponent into floating point format.
>

Hi Tim,
finding the leading 1 is not necessarily taking the longest, on power
it is 1 cycle (cntlz, count leading zeroes). The 68020 had "bitfield
find first 1" (don't remember the exact mnemonic, I did miss that
on the CPU32 which was more or less a downgraded 020).
I expect ARM have that too in some form (?), it is key to being able
to say find the largest contiguous block in a bitmap allocated
space.

Dimiter

------------------------------------------------------
Dimiter Popoff, TGI             http://www.tgi-sci.com
------------------------------------------------------
http://www.flickr.com/photos/didi_tgi/


[toc] | [prev] | [next] | [standalone]


#23552

FromRob Gaddi <rgaddi@highlandtechnology.invalid>
Date2016-04-06 21:30 +0000
Message-ID<ne3v5q$roa$1@dont-email.me>
In reply to#23551
Dimiter_Popoff wrote:

> On 07.4.2016 г. 00:05, Tim Wescott wrote:
>> On Wed, 06 Apr 2016 15:17:05 +0200, Vincent vB wrote:
>>
>>> Currently I'm in the process of replacing a custom compass /
>>> accelerometer with an ST LSM303D. The 'old' custom device produced
>>> single precision floats. Without parsing the values there where just
>>> passed inside a UDP packet.
>>>
>>> Unfortunately the LSM303D produces 16 bit signed integers. So, the
>>> embedded system needs to convert these values to floats. The scaling it
>>> self is quite simple: Values -32768..32767 need to be scaled to [-2,2).
>>>
>>> Now, my hardware has no floating point support. However doing the
>>> following:
>>>
>>>     float output = ( (float)input ) / 16384f;
>>>
>>> will require quite a bit of FP magic. I would imagine that this:
>>>
>>>    const float scale = 1f / 16384f;
>>>    float output = ( (float)input ) * scale;
>>>
>>> may be faster, but still requires FP multiply support.
>>>
>>> Is there a simple and fast way which I can use to convert these integers
>>> to floats without the aid of an FP library? I have not found much code
>>> in this respect.
>>
>> What processor are you using that does not have easy to find floating
>> point software?  I would expect that to come with the tools.
>>
>> IIRC, the IEEE floating point standard has a whole bunch of stuff that
>> you need to do to handle exceptions, but doing your conversion should be
>> pretty direct.  You'll need to find the leading significant digit (which
>> will probably be what takes the longest), shift, and then shove mantissa,
>> sign, and exponent into floating point format.
>>
>
> Hi Tim,
> finding the leading 1 is not necessarily taking the longest, on power
> it is 1 cycle (cntlz, count leading zeroes). The 68020 had "bitfield
> find first 1" (don't remember the exact mnemonic, I did miss that
> on the CPU32 which was more or less a downgraded 020).
> I expect ARM have that too in some form (?), it is key to being able
> to say find the largest contiguous block in a bitmap allocated
> space.
>
> Dimiter
>
> ------------------------------------------------------
> Dimiter Popoff, TGI             http://www.tgi-sci.com
> ------------------------------------------------------
> http://www.flickr.com/photos/didi_tgi/
>
>

CLZ.  Available for ARM v.5 and above in full instruction mode (not
available in Thumb).

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com

Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#23553

FromClifford Heath <no.spam@please.net>
Date2016-04-07 09:23 +1000
Message-ID<57059a91$0$11965$c3e8da3$3a1a2348@news.astraweb.com>
In reply to#23552
On 07/04/16 07:30, Rob Gaddi wrote:
> CLZ.  Available for ARM v.5 and above in full instruction mode (not
> available in Thumb).

gcc has "int __builtin_clz (unsigned int x)", lso long and long-long 
versions. These map to whatever is most efficient for your hardware.

It's a pity that there's no integer equivalent of ldexp; maybe called 
ldiexp.

To the OP: If your endian-ness and compiler bit-fields work out, you
can use this (works for me on x64 with gcc) for building and breaking
float values.

typedef union {
         float   f;
         struct {
                 uint32_t        mantissa:23;
                 uint32_t        exponent:8;
                 uint32_t        sign:1;
         };
} FloatU;

Note that building a floating point value like this is likely to
be slower than just saying "(float)l" - with any decent compiler.
But it will help you understand what's going on.

Clifford Heath.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.arch.embedded


csiph-web