Skip to content

Commit a5962dc

Browse files
committed
Wasm: f32 min and max codegen
1 parent 5bbcc4e commit a5962dc

File tree

16 files changed

+123
-40
lines changed

16 files changed

+123
-40
lines changed

lib/Backend/IRBuilderAsmJs.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,6 +2921,14 @@ IRBuilderAsmJs::BuildFloat3(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlo
29212921
instr = IR::Instr::New(Js::OpCode::Copysign_A, dstOpnd, src1Opnd, src2Opnd, m_func);
29222922
break;
29232923

2924+
case Js::OpCodeAsmJs::Min_Flt:
2925+
instr = IR::Instr::New(Js::OpCode::InlineMathMin, dstOpnd, src1Opnd, src2Opnd, m_func);
2926+
break;
2927+
2928+
case Js::OpCodeAsmJs::Max_Flt:
2929+
instr = IR::Instr::New(Js::OpCode::InlineMathMax, dstOpnd, src1Opnd, src2Opnd, m_func);
2930+
break;
2931+
29242932
default:
29252933
Assume(UNREACHED);
29262934
}

lib/Backend/Lower.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13557,9 +13557,9 @@ IR::BranchInstr *Lowerer::InsertCompareBranch(
1355713557

1355813558
Func *const func = insertBeforeInstr->m_func;
1355913559

13560-
if(compareSrc1->IsFloat64())
13560+
if(compareSrc1->IsFloat())
1356113561
{
13562-
Assert(compareSrc2->IsFloat64());
13562+
Assert(compareSrc2->IsFloat());
1356313563
Assert(!isUnsigned);
1356413564
IR::BranchInstr *const instr = IR::BranchInstr::New(branchOpCode, target, compareSrc1, compareSrc2, func);
1356513565
insertBeforeInstr->InsertBefore(instr);

lib/Backend/LowerMDShared.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9274,41 +9274,40 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
92749274
instr->InsertBefore(branchInstr);
92759275
LowererMDArch::EmitInt4Instr(branchInstr);
92769276
}
9277-
// MOV dst, src1
9278-
this->m_lowerer->InsertMove(dst, src1, instr);
9277+
// MOV dst, src1
9278+
this->m_lowerer->InsertMove(dst, src1, instr);
92799279
}
9280-
9281-
else if(dst->IsFloat64())
9280+
else if(dst->IsFloat())
92829281
{
9283-
// COMISD src1 (src2), src2 (src1)
9282+
// COMISD/COMISS src1 (src2), src2 (src1)
92849283
// JA $doneLabel
92859284
// JEQ $labelNegZeroAndNaNCheckHelper
9286-
// MOVSD dst, src2
9285+
// MOVSD/MOVSS dst, src2
92879286
// JMP $doneLabel
92889287
//
92899288
// $labelNegZeroAndNaNCheckHelper
92909289
// JP $labelNaNHelper
92919290
// if(min)
92929291
// {
92939292
// if(src2 == -0.0)
9294-
// MOVSD dst, src2
9293+
// MOVSD/MOVSS dst, src2
92959294
// }
92969295
// else
92979296
// {
92989297
// if(src1 == -0.0)
9299-
// MOVSD dst, src2
9298+
// MOVSD/MOVSS dst, src2
93009299
// }
93019300
// JMP $doneLabel
93029301
//
93039302
// $labelNaNHelper
9304-
// MOVSD dst, NaN
9303+
// MOVSD/MOVSS dst, NaN
93059304
//
93069305
// $doneLabel
93079306

9308-
//MOVSD dst, src1;
9307+
//MOVSD/MOVSS dst, src1;
93099308
Assert(!dst->IsEqual(src1));
9310-
this->m_lowerer->InsertMove(dst, src1, instr);
93119309

9310+
this->m_lowerer->InsertMove(dst, src1, instr);
93129311
if(min)
93139312
{
93149313
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
@@ -9343,7 +9342,17 @@ void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMeth
93439342
instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));
93449343

93459344
instr->InsertBefore(labelNaNHelper);
9346-
IR::Opnd * opndNaN = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_Nan), IRType::TyFloat64, this->m_func);
9345+
IR::Opnd * opndNaN = nullptr;
9346+
9347+
if (dst->IsFloat32())
9348+
{
9349+
opndNaN = IR::MemRefOpnd::New((float*)&(Js::JavascriptNumber::k_Nan32), IRType::TyFloat32, this->m_func);
9350+
}
9351+
else
9352+
{
9353+
opndNaN = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_Nan), IRType::TyFloat64, this->m_func);
9354+
}
9355+
93479356
this->m_lowerer->InsertMove(dst, opndNaN, instr);
93489357
}
93499358
instr->InsertBefore(doneLabel);

