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


Groups > comp.arch.embedded > #23569

Re: Integer/Fixedpoint to 32 bit float

From Vincent vB <embedded@spam.com>
Newsgroups comp.arch.embedded
Subject Re: Integer/Fixedpoint to 32 bit float
Date 2016-04-07 11:26 +0200
Organization Aioe.org NNTP Server
Message-ID <ne593s$ou$1@gioia.aioe.org> (permalink)
References <ne3289$f5a$1@gioia.aioe.org> <ne35hv$kaq$1@dont-email.me> <ne375g$huo$2@reader1.panix.com> <orgagbtnru3kiugf47goslh5mirfiqn944@4ax.com>

Show all headers | View raw


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

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web