Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Backend/IRBuilderAsmJs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2921,6 +2921,14 @@ IRBuilderAsmJs::BuildFloat3(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlo
instr = IR::Instr::New(Js::OpCode::Copysign_A, dstOpnd, src1Opnd, src2Opnd, m_func);
break;

case Js::OpCodeAsmJs::Min_Flt:
instr = IR::Instr::New(Js::OpCode::InlineMathMin, dstOpnd, src1Opnd, src2Opnd, m_func);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look and lowering codepath also needs to be changed, i.e. LowerMDShared.cpp around line 9200 we need a case for float32.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to wait and implement in another checkin that is ok, but I would like if we have explicit failure here. Otherwise if someone tries to JIT this it might lead to confusing failure (crash will not happen until lowering or maybe even further)

break;

case Js::OpCodeAsmJs::Max_Flt:
instr = IR::Instr::New(Js::OpCode::InlineMathMax, dstOpnd, src1Opnd, src2Opnd, m_func);
break;

default:
Assume(UNREACHED);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Backend/Lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13557,9 +13557,9 @@ IR::BranchInstr *Lowerer::InsertCompareBranch(

Func *const func = insertBeforeInstr->m_func;

if(compareSrc1->IsFloat64())
if(compareSrc1->IsFloat())
{
Assert(compareSrc2->IsFloat64());
Assert(compareSrc2->IsFloat());
Assert(!isUnsigned);
IR::BranchInstr *const instr = IR::BranchInstr::New(branchOpCode, target, compareSrc1, compareSrc2, func);
insertBeforeInstr->InsertBefore(instr);
Expand Down
38 changes: 25 additions & 13 deletions lib/Backend/LowerMDShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9274,41 +9274,40 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
instr->InsertBefore(branchInstr);
LowererMDArch::EmitInt4Instr(branchInstr);
}
// MOV dst, src1
this->m_lowerer->InsertMove(dst, src1, instr);
// MOV dst, src1
this->m_lowerer->InsertMove(dst, src1, instr);
}

else if(dst->IsFloat64())
else if(dst->IsFloat())
{
// COMISD src1 (src2), src2 (src1)
// COMISD/COMISS src1 (src2), src2 (src1)
// JA $doneLabel
// JEQ $labelNegZeroAndNaNCheckHelper
// MOVSD dst, src2
// MOVSD/MOVSS dst, src2
// JMP $doneLabel
//
// $labelNegZeroAndNaNCheckHelper
// JP $labelNaNHelper
// if(min)
// {
// if(src2 == -0.0)
// MOVSD dst, src2
// MOVSD/MOVSS dst, src2
// }
// else
// {
// if(src1 == -0.0)
// MOVSD dst, src2
// MOVSD/MOVSS dst, src2
// }
// JMP $doneLabel
//
// $labelNaNHelper
// MOVSD dst, NaN
// MOVSD/MOVSS dst, NaN
//
// $doneLabel

//MOVSD dst, src1;
//MOVSD/MOVSS dst, src1;
Assert(!dst->IsEqual(src1));
this->m_lowerer->InsertMove(dst, src1, instr);

this->m_lowerer->InsertMove(dst, src1, instr);
if(min)
{
this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrLt_A, doneLabel, instr); // Lowering of BrLt_A for floats is done to JA with operands swapped
Expand Down Expand Up @@ -9343,7 +9342,17 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));

instr->InsertBefore(labelNaNHelper);
IR::Opnd * opndNaN = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_Nan), IRType::TyFloat64, this->m_func);
IR::Opnd * opndNaN = nullptr;

if (dst->IsFloat32())
{
opndNaN = IR::MemRefOpnd::New((float*)&(Js::JavascriptNumber::k_Nan32), IRType::TyFloat32, this->m_func);
}
else
{
opndNaN = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_Nan), IRType::TyFloat64, this->m_func);
}

