Merge pull request #669 from adriadam10/fix/dot2-f16-hw-rounding

fix(shader): match v_dot2_f32_f16 and v_dot2c_f32_f16 to hardware
This commit is contained in:
Dean
2026-10-06 12:25:50 +13:00
committed by GitHub
8 changed files with 217 additions and 91 deletions
@@ -9,7 +9,6 @@
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <span>
#include <string>
#include <vector>
@@ -18,52 +17,94 @@ namespace {
using AgcDriver::Graphics::Require;
using ShaderRecompiler::ShaderStage;
constexpr std::uint32_t Threads = 64;
constexpr std::uint32_t Threads = 32;
constexpr std::uint32_t Inputs = 4;
constexpr std::uint32_t Results = 8;
constexpr std::uint32_t Results = 16;
alignas(256) std::array<std::uint32_t, Threads * Inputs> Input{};
alignas(256) std::array<std::uint32_t, Threads * Results> Output{};
alignas(256) constexpr std::array<std::uint32_t, 26> DotCode{
0x34020084, 0x34060085, 0xe0301000, 0x80000401, 0xe0301004, 0x80000501, 0xe0301008, 0x80000601,
0xbf8c3f70, 0xcc13400a, 0x1c1a0b04, 0xcc13480b, 0x141a0b04, 0xcc13420c, 0xbc1a0b04, 0xcc13c00d,
0x1c1a0b04, 0xe0701000, 0x80010a03, 0xe0701004, 0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c,
0x80010d03, 0xbf810000,
alignas(256) constexpr std::array<std::uint32_t, 37> Code{
0x34020084, 0x34060086, 0xe0381000, 0x80000401, 0xbf8c3f70, 0x7e140306, 0x04140b04, 0xcc13400b,
0x1c1a0b04, 0xcc13420c, 0xbc1a0b04, 0xcc13410d, 0x5c1a0b04, 0xcc13480e, 0x141a0b04, 0xcc13c00f,
0x1c1a0b04, 0x7e200306, 0x04200af2, 0x7e220306, 0x04220aff, 0x40003c00, 0x7e240306, 0x04240afa,
0xff00b104, 0x7e260306, 0xcc135013, 0x0c1a0b04, 0xe0781000, 0x80010a03, 0xe0781010, 0x80010e03,
0xe0781020, 0x80011203, 0xe0781030, 0x80011603, 0xbf810000,
};
constexpr std::array<std::array<std::uint32_t, 3>, 8> Edges{{
{0x3c003c00u, 0x3c003c00u, 0x00000000u}, {0x7c000000u, 0x3c000000u, 0x3f800000u}, {0x7c003c00u, 0x00003c00u, 0x3f800000u},
{0xfc007c00u, 0x3c003c00u, 0x40000000u}, {0x80000000u, 0x00008000u, 0x80000000u}, {0x7bff7bffu, 0x7bff7bffu, 0xcf800000u},
{0x38003800u, 0xb800b800u, 0x3e800000u}, {0x7e003c00u, 0x3c003c00u, 0x00000000u},
}};
constexpr std::uint32_t Rows[32][4] = {
{0x0001bc00u, 0x81bfacbau, 0xc541aa2fu, 0x00000000u},
{0x01f61a79u, 0x3c000381u, 0xc1729fffu, 0x00000000u},
{0x02c9be80u, 0xb252213du, 0x41880526u, 0x00000000u},
{0x032f030cu, 0x0d88ca10u, 0x3f800000u, 0x00000000u},
{0x04003848u, 0x2427b61cu, 0x3920d8c0u, 0x00000000u},
{0x04008190u, 0x01c85a38u, 0xc1e5655cu, 0x00000000u},
{0x00ba3faeu, 0x02bcc61fu, 0x439ff26au, 0x00000000u},
{0x00bc0400u, 0xaed23800u, 0xaf78b10fu, 0x00000000u},
{0x47a37c85u, 0x7ceb0000u, 0x80521c95u, 0x00000000u},
{0x6bc64fd7u, 0x02f45f4du, 0xffc8ab86u, 0x00000000u},
{0x034102d0u, 0xac427d00u, 0x7fa00000u, 0x00000000u},
{0x00000000u, 0xf35dfc00u, 0x805c769au, 0x00000000u},
{0x0000030cu, 0x8370c506u, 0x00000001u, 0x00000000u},
{0x00002783u, 0x5f2c0896u, 0x7f800000u, 0x00000000u},
{0x00010001u, 0xfc00004cu, 0xca63705au, 0x00000000u},
{0x64ba817bu, 0x80000000u, 0x00000001u, 0x00000000u},
{0x000083ffu, 0x7d00def6u, 0x95a99701u, 0x00000000u},
{0x7c210c47u, 0x825c8000u, 0x45b66cc3u, 0x00000000u},
{0x00003134u, 0x8000c266u, 0x8029d2f0u, 0x00000000u},
{0x3b898000u, 0x0000e8b9u, 0x0034ccdcu, 0x00000000u},
{0x00000000u, 0x04008000u, 0x5f834da7u, 0x00000000u},
{0x00000001u, 0x366e51ddu, 0x7f7fffffu, 0x00000000u},
{0x00000001u, 0xdc472c9du, 0x42750eeeu, 0x00000000u},
{0x000000c8u, 0x83ff3c00u, 0x43380747u, 0x00000000u},
{0x0000010cu, 0x7c00f56fu, 0x005df72bu, 0x00000000u},
{0x00000377u, 0xdaabea88u, 0xf540cddbu, 0x00000000u},
{0x000003cau, 0x5b9f7e00u, 0xcaba68d5u, 0x00000000u},
{0x00000400u, 0x785b8ab8u, 0x7fa00000u, 0x00000000u},
{0x0000051cu, 0x6f115c7fu, 0x0047beccu, 0x00000000u},
{0x00000c55u, 0x3c00ec7fu, 0x263e4496u, 0x00000000u},
{0x00000f28u, 0x9e2d0123u, 0x565f49ebu, 0x00000000u},
{0x00001209u, 0x3fe3c10fu, 0x801b0c3eu, 0x00000000u}
};
float Half(std::uint32_t bits) {
const std::uint32_t sign = (bits & 0x8000u) << 16u;
const std::uint32_t exponent = (bits >> 10u) & 0x1fu;
const std::uint32_t mantissa = bits & 0x3ffu;
if (exponent == 0x1fu) return std::bit_cast<float>(sign | 0x7f800000u | (mantissa << 13u));
if (exponent == 0u) return std::bit_cast<float>(sign) + (sign != 0u ? -1.0f : 1.0f) * std::ldexp(static_cast<float>(mantissa), -24);
return std::bit_cast<float>(sign | ((exponent + 112u) << 23u) | (mantissa << 13u));
}
constexpr std::uint32_t Expected[32][10] = {
{0xc541a901u, 0xc541a901u, 0x4541a901u, 0xc541ab5du, 0xc541aa2fu, 0xc541a901u, 0xc541ab5eu, 0xc541ab5eu, 0xc541aa30u, 0xc541aa2fu},
{0xc1729fdfu, 0xc1729fdfu, 0x41729fdfu, 0xc172a01fu, 0xc172930du, 0xc1729fdfu, 0xc1729fc7u, 0xc1529fc7u, 0xc172a037u, 0xc172930du},
{0x4187e315u, 0x4187e315u, 0xc187e315u, 0x41882737u, 0x418a9676u, 0x4187e315u, 0x41881a1au, 0x4184f11au, 0x41880521u, 0x418a9676u},
{0x3f7fdb0fu, 0x3f7fdb0fu, 0xbf7fdb0fu, 0x3f801279u, 0x3f7fd966u, 0x3f7fdb0fu, 0xc1320000u, 0xc131fd3cu, 0x41a5a000u, 0x3f7fd966u},
{0xbe511687u, 0xbe511687u, 0x3e511687u, 0x3e5166f4u, 0x3c105963u, 0xbe511687u, 0xbec36be5u, 0xbeb2cfe5u, 0x392b6e40u, 0x3c105963u},
{0xc1e56f13u, 0xc1e56f13u, 0x41e56f13u, 0xc1e55ba5u, 0xc1e54c7cu, 0xc1e56f13u, 0x432a5354u, 0x432a5358u, 0x429ba4a9u, 0xc1e54c7cu},
{0x439a1228u, 0x439a1228u, 0xc39a1228u, 0x43a5d2acu, 0x439ff26au, 0x439a1228u, 0x439ce2eau, 0x439ce2edu, 0x439ff25eu, 0x439ff26au},
{0x37f5fb14u, 0x37f5fb14u, 0xb7f5fb14u, 0xb7f5fc0cu, 0xb5720f8bu, 0x37f5fb14u, 0x3f000000u, 0x3e92e000u, 0x3f75bfecu, 0xb5720f8bu},
{0x7fd0a000u, 0x7fd0a000u, 0xffd0a000u, 0x7fd0a000u, 0x7fd0a000u, 0x7fd0a000u, 0x7fdd6000u, 0x7fdd6000u, 0x7fdd6000u, 0x7fd0a000u},
{0xffc8ab86u, 0xffc8ab86u, 0x7fc8ab86u, 0xffc8ab86u, 0xffc8ab86u, 0xffc8ab86u, 0xffc8ab86u, 0xffc8ab86u, 0x7fd0a000u, 0xffc8ab86u},
{0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0xffe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u},
{0xffc00000u, 0xffc00000u, 0xffc00000u, 0xffc00000u, 0xffc00000u, 0xffc00000u, 0xff800000u, 0xff800000u, 0xff800000u, 0xffc00000u},
{0xb974e480u, 0xb974e480u, 0x3974e480u, 0x3974e480u, 0xb1279400u, 0xb974e480u, 0xc0a0c000u, 0xc0a0c0dcu, 0xbe16f048u, 0xb1279400u},
{0x7f800000u, 0x7f800000u, 0xff800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u, 0x7f800000u},
{0xff800000u, 0xff800000u, 0x7f800000u, 0x7f800000u, 0xff800000u, 0xff800000u, 0xffc00000u, 0xff800000u, 0xff800000u, 0xff800000u},
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u},
{0x7fe00000u, 0x7fe00000u, 0xffe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fc42000u, 0x7fe00000u},
{0x7fc42000u, 0x7fc42000u, 0x7fc42000u, 0xffc42000u, 0x7fc42000u, 0x7fc42000u, 0x45b66cc3u, 0x45b66cc3u, 0x45b66cc3u, 0x7fc42000u},
{0xbf052ae0u, 0xbf052ae0u, 0x3f052ae0u, 0x3f052ae0u, 0x80000000u, 0xbf052ae0u, 0xc04cc000u, 0xc04cc000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0xc50e5804u, 0x00000000u, 0xc5172000u, 0xc5172000u, 0xc3c494a0u, 0xc50e5804u},
{0x5f834da7u, 0x5f834da7u, 0xdf834da7u, 0x5f834da7u, 0x5f834da7u, 0x5f834da7u, 0x5f834da7u, 0x5f834da7u, 0x5f834da7u, 0x5f834da7u},
{0x7f7fffffu, 0x7f7fffffu, 0xff7fffffu, 0x7f7fffffu, 0x7f7fffffu, 0x7f7fffffu, 0x7f7fffffu, 0x7f7fffffu, 0x7f7fffffu, 0x7f7fffffu},
{0x42750eeeu, 0x42750eeeu, 0xc2750eeeu, 0x42750eeeu, 0x42750eeau, 0x42750eeeu, 0x427558beu, 0xc3f314e8u, 0x42750eeeu, 0x42750eeau},
{0x43380748u, 0x43380748u, 0xc3380748u, 0x43380746u, 0x43380747u, 0x43380748u, 0x43390747u, 0x4339073fu, 0x43380747u, 0x43380747u},
{0xffc00000u, 0xffc00000u, 0xffc00000u, 0xffc00000u, 0x7f800000u, 0xffc00000u, 0xffc00000u, 0x7f800000u, 0xffc00000u, 0x7f800000u},
{0xf540cddbu, 0xf540cddbu, 0x7540cddbu, 0xf540cddbu, 0xf540cddbu, 0xf540cddbu, 0xf540cddbu, 0xf540cddbu, 0xf540cddbu, 0xf540cddbu},
{0x7fc00000u, 0x7fc00000u, 0x7fc00000u, 0xffc00000u, 0x7fc00000u, 0x7fc00000u, 0x7fc00000u, 0x7fc00000u, 0x7fc00000u, 0x7fc00000u},
{0x7fe00000u, 0x7fe00000u, 0xffe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u, 0x7fe00000u},
{0x3cb7c720u, 0x3cb7c720u, 0xbcb7c720u, 0xbcb7c720u, 0x3f106b70u, 0x3cb7c720u, 0x438fe000u, 0x46669f00u, 0x3d9bd158u, 0x3f106b70u},
{0xbf9bd158u, 0xbf9bd158u, 0x3f9bd158u, 0x3f9bd158u, 0x398aa000u, 0xbf9bd158u, 0xc58fe000u, 0xc58fd000u, 0xbeb7c720u, 0x398aa000u},
{0x565f49ebu, 0x565f49ebu, 0xd65f49ebu, 0x565f49ebu, 0x565f49ebu, 0x565f49ebu, 0x565f49ebu, 0x565f49ebu, 0x565f49ebu, 0x565f49ebu},
{0xbaf43c38u, 0xbaf43c38u, 0x3af43c38u, 0x3af43c38u, 0x3abe63ecu, 0xbaf43c38u, 0xc021e000u, 0x3fb50000u, 0xba90cd60u, 0x3abe63ecu}
};
std::uint32_t RandomHalf(std::uint64_t& state) {
state = state * 6364136223846793005ull + 1442695040888963407ull;
const auto bits = static_cast<std::uint32_t>(state >> 33u);
return (bits & 0x8000u) | ((8u + (bits >> 16u) % 15u) << 10u) | (bits & 0x3ffu);
}
constexpr const char* Names[10] = {"v_dot2c", "v_dot2", "v_dot2 neg_lo src0/src2 neg_hi src1", "v_dot2 neg_lo src1 neg_hi src0", "v_dot2 op_sel src0", "v_dot2 clamp", "v_dot2c inline 1.0", "v_dot2c literal", "v_dot2c dpp quad_perm", "v_dot2 op_sel src1"};
void FillInput() {
std::uint64_t state = 0x9e3779b97f4a7c15ull;
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
auto* words = &Input[tid * Inputs];
if (tid < Edges.size()) {
std::copy(Edges[tid].begin(), Edges[tid].end(), words);
continue;
}
words[0] = RandomHalf(state) | (RandomHalf(state) << 16u);
words[1] = RandomHalf(state) | (RandomHalf(state) << 16u);
words[2] = std::bit_cast<std::uint32_t>(Half(RandomHalf(state)) * Half(RandomHalf(state)));
}
void Fill(std::uint32_t tid, std::uint32_t* words) {
for (std::uint32_t i = 0; i < 4; ++i) words[i] = Rows[tid][i];
}
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t bytes) {
@@ -77,6 +118,10 @@ std::string Hex(std::uint32_t value) {
return text;
}
void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) {
Require(actual == expected, std::string("v_dot2_f32_f16: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected));
}
auto Compile(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code) {
std::vector<std::uint32_t> userData(8, 0u);
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size() * 4u));
@@ -84,7 +129,7 @@ auto Compile(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> cod
std::copy(input.begin(), input.end(), userData.begin());
std::copy(output.begin(), output.end(), userData.begin() + 4);
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}};
const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {false, false, false}, false, 1};
const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1};
ShaderRecompiler::RecompileRequest request{
{ShaderStage::Compute, reinterpret_cast<std::uintptr_t>(code.data()), code, 0, {}},
{32, 0, userData, compute, std::nullopt, std::nullopt, memory},
@@ -95,14 +140,6 @@ auto Compile(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> cod
return ShaderRecompiler::Recompile(request);
}
void Run(AgcDriver::VulkanDevice& device) {
Output.fill(0xdeadbeefu);
const std::span<const std::uint32_t> code(DotCode);
const auto result = Compile(device, code);
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data()));
device.WaitIdle();
}
void CheckRefused(AgcDriver::VulkanDevice& device, std::uint32_t word0, const std::string& what) {
alignas(256) const std::array<std::uint32_t, 3> code{word0, 0x1c1a0b04u, 0xbf810000u};
std::string refusal;
@@ -114,30 +151,19 @@ void CheckRefused(AgcDriver::VulkanDevice& device, std::uint32_t word0, const st
Require(refusal.find("v_dot2_f32_f16 accumulator op_sel, op_sel_hi and neg_hi are not implemented") != std::string::npos, what + " was not refused");
}
float Dot(float aLow, float aHigh, float bLow, float bHigh, float c) {
return std::fma(aHigh, bHigh, std::fma(aLow, bLow, c));
}
void Expect(std::uint32_t tid, std::uint32_t actual, float expected, const char* name) {
const std::uint32_t bits = std::bit_cast<std::uint32_t>(expected);
const bool nan = std::isnan(expected) && std::isnan(std::bit_cast<float>(actual));
Require(nan || actual == bits, std::string("v_dot2_f32_f16: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(bits));
void Run(AgcDriver::VulkanDevice& device) {
for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]);
Output.fill(0xdeadbeefu);
const std::span<const std::uint32_t> code(Code);
const auto result = Compile(device, code);
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data()));
device.WaitIdle();
}
void Check() {
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
const auto* in = &Input[tid * Inputs];
const auto* out = &Output[tid * Results];
const float aLow = Half(in[0] & 0xffffu);
const float aHigh = Half(in[0] >> 16u);
const float bLow = Half(in[1] & 0xffffu);
const float bHigh = Half(in[1] >> 16u);
const float c = std::bit_cast<float>(in[2]);
const float plain = Dot(aLow, aHigh, bLow, bHigh, c);
Expect(tid, out[0], plain, "plain");
Expect(tid, out[1], Dot(aHigh, aLow, bLow, bHigh, c), "op_sel swapping the halves of src0");
Expect(tid, out[2], Dot(-aLow, aHigh, bLow, -bHigh, -c), "neg_lo src0, neg_hi src1 and neg src2");
if (!std::isnan(plain)) Expect(tid, out[3], plain > 0.0f ? std::min(plain, 1.0f) : 0.0f, "clamp");
const std::uint32_t* out = &Output[tid * Results];
for (std::uint32_t i = 0; i < 10; ++i) Expect(tid, out[i], Expected[tid][i], Names[i]);
}
}
@@ -147,7 +173,6 @@ int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
FillInput();
Run(*device);
Check();
CheckRefused(*device, 0xcc13600au, "op_sel on the accumulator");
@@ -261,6 +261,7 @@ enum class IrOpcode : std::uint16_t {
FPRsq64,
FPSqrt64,
FPTrigPreop64,
FPDot2F32F16,
ConvertF32F64,
ConvertF64F32,
ConvertF64S32,
@@ -302,6 +302,7 @@ constexpr std::array<OpcodeMeta, static_cast<std::size_t>(IrOpcode::Count)> Meta
makeMeta("FPRsq64", U64, U64),
makeMeta("FPSqrt64", U64, U64),
makeMeta("FPTrigPreop64", U64, U64, U32),
makeMeta("FPDot2F32F16", U32, U32, U32, U32),
makeMeta("ConvertF32F64", F32, U64),
makeMeta("ConvertF64F32", U64, F32),
makeMeta("ConvertF64S32", U64, U32),
@@ -195,6 +195,7 @@ std::uint32_t EmitFPRcp64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPRsq64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPSqrt64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPTrigPreop64(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1);
std::uint32_t EmitFPDot2F32F16(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1, std::uint32_t arg2);
std::uint32_t EmitConvertF32F64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitConvertF64F32(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitConvertF64S32(SpirvEmitterState& state, std::uint32_t arg0);
@@ -442,6 +442,96 @@ std::uint32_t EmitFPTrigPreop64(SpirvEmitterState& state, std::uint32_t arg0, st
return FromF64(state, Exact(state, spv::OpFMul, Exact(state, spv::OpFMul, mantissa, scale), ConstantF64(state, 0x1a70000000000000ull)));
}
std::uint32_t EmitFPDot2F32F16(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1, std::uint32_t arg2) {
const auto u32 = TypeU32(state);
const auto op = [&](spv::Op opcode, std::uint32_t lhs, std::uint32_t value) { return Binary(state, opcode, u32, lhs, ConstantU32(state, value)); };
const auto test = [&](spv::Op opcode, std::uint32_t lhs, std::uint32_t value) { return Binary(state, opcode, TypeBool(state), lhs, ConstantU32(state, value)); };
const auto logical = [&](spv::Op opcode, std::uint32_t lhs, std::uint32_t rhs) { return Binary(state, opcode, TypeBool(state), lhs, rhs); };
const auto differ = [&](std::uint32_t lhs, std::uint32_t rhs) { return Binary(state, spv::OpINotEqual, TypeBool(state), lhs, rhs); };
const auto pick = [&](std::uint32_t condition, std::uint32_t trueValue, std::uint32_t falseValue) { return Select(state, u32, condition, trueValue, falseValue); };
struct Half {
std::uint32_t bits = 0;
std::uint32_t sign = 0;
std::uint32_t nan = 0;
std::uint32_t infinite = 0;
std::uint32_t zero = 0;
std::uint32_t value = 0;
};
const auto half = [&](std::uint32_t packed, std::uint32_t shift) {
Half result;
result.bits = op(spv::OpBitwiseAnd, op(spv::OpShiftRightLogical, packed, shift), 0xffffu);
const auto magnitude = op(spv::OpBitwiseAnd, result.bits, 0x7fffu);
result.sign = op(spv::OpShiftLeftLogical, op(spv::OpBitwiseAnd, result.bits, 0x8000u), 16u);
result.nan = test(spv::OpUGreaterThan, magnitude, 0x7c00u);
result.infinite = test(spv::OpIEqual, magnitude, 0x7c00u);
result.zero = test(spv::OpIEqual, magnitude, 0u);
const auto subnormal = Exact(state, spv::OpFMul, Unary(state, spv::OpConvertUToF, TypeF64(state), magnitude), ConstantF64(state, 0x3e70000000000000ull));
const auto normal = ToF64(state, Join(state, {ConstantU32(state, 0u), op(spv::OpIAdd, op(spv::OpShiftLeftLogical, magnitude, 10u), 1008u << 20u)}));
const auto unsignedBits = Split(state, FromF64(state, Select(state, TypeF64(state), test(spv::OpULessThan, magnitude, 0x400u), subnormal, normal)));
result.value = ToF64(state, Join(state, {unsignedBits.low, Binary(state, spv::OpBitwiseOr, u32, unsignedBits.high, result.sign)}));
return result;
};
const auto aLow = half(arg0, 0u);
const auto bLow = half(arg1, 0u);
const auto aHigh = half(arg0, 16u);
const auto bHigh = half(arg1, 16u);
const auto magnitudeC = op(spv::OpBitwiseAnd, arg2, 0x7fffffffu);
const auto signC = op(spv::OpBitwiseAnd, arg2, 0x80000000u);
const auto nanC = test(spv::OpUGreaterThan, magnitudeC, 0x7f800000u);
const auto infiniteC = test(spv::OpIEqual, magnitudeC, 0x7f800000u);
const auto flushedC = test(spv::OpULessThan, magnitudeC, 0x00800000u);
const auto highC = pick(flushedC, ConstantU32(state, 0u), op(spv::OpIAdd, op(spv::OpShiftRightLogical, magnitudeC, 3u), 896u << 20u));
const auto lowC = pick(flushedC, ConstantU32(state, 0u), op(spv::OpShiftLeftLogical, magnitudeC, 29u));
const auto valueC = ToF64(state, Join(state, {lowC, Binary(state, spv::OpBitwiseOr, u32, highC, signC)}));
const auto productLow = Exact(state, spv::OpFMul, aLow.value, bLow.value);
const auto productHigh = Exact(state, spv::OpFMul, aHigh.value, bHigh.value);
const auto upper = TwoSum(state, productHigh, valueC);
const auto total = TwoSum(state, productLow, upper.high);
const auto sum = Split(state, FromF64(state, AddRoundToOdd(state, total.high, AddRoundToOdd(state, total.low, upper.low))));
const auto exponent = op(spv::OpBitwiseAnd, op(spv::OpShiftRightLogical, sum.high, 20u), 0x7ffu);
const auto mantissa = Binary(state, spv::OpBitwiseOr, u32, op(spv::OpShiftLeftLogical, op(spv::OpBitwiseAnd, sum.high, 0x000fffffu), 3u), op(spv::OpShiftRightLogical, sum.low, 29u));
const auto base = Binary(state, spv::OpIAdd, u32, op(spv::OpShiftLeftLogical, op(spv::OpISub, exponent, 896u), 23u), mantissa);
const auto rest = op(spv::OpBitwiseAnd, sum.low, 0x1fffffffu);
const auto tie = logical(spv::OpLogicalAnd, test(spv::OpIEqual, rest, 0x10000000u), test(spv::OpINotEqual, op(spv::OpBitwiseAnd, base, 1u), 0u));
const auto roundUp = logical(spv::OpLogicalOr, test(spv::OpUGreaterThan, rest, 0x10000000u), tie);
auto result = Binary(state, spv::OpBitwiseOr, u32, op(spv::OpBitwiseAnd, sum.high, 0x80000000u), Binary(state, spv::OpIAdd, u32, base, pick(roundUp, ConstantU32(state, 1u), ConstantU32(state, 0u))));
const auto signLow = Binary(state, spv::OpBitwiseXor, u32, aLow.sign, bLow.sign);
const auto signHigh = Binary(state, spv::OpBitwiseXor, u32, aHigh.sign, bHigh.sign);
const auto zeroSign = Binary(state, spv::OpBitwiseAnd, u32, Binary(state, spv::OpBitwiseAnd, u32, signLow, signHigh), signC);
const auto zero = test(spv::OpIEqual, Binary(state, spv::OpBitwiseOr, u32, op(spv::OpBitwiseAnd, sum.high, 0x7fffffffu), sum.low), 0u);
result = pick(zero, zeroSign, result);
const auto notZero = [&](const Half& value) { return Unary(state, spv::OpLogicalNot, TypeBool(state), value.zero); };
const auto infiniteProduct = [&](const Half& lhs, const Half& rhs) {
return logical(spv::OpLogicalOr, logical(spv::OpLogicalAnd, lhs.infinite, notZero(rhs)), logical(spv::OpLogicalAnd, rhs.infinite, notZero(lhs)));
};
const auto invalidProduct = [&](const Half& lhs, const Half& rhs) {
return logical(spv::OpLogicalOr, logical(spv::OpLogicalAnd, lhs.infinite, rhs.zero), logical(spv::OpLogicalAnd, rhs.infinite, lhs.zero));
};
const auto infiniteLow = infiniteProduct(aLow, bLow);
const auto infiniteHigh = infiniteProduct(aHigh, bHigh);
auto invalid = logical(spv::OpLogicalOr, invalidProduct(aLow, bLow), invalidProduct(aHigh, bHigh));
invalid = logical(spv::OpLogicalOr, invalid, logical(spv::OpLogicalAnd, logical(spv::OpLogicalAnd, infiniteLow, infiniteHigh), differ(signLow, signHigh)));
invalid = logical(spv::OpLogicalOr, invalid, logical(spv::OpLogicalAnd, logical(spv::OpLogicalAnd, infiniteLow, infiniteC), differ(signLow, signC)));
invalid = logical(spv::OpLogicalOr, invalid, logical(spv::OpLogicalAnd, logical(spv::OpLogicalAnd, infiniteHigh, infiniteC), differ(signHigh, signC)));
const auto infinite = logical(spv::OpLogicalOr, logical(spv::OpLogicalOr, infiniteLow, infiniteHigh), infiniteC);
const auto infiniteSign = pick(infiniteLow, signLow, pick(infiniteHigh, signHigh, signC));
result = pick(infinite, op(spv::OpBitwiseOr, infiniteSign, 0x7f800000u), result);
result = pick(invalid, ConstantU32(state, 0xffc00000u), result);
const auto quietHalf = [&](const Half& value) {
return Binary(state, spv::OpBitwiseOr, u32, op(spv::OpBitwiseOr, value.sign, 0x7fc00000u), op(spv::OpShiftLeftLogical, op(spv::OpBitwiseAnd, value.bits, 0x3ffu), 13u));
};
result = pick(nanC, op(spv::OpBitwiseOr, arg2, 0x00400000u), result);
result = pick(bHigh.nan, quietHalf(bHigh), result);
result = pick(aHigh.nan, quietHalf(aHigh), result);
result = pick(bLow.nan, quietHalf(bLow), result);
return pick(aLow.nan, quietHalf(aLow), result);
}
std::uint32_t EmitConvertF32F64(SpirvEmitterState& state, std::uint32_t arg0) {
const auto converted = Unary(state, spv::OpBitcast, TypeU32(state), Unary(state, spv::OpFConvert, TypeF32(state), ToF64(state, arg0)));
const auto cls = Classify(state, arg0);
@@ -466,6 +466,7 @@ void EmitDirectInstruction(SpirvValueEmitContext& ctx, const IrValue& inst) {
case IrOpcode::FPRsq64: return Invoke(EmitFPRsq64, ctx, inst);
case IrOpcode::FPSqrt64: return Invoke(EmitFPSqrt64, ctx, inst);
case IrOpcode::FPTrigPreop64: return Invoke(EmitFPTrigPreop64, ctx, inst);
case IrOpcode::FPDot2F32F16: return Invoke(EmitFPDot2F32F16, ctx, inst);
case IrOpcode::ConvertF32F64: return Invoke(EmitConvertF32F64, ctx, inst);
case IrOpcode::ConvertF64F32: return Invoke(EmitConvertF64F32, ctx, inst);
case IrOpcode::ConvertF64S32: return Invoke(EmitConvertF64S32, ctx, inst);
@@ -185,6 +185,7 @@ private:
bool vDivFixupF64(const RdnaInstruction& inst);
bool vDot2cF32F16(const RdnaInstruction& inst);
bool vDot2F32F16(const RdnaInstruction& inst);
bool float16Dot2(const RdnaInstruction& inst, const RdnaOperand& lhs, const RdnaOperand& rhs, IrU32 accumulator);
bool vCubeidF32(const RdnaInstruction& inst);
bool vCubescF32(const RdnaInstruction& inst);
bool vCubetcF32(const RdnaInstruction& inst);
@@ -541,32 +541,38 @@ bool TranslationContext::vLdexpF32(const RdnaInstruction& inst) {
}
bool TranslationContext::vDot2cF32F16(const RdnaInstruction& inst) {
RdnaOperand a = sourceAt(inst, 0u);
a.opSel = false;
a.opSelHi = true;
RdnaOperand b = sourceAt(inst, 1u);
b.opSel = false;
b.opSelHi = true;
const IrF32 aLow = readF16LaneAsF32(a, false);
const IrF32 aHigh = readF16LaneAsF32(a, true);
const IrF32 bLow = readF16LaneAsF32(b, false);
const IrF32 bHigh = readF16LaneAsF32(b, true);
const IrF32 accumulator = readMixF32(inst.destination);
const IrF32 low(ir.Emit(IrOpcode::FPFma32, IrType::F32, {&aLow.Value(), &bLow.Value(), &accumulator.Value()}));
const IrF32 result(ir.Emit(IrOpcode::FPFma32, IrType::F32, {&aHigh.Value(), &bHigh.Value(), &low.Value()}));
writeOperand(inst.destination, &result.Value());
return true;
RdnaOperand lhs = sourceAt(inst, 0u);
lhs.opSel = false;
lhs.opSelHi = true;
RdnaOperand rhs = sourceAt(inst, 1u);
rhs.opSel = false;
rhs.opSelHi = true;
return float16Dot2(inst, lhs, rhs, readU32(accumulatorOperand(inst)));
}
bool TranslationContext::vDot2F32F16(const RdnaInstruction& inst) {
const IrF32 aLow = readF16LaneAsF32(sourceAt(inst, 0u), false, true);
const IrF32 aHigh = readF16LaneAsF32(sourceAt(inst, 0u), true, true);
const IrF32 bLow = readF16LaneAsF32(sourceAt(inst, 1u), false, true);
const IrF32 bHigh = readF16LaneAsF32(sourceAt(inst, 1u), true, true);
IrValue* accumulator = readOperand(sourceAt(inst, 2u), IrType::F32);
const IrF32 low(ir.Emit(IrOpcode::FPFma32, IrType::F32, {&aLow.Value(), &bLow.Value(), accumulator}));
const IrF32 result(ir.Emit(IrOpcode::FPFma32, IrType::F32, {&aHigh.Value(), &bHigh.Value(), &low.Value()}));
writeOperand(inst.destination, &result.Value());
return float16Dot2(inst, sourceAt(inst, 0u), sourceAt(inst, 1u), readU32(sourceAt(inst, 2u)));
}
bool TranslationContext::float16Dot2(const RdnaInstruction& inst, const RdnaOperand& lhs, const RdnaOperand& rhs, IrU32 accumulator) {
const auto packed = [&](const RdnaOperand& operand) {
const IrU32 source = readF16SourceBits(operand);
const auto half = [&](bool highLane) {
const bool selectHigh = highLane ? operand.opSelHi : operand.opSel;
IrU32 bits(selectHigh ? ir.ShiftRightLogical(source.Value(), ir.Constant(16u)) : ir.BitwiseAnd(source.Value(), ir.Constant(0xffffu)));
if (highLane ? operand.negateHi : operand.negate) {
bits = IrU32(ir.BitwiseXor(bits.Value(), ir.Constant(0x8000u)));
}
return bits;
};
return IrU32(ir.BitwiseOr(half(false).Value(), ir.ShiftLeftLogical(half(true).Value(), ir.Constant(16u))));
};
const IrU32 a = packed(lhs);
const IrU32 b = packed(rhs);
const IrU32 result(ir.Emit(IrOpcode::FPDot2F32F16, IrType::U32, {&a.Value(), &b.Value(), &accumulator.Value()}));
RdnaOperand destination = inst.destination;
destination.clamp = false;
writeOperand(destination, &ir.BitCastF32(result.Value()));
return true;
}