Skip to content

Commit

Permalink
[GR-22718] Fix inconsistency between int=>float and double=>float con…
Browse files Browse the repository at this point in the history
…version.

PullRequest: graal/6078
  • Loading branch information
wirthi committed May 7, 2020
2 parents 8b5f8a1 + 2b2e132 commit 2664180
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
package com.oracle.truffle.api.interop;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.source.SourceSection;
Expand Down Expand Up @@ -73,7 +73,9 @@ static boolean fitsInShort(Integer receiver) {

@ExportMessage
static boolean fitsInFloat(Integer receiver) {
return NumberUtils.inSafeFloatRange(receiver);
int i = receiver;
float f = i;
return f == i;
}

@ExportMessage
Expand Down Expand Up @@ -101,8 +103,9 @@ static short asShort(Integer receiver) throws UnsupportedMessageException {
@ExportMessage
static float asFloat(Integer receiver) throws UnsupportedMessageException {
int i = receiver;
if (NumberUtils.inSafeFloatRange(i)) {
return i;
float f = i;
if (f == i) {
return f;
}
CompilerDirectives.transferToInterpreter();
throw UnsupportedMessageException.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@
*/
package com.oracle.truffle.api.interop;

import static com.oracle.truffle.api.interop.NumberUtils.INT_MAX_SAFE_FLOAT;
import static com.oracle.truffle.api.interop.NumberUtils.LONG_MAX_SAFE_DOUBLE;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.source.SourceSection;
Expand Down Expand Up @@ -74,12 +71,13 @@ static boolean fitsInShort(Long receiver) {

@ExportMessage
static boolean fitsInFloat(Long receiver) {
return receiver >= -INT_MAX_SAFE_FLOAT && receiver <= INT_MAX_SAFE_FLOAT;
float f = receiver;
return f == receiver;
}

@ExportMessage
static boolean fitsInDouble(Long receiver) {
return receiver >= -LONG_MAX_SAFE_DOUBLE && receiver <= LONG_MAX_SAFE_DOUBLE;
return NumberUtils.inSafeDoubleRange(receiver);
}

@ExportMessage
Expand Down Expand Up @@ -118,8 +116,9 @@ static int asInt(Long receiver) throws UnsupportedMessageException {
@ExportMessage
static float asFloat(Long receiver) throws UnsupportedMessageException {
long l = receiver;
if (NumberUtils.inSafeFloatRange(l)) {
return l;
float f = l;
if (f == l) {
return f;
}
CompilerDirectives.transferToInterpreter();
throw UnsupportedMessageException.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void testShortDefault() throws InteropException {

@Test
public void testIntDefault() throws InteropException {
assertNumber(Integer.MIN_VALUE, false, false, true, true, false, true);
assertNumber(Integer.MIN_VALUE, false, false, true, true, true, true);
assertNumber(Short.MIN_VALUE - 1, false, false, true, true, true, true);
assertNumber((int) Short.MIN_VALUE, false, true, true, true, true, true);
assertNumber(Byte.MIN_VALUE - 1, false, true, true, true, true, true);
Expand All @@ -126,14 +126,14 @@ public void testIntDefault() throws InteropException {
assertNumber(Byte.MAX_VALUE + 1, false, true, true, true, true, true);
assertNumber((int) Short.MAX_VALUE, false, true, true, true, true, true);
assertNumber(Short.MAX_VALUE + 1, false, false, true, true, true, true);
assertNumber(Integer.MAX_VALUE, false, false, true, true, false, true);
assertNumber(Integer.MAX_VALUE, false, false, true, true, true, true);
}

@Test
public void testLongDefault() throws InteropException {
assertNumber(Long.MIN_VALUE, false, false, false, true, false, false);
assertNumber((long) Integer.MIN_VALUE - 1, false, false, false, true, false, true);
assertNumber((long) Integer.MIN_VALUE, false, false, true, true, false, true);
assertNumber(Long.MIN_VALUE, false, false, false, true, true, false);
assertNumber((long) Integer.MIN_VALUE - 1, false, false, false, true, true, true);
assertNumber((long) Integer.MIN_VALUE, false, false, true, true, true, true);
assertNumber((long) Short.MIN_VALUE - 1, false, false, true, true, true, true);
assertNumber((long) Short.MIN_VALUE, false, true, true, true, true, true);
assertNumber((long) Byte.MIN_VALUE - 1, false, true, true, true, true, true);
Expand All @@ -143,8 +143,8 @@ public void testLongDefault() throws InteropException {
assertNumber((long) Byte.MAX_VALUE + 1, false, true, true, true, true, true);
assertNumber((long) Short.MAX_VALUE, false, true, true, true, true, true);
assertNumber((long) Short.MAX_VALUE + 1, false, false, true, true, true, true);
assertNumber((long) Integer.MAX_VALUE, false, false, true, true, false, true);
assertNumber(Long.MAX_VALUE, false, false, false, true, false, false);
assertNumber((long) Integer.MAX_VALUE, false, false, true, true, true, true);
assertNumber(Long.MAX_VALUE, false, false, false, true, true, false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,12 @@ public void testAsFloat() {
Number[] canConvert = {
Byte.MIN_VALUE, Byte.MAX_VALUE,
Short.MIN_VALUE, Short.MAX_VALUE,
-(1 << 24 - 1), 1 << 24 - 1,
Integer.MIN_VALUE, Integer.MAX_VALUE, -(1 << 24 - 1), 1 << 24 - 1,
-(1 << 24), 1 << 24,
-(1L << 24), 1L << 24,
-(1L << 24 - 1), 1L << 24 - 1,
Long.MIN_VALUE, Long.MAX_VALUE,
0.5d, -0.5d,
0d, -0d, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, -(Math.pow(2, 24) - 1), +(Math.pow(2, 24) - 1),
};
for (Number number : canConvert) {
Expand All @@ -744,8 +748,8 @@ public void testAsFloat() {
}

Number[] cannotConvert = {
Integer.MIN_VALUE, Integer.MAX_VALUE, -(1 << 24), 1 << 24,
Long.MIN_VALUE, Long.MAX_VALUE, -(1L << 24), 1L << 24,
0.1d, -0.1d,
0.2d, -0.2d,
Double.MIN_VALUE, Double.MAX_VALUE,
};
for (Number number : cannotConvert) {
Expand Down

0 comments on commit 2664180

Please sign in to comment.