this->m_lowerer->InsertMove(dst, opndNaN, instr);
}
instr->InsertBefore(doneLabel);
Expand All @@ -9362,7 +9371,10 @@ IR::Opnd* LowererMD::IsOpndNegZero(IR::Opnd* opnd, IR::Instr* instr)
{
IR::Opnd * isNegZero = IR::RegOpnd::New(TyInt32, this->m_func);

LoadDoubleHelperArgument(instr, opnd);
if (opnd->IsFloat64())
LoadDoubleHelperArgument(instr, opnd);
else
LoadFloatHelperArgument(instr, opnd);
IR::Instr * helperCallInstr = IR::Instr::New(Js::OpCode::CALL, isNegZero, this->m_func);
instr->InsertBefore(helperCallInstr);
this->ChangeToHelperCall(helperCallInstr, IR::HelperIsNegZero);
Expand Down
3 changes: 2 additions & 1 deletion lib/Common/Common/NumberUtilitiesBase.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
//////////////////////////////////////////////////////////
Expand All @@ -13,6 +13,7 @@ namespace Js
{
public:
static const UINT64 k_Nan = 0xFFF8000000000000ull;
static const UINT32 k_Nan32 = 0x7FFF8000ul;
};

class NumberUtilitiesBase
Expand Down
6 changes: 3 additions & 3 deletions lib/Runtime/ByteCode/OpCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,9 @@ MACRO_BACKEND_ONLY( SlotArrayCheck, Empty, OpCanCSE)
MACRO_BACKEND_ONLY( FrameDisplayCheck, Empty, OpCanCSE)
MACRO_EXTEND( BeginBodyScope, Empty, OpSideEffect)

MACRO_BACKEND_ONLY( Copysign_A, Empty, OpTempNumberSources | OpCanCSE | OpProducesNumber)
MACRO_BACKEND_ONLY( Trunc_A, Empty, OpTempNumberSources | OpCanCSE | OpProducesNumber)
MACRO_BACKEND_ONLY( Nearest_A, Empty, OpTempNumberSources | OpCanCSE | OpProducesNumber)
MACRO_BACKEND_ONLY( Copysign_A, Empty, OpTempNumberSources|OpCanCSE|OpProducesNumber)
MACRO_BACKEND_ONLY( Trunc_A, Empty, OpTempNumberSources|OpCanCSE|OpProducesNumber)
MACRO_BACKEND_ONLY( Nearest_A, Empty, OpTempNumberSources|OpCanCSE|OpProducesNumber)

// All SIMD ops are backend only for non-asmjs.
#define MACRO_SIMD(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr, ...) MACRO_BACKEND_ONLY(opcode, Empty, OpCodeAttr)
Expand Down
7 changes: 4 additions & 3 deletions lib/Runtime/ByteCode/OpCodesAsmJs.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,17 @@ MACRO_WMS ( Sqrt_Flt , Float2 , None )
MACRO_WMS ( Abs_Flt , Float2 , None )
MACRO_WMS ( Atan2_Db , Double3 , None )
MACRO_WMS ( Min_Db , Double3 , None )
MACRO_WMS ( Min_Flt , Float3 , None )
MACRO_WMS ( Max_Db , Double3 , None )
MACRO_WMS ( Max_Flt , Float3 , None )

// Fround
MACRO_WMS ( Fround_Flt , Float2 , None )
MACRO_WMS ( Fround_Db , Float1Double1 , None )
MACRO_WMS ( Fround_Int , Float1Int1 , None )

MACRO_WMS(Copysign_Db, Double3, None)
MACRO_WMS(Copysign_Flt, Float3, None)

MACRO_EXTEND_WMS(Copysign_Db, Double3, None)
MACRO_EXTEND_WMS(Copysign_Flt, Float3, None)
MACRO_EXTEND_WMS(Trunc_Db, Double2, None)
MACRO_EXTEND_WMS(Trunc_Flt, Float2, None)
MACRO_EXTEND_WMS(Nearest_Db, Double2, None)
Expand Down
6 changes: 4 additions & 2 deletions lib/Runtime/Language/InterpreterHandlerAsmJs.inl
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ EXDEF2 (NOPASMJS , NopEx , Empty
DEF2_WMS( F1toF1Mem , Abs_Flt , ::fabsf )
DEF2_WMS( D2toD1Mem , Atan2_Db , Math::Atan2 )
DEF2_WMS( D2toD1Mem , Min_Db , AsmJsMath::Min<double> )
DEF2_WMS( F2toF1Mem , Min_Flt , AsmJsMath::Min<float> )
DEF2_WMS( D2toD1Mem , Max_Db , AsmJsMath::Max<double> )
DEF2_WMS( F2toF1Mem , Max_Flt , AsmJsMath::Max<float> )

DEF2_WMS( F1toF1Mem , Fround_Flt , (float) )
DEF2_WMS( D1toF1Mem , Fround_Db , (float) )
DEF2_WMS( I1toF1Mem , Fround_Int , (float) )
DEF2_WMS( F2toF1Mem , Copysign_Flt , Wasm::WasmMath::Copysign<float> )
DEF2_WMS( D2toD1Mem , Copysign_Db , Wasm::WasmMath::Copysign<double> )
EXDEF2_WMS( F2toF1Mem , Copysign_Flt , Wasm::WasmMath::Copysign<float> )
EXDEF2_WMS( D2toD1Mem , Copysign_Db , Wasm::WasmMath::Copysign<double> )

EXDEF2_WMS( F1toF1Mem , Trunc_Flt , Wasm::WasmMath::Trunc<float> )
EXDEF2_WMS( F1toF1Mem , Nearest_Flt , Wasm::WasmMath::Nearest<float> )
Expand Down
2 changes: 1 addition & 1 deletion lib/Runtime/Library/JavascriptNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Js
static bool TryToVarFast(int32 nValue, Var* result);
static bool TryToVarFastWithCheck(double value, Var* result);

inline static bool IsNan(double value) { return NumberUtilities::IsNan(value); }
inline static BOOL IsNan(double value) { return NumberUtilities::IsNan(value); }
static bool IsZero(double value);
static BOOL IsNegZero(double value);
static bool IsPosInf(double value);
Expand Down
37 changes: 25 additions & 12 deletions lib/Runtime/Math/AsmJsMath.inl
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,51 @@
//-------------------------------------------------------------------------------------------------------
namespace Js
{

template<typename T>
inline T AsmJsMath::Min(T aLeft, T aRight)
inline T minCheckNan(T aLeft, T aRight)
{
if (NumberUtilities::IsNan(aLeft) || NumberUtilities::IsNan(aRight))
{
return (T)NumberConstants::NaN;
}
return aLeft < aRight ? aLeft : aRight;
}

template<>
inline double AsmJsMath::Min<double>(double aLeft, double aRight)
{
if (NumberUtilities::IsNan(aLeft) || NumberUtilities::IsNan(aRight))
{
return NumberConstants::NaN;
}
return aLeft < aRight ? aLeft : aRight;
return minCheckNan(aLeft, aRight);
}

template<typename T>
inline T AsmJsMath::Max(T aLeft, T aRight)
template<>
inline float AsmJsMath::Min<float>(float aLeft, float aRight)
{
return aLeft > aRight ? aLeft : aRight;
return minCheckNan(aLeft, aRight);
}

template<>
inline double AsmJsMath::Max<double>(double aLeft, double aRight)
template<typename T>
inline T maxCheckNan(T aLeft, T aRight)
{
if (NumberUtilities::IsNan(aLeft) || NumberUtilities::IsNan(aRight))
{
return NumberConstants::NaN;
return (T)NumberConstants::NaN;
}
return aLeft > aRight ? aLeft : aRight;
}

template<>
inline double AsmJsMath::Max<double>(double aLeft, double aRight)
{
return maxCheckNan(aLeft, aRight);
}

template<>
inline float AsmJsMath::Max<float>(float aLeft, float aRight)
{
return maxCheckNan(aLeft, aRight);
}

template<typename T>
inline T AsmJsMath::Add( T aLeft, T aRight )
{
Expand Down
4 changes: 2 additions & 2 deletions lib/WasmReader/WasmBinaryOpCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ WASM_SIMPLE_OPCODE(F32Add, 0x75, ADD_F32, F_FF)
WASM_SIMPLE_OPCODE(F32Sub, 0x76, SUB_F32, F_FF)
WASM_SIMPLE_OPCODE(F32Mul, 0x77, MUL_F32, F_FF)
WASM_SIMPLE_OPCODE(F32DIv, 0x78, DIV_F32, F_FF)
WASM_SIMPLE_OPCODE(F32Min, 0x79, NYI, F_FF)
WASM_SIMPLE_OPCODE(F32Max, 0x7a, NYI, F_FF)
WASM_SIMPLE_OPCODE(F32Min, 0x79, MIN_F32, F_FF)
WASM_SIMPLE_OPCODE(F32Max, 0x7a, MAX_F32, F_FF)
WASM_SIMPLE_OPCODE(F32Abs, 0x7b, ABS_F32, F_F)
WASM_SIMPLE_OPCODE(F32Neg, 0x7c, NEG_F32, F_F)
WASM_SIMPLE_OPCODE(F32CopySign, 0x7d, COPYSIGN_F32, F_FF)
Expand Down
4 changes: 2 additions & 2 deletions lib/WasmReader/WasmKeywords.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ WASM_KEYWORD_BIN_MATH_I(ROL, rotl, Rol_Int)

WASM_KEYWORD_BIN_MATH_FD(DIV, div, Div)
WASM_KEYWORD_BIN_MATH_FD(COPYSIGN, copysign, Copysign)
WASM_KEYWORD_BIN_MATH_FD(MIN, min, Min)
WASM_KEYWORD_BIN_MATH_FD(MAX, max, Max)

WASM_KEYWORD_BIN_MATH_D(MOD, mod, Rem_Db)
WASM_KEYWORD_BIN_MATH_D(MIN, min, Min_Db)
WASM_KEYWORD_BIN_MATH_D(MAX, max, Max_Db)


// compare ops
Expand Down
9 changes: 9 additions & 0 deletions test/wasm/f32.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
11
11.010000228881836
NaN
NaN
NaN
Infinity
NaN
NaN
NaN
18 changes: 18 additions & 0 deletions test/wasm/f32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

const blob = WScript.LoadBinaryFile('f32.wasm');
const moduleBytesView = new Uint8Array(blob);
var a = Wasm.instantiateModule(moduleBytesView, {}).exports;
print(a.min(11, 11.01)); // 11
print(a.max(11, 11.01)); // 11.010000228881836
print(a.min(NaN, 11.01)); // NaN
print(a.max(NaN, 11.01)); // NaN
print(a.min(11, NaN)); // NaN
print(a.max(1/0, 11.01)); // Infinity
print(a.max(11.01, 0/0)); // NaN
print(a.max(0/0, 11.01)); // NaN
print(a.max(NaN, -NaN)); // NaN

Binary file added test/wasm/f32.wasm
Binary file not shown.
15 changes: 15 additions & 0 deletions test/wasm/f32.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;;-------------------------------------------------------------------------------------------------------
;; Copyright (C) Microsoft Corporation and contributors. All rights reserved.
;; Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
;;-------------------------------------------------------------------------------------------------------

(module
(func (param f32) (param f32) (result f32)
(return (f32.min (get_local 0) (get_local 1)))
)
(func (param f32) (param f32) (result f32)
(return (f32.max (get_local 0) (get_local 1)))
)
(export "min" 0)
(export "max" 1)
)
7 changes: 7 additions & 0 deletions test/wasm/rlexe.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
<compile-flags>-on:wasm</compile-flags>
</default>
</test>
<test>
<default>
<files>f32.js</files>
<baseline>f32.baseline</baseline>
<compile-flags>-on:Wasm</compile-flags>
</default>
</test>
</regress-exe>