Skip to content

Commit

Permalink
1. Add unit case for non-default rounding mode
Browse files Browse the repository at this point in the history
2. removed round-to-even, the default option from InsOpts as it will be handled on the default path.
  • Loading branch information
Ruihan-Yin committed Jan 2, 2024
1 parent 3973035 commit 07b7d21
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/jit/emit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ class emitter
void idSetEvexbContext(insOpts instOptions)
{
assert(_idEvexbContext == 0);
if (instOptions == INS_OPTS_EVEX_eb_er_rn)
if (instOptions == INS_OPTS_EVEX_eb_er_rd)
{
_idEvexbContext = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/emitxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ insOpts emitter::GetEmbRoundingMode(uint8_t mode) const
switch (mode)
{
case 1:
return INS_OPTS_EVEX_er_rd;
return INS_OPTS_EVEX_eb_er_rd;
case 2:
return INS_OPTS_EVEX_er_ru;
case 3:
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/emitxarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ code_t AddSimdPrefixIfNeeded(const instrDesc* id, code_t code, emitAttr size)
// instOptions - emit options
void SetEvexBroadcastIfNeeded(instrDesc* id, insOpts instOptions)
{
if((instOptions & INS_OPTS_b_MASK) == INS_OPTS_EVEX_eb_er_rn)
if((instOptions & INS_OPTS_b_MASK) == INS_OPTS_EVEX_eb_er_rd)
{
assert(UseEvexEncoding());
id->idSetEvexbContext(instOptions);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/instr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ void CodeGen::inst_RV_RV_TT(
bool IsEmbBroadcast = CodeGenInterface::IsEmbeddedBroadcastEnabled(ins, op2);
if (IsEmbBroadcast)
{
instOptions = INS_OPTS_EVEX_eb_er_rn;
instOptions = INS_OPTS_EVEX_eb_er_rd;
if (emitter::IsBitwiseInstruction(ins) && varTypeIsLong(op2->AsHWIntrinsic()->GetSimdBaseType()))
{
switch (ins)
Expand Down
10 changes: 4 additions & 6 deletions src/coreclr/jit/instr.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,13 @@ enum insOpts: unsigned
{
INS_OPTS_NONE = 0,

INS_OPTS_EVEX_eb_er_rn = 1, // Round to even
INS_OPTS_EVEX_eb_er_rd = 1, // Embedded Broadcast or Round down

INS_OPTS_EVEX_er_rd = 2, // Round down
INS_OPTS_EVEX_er_ru = 2, // Round up

INS_OPTS_EVEX_er_ru = 3, // Round up
INS_OPTS_EVEX_er_rz = 3, // Round towards zero

INS_OPTS_EVEX_er_rz = 4, // Round towards zero

INS_OPTS_b_MASK = (INS_OPTS_EVEX_eb_er_rn | INS_OPTS_EVEX_er_rd | INS_OPTS_EVEX_er_ru | INS_OPTS_EVEX_er_rz), // mask for Evex.b related features.
INS_OPTS_b_MASK = (INS_OPTS_EVEX_eb_er_rd | INS_OPTS_EVEX_er_ru | INS_OPTS_EVEX_er_rz), // mask for Evex.b related features.
};

#elif defined(TARGET_ARM) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,62 @@ public partial class Program
public static unsafe void AddEmbeddedRounding()
{
int testResult = 1;
ulong[] answerTable_ToNegativeInfinity = new ulong[8] {0x3fe0000000000000, 0x3fe0000000000000, 0x3fdfffffffffffff, 0x3fe0000000000000,
0xbfe0000000000000, 0xbfe0000000000000, 0xbfe0000000000000, 0xbfe0000000000001};
ulong[] answerTable_ToPositiveInfinity = new ulong[8] {0x3fe0000000000001, 0x3fe0000000000001, 0x3fe0000000000000, 0x3fe0000000000000,
0xbfe0000000000000, 0xbfe0000000000000, 0xbfdfffffffffffff, 0xbfe0000000000000};
ulong[] answerTable_ToZero = new ulong[8] {0x3fe0000000000000, 0x3fe0000000000000, 0x3fdfffffffffffff, 0x3fe0000000000000,
0xbfe0000000000000, 0xbfe0000000000000, 0xbfdfffffffffffff, 0xbfe0000000000000};

if (Avx512F.IsSupported)
{
using (TestTable_2Input<double> doubleTable = new TestTable_2Input<double>(new double[8] { 1, -5, 100, 0, 1, -5, 100, 0 }, new double[8] { 1, 1, 50, 0, 1, 1, 50, 0 }, new double[8]))
using (TestTable_2Input<double> doubleTable = new TestTable_2Input<double>(new double[8] { 0.05 , 0.10, 0.15, 0.20, -0.25, -0.30, -0.35, -0.40 }, new double[8] { 0.45 , 0.40, 0.35, 0.30, -0.25, -0.20, -0.15, -0.10 }, new double[8]))
{

var vd1 = Unsafe.Read<Vector512<double>>(doubleTable.inArray1Ptr);
var vd2 = Unsafe.Read<Vector512<double>>(doubleTable.inArray2Ptr);
var vd3 = Avx512F.Add(vd1, vd2, FloatRoundingMode.ToEven);
var vd3 = Avx512F.Add(vd1, vd2, FloatRoundingMode.ToNegativeInfinity);
Unsafe.Write(doubleTable.outArrayPtr, vd3);


for (int i = 0; i < doubleTable.outArray.Length; i++)
{
if (BitConverter.DoubleToUInt64Bits(doubleTable.outArray[i]) != answerTable_ToNegativeInfinity[i])
{
Console.WriteLine("Avx512 Add Embedded rounding failed on double with ToNegativeInfinity:");
foreach (var item in doubleTable.outArray)
{
Console.Write(item + ", ");
}
Console.WriteLine();
Assert.Fail("");
}
}

vd3 = Avx512F.Add(vd1, vd2, FloatRoundingMode.ToPositiveInfinity);
Unsafe.Write(doubleTable.outArrayPtr, vd3);

for (int i = 0; i < doubleTable.outArray.Length; i++)
{
if (BitConverter.DoubleToUInt64Bits(doubleTable.outArray[i]) != answerTable_ToPositiveInfinity[i])
{
Console.WriteLine("Avx512 Add Embedded rounding failed on double with ToPositiveInfinity:");
foreach (var item in doubleTable.outArray)
{
Console.Write(item + ", ");
}
Console.WriteLine();
Assert.Fail("");
}
}

vd3 = Avx512F.Add(vd1, vd2, FloatRoundingMode.ToZero);
Unsafe.Write(doubleTable.outArrayPtr, vd3);

for (int i = 0; i < doubleTable.outArray.Length; i++)
{
if (BitConverter.DoubleToInt64Bits(doubleTable.outArray[i]) != BitConverter.DoubleToInt64Bits(doubleTable.inArray1[i] + doubleTable.inArray2[i]))
if (BitConverter.DoubleToUInt64Bits(doubleTable.outArray[i]) != answerTable_ToZero[i])
{
Console.WriteLine("Avx512 Add Embedded rounding failed on double:");
Console.WriteLine("Avx512 Add Embedded rounding failed on double with ToZero:");
foreach (var item in doubleTable.outArray)
{
Console.Write(item + ", ");
Expand Down

0 comments on commit 07b7d21

Please sign in to comment.