From 8d4c273df9bb8993310d35d3e2af1ec99d72ad65 Mon Sep 17 00:00:00 2001 From: tinebp Date: Thu, 19 Nov 2026 03:37:01 -0800 Subject: [PATCH 1/2] fpu: raise NV only for compares in sign injection; NV for fmin/fmax of two NaNs with an sNaN fsgnj/fsgnjn/fsgnjx share the compare arm of the non-computational unit and took the compare's NV verdict, so a signaling NaN operand set fflags.NV. Sign injection never raises a flag. Gate NV on the compare select in the STD/DSP unit (VX_fncp_unit) and on is_fcmp in the DPI backend. fmin/fmax with two NaN operands returned the canonical NaN through an early exit that skipped the signaling check, so NV was lost when either input was an sNaN. rvfloats is shared by SimX and the DPI backend, so both were wrong; the RTL STD unit was already correct. New regression fp_flags checks results and fflags of sign injection, min/max and compares on NaN operands against values from the F/D specification (not softfloat, which the simulators share), single and, at XLEN 64, double precision. CI runs it on SimX and on rtlsim with the DPI, STD and DSP backends. On the old code it fails 4 min/max cases on SimX and 5 sign-injection cases on STD. At XLEN 64 it also runs single-precision ops on raw 64-bit register values: an improperly NaN-boxed operand must read as the canonical NaN and every result must come back NaN-boxed; SimX, DPI and STD pass them. Fixes #444, fixes #449. Co-Authored-By: Claude Opus 5.5 --- ci/testcases/isa/fp_flags.yaml | 33 +++++ hw/rtl/fpu/VX_fncp_unit.sv | 2 +- hw/rtl/fpu/VX_fpu_dpi.sv | 6 +- sw/common/rvfloats.cpp | 12 ++ tests/regression/Makefile | 2 +- tests/regression/fp_flags/Makefile | 14 ++ tests/regression/fp_flags/common.h | 44 +++++++ tests/regression/fp_flags/kernel.cpp | 123 +++++++++++++++++ tests/regression/fp_flags/main.cpp | 190 +++++++++++++++++++++++++++ 9 files changed, 421 insertions(+), 5 deletions(-) create mode 100644 ci/testcases/isa/fp_flags.yaml create mode 100644 tests/regression/fp_flags/Makefile create mode 100644 tests/regression/fp_flags/common.h create mode 100644 tests/regression/fp_flags/kernel.cpp create mode 100644 tests/regression/fp_flags/main.cpp diff --git a/ci/testcases/isa/fp_flags.yaml b/ci/testcases/isa/fp_flags.yaml new file mode 100644 index 0000000000..65d1bde251 --- /dev/null +++ b/ci/testcases/isa/fp_flags.yaml @@ -0,0 +1,33 @@ +# F/D sign injection, min/max and compare on NaN operands: bit-exact results and +# fflags against values from the RISC-V spec (no softfloat reference, since the +# simulators share it): fsgnj* never raise NV, and fmin/fmax of two NaNs raise +# NV when either is signaling, on every FPU backend. +category: isa +defaults: + xlen: + - 32 + - 64 + tier: smoke + touches: [hw/rtl/fpu, hw/dpi/dpi_float.cpp, sim/simx/fpu_unit.cpp, sw/common/rvfloats.cpp] +tests: +- id: fp_flags-1 + via: blackbox + drivers: + - simx + - rtlsim + app: fp_flags +# The synthesizable backends rather than the DPI model rtlsim uses by default. +- id: fp_flags-std + via: blackbox + drivers: + - rtlsim + app: fp_flags + configs: -DVX_CFG_FPU_TYPE_STD +- id: fp_flags-dsp + via: blackbox + drivers: + - rtlsim + app: fp_flags + configs: -DVX_CFG_FPU_TYPE_DSP + xlen: + - 32 diff --git a/hw/rtl/fpu/VX_fncp_unit.sv b/hw/rtl/fpu/VX_fncp_unit.sv index 30f480b18e..4b33f9cecf 100644 --- a/hw/rtl/fpu/VX_fncp_unit.sv +++ b/hw/rtl/fpu/VX_fncp_unit.sv @@ -292,7 +292,7 @@ module VX_fncp_unit import VX_gpu_pkg::*, VX_fpu_pkg::*; #( 0,1,2: begin // SGNJ, CMP result_s0 = op_mod_s0[3] ? FLEN'(fcmp_res_s0) : fsgnj_res_s0; - fflags_NV_s0 = fcmp_fflags_NV_s0; + fflags_NV_s0 = op_mod_s0[3] & fcmp_fflags_NV_s0; end 3: begin // CLASS diff --git a/hw/rtl/fpu/VX_fpu_dpi.sv b/hw/rtl/fpu/VX_fpu_dpi.sv index cab880327c..577e01e587 100644 --- a/hw/rtl/fpu/VX_fpu_dpi.sv +++ b/hw/rtl/fpu/VX_fpu_dpi.sv @@ -571,9 +571,9 @@ module VX_fpu_dpi import VX_gpu_pkg::*, VX_fpu_pkg::*; #( fflags_fncp = 'x; for (integer i = 0; i < NUM_LANES; ++i) begin case (frm) - 0: begin result_fncp[i] = is_fcmp ? result_fle[i][`VX_CFG_XLEN-1:0] : result_fsgnj[i][`VX_CFG_XLEN-1:0]; fflags_fncp[i] = fflags_fle[i]; end - 1: begin result_fncp[i] = is_fcmp ? result_flt[i][`VX_CFG_XLEN-1:0] : result_fsgnjn[i][`VX_CFG_XLEN-1:0]; fflags_fncp[i] = fflags_flt[i]; end - 2: begin result_fncp[i] = is_fcmp ? result_feq[i][`VX_CFG_XLEN-1:0] : result_fsgnjx[i][`VX_CFG_XLEN-1:0]; fflags_fncp[i] = fflags_feq[i]; end + 0: begin result_fncp[i] = is_fcmp ? result_fle[i][`VX_CFG_XLEN-1:0] : result_fsgnj[i][`VX_CFG_XLEN-1:0]; fflags_fncp[i] = is_fcmp ? fflags_fle[i] : '0; end + 1: begin result_fncp[i] = is_fcmp ? result_flt[i][`VX_CFG_XLEN-1:0] : result_fsgnjn[i][`VX_CFG_XLEN-1:0]; fflags_fncp[i] = is_fcmp ? fflags_flt[i] : '0; end + 2: begin result_fncp[i] = is_fcmp ? result_feq[i][`VX_CFG_XLEN-1:0] : result_fsgnjx[i][`VX_CFG_XLEN-1:0]; fflags_fncp[i] = is_fcmp ? fflags_feq[i] : '0; end 3: begin result_fncp[i] = result_fclss[i][`VX_CFG_XLEN-1:0]; end 4: begin result_fncp[i] = result_fmvx[i][`VX_CFG_XLEN-1:0]; end 5: begin result_fncp[i] = result_fmvf[i][`VX_CFG_XLEN-1:0]; end diff --git a/sw/common/rvfloats.cpp b/sw/common/rvfloats.cpp index 607ae472d7..0742156e19 100644 --- a/sw/common/rvfloats.cpp +++ b/sw/common/rvfloats.cpp @@ -364,6 +364,9 @@ uint32_t rv_fmin_s(uint32_t a, uint32_t b, uint32_t* fflags) { uint32_t r; rv_init(0); if (isNaNF32UI(a) && isNaNF32UI(b)) { + if (softfloat_isSigNaNF32UI(a) || softfloat_isSigNaNF32UI(b)) { + softfloat_raiseFlags(softfloat_flag_invalid); + } r = defaultNaNF32UI; } else { auto fa = to_float32_t(a); @@ -383,6 +386,9 @@ uint64_t rv_fmin_d(uint64_t a, uint64_t b, uint32_t* fflags) { uint64_t r; rv_init(0); if (isNaNF64UI(a) && isNaNF64UI(b)) { + if (softfloat_isSigNaNF64UI(a) || softfloat_isSigNaNF64UI(b)) { + softfloat_raiseFlags(softfloat_flag_invalid); + } r = defaultNaNF64UI; } else { auto fa = to_float64_t(a); @@ -402,6 +408,9 @@ uint32_t rv_fmax_s(uint32_t a, uint32_t b, uint32_t* fflags) { uint32_t r; rv_init(0); if (isNaNF32UI(a) && isNaNF32UI(b)) { + if (softfloat_isSigNaNF32UI(a) || softfloat_isSigNaNF32UI(b)) { + softfloat_raiseFlags(softfloat_flag_invalid); + } r = defaultNaNF32UI; } else { auto fa = to_float32_t(a); @@ -421,6 +430,9 @@ uint64_t rv_fmax_d(uint64_t a, uint64_t b, uint32_t* fflags) { uint64_t r; rv_init(0); if (isNaNF64UI(a) && isNaNF64UI(b)) { + if (softfloat_isSigNaNF64UI(a) || softfloat_isSigNaNF64UI(b)) { + softfloat_raiseFlags(softfloat_flag_invalid); + } r = defaultNaNF64UI; } else { auto fa = to_float64_t(a); diff --git a/tests/regression/Makefile b/tests/regression/Makefile index 1dabd029c6..a5f6215b13 100644 --- a/tests/regression/Makefile +++ b/tests/regression/Makefile @@ -8,7 +8,7 @@ TESTS := \ arith printf diverge diverge_large sort fence fence_mp ldst_order \ vecadd vecadd_v1 sgemm sgemmx sgemm_v1 conv3 relu sgemv \ sgemm2 sgemm2_v1 madmax mixfu stencil3d raycast bfs jacobi pathfinder \ - softmax uniform_ld lmem_access ldst_wide ldst_matrix tfu zfhmin \ + softmax uniform_ld lmem_access ldst_wide ldst_matrix tfu zfhmin fp_flags \ flash_attn flash_attn_tcu_wg \ sgemm_tcu sgemm_tcu_mx sgemm_tcu_sp_mx sgemm_tcu_wg sgemm_tcu_wg_mx sgemm_tcu_sp sgemm_tcu_wg_sp \ sgemm2_dxa sgemm2_tcu sgemm_tcu_wg_dxa sgemm_tcu_wg_sp_dxa \ diff --git a/tests/regression/fp_flags/Makefile b/tests/regression/fp_flags/Makefile new file mode 100644 index 0000000000..abb61cb6d7 --- /dev/null +++ b/tests/regression/fp_flags/Makefile @@ -0,0 +1,14 @@ +ROOT_DIR := $(realpath ../../..) +include $(ROOT_DIR)/config.mk + +PROJECT := fp_flags + +SRC_DIR := $(VORTEX_HOME)/tests/regression/$(PROJECT) + +SRCS := $(SRC_DIR)/main.cpp + +VX_SRCS := $(SRC_DIR)/kernel.cpp + +KERNEL_LIB := vortex2 + +include ../common.mk diff --git a/tests/regression/fp_flags/common.h b/tests/regression/fp_flags/common.h new file mode 100644 index 0000000000..1c749bc5aa --- /dev/null +++ b/tests/regression/fp_flags/common.h @@ -0,0 +1,44 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#include + +// Sign-injection, min/max and compare ops, single then double precision. +enum { + FF_OP_FSGNJ = 0, + FF_OP_FSGNJN, + FF_OP_FSGNJX, + FF_OP_FMIN, + FF_OP_FMAX, + FF_OP_FEQ, + FF_OP_FLT, + FF_OP_FLE, + FF_NUM_OPS, +}; + +#define FF_OP_D(op) ((op) + FF_NUM_OPS) + +// raw: 64-bit FP registers only; a single-precision op reads its operands as +// the raw 64-bit register values, which may be improperly NaN-boxed, and the +// result is checked in full, boxing included. +typedef struct { + uint32_t op; + uint32_t raw; + uint64_t a; + uint64_t b; +} ff_case_t; + +typedef struct { + uint64_t result; + uint32_t fflags; + uint32_t pad; +} ff_out_t; + +typedef struct { + uint32_t count; + uint32_t pad; + uint64_t src_addr; + uint64_t dst_addr; +} kernel_arg_t; + +#endif diff --git a/tests/regression/fp_flags/kernel.cpp b/tests/regression/fp_flags/kernel.cpp new file mode 100644 index 0000000000..a21323a9f4 --- /dev/null +++ b/tests/regression/fp_flags/kernel.cpp @@ -0,0 +1,123 @@ +#include +#include +#include "common.h" + +#define FOP_S(insn, r, a, b) __asm__ volatile(insn " %0, %1, %2" : "=f"(r) : "f"(a), "f"(b)) +#define FCMP(insn, r, a, b) __asm__ volatile(insn " %0, %1, %2" : "=r"(r) : "f"(a), "f"(b)) + +static inline __attribute__((always_inline)) float mv_w_x(uint64_t x) { + float f; + __asm__ volatile("fmv.w.x %0, %1" : "=f"(f) : "r"((long)x)); + return f; +} + +static inline __attribute__((always_inline)) uint64_t mv_x_w(float f) { + long r; + __asm__ volatile("fmv.x.w %0, %1" : "=r"(r) : "f"(f)); + return (uint32_t)r; +} + +#if __riscv_xlen == 64 +// A single-precision op on raw 64-bit register values, result read in full. +#define FOP_S_RAW(insn, r, a, b) __asm__ volatile( \ + "fmv.d.x ft0, %1\n\tfmv.d.x ft1, %2\n\t" insn " ft2, ft0, ft1\n\tfmv.x.d %0, ft2" \ + : "=r"(r) : "r"(a), "r"(b) : "ft0", "ft1", "ft2") +#define FCMP_S_RAW(insn, r, a, b) __asm__ volatile( \ + "fmv.d.x ft0, %1\n\tfmv.d.x ft1, %2\n\t" insn " %0, ft0, ft1" \ + : "=r"(r) : "r"(a), "r"(b) : "ft0", "ft1") + +static uint64_t run_raw(uint32_t op, uint64_t a, uint64_t b) { + uint64_t r = 0; + switch (op) { + case FF_OP_FSGNJ: FOP_S_RAW("fsgnj.s", r, a, b); break; + case FF_OP_FSGNJN: FOP_S_RAW("fsgnjn.s", r, a, b); break; + case FF_OP_FSGNJX: FOP_S_RAW("fsgnjx.s", r, a, b); break; + case FF_OP_FMIN: FOP_S_RAW("fmin.s", r, a, b); break; + case FF_OP_FMAX: FOP_S_RAW("fmax.s", r, a, b); break; + case FF_OP_FEQ: FCMP_S_RAW("feq.s", r, a, b); break; + case FF_OP_FLT: FCMP_S_RAW("flt.s", r, a, b); break; + case FF_OP_FLE: FCMP_S_RAW("fle.s", r, a, b); break; + default: break; + } + return r; +} + +static inline __attribute__((always_inline)) double mv_d_x(uint64_t x) { + double d; + __asm__ volatile("fmv.d.x %0, %1" : "=f"(d) : "r"(x)); + return d; +} + +static inline __attribute__((always_inline)) uint64_t mv_x_d(double d) { + uint64_t r; + __asm__ volatile("fmv.x.d %0, %1" : "=r"(r) : "f"(d)); + return r; +} +#endif + +static void run_case(const kernel_arg_t* arg, uint32_t i) { + auto src = reinterpret_cast(arg->src_addr); + auto dst = reinterpret_cast(arg->dst_addr); + uint32_t op = src[i].op; + float fa = mv_w_x(src[i].a); + float fb = mv_w_x(src[i].b); + float fr = 0; + long cmp = 0; + uint64_t res = 0; + long fflags; +#if __riscv_xlen == 64 + if (src[i].raw) { + __asm__ volatile("csrw fflags, zero"); + res = run_raw(op, src[i].a, src[i].b); + __asm__ volatile("csrr %0, fflags" : "=r"(fflags)); + dst[i].result = res; + dst[i].fflags = (uint32_t)fflags; + return; + } +#endif + __asm__ volatile("csrw fflags, zero"); + switch (op) { + case FF_OP_FSGNJ: FOP_S("fsgnj.s", fr, fa, fb); res = mv_x_w(fr); break; + case FF_OP_FSGNJN: FOP_S("fsgnjn.s", fr, fa, fb); res = mv_x_w(fr); break; + case FF_OP_FSGNJX: FOP_S("fsgnjx.s", fr, fa, fb); res = mv_x_w(fr); break; + case FF_OP_FMIN: FOP_S("fmin.s", fr, fa, fb); res = mv_x_w(fr); break; + case FF_OP_FMAX: FOP_S("fmax.s", fr, fa, fb); res = mv_x_w(fr); break; + case FF_OP_FEQ: FCMP("feq.s", cmp, fa, fb); res = cmp; break; + case FF_OP_FLT: FCMP("flt.s", cmp, fa, fb); res = cmp; break; + case FF_OP_FLE: FCMP("fle.s", cmp, fa, fb); res = cmp; break; +#if __riscv_xlen == 64 + default: { + double da = mv_d_x(src[i].a); + double db = mv_d_x(src[i].b); + double dr = 0; + switch (op - FF_NUM_OPS) { + case FF_OP_FSGNJ: FOP_S("fsgnj.d", dr, da, db); res = mv_x_d(dr); break; + case FF_OP_FSGNJN: FOP_S("fsgnjn.d", dr, da, db); res = mv_x_d(dr); break; + case FF_OP_FSGNJX: FOP_S("fsgnjx.d", dr, da, db); res = mv_x_d(dr); break; + case FF_OP_FMIN: FOP_S("fmin.d", dr, da, db); res = mv_x_d(dr); break; + case FF_OP_FMAX: FOP_S("fmax.d", dr, da, db); res = mv_x_d(dr); break; + case FF_OP_FEQ: FCMP("feq.d", cmp, da, db); res = cmp; break; + case FF_OP_FLT: FCMP("flt.d", cmp, da, db); res = cmp; break; + case FF_OP_FLE: FCMP("fle.d", cmp, da, db); res = cmp; break; + default: break; + } + } break; +#else + default: break; +#endif + } + __asm__ volatile("csrr %0, fflags" : "=r"(fflags)); + dst[i].result = res; + dst[i].fflags = (uint32_t)fflags; +} + +__kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) { + uint32_t i = blockIdx.x * blockDim.x + threadIdx.x; + // fflags is shared by the warp: run one lane at a time so each case sees only its own flags. + int nt = vx_num_threads(); + for (int l = 0; l < nt; ++l) { + if (vx_thread_id() == l && i < arg->count) { + run_case(arg, i); + } + } +} diff --git a/tests/regression/fp_flags/main.cpp b/tests/regression/fp_flags/main.cpp new file mode 100644 index 0000000000..ef411e7054 --- /dev/null +++ b/tests/regression/fp_flags/main.cpp @@ -0,0 +1,190 @@ +// Bit-exact results and fflags of sign-injection, min/max and compare on NaN +// operands, checked against values taken from the RISC-V F/D specification +// (not softfloat, which the simulators share). +#include +#include "common.h" +#include +#include +#include +#include +#include +#include + +#define CHECK(expr) do { \ + vx_result_t _r = (expr); \ + if (_r != VX_SUCCESS) { \ + std::fprintf(stderr, "FAIL %s:%d: '%s' returned %s\n", \ + __FILE__, __LINE__, #expr, vx_result_string(_r)); \ + std::exit(1); \ + } \ +} while (0) + +namespace { + +constexpr uint32_t NV = 0x10; + +constexpr uint64_t S_QNAN = 0x7fc00000, S_SNAN = 0x7fa00000, S_NSNAN = 0xffa00000; +constexpr uint64_t S_ONE = 0x3f800000, S_NONE = 0xbf800000, S_TWO = 0x40000000; + +constexpr uint64_t D_QNAN = 0x7ff8000000000000, D_SNAN = 0x7ff4000000000000, D_NSNAN = 0xfff4000000000000; +constexpr uint64_t D_ONE = 0x3ff0000000000000, D_NONE = 0xbff0000000000000, D_TWO = 0x4000000000000000; + +const char* op_names[] = {"fsgnj", "fsgnjn", "fsgnjx", "fmin", "fmax", "feq", "flt", "fle"}; + +struct Case { + uint32_t op; + uint64_t a, b; + uint64_t result; + uint32_t fflags; + uint32_t raw = 0; +}; + +// One table per precision: sign injection never raises flags; fmin/fmax +// with two NaN operands return the canonical NaN and raise NV if either one is +// signaling. The compare rows check that NV still comes from compares. +std::vector cases(uint64_t qnan, uint64_t snan, uint64_t nsnan, + uint64_t one, uint64_t none, uint64_t two, uint32_t d) { + return { + {FF_OP_FSGNJ + d, snan, one, snan, 0}, + {FF_OP_FSGNJN + d, snan, one, nsnan, 0}, + {FF_OP_FSGNJX + d, snan, none, nsnan, 0}, + {FF_OP_FSGNJ + d, one, nsnan, none, 0}, + {FF_OP_FSGNJN + d, snan, snan, nsnan, 0}, + {FF_OP_FSGNJX + d, qnan, one, qnan, 0}, + {FF_OP_FMIN + d, snan, snan, qnan, NV}, + {FF_OP_FMAX + d, qnan, snan, qnan, NV}, + {FF_OP_FMIN + d, nsnan, qnan, qnan, NV}, + {FF_OP_FMAX + d, snan, qnan, qnan, NV}, + {FF_OP_FMIN + d, qnan, qnan, qnan, 0}, + {FF_OP_FMAX + d, qnan, qnan, qnan, 0}, + {FF_OP_FMIN + d, snan, one, one, NV}, + {FF_OP_FMAX + d, qnan, two, two, 0}, + {FF_OP_FEQ + d, snan, one, 0, NV}, + {FF_OP_FEQ + d, qnan, one, 0, 0}, + {FF_OP_FLT + d, qnan, one, 0, NV}, + {FF_OP_FLE + d, one, two, 1, 0}, + }; +} + +// Single-precision ops on raw 64-bit registers: an operand whose upper half is +// not all ones reads as the canonical NaN, and every result is NaN-boxed. +constexpr uint64_t BOX = 0xffffffff00000000; +constexpr uint64_t R_UNBOXED_ONE = 0x000000003f800000; +constexpr uint64_t R_CANON = BOX | 0x7fc00000, R_NCANON = BOX | 0xffc00000; +constexpr uint64_t R_ONE = BOX | S_ONE, R_NONE = BOX | S_NONE, R_TWO = BOX | S_TWO; + +std::vector raw_cases() { + return { + {FF_OP_FSGNJ, R_UNBOXED_ONE, R_ONE, R_CANON, 0, 1}, + {FF_OP_FSGNJN, R_UNBOXED_ONE, R_ONE, R_NCANON, 0, 1}, + {FF_OP_FSGNJ, R_ONE, R_UNBOXED_ONE, R_ONE, 0, 1}, + {FF_OP_FSGNJN, R_ONE, R_UNBOXED_ONE, R_NONE, 0, 1}, + {FF_OP_FSGNJ, R_ONE, R_TWO, R_ONE, 0, 1}, + {FF_OP_FMIN, R_UNBOXED_ONE, R_TWO, R_TWO, 0, 1}, + {FF_OP_FMAX, R_UNBOXED_ONE, R_UNBOXED_ONE, R_CANON, 0, 1}, + {FF_OP_FMIN, R_ONE, R_TWO, R_ONE, 0, 1}, + {FF_OP_FEQ, R_UNBOXED_ONE, R_ONE, 0, 0, 1}, + {FF_OP_FLT, R_UNBOXED_ONE, R_ONE, 0, NV, 1}, + }; +} + +} // namespace + +int main(int argc, char** argv) { + const char* kernel_file = "kernel.vxbin"; + int c; + while ((c = getopt(argc, argv, "k:h")) != -1) { + switch (c) { + case 'k': kernel_file = optarg; break; + default: + std::cout << "Usage: [-k kernel] [-h]" << std::endl; + return (c == 'h') ? 0 : -1; + } + } + + vx_device_h dev = nullptr; + CHECK(vx_device_open(0, &dev)); + uint64_t isa = 0; + CHECK(vx_device_query(dev, VX_CAPS_ISA_FLAGS, &isa)); + uint32_t xlen = VX_ISA_ARCH(isa); + + auto tests = cases(S_QNAN, S_SNAN, S_NSNAN, S_ONE, S_NONE, S_TWO, 0); + if (xlen == 64) { + auto dt = cases(D_QNAN, D_SNAN, D_NSNAN, D_ONE, D_NONE, D_TWO, FF_NUM_OPS); + tests.insert(tests.end(), dt.begin(), dt.end()); + auto rt = raw_cases(); + tests.insert(tests.end(), rt.begin(), rt.end()); + } + uint32_t count = tests.size(); + + std::vector in(count); + for (uint32_t i = 0; i < count; ++i) { + in[i] = {tests[i].op, tests[i].raw, tests[i].a, tests[i].b}; + } + std::vector out(count); + + vx_queue_h q = nullptr; + vx_queue_info_t qi = { sizeof(qi), nullptr, VX_QUEUE_PRIORITY_NORMAL, 0 }; + CHECK(vx_queue_create(dev, &qi, &q)); + vx_module_h mod = nullptr; + vx_kernel_h kern = nullptr; + CHECK(vx_module_load_file(dev, kernel_file, &mod)); + CHECK(vx_module_get_kernel(mod, "main", &kern)); + + uint64_t in_size = count * sizeof(ff_case_t); + uint64_t out_size = count * sizeof(ff_out_t); + vx_buffer_h src = nullptr, dst = nullptr; + kernel_arg_t arg{}; + arg.count = count; + CHECK(vx_buffer_create(dev, in_size, VX_MEM_READ, &src)); + CHECK(vx_buffer_create(dev, out_size, VX_MEM_WRITE, &dst)); + CHECK(vx_buffer_address(src, &arg.src_addr)); + CHECK(vx_buffer_address(dst, &arg.dst_addr)); + CHECK(vx_enqueue_write(q, src, 0, in.data(), in_size, 0, nullptr, nullptr)); + + uint32_t grid[1], block[1]; + CHECK(vx_device_max_occupancy_grid(dev, 1, &count, grid, block)); + vx_launch_info_t li{}; + li.struct_size = sizeof(li); + li.kernel = kern; + li.args_host = &arg; + li.args_size = sizeof(arg); + li.ndim = 1; + li.grid_dim[0] = grid[0]; + li.block_dim[0]= block[0]; + vx_event_h launch_ev = nullptr, read_ev = nullptr; + CHECK(vx_enqueue_launch(q, &li, 0, nullptr, &launch_ev)); + CHECK(vx_enqueue_read(q, out.data(), dst, 0, out_size, 1, &launch_ev, &read_ev)); + CHECK(vx_event_wait_value(read_ev, 1, VX_TIMEOUT_INFINITE)); + vx_event_release(read_ev); + vx_event_release(launch_ev); + + int errors = 0; + for (uint32_t i = 0; i < count; ++i) { + const Case& t = tests[i]; + bool is_d = t.op >= FF_NUM_OPS; + if (out[i].result != t.result || out[i].fflags != t.fflags) { + std::printf("*** %s.%c a=0x%llx b=0x%llx: expected 0x%llx/fflags 0x%x, actual 0x%llx/fflags 0x%x\n", + op_names[t.op % FF_NUM_OPS], t.raw ? 'r' : (is_d ? 'd' : 's'), + (unsigned long long)t.a, (unsigned long long)t.b, + (unsigned long long)t.result, t.fflags, + (unsigned long long)out[i].result, out[i].fflags); + ++errors; + } + } + std::printf("fp_flags: xlen=%u, %u cases\n", xlen, count); + + vx_buffer_release(src); + vx_buffer_release(dst); + vx_kernel_release(kern); + vx_module_release(mod); + vx_queue_release(q); + vx_device_release(dev); + + if (errors) { + std::cout << "Found " << errors << " errors!\nFAILED!" << std::endl; + return 1; + } + std::cout << "PASSED!" << std::endl; + return 0; +} From 909715d68df21da6985ce1e87f295a555124385d Mon Sep 17 00:00:00 2001 From: tinebp Date: Thu, 19 Nov 2026 03:37:17 -0800 Subject: [PATCH 2/2] tests/dotproduct: compare against a double reference with a summation error bound The check summed the products sequentially in float on the host and allowed a fixed 6 ulp. A float sum's rounding error grows with n, so -n4096 failed even though the device result (one product per thread, grid-stride accumulation, block tree reduction) was closer to the exact sum than the reference. Compute the reference and the sum of the block partials in double, and accept |device - exact| <= gamma_depth * sum|a*b|, where depth counts the float roundings on any product's path to its block sum (the product, the grid-stride accumulations, the tree levels). The bound is rigorous for any IEEE-correct device and stays tight: at -n4096 it is about 6 ulp of the result, far below a single dropped product. The integer variant keeps the exact comparison, now in unsigned arithmetic, so the reference wraps modulo 2^32 as the device does instead of overflowing a signed int. The partials buffer is now sized in elements, not bytes. Fixes #450. Co-Authored-By: Claude Opus 5.5 --- tests/regression/dotproduct/main.cpp | 77 ++++++++++++++++------------ 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/tests/regression/dotproduct/main.cpp b/tests/regression/dotproduct/main.cpp index 974f6d1291..d29f701ba2 100644 --- a/tests/regression/dotproduct/main.cpp +++ b/tests/regression/dotproduct/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,8 +6,6 @@ #include #include "common.h" -#define FLOAT_ULP 6 - #define RT_CHECK(_expr) \ do { \ int _ret = _expr; \ @@ -31,11 +30,19 @@ class Comparator { static int generate() { return rand(); } - static bool compare(int a, int b, int index, int errors) { - if (a != b) { - if (errors < 100) { - printf("*** error: [%d] expected=%d, actual=%d\n", index, b, a); - } + // Integer sums wrap modulo 2^32 on the device, so they are exact in any + // order; the reference wraps the same way, in unsigned arithmetic. + static bool verify(const std::vector& a, const std::vector& b, + const std::vector& partials, uint32_t /*depth*/) { + uint32_t ref = 0, cur = 0; + for (size_t i = 0; i < a.size(); ++i) { + ref += uint32_t(a[i]) * uint32_t(b[i]); + } + for (int p : partials) { + cur += uint32_t(p); + } + if (cur != ref) { + printf("*** error: expected=%d, actual=%d\n", int(ref), int(cur)); return false; } return true; @@ -44,8 +51,6 @@ class Comparator { template <> class Comparator { -private: - union Float_t { float f; int i; }; public: static const char* type_str() { return "float"; @@ -53,16 +58,26 @@ class Comparator { static float generate() { return static_cast(rand()) / RAND_MAX; } - static bool compare(float a, float b, int index, int errors) { - union fi_t { float f; int32_t i; }; - fi_t fa, fb; - fa.f = a; - fb.f = b; - auto d = std::abs(fa.i - fb.i); - if (d > FLOAT_ULP) { - if (errors < 100) { - printf("*** error: [%d] expected=%f, actual=%f\n", index, b, a); - } + // The device rounds through at most `depth` float operations on the way from + // each product to its block sum, so the forward error bound is + // gamma_depth * sum|a*b| with gamma_k = k*u/(1 - k*u), u = 2^-24 (Higham, + // summation in any order). The reference and the sum of the block partials are + // computed in double, so the bound holds for every n. + static bool verify(const std::vector& a, const std::vector& b, + const std::vector& partials, uint32_t depth) { + double ref = 0, abs_sum = 0, cur = 0; + for (size_t i = 0; i < a.size(); ++i) { + double p = double(a[i]) * double(b[i]); + ref += p; + abs_sum += std::abs(p); + } + for (float p : partials) { + cur += p; + } + double ku = depth * std::ldexp(1.0, -24); + double tol = ku / (1 - ku) * abs_sum; + if (std::abs(cur - ref) > tol) { + printf("*** error: expected=%f, actual=%f, tolerance=%g\n", ref, cur, tol); return false; } return true; @@ -168,7 +183,7 @@ int main(int argc, char *argv[]) { std::cout << "allocate host buffers" << std::endl; std::vector h_src0(num_points); std::vector h_src1(num_points); - std::vector h_dst(dst_buf_size); + std::vector h_dst(blocksPerGrid); for (uint32_t i = 0; i < num_points; ++i) { h_src0[i] = Comparator::generate(); @@ -217,22 +232,18 @@ int main(int argc, char *argv[]) { // verify result std::cout << "verify result" << std::endl; - int errors = 0; - TYPE ref = 0; - TYPE cur = 0; - - for (uint32_t i = 0; i < num_points; ++i) { - ref += h_src0[i] * h_src1[i]; - } - - for(uint32_t i = 0; i < blocksPerGrid; i++) { - cur += h_dst[i]; + // Per element: one product, ceil(n/stride) grid-stride accumulations, then + // log2(threadsPerBlock) tree levels. + uint32_t stride = threadsPerBlock * blocksPerGrid; + uint32_t depth = 1 + (num_points + stride - 1) / stride; + for (uint32_t t = 1; t < threadsPerBlock; t *= 2) { + ++depth; } - - if (!Comparator::compare(cur, ref, 0, errors)) { + int errors = 0; + if (!Comparator::verify(h_src0, h_src1, h_dst, depth)) { ++errors; } - + // cleanup std::cout << "cleanup" << std::endl; cleanup();