commit-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [commit-cp] [bugs #9895] double to int conversion problem


From: Tom Tromey
Subject: Re: [commit-cp] [bugs #9895] double to int conversion problem
Date: 05 Aug 2004 10:23:22 -0600
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

>>>>> "Archie" == Archie Cobbs <address@hidden> writes:

Archie> I think this is a VM bug rather than a Classpath bug (JC has
Archie> the same bug). It results from doing the jdouble -> jint
Archie> conversion in C via a `(jint)d' cast.

Yeah, that's wrong.

Archie> I'm still curious what the solution is though :-)

Language rules for converting a floating point number to an integral
type are here:

http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363

There are similar rules for the VM in the JVM Spec, since the java
bytecodes are defined to do exactly what the language requires.

Basically, NaN -> 0, +Inf -> MAX_INT (or long), and -Inf -> MIN_INT
(or long).  When casting to something smaller than int, there is an
intermediate cast to int; VMs don't need to worry about this since the
compiler will emit two cast operations, e.g. `d2i ; i2b'.

In libgcj we do this with a template:

// Used to convert from floating types to integral types.
template<typename TO, typename FROM>
static inline TO
convert (FROM val, TO min, TO max)
{
  TO ret;
  if (val >= (FROM) max)
    ret = max;
  else if (val <= (FROM) min)
    ret = min;
  else if (val != val)
    ret = 0;
  else
    ret = (TO) val;
  return ret;
}


Use, eg:

        jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);

Tom




reply via email to

[Prev in Thread] Current Thread [Next in Thread]