public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Soft float support and casting
@ 2006-11-29 12:34 Simon Kagstrom
  2006-11-29 12:42 ` John Love-Jensen
  2006-11-29 12:43 ` Michael Eager
  0 siblings, 2 replies; 4+ messages in thread
From: Simon Kagstrom @ 2006-11-29 12:34 UTC (permalink / raw)
  To: gcc-help

Hello!

I'm working on adding floating point support to Cibyl, my MIPS to Java
bytecode binary translator. After having studied the MIPS FPU and
NestedVM, I've decided that it's easier to rely on GCC's -msoft-float
option and implementing efficient versions of the library routines
(normally found in libgcc).

I believe I will be able to fairly easily construct efficient
implementations by invoking Java code which works on "native" floats,
i.e., something like


   /* C implementation */
   float __addsf3(float a, float b)
   {
     /* This is actually a call to the Java method below */
     return __addsf3_helper(a,b);
   }

   /* Java method */
   public static int __addsf3_helper(int _a, int _b)
   {
     float a = Float.intBitsToFloat(_a);
     float b = Float.intBitsToFloat(_b);

     return Float.floatToIntBits(a + b);
   }


I've looked in libgcc2.c and am unfortunately a bit unsure about how
to get GCC to directly cast an integer value to a float type. For
example, I would like to do something like this:

   float __floatsisf(long i)
   {
       return (float)0x40c00000; /* float value 6.0 */
   }

but the cast will of course turn this into some completely different
number. Can someone quickly explain how to do this?

// Simon

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Soft float support and casting
  2006-11-29 12:34 Soft float support and casting Simon Kagstrom
@ 2006-11-29 12:42 ` John Love-Jensen
  2006-11-29 12:43 ` Michael Eager
  1 sibling, 0 replies; 4+ messages in thread
From: John Love-Jensen @ 2006-11-29 12:42 UTC (permalink / raw)
  To: Simon Kagstrom, MSX to GCC

Hi Simon,

Would this work for your needs:

float BitsAsFloat(long l)
{
  union
  {
    float f;
    long l;
  } retval;

  retval.l = l;

  return retval.f;
}

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Soft float support and casting
  2006-11-29 12:34 Soft float support and casting Simon Kagstrom
  2006-11-29 12:42 ` John Love-Jensen
@ 2006-11-29 12:43 ` Michael Eager
  2006-11-29 13:23   ` Simon Kagstrom
  1 sibling, 1 reply; 4+ messages in thread
From: Michael Eager @ 2006-11-29 12:43 UTC (permalink / raw)
  To: Simon Kagstrom; +Cc: gcc-help

Simon Kagstrom wrote:

> I've looked in libgcc2.c and am unfortunately a bit unsure about how
> to get GCC to directly cast an integer value to a float type. For
> example, I would like to do something like this:
> 
>    float __floatsisf(long i)
>    {
>        return (float)0x40c00000; /* float value 6.0 */
>    }
> 
> but the cast will of course turn this into some completely different
> number. Can someone quickly explain how to do this?

Use a union:

union { float f; unsigned int i; } u;

float __floatsisf(long i)
{
    u.i = 0x40c00000;
    return u.f;
}


-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Soft float support and casting
  2006-11-29 12:43 ` Michael Eager
@ 2006-11-29 13:23   ` Simon Kagstrom
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Kagstrom @ 2006-11-29 13:23 UTC (permalink / raw)
  To: Michael Eager; +Cc: gcc-help

At Wed, 29 Nov 2006 04:43:06 -0800,
Michael Eager wrote:
> 
> Simon Kagstrom wrote:
> 
> > I've looked in libgcc2.c and am unfortunately a bit unsure about how
> > to get GCC to directly cast an integer value to a float type. For
> > example, I would like to do something like this:
> > 
> >    float __floatsisf(long i)
> >    {
> >        return (float)0x40c00000; /* float value 6.0 */
> >    }
> > 
> > but the cast will of course turn this into some completely different
> > number. Can someone quickly explain how to do this?
> 
> Use a union:
> 
> union { float f; unsigned int i; } u;
> 
> float __floatsisf(long i)
> {
>     u.i = 0x40c00000;
>     return u.f;
> }

Ah, of course - less magic than I was expecting. :-)

Thanks,
// Simon

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-11-29 13:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-29 12:34 Soft float support and casting Simon Kagstrom
2006-11-29 12:42 ` John Love-Jensen
2006-11-29 12:43 ` Michael Eager
2006-11-29 13:23   ` Simon Kagstrom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).