diff --git a/src/jit/gentree.cpp b/src/jit/gentree.cpp index 01c383a7f908..38f7c0fd7d90 100644 --- a/src/jit/gentree.cpp +++ b/src/jit/gentree.cpp @@ -12042,49 +12042,20 @@ GenTreePtr Compiler::gtFoldExprConst(GenTreePtr tree) // constants in a target-specific function. CLANG_FORMAT_COMMENT_ANCHOR; -#ifdef _TARGET_XARCH_ - // Don't fold conversions of +inf/-inf to integral value as the value returned by JIT helper - // doesn't match with the C compiler's cast result. + // Don't fold conversions of +inf/-inf to integral value on all platforms + // as the value returned by JIT helper doesn't match with the C compiler's cast result. + // We want the behavior to be same with or without folding. return tree; -#else //!_TARGET_XARCH_ - - switch (tree->CastToType()) - { - case TYP_BYTE: - i1 = ssize_t(INT8(d1)); - goto CNS_INT; - case TYP_UBYTE: - i1 = ssize_t(UINT8(d1)); - goto CNS_INT; - case TYP_SHORT: - i1 = ssize_t(INT16(d1)); - goto CNS_INT; - case TYP_CHAR: - i1 = ssize_t(UINT16(d1)); - goto CNS_INT; - case TYP_INT: - i1 = ssize_t(INT32(d1)); - goto CNS_INT; - case TYP_UINT: - i1 = ssize_t(UINT32(d1)); - goto CNS_INT; - case TYP_LONG: - lval1 = INT64(d1); - goto CNS_LONG; - case TYP_ULONG: - lval1 = UINT64(d1); - goto CNS_LONG; - case TYP_FLOAT: - case TYP_DOUBLE: - if (op1->gtType == TYP_FLOAT) - d1 = forceCastToFloat(d1); // it's only !_finite() after this conversion - goto CNS_DOUBLE; - default: - unreached(); - } -#endif //!_TARGET_XARCH_ } + if (d1 <= -1.0 && varTypeIsUnsigned(tree->CastToType())) + { + // Don't fold conversions of these cases becasue the result is unspecified per ECMA spec + // and the native math doing the fold doesn't match the run-time computation on all platforms. + // We want the behavior to be same with or without folding. + return tree; + } + switch (tree->CastToType()) { case TYP_BYTE: diff --git a/src/vm/jithelpers.cpp b/src/vm/jithelpers.cpp index 1626810758e1..276c2d6c5827 100644 --- a/src/vm/jithelpers.cpp +++ b/src/vm/jithelpers.cpp @@ -619,7 +619,7 @@ HCIMPL1_V(UINT64, JIT_Dbl2ULng, double val) else { // subtract 0x8000000000000000, do the convert then add it back again ret = FastDbl2Lng(val - two63) + I64(0x8000000000000000); -} + } return ret; } HCIMPLEND