Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.arch.embedded > #23524 > unrolled thread
| Started by | Vincent vB <embedded@spam.com> |
|---|---|
| First post | 2016-04-06 15:17 +0200 |
| Last post | 2016-04-08 03:39 +0100 |
| Articles | 14 on this page of 34 — 14 participants |
Back to article view | Back to comp.arch.embedded
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 2 of 2 — ← Prev page 1 [2]
| From | Vincent vB <embedded@spam.com> |
|---|---|
| Date | 2016-04-07 09:07 +0200 |
| Message-ID | <ne50ub$1glj$5@gioia.aioe.org> |
| In reply to | #23553 |
On 7-4-2016 at 1:23, Clifford Heath wrote:
> 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.
Well, its not really a microcontroller. It is a LatticeMico 32 on a
Xilinx FPGA. I think its a big-endian processor, so this may work out.
I've tried doing the (float)l, but then the LM32 compiler really
attempts to convert the integer into a float. Horrible tricks like this
would work (except for the compiler screaming 'murder and fire' as we
say in Dutch):
uint32_t l = ...;
float f;
*f = (float*)&l;
[toc] | [prev] | [next] | [standalone]
| From | Tim Wescott <tim@seemywebsite.com> |
|---|---|
| Date | 2016-04-07 11:14 -0500 |
| Message-ID | <-PSdnQuwLNrgGpvKnZ2dnUU7-dGdnZ2d@giganews.com> |
| In reply to | #23561 |
On Thu, 07 Apr 2016 09:07:00 +0200, Vincent vB wrote:
> On 7-4-2016 at 1:23, Clifford Heath wrote:
>> 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;
>>
That gives you the template, but not the values to stick therein.
>> 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.
>
> Well, its not really a microcontroller. It is a LatticeMico 32 on a
> Xilinx FPGA. I think its a big-endian processor, so this may work out.
> I've tried doing the (float)l, but then the LM32 compiler really
> attempts to convert the integer into a float. Horrible tricks like this
> would work (except for the compiler screaming 'murder and fire' as we
> say in Dutch):
>
> uint32_t l = ...;
> float f;
> *f = (float*)&l;
If it's on a Xilinx, and if the processor doesn't have a clz instruction,
and if it's really not fast enough to do it "by hand", how about making
that functionality out in FPGA-land? You could either do it as an
extension to the instruction set (if Lattice makes it easy), or as a
peripheral that coughs up an answer somewhere in the memory map when the
operand is written somewhere in the memory map.
This seems to be a fairly easy thing to do in an FPGA; I'd be tempted to
make an entire int-to-float converter "out there" if it really needed to
be that fast.
--
www.wescottdesign.com
[toc] | [prev] | [next] | [standalone]
| From | Tim Wescott <seemywebsite@myfooter.really> |
|---|---|
| Date | 2016-04-06 18:36 -0500 |
| Message-ID | <uMOdnYh0ifjzAJjKnZ2dnUU7-RudnZ2d@giganews.com> |
| In reply to | #23551 |
On Thu, 07 Apr 2016 00:23:42 +0300, 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. Based solely on the fact that he doesn't have floating point support already, I'm assuming that he's using some dire little 8-bitter. Which could be horribly wrong, but -- no floating point support? -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
[toc] | [prev] | [next] | [standalone]
| From | lasselangwadtchristensen@gmail.com |
|---|---|
| Date | 2016-04-06 17:03 -0700 |
| Message-ID | <4c37dabd-29f0-4c6b-a4c1-653b2b2770ee@googlegroups.com> |
| In reply to | #23524 |
http://locklessinc.com/articles/i2f/ tweak the exponent to get scale to +-2 -Lasse
[toc] | [prev] | [next] | [standalone]
| From | Vincent vB <embedded@spam.com> |
|---|---|
| Date | 2016-04-07 09:04 +0200 |
| Message-ID | <ne50qb$1glj$1@gioia.aioe.org> |
| In reply to | #23555 |
On 7-4-2016 at 2:03, lasselangwadtchristensen@gmail.com wrote: > http://locklessinc.com/articles/i2f/ > > tweak the exponent to get scale to +-2 > > -Lasse > Thanks for the link! Very informative
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-04-07 10:21 +0200 |
| Message-ID | <ne5547$3u2$1@dont-email.me> |
| In reply to | #23524 |
On 06/04/16 15: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 > You handle this by writing: float output = ((float) input) * 2.0f / 32768; Then you let the /compiler/ generate code that works. Ignore everyone here who has suggested "count leading ones", "handle 0 as a special case", "subtract 14 from the exponent", etc. That is not your job - other people have already figured out this stuff long ago, and the bugs have been ironed out. The LatticeMicro 32 compiler is gcc. Use the "-ffast-math" option to tell it that you are happy with a bit of obvious code re-arrangements rather than insisting on perfect IEEE operation - this lets you write "* 2.0f / 32768" to clearly express your intent in the code, while the /compiler/ turns it into "* (1f/32768f)". Write your code clearly and correctly, and let the tools do the work. Then all you need to do is make sure that you give the tools the best chance to generate fast code (such as -O2 -ffast-math, and whatever LM32 flags such as -mbarrel-shift-enabled and -mmultiply-enabled are appropriate for your particular cpu).
[toc] | [prev] | [next] | [standalone]
| From | Dimiter_Popoff <dp@tgi-sci.com> |
|---|---|
| Date | 2016-04-07 11:28 +0300 |
| Message-ID | <ne55hg$4v2$1@dont-email.me> |
| In reply to | #23565 |
On 07.4.2016 г. 11:21, David Brown wrote: > On 06/04/16 15: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 >> > > You handle this by writing: > > float output = ((float) input) * 2.0f / 32768; > > Then you let the /compiler/ generate code that works. Ignore everyone > here who has suggested "count leading ones", "handle 0 as a special > case", "subtract 14 from the exponent", etc. That is not your job - > other people have already figured out this stuff long ago, and the bugs > have been ironed out. > > The LatticeMicro 32 compiler is gcc. Use the "-ffast-math" option to > tell it that you are happy with a bit of obvious code re-arrangements > rather than insisting on perfect IEEE operation - this lets you write "* > 2.0f / 32768" to clearly express your intent in the code, while the > /compiler/ turns it into "* (1f/32768f)". > > Write your code clearly and correctly, and let the tools do the work. > > Then all you need to do is make sure that you give the tools the best > chance to generate fast code (such as -O2 -ffast-math, and whatever LM32 > flags such as -mbarrel-shift-enabled and -mmultiply-enabled are > appropriate for your particular cpu). > > Are you saying this will work without the compiler bringing in an FP library? Dimiter
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-04-07 10:41 +0200 |
| Message-ID | <ne5693$7q5$1@dont-email.me> |
| In reply to | #23566 |
On 07/04/16 10:28, Dimiter_Popoff wrote: > On 07.4.2016 г. 11:21, David Brown wrote: >> On 06/04/16 15: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 >>> >> >> You handle this by writing: >> >> float output = ((float) input) * 2.0f / 32768; >> >> Then you let the /compiler/ generate code that works. Ignore everyone >> here who has suggested "count leading ones", "handle 0 as a special >> case", "subtract 14 from the exponent", etc. That is not your job - >> other people have already figured out this stuff long ago, and the bugs >> have been ironed out. >> >> The LatticeMicro 32 compiler is gcc. Use the "-ffast-math" option to >> tell it that you are happy with a bit of obvious code re-arrangements >> rather than insisting on perfect IEEE operation - this lets you write "* >> 2.0f / 32768" to clearly express your intent in the code, while the >> /compiler/ turns it into "* (1f/32768f)". >> >> Write your code clearly and correctly, and let the tools do the work. >> >> Then all you need to do is make sure that you give the tools the best >> chance to generate fast code (such as -O2 -ffast-math, and whatever LM32 >> flags such as -mbarrel-shift-enabled and -mmultiply-enabled are >> appropriate for your particular cpu). >> >> > > Are you saying this will work without the compiler bringing in > an FP library? > Almost certainly the compiler will bring in parts of its FP library. But as long as you have /any/ floating point in the code, that is usually the case anyway. And assuming your library is constructed reasonably, you will only get the required functions linked in, not the entire library. Of course it would be possible to write a dedicated and optimised function to handle this conversion from integer to floating point, combined with scaling. But you would be doing an enormous amount of work in order to save a few KB of code space and/or a few microseconds of run time - not to mention the significant effort in testing, the risk of code having bugs or portability issues, and the maintenance effort when the scale factors change. Therefore my advice is to write the code simply, cleanly and in an obviously correct manner. Understand your tools and how to help them generate optimal code. And then get on with the rest of the project, having handled this tasks in a few minutes rather than days.
[toc] | [prev] | [next] | [standalone]
| From | Dimiter_Popoff <dp@tgi-sci.com> |
|---|---|
| Date | 2016-04-07 11:47 +0300 |
| Message-ID | <ne56lk$8lu$1@dont-email.me> |
| In reply to | #23567 |
On 07.4.2016 г. 11:41, David Brown wrote: > On 07/04/16 10:28, Dimiter_Popoff wrote: >> On 07.4.2016 г. 11:21, David Brown wrote: >>> On 06/04/16 15: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 >>>> >>> >>> You handle this by writing: >>> >>> float output = ((float) input) * 2.0f / 32768; >>> >>> Then you let the /compiler/ generate code that works. Ignore everyone >>> here who has suggested "count leading ones", "handle 0 as a special >>> case", "subtract 14 from the exponent", etc. That is not your job - >>> other people have already figured out this stuff long ago, and the bugs >>> have been ironed out. >>> >>> The LatticeMicro 32 compiler is gcc. Use the "-ffast-math" option to >>> tell it that you are happy with a bit of obvious code re-arrangements >>> rather than insisting on perfect IEEE operation - this lets you write "* >>> 2.0f / 32768" to clearly express your intent in the code, while the >>> /compiler/ turns it into "* (1f/32768f)". >>> >>> Write your code clearly and correctly, and let the tools do the work. >>> >>> Then all you need to do is make sure that you give the tools the best >>> chance to generate fast code (such as -O2 -ffast-math, and whatever LM32 >>> flags such as -mbarrel-shift-enabled and -mmultiply-enabled are >>> appropriate for your particular cpu). >>> >>> >> >> Are you saying this will work without the compiler bringing in >> an FP library? >> > > Almost certainly the compiler will bring in parts of its FP library. The question you replied to was how to do the conversion _without_ bringing in an FP library. Dimiter
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-04-07 11:40 +0200 |
| Message-ID | <ne59op$jjs$1@dont-email.me> |
| In reply to | #23568 |
On 07/04/16 10:47, Dimiter_Popoff wrote: > The question you replied to was how to do the conversion _without_ > bringing in an FP library. > The OP said that his cpu had no hardware floating point, and then he said he wanted to do the conversion "without the aid of an FP library". And yes, my recommendation uses floating point library code (technically it is part of the compiler language support library, rather than being part of the standard C library or other library, but it is still library code). I should really have first asked the OP exactly why he requires the code without using the compiler's library. Usually when people say the don't want an FP library, it is because they have a fixed idea that software FP is always big and slow - but they have not properly considered or tested whether it is /too/ big or /too/ slow for the job, nor thought enough about the complications (size, time, development effort and risk) of alternatives. Of course, it may be that the OP /has/ worked through this and concluded that even the small amount of library code needed for the conversion is too large.
[toc] | [prev] | [next] | [standalone]
| From | Vincent vB <embedded@spam.com> |
|---|---|
| Date | 2016-04-07 12:50 +0200 |
| Message-ID | <ne5e20$avb$1@gioia.aioe.org> |
| In reply to | #23568 |
On 7-4-2016 at 10:47, Dimiter_Popoff wrote: > On 07.4.2016 г. 11:41, David Brown wrote: >> On 07/04/16 10:28, Dimiter_Popoff wrote: >>> On 07.4.2016 г. 11:21, David Brown wrote: >>>> ... >>>> The LatticeMicro 32 compiler is gcc. Use the "-ffast-math" option to >>>> tell it that you are happy with a bit of obvious code re-arrangements >>>> rather than insisting on perfect IEEE operation - this lets you >>>> write "* >>>> 2.0f / 32768" to clearly express your intent in the code, while the >>>> /compiler/ turns it into "* (1f/32768f)". >>>> >>>> Write your code clearly and correctly, and let the tools do the work. >>>> >>>> Then all you need to do is make sure that you give the tools the best >>>> chance to generate fast code (such as -O2 -ffast-math, and whatever >>>> LM32 >>>> flags such as -mbarrel-shift-enabled and -mmultiply-enabled are >>>> appropriate for your particular cpu). >>>> >>>> >>> >>> Are you saying this will work without the compiler bringing in >>> an FP library? >>> >> >> Almost certainly the compiler will bring in parts of its FP library. > > The question you replied to was how to do the conversion _without_ > bringing in an FP library. > > Dimiter > I wrote a test, creating the fltFromI16 using floats, as suggested by expert David Brown. I think I wrote my code clearly and correctly and let the /compiler/ do the work, with the following additional objects as result: /libgcc.a(_mul_sf.o) /libgcc.a(_div_sf.o) /libgcc.a(_si_to_sf.o) /libgcc.a(_thenan_sf.o) /libgcc.a(_muldi3.o) /libgcc.a(_lshrdi3.o) /libgcc.a(_clzsi2.o) /libgcc.a(_pack_sf.o) /libgcc.a(_unpack_sf.o) /libgcc.a(_mulsi3.o) /libgcc.a(_udivmodsi4.o) /libgcc.a(_clz.o) Used GCC flags that matter: -mbarrel-shift-enabled -mmultiply-enabled -msign-extend-enabled -Os -ffast-math 'The tools' also required 2736 more bytes for the same task than my highly flawed and inferior code. Vincent
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-04-07 13:52 +0200 |
| Message-ID | <ne5hfl$emc$1@dont-email.me> |
| In reply to | #23572 |
On 07/04/16 12:50, Vincent vB wrote: > > On 7-4-2016 at 10:47, Dimiter_Popoff wrote: >> On 07.4.2016 г. 11:41, David Brown wrote: >>> On 07/04/16 10:28, Dimiter_Popoff wrote: >>>> On 07.4.2016 г. 11:21, David Brown wrote: >>>>> ... >>>>> The LatticeMicro 32 compiler is gcc. Use the "-ffast-math" option to >>>>> tell it that you are happy with a bit of obvious code re-arrangements >>>>> rather than insisting on perfect IEEE operation - this lets you >>>>> write "* >>>>> 2.0f / 32768" to clearly express your intent in the code, while the >>>>> /compiler/ turns it into "* (1f/32768f)". >>>>> >>>>> Write your code clearly and correctly, and let the tools do the work. >>>>> >>>>> Then all you need to do is make sure that you give the tools the best >>>>> chance to generate fast code (such as -O2 -ffast-math, and whatever >>>>> LM32 >>>>> flags such as -mbarrel-shift-enabled and -mmultiply-enabled are >>>>> appropriate for your particular cpu). >>>>> >>>>> >>>> >>>> Are you saying this will work without the compiler bringing in >>>> an FP library? >>>> >>> >>> Almost certainly the compiler will bring in parts of its FP library. >> >> The question you replied to was how to do the conversion _without_ >> bringing in an FP library. >> >> Dimiter >> > > I wrote a test, creating the fltFromI16 using floats, as suggested by > expert David Brown. > > I think I wrote my code clearly and correctly and let the /compiler/ do > the work, with the following additional objects as result: > > /libgcc.a(_mul_sf.o) > /libgcc.a(_div_sf.o) > /libgcc.a(_si_to_sf.o) > /libgcc.a(_thenan_sf.o) > /libgcc.a(_muldi3.o) > /libgcc.a(_lshrdi3.o) > /libgcc.a(_clzsi2.o) > /libgcc.a(_pack_sf.o) > /libgcc.a(_unpack_sf.o) > /libgcc.a(_mulsi3.o) > /libgcc.a(_udivmodsi4.o) > /libgcc.a(_clz.o) > > Used GCC flags that matter: > -mbarrel-shift-enabled -mmultiply-enabled > -msign-extend-enabled -Os -ffast-math > > 'The tools' also required 2736 more bytes for the same task than my > highly flawed and inferior code. > Whether your own code is flawed or not depends on whether or not it works, and what coding standards or practices you follow. And whether it is inferior or not depends on your requirements - some would say that baking the scaling constants into a somewhat complicated manually optimised function is inferior, while others would say that requiring a couple of extra KB of library code is inferior. I can't tell you what /you/ need for /your/ design. But I can ask you - are you so short on code space that these 2.7 KB is a significant issue? Or is it just that it "feels wrong" to "waste" this space? If it is the former (which is not unlikely if the code is in "rom" inside a small FPGA), then that's fine. If it is the later, then think about what is really the /best/ solution for you, the project, and the customer.
[toc] | [prev] | [next] | [standalone]
| From | Dimiter_Popoff <dp@tgi-sci.com> |
|---|---|
| Date | 2016-04-07 22:14 +0300 |
| Message-ID | <ne6bcb$r7c$1@dont-email.me> |
| In reply to | #23572 |
On 07.4.2016 г. 13:50, Vincent vB wrote: > > On 7-4-2016 at 10:47, Dimiter_Popoff wrote: >> On 07.4.2016 г. 11:41, David Brown wrote: >>> On 07/04/16 10:28, Dimiter_Popoff wrote: >>>> On 07.4.2016 г. 11:21, David Brown wrote: >>>>> ... >>>>> The LatticeMicro 32 compiler is gcc. Use the "-ffast-math" option to >>>>> tell it that you are happy with a bit of obvious code re-arrangements >>>>> rather than insisting on perfect IEEE operation - this lets you >>>>> write "* >>>>> 2.0f / 32768" to clearly express your intent in the code, while the >>>>> /compiler/ turns it into "* (1f/32768f)". >>>>> >>>>> Write your code clearly and correctly, and let the tools do the work. >>>>> >>>>> Then all you need to do is make sure that you give the tools the best >>>>> chance to generate fast code (such as -O2 -ffast-math, and whatever >>>>> LM32 >>>>> flags such as -mbarrel-shift-enabled and -mmultiply-enabled are >>>>> appropriate for your particular cpu). >>>>> >>>>> >>>> >>>> Are you saying this will work without the compiler bringing in >>>> an FP library? >>>> >>> >>> Almost certainly the compiler will bring in parts of its FP library. >> >> The question you replied to was how to do the conversion _without_ >> bringing in an FP library. >> >> Dimiter >> > > I wrote a test, creating the fltFromI16 using floats, as suggested by > expert David Brown. > > I think I wrote my code clearly and correctly and let the /compiler/ do > the work, with the following additional objects as result: > > /libgcc.a(_mul_sf.o) > /libgcc.a(_div_sf.o) > /libgcc.a(_si_to_sf.o) > /libgcc.a(_thenan_sf.o) > /libgcc.a(_muldi3.o) > /libgcc.a(_lshrdi3.o) > /libgcc.a(_clzsi2.o) > /libgcc.a(_pack_sf.o) > /libgcc.a(_unpack_sf.o) > /libgcc.a(_mulsi3.o) > /libgcc.a(_udivmodsi4.o) > /libgcc.a(_clz.o) > > Used GCC flags that matter: > -mbarrel-shift-enabled -mmultiply-enabled > -msign-extend-enabled -Os -ffast-math > > 'The tools' also required 2736 more bytes for the same task than my > highly flawed and inferior code. > > > Vincent I spent a few minutes to do it in my vpa (for power architecture), takes 60 bytes, destroys 2 registers. Might be some help, here it is: http://tgi-sci.com/vpaex/i16tofp32.sa <-- source, http://tgi-sci.com/vpaex/i16tofp32.txt <-- vpa list with native code. Dimiter ------------------------------------------------------ Dimiter Popoff, TGI http://www.tgi-sci.com ------------------------------------------------------ http://www.flickr.com/photos/didi_tgi/
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.invalid> |
|---|---|
| Date | 2016-04-08 03:39 +0100 |
| Message-ID | <pan.2016.04.08.02.39.45.910000@nowhere.invalid> |
| In reply to | #23524 |
On Wed, 06 Apr 2016 15:17:05 +0200, Vincent vB wrote:
> 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).
If you have 256 kiB to spare, you could use a lookup table.
The following should work on any 32-bit architecture. count_bits() can be
optimised, possibly to a single instruction.
static int count_bits(unsigned int x)
{
for (int i = 0; i < 32; i++)
if (x>>i == 0)
return i;
abort();
}
static void int_to_float(int x, float *result)
{
if (x == 0) {
*(uint32_t*)result = 0;
return;
}
int negative = x < 0;
if (negative)
x = -x;
int exponent = count_bits(x) - 1; // 0 .. 15
uint32_t bits = x << (23 - exponent);
bits &= ~(1U<<23);
bits |= (exponent + 127U) << 23;
if (negative)
bits |= (1U<<31);
*(uint32_t*)result = bits;
}
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.arch.embedded
csiph-web