Skip to content

Commit

Permalink
sha* supports apx
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Dec 20, 2023
1 parent c976558 commit e690a2a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
28 changes: 20 additions & 8 deletions gen/gen_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,14 +1402,6 @@ void put()

{ 0x2E, "ucomisd", T_0F | T_66 | T_EVEX | T_EW1 | T_SAE_X | T_N8, false, 2 },
{ 0x2E, "ucomiss", T_0F | T_EVEX | T_EW0 | T_SAE_X | T_N4, false, 2 },

{ 0xCC, "sha1rnds4", T_0F3A, true, 1 },
{ 0xC8, "sha1nexte", T_0F38, false, 1 },
{ 0xC9, "sha1msg1", T_0F38, false, 1 },
{ 0xCA, "sha1msg2", T_0F38, false, 1 },
{ 0xCB, "sha256rnds2", T_0F38, false, 1 },
{ 0xCC, "sha256msg1", T_0F38, false, 1 },
{ 0xCD, "sha256msg2", T_0F38, false, 1 },
};
for (size_t i = 0; i < NUM_OF_ARRAY(tbl); i++) {
const Tbl *p = &tbl[i];
Expand All @@ -1425,6 +1417,26 @@ void put()
}
}
}
// sha
{
const struct Tbl {
uint8_t code;
uint8_t code2;
const char *name;
} tbl[] = {
{ 0xC8, 0xD8, "sha1nexte" },
{ 0xC9, 0xD9, "sha1msg1" },
{ 0xCA, 0xDA, "sha1msg2" },
{ 0xCB, 0xDB, "sha256rnds2" },
{ 0xCC, 0xDC, "sha256msg1" },
{ 0xCD, 0xDD, "sha256msg2" },
};
for (size_t i = 0; i < NUM_OF_ARRAY(tbl); i++) {
const Tbl *p = &tbl[i];
printf("void %s(const Xmm& x, const Operand& op) { opSHA(x, op, T_0F38, 0x%02X, 0x%02X); }\n", p->name, p->code, p->code2);
}
puts("void sha1rnds4(const Xmm& x, const Operand& op, uint8_t imm) { opSHA(x, op, T_0F3A, 0xCC, 0xD4, imm); }");
}
// (m, x), (m, y)
{
const struct Tbl {
Expand Down
28 changes: 28 additions & 0 deletions test/apx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1870,3 +1870,31 @@ CYBOZU_TEST_AUTO(encodekey)
CYBOZU_TEST_EQUAL_ARRAY(c.getCode(), tbl, n);
}

CYBOZU_TEST_AUTO(sha)
{
struct Code : Xbyak::CodeGenerator {
Code()
{
sha1msg1(xmm15, ptr [r30+r29*8+0x12]);
sha1msg2(xmm15, ptr [r30+r29*8+0x12]);
sha1nexte(xmm15, ptr [r30+r29*8+0x12]);
sha256msg1(xmm15, ptr [r30+r29*8+0x12]);
sha256msg2(xmm15, ptr [r30+r29*8+0x12]);
sha256rnds2(xmm15, ptr [r30+r29*8+0x12]);
sha1rnds4(xmm15, ptr [r30+r29*8+0x12], 0x23);
}
} c;
const uint8_t tbl[] = {
0x62, 0x1c, 0x78, 0x08, 0xd9, 0x7c, 0xee, 0x12,
0x62, 0x1c, 0x78, 0x08, 0xda, 0x7c, 0xee, 0x12,
0x62, 0x1c, 0x78, 0x08, 0xd8, 0x7c, 0xee, 0x12,
0x62, 0x1c, 0x78, 0x08, 0xdc, 0x7c, 0xee, 0x12,
0x62, 0x1c, 0x78, 0x08, 0xdd, 0x7c, 0xee, 0x12,
0x62, 0x1c, 0x78, 0x08, 0xdb, 0x7c, 0xee, 0x12,
0x62, 0x1c, 0x78, 0x08, 0xd4, 0x7c, 0xee, 0x12, 0x23,
};
const size_t n = sizeof(tbl);
CYBOZU_TEST_EQUAL(c.getSize(), n);
CYBOZU_TEST_EQUAL_ARRAY(c.getCode(), tbl, n);
}

12 changes: 12 additions & 0 deletions xbyak/xbyak.h
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,14 @@ class CodeGenerator : public CodeArray {
}
opROO(Reg(), r2, r1, T_MUST_EVEX|T_F3, code2);
}
void opSHA(const Xmm& x, const Operand& op, uint64_t type, uint8_t code1, uint8_t code2, int imm = NONE)
{
if (x.getIdx() <= 15 && op.hasRex2() && opROO(Reg(), op, x, T_MUST_EVEX, code2, imm != NONE ? 1 : 0)) {
if (imm != NONE) db(imm);
return;
}
opSSE(x, op, type, code1, isXMM_XMMorMEM, imm);
}
public:
unsigned int getVersion() const { return VERSION; }
using CodeArray::db;
Expand Down Expand Up @@ -3139,6 +3147,10 @@ class CodeGenerator : public CodeArray {
// set default encoding to select Vex or Evex
void setDefaultEncoding(PreferredEncoding encoding) { defaultEncoding_ = encoding; }

void sha1msg12(const Xmm& x, const Operand& op)
{
opROO(Reg(), op, x, T_MUST_EVEX, 0xD9);
}
/*
use single byte nop if useMultiByteNop = false
*/
Expand Down
14 changes: 7 additions & 7 deletions xbyak/xbyak_mnemonic.h
Original file line number Diff line number Diff line change
Expand Up @@ -988,13 +988,13 @@ void setpo(const Operand& op) { if (opROO(Reg(), op, Reg(), T_APX|T_ZU|T_F2, 0x4
void sets(const Operand& op) { if (opROO(Reg(), op, Reg(), T_APX|T_ZU|T_F2, 0x40 | 8)) return; opRext(op, 8, 0, T_0F, 0x90 | 8); }//-V524
void setz(const Operand& op) { if (opROO(Reg(), op, Reg(), T_APX|T_ZU|T_F2, 0x40 | 4)) return; opRext(op, 8, 0, T_0F, 0x90 | 4); }//-V524
void sfence() { db(0x0F); db(0xAE); db(0xF8); }
void sha1msg1(const Xmm& xmm, const Operand& op) { opSSE(xmm, op, T_0F38, 0xC9, isXMM_XMMorMEM, NONE); }
void sha1msg2(const Xmm& xmm, const Operand& op) { opSSE(xmm, op, T_0F38, 0xCA, isXMM_XMMorMEM, NONE); }
void sha1nexte(const Xmm& xmm, const Operand& op) { opSSE(xmm, op, T_0F38, 0xC8, isXMM_XMMorMEM, NONE); }
void sha1rnds4(const Xmm& xmm, const Operand& op, uint8_t imm) { opSSE(xmm, op, T_0F3A, 0xCC, isXMM_XMMorMEM, imm); }
void sha256msg1(const Xmm& xmm, const Operand& op) { opSSE(xmm, op, T_0F38, 0xCC, isXMM_XMMorMEM, NONE); }
void sha256msg2(const Xmm& xmm, const Operand& op) { opSSE(xmm, op, T_0F38, 0xCD, isXMM_XMMorMEM, NONE); }
void sha256rnds2(const Xmm& xmm, const Operand& op) { opSSE(xmm, op, T_0F38, 0xCB, isXMM_XMMorMEM, NONE); }
void sha1msg1(const Xmm& x, const Operand& op) { opSHA(x, op, T_0F38, 0xC9, 0xD9); }
void sha1msg2(const Xmm& x, const Operand& op) { opSHA(x, op, T_0F38, 0xCA, 0xDA); }
void sha1nexte(const Xmm& x, const Operand& op) { opSHA(x, op, T_0F38, 0xC8, 0xD8); }
void sha1rnds4(const Xmm& x, const Operand& op, uint8_t imm) { opSHA(x, op, T_0F3A, 0xCC, 0xD4, imm); }
void sha256msg1(const Xmm& x, const Operand& op) { opSHA(x, op, T_0F38, 0xCC, 0xDC); }
void sha256msg2(const Xmm& x, const Operand& op) { opSHA(x, op, T_0F38, 0xCD, 0xDD); }
void sha256rnds2(const Xmm& x, const Operand& op) { opSHA(x, op, T_0F38, 0xCB, 0xDB); }
void shl(const Operand& op, const Reg8& _cl) { opShift(op, _cl, 12); }
void shl(const Operand& op, int imm) { opShift(op, imm, 12); }
void shl(const Reg& d, const Operand& op, const Reg8& _cl) { opShift(op, _cl, 12, &d); }
Expand Down

0 comments on commit e690a2a

Please sign in to comment.