lib/Common/Common/NumberUtilitiesBase.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//-------------------------------------------------------------------------------------------------------
2-
// Copyright (C) Microsoft. All rights reserved.
2+
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
33
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
44
//-------------------------------------------------------------------------------------------------------
55
//////////////////////////////////////////////////////////
@@ -13,6 +13,7 @@ namespace Js
1313
{
1414
public:
1515
static const UINT64 k_Nan = 0xFFF8000000000000ull;
16+
static const UINT32 k_Nan32 = 0x7FFF8000ul;
1617
};
1718

1819
class NumberUtilitiesBase

lib/Runtime/ByteCode/OpCodes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,9 @@ MACRO_BACKEND_ONLY( SlotArrayCheck, Empty, OpCanCSE)
752752
MACRO_BACKEND_ONLY( FrameDisplayCheck, Empty, OpCanCSE)
753753
MACRO_EXTEND( BeginBodyScope, Empty, OpSideEffect)
754754

755-
MACRO_BACKEND_ONLY( Copysign_A, Empty, OpTempNumberSources | OpCanCSE | OpProducesNumber)
756-
MACRO_BACKEND_ONLY( Trunc_A, Empty, OpTempNumberSources | OpCanCSE | OpProducesNumber)
757-
MACRO_BACKEND_ONLY( Nearest_A, Empty, OpTempNumberSources | OpCanCSE | OpProducesNumber)
755+
MACRO_BACKEND_ONLY( Copysign_A, Empty, OpTempNumberSources|OpCanCSE|OpProducesNumber)
756+
MACRO_BACKEND_ONLY( Trunc_A, Empty, OpTempNumberSources|OpCanCSE|OpProducesNumber)
757+
MACRO_BACKEND_ONLY( Nearest_A, Empty, OpTempNumberSources|OpCanCSE|OpProducesNumber)
758758

759759
// All SIMD ops are backend only for non-asmjs.
760760
#define MACRO_SIMD(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr, ...) MACRO_BACKEND_ONLY(opcode, Empty, OpCodeAttr)

lib/Runtime/ByteCode/OpCodesAsmJs.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,17 @@ MACRO_WMS ( Sqrt_Flt , Float2 , None )
215215
MACRO_WMS ( Abs_Flt , Float2 , None )
216216
MACRO_WMS ( Atan2_Db , Double3 , None )
217217
MACRO_WMS ( Min_Db , Double3 , None )
218+
MACRO_WMS ( Min_Flt , Float3 , None )
218219
MACRO_WMS ( Max_Db , Double3 , None )
220+
MACRO_WMS ( Max_Flt , Float3 , None )
219221

220222
// Fround
221223
MACRO_WMS ( Fround_Flt , Float2 , None )
222224
MACRO_WMS ( Fround_Db , Float1Double1 , None )
223225
MACRO_WMS ( Fround_Int , Float1Int1 , None )
224226

225-
MACRO_WMS(Copysign_Db, Double3, None)
226-
MACRO_WMS(Copysign_Flt, Float3, None)
227-
227+
MACRO_EXTEND_WMS(Copysign_Db, Double3, None)
228+
MACRO_EXTEND_WMS(Copysign_Flt, Float3, None)
228229
MACRO_EXTEND_WMS(Trunc_Db, Double2, None)
229230
MACRO_EXTEND_WMS(Trunc_Flt, Float2, None)
230231
MACRO_EXTEND_WMS(Nearest_Db, Double2, None)

