Repository: dotnet/runtime
File: src/coreclr/jit/codegenwasm.cpp (C++)

Code before:
            break;
        }

        case GenIntCastDesc::CHECK_SMALL_INT_RANGE:
        {
            // (U)(INT|LONG) to Small INT
            const int castMaxValue = desc.CheckSmallIntMax();
            const int castMinValue = desc.CheckSmallIntMin();

            if (castMinValue == 0)
            {
                // When the minimum is 0, a single unsigned upper-bound check is sufficient.
                // For signed sources, negative values become large unsigned values and
                // thus also trigger the overflow via the same comparison.
                GetEmitter()->emitIns_I(is64BitSrc ? INS_i64_const : INS_i32_const, srcSize, castMaxValue);
                GetEmitter()->emitIns(is64BitSrc ? INS_i64_gt_u : INS_i32_gt_u);
            }
            else
            {
                GetEmitter()->emitIns_I(is64BitSrc ? INS_i64_const : INS_i32_const, srcSize, castMaxValue);
                GetEmitter()->emitIns(is64BitSrc ? (srcUnsigned ? INS_i64_gt_u : INS_i64_gt_s)
                                                 : (srcUnsigned ? INS_i32_gt_u : INS_i32_gt_s));
                GetEmitter()->emitIns_I(INS_local_get, srcSize, WasmRegToIndex(reg));
                GetEmitter()->emitIns_I(is64BitSrc ? INS_i64_const : INS_i32_const, srcSize, castMinValue);
                GetEmitter()->emitIns(is64BitSrc ? (srcUnsigned ? INS_i64_lt_u : INS_i64_lt_s)
                                                 : (srcUnsigned ? INS_i32_lt_u : INS_i32_lt_s));
                GetEmitter()->emitIns(INS_i32_or);
            }
            genJumpToThrowHlpBlk(SCK_OVERFLOW);
            break;
        }

        default:
            unreached();
    }
}

//------------------------------------------------------------------------
// genFloatToIntCast: Generate code for a floating point to integer cast
//
// Arguments:
//    tree - The GT_CAST node for the float-to-int cast operation
//
// Notes:
//    Handles casts from TYP_FLOAT/TYP_DOUBLE to TYP_INT/TYP_LONG.
//    Uses saturating truncation instructions (trunc_sat) which clamp
//    out-of-range values rather than trapping.
//
void CodeGen::genFloatToIntCast(GenTree* tree)
{
    if (tree->gtOverflow())

Review comment (suggestion):
```suggestion
                GetEmitter()->emitIns(is64BitSrc ? INS_i64_gt_s : INS_i32_gt_s);
                GetEmitter()->emitIns_I(INS_local_get, srcSize, WasmRegToIndex(reg));
                GetEmitter()->emitIns_I(is64BitSrc ? INS_i64_const : INS_i32_const, srcSize, castMinValue);
                GetEmitter()->emitIns(is64BitSrc ? INS_i64_lt_s : INS_i32_lt_s);
```
(Also delete `srcUnsigned`)

`castMinValue  != 0` implies `srcUnsigned == false`.

Code after:

        case GenIntCastDesc::CHECK_SMALL_INT_RANGE:
        {
            // (U)(INT|LONG) to Small INT
            const int castMaxValue = desc.CheckSmallIntMax();
            const int castMinValue = desc.CheckSmallIntMin();

            if (castMinValue == 0)
            {
                // When the minimum is 0, a single unsigned upper-bound check is sufficient.
                // For signed sources, negative values become large unsigned values and
                // thus also trigger the overflow via the same comparison.
                GetEmitter()->emitIns_I(is64BitSrc ? INS_i64_const : INS_i32_const, srcSize, castMaxValue);
                GetEmitter()->emitIns(is64BitSrc ? INS_i64_gt_u : INS_i32_gt_u);
            }
            else
            {
                // We need to check a range around zero, eg [-128, 127] for 8-bit signed.
                // Do two compares and combine the results: (src > max) | (src < min).
                assert(!cast->IsUnsigned());
                GetEmitter()->emitIns_I(is64BitSrc ? INS_i64_const : INS_i32_const, srcSize, castMaxValue);
                GetEmitter()->emitIns(is64BitSrc ? INS_i64_gt_s : INS_i32_gt_s);
                GetEmitter()->emitIns_I(INS_local_get, srcSize, WasmRegToIndex(reg));
                GetEmitter()->emitIns_I(is64BitSrc ? INS_i64_const : INS_i32_const, srcSize, castMinValue);
                GetEmitter()->emitIns(is64BitSrc ? INS_i64_lt_s : INS_i32_lt_s);
                GetEmitter()->emitIns(INS_i32_or);
            }
            genJumpToThrowHlpBlk(SCK_OVERFLOW);
            break;
        }

        default:
            unreached();
    }
}

//------------------------------------------------------------------------
// genFloatToIntCast: Generate code for a floating point to integer cast
//
// Arguments:
//    tree - The GT_CAST node for the float-to-int cast operation
//
// Notes:
//    Handles casts from TYP_FLOAT/TYP_DOUBLE to TYP_INT/TYP_LONG.
//    Uses saturating truncation instructions (trunc_sat) which clamp
//    out-of-range values rather than trapping.
//
void CodeGen::genFloatToIntCast(GenTree* tree)
{
    if (tree->gtOverflow())
    {