lib/Runtime/Language/InterpreterHandlerAsmJs.inl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,15 @@ EXDEF2 (NOPASMJS , NopEx , Empty
164164
DEF2_WMS( F1toF1Mem , Abs_Flt , ::fabsf )
165165
DEF2_WMS( D2toD1Mem , Atan2_Db , Math::Atan2 )
166166
DEF2_WMS( D2toD1Mem , Min_Db , AsmJsMath::Min<double> )
167+
DEF2_WMS( F2toF1Mem , Min_Flt , AsmJsMath::Min<float> )
167168
DEF2_WMS( D2toD1Mem , Max_Db , AsmJsMath::Max<double> )
169+
DEF2_WMS( F2toF1Mem , Max_Flt , AsmJsMath::Max<float> )
168170

169171
DEF2_WMS( F1toF1Mem , Fround_Flt , (float) )
170172
DEF2_WMS( D1toF1Mem , Fround_Db , (float) )
171173
DEF2_WMS( I1toF1Mem , Fround_Int , (float) )
172-
DEF2_WMS( F2toF1Mem , Copysign_Flt , Wasm::WasmMath::Copysign<float> )
173-
DEF2_WMS( D2toD1Mem , Copysign_Db , Wasm::WasmMath::Copysign<double> )
174+
EXDEF2_WMS( F2toF1Mem , Copysign_Flt , Wasm::WasmMath::Copysign<float> )
175+
EXDEF2_WMS( D2toD1Mem , Copysign_Db , Wasm::WasmMath::Copysign<double> )
174176

175177
EXDEF2_WMS( F1toF1Mem , Trunc_Flt , Wasm::WasmMath::Trunc<float> )
176178
EXDEF2_WMS( F1toF1Mem , Nearest_Flt , Wasm::WasmMath::Nearest<float> )

lib/Runtime/Library/JavascriptNumber.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace Js
4848
static bool TryToVarFast(int32 nValue, Var* result);
4949
static bool TryToVarFastWithCheck(double value, Var* result);
5050

51-
inline static bool IsNan(double value) { return NumberUtilities::IsNan(value); }
51+
inline static BOOL IsNan(double value) { return NumberUtilities::IsNan(value); }
5252
static bool IsZero(double value);
5353
static BOOL IsNegZero(double value);
5454
static bool IsPosInf(double value);

lib/Runtime/Math/AsmJsMath.inl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,51 @@
44
//-------------------------------------------------------------------------------------------------------
55
namespace Js
66
{
7+
78
template<typename T>
8-
inline T AsmJsMath::Min(T aLeft, T aRight)
9+
inline T minCheckNan(T aLeft, T aRight)
910
{
11+
if (NumberUtilities::IsNan(aLeft) || NumberUtilities::IsNan(aRight))
12+
{
13+
return (T)NumberConstants::NaN;
14+
}
1015
return aLeft < aRight ? aLeft : aRight;
1116
}
1217

1318
template<>
1419
inline double AsmJsMath::Min<double>(double aLeft, double aRight)
1520
{
16-
if (NumberUtilities::IsNan(aLeft) || NumberUtilities::IsNan(aRight))
17-
{
18-
return NumberConstants::NaN;
19-
}
20-
return aLeft < aRight ? aLeft : aRight;
21+
return minCheckNan(aLeft, aRight);
2122
}
2223

23-
template<typename T>
24-
inline T AsmJsMath::Max(T aLeft, T aRight)
24+
template<>
25+
inline float AsmJsMath::Min<float>(float aLeft, float aRight)
2526
{
26-
return aLeft > aRight ? aLeft : aRight;
27+
return minCheckNan(aLeft, aRight);
2728
}
2829

29-
template<>
30-
inline double AsmJsMath::Max<double>(double aLeft, double aRight)
30+
template<typename T>
31+
inline T maxCheckNan(T aLeft, T aRight)
3132
{
3233
if (NumberUtilities::IsNan(aLeft) || NumberUtilities::IsNan(aRight))
3334
{
34-
return NumberConstants::NaN;
35+
return (T)NumberConstants::NaN;
3536
}
3637
return aLeft > aRight ? aLeft : aRight;
3738
}
3839

40+
template<>
41+
inline double AsmJsMath::Max<double>(double aLeft, double aRight)
42+
{
43+
return maxCheckNan(aLeft, aRight);
44+
}
45+
46+
template<>
47+
inline float AsmJsMath::Max<float>(float aLeft, float aRight)
48+
{
49+
return maxCheckNan(aLeft, aRight);
50+
}
51+
3952
template<typename T>
4053
inline T AsmJsMath::Add( T aLeft, T aRight )
4154
{

lib/WasmReader/WasmBinaryOpCodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ WASM_SIMPLE_OPCODE(F32Add, 0x75, ADD_F32, F_FF)
175175
WASM_SIMPLE_OPCODE(F32Sub, 0x76, SUB_F32, F_FF)
176176
WASM_SIMPLE_OPCODE(F32Mul, 0x77, MUL_F32, F_FF)
177177
WASM_SIMPLE_OPCODE(F32DIv, 0x78, DIV_F32, F_FF)
178-
WASM_SIMPLE_OPCODE(F32Min, 0x79, NYI, F_FF)
179-
WASM_SIMPLE_OPCODE(F32Max, 0x7a, NYI, F_FF)
178+
WASM_SIMPLE_OPCODE(F32Min, 0x79, MIN_F32, F_FF)
179+
WASM_SIMPLE_OPCODE(F32Max, 0x7a, MAX_F32, F_FF)
180180
WASM_SIMPLE_OPCODE(F32Abs, 0x7b, ABS_F32, F_F)
181181
WASM_SIMPLE_OPCODE(F32Neg, 0x7c, NEG_F32, F_F)
182182
WASM_SIMPLE_OPCODE(F32CopySign, 0x7d, COPYSIGN_F32, F_FF)

0 commit comments

Comments
 (0)