Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ci/testcases/isa/fp_flags.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion hw/rtl/fpu/VX_fncp_unit.sv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions hw/rtl/fpu/VX_fpu_dpi.sv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions sw/common/rvfloats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/regression/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
77 changes: 44 additions & 33 deletions tests/regression/dotproduct/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <cmath>
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <vector>
#include <vortex2.h>
#include "common.h"

#define FLOAT_ULP 6

#define RT_CHECK(_expr) \
do { \
int _ret = _expr; \
Expand All @@ -31,11 +30,19 @@ class Comparator<int> {
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<int>& a, const std::vector<int>& b,
const std::vector<int>& 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;
Expand All @@ -44,25 +51,33 @@ class Comparator<int> {

template <>
class Comparator<float> {
private:
union Float_t { float f; int i; };
public:
static const char* type_str() {
return "float";
}
static float generate() {
return static_cast<float>(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<float>& a, const std::vector<float>& b,
const std::vector<float>& 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;
Expand Down Expand Up @@ -168,7 +183,7 @@ int main(int argc, char *argv[]) {
std::cout << "allocate host buffers" << std::endl;
std::vector<TYPE> h_src0(num_points);
std::vector<TYPE> h_src1(num_points);
std::vector<TYPE> h_dst(dst_buf_size);
std::vector<TYPE> h_dst(blocksPerGrid);

for (uint32_t i = 0; i < num_points; ++i) {
h_src0[i] = Comparator<TYPE>::generate();
Expand Down Expand Up @@ -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<TYPE>::compare(cur, ref, 0, errors)) {
int errors = 0;
if (!Comparator<TYPE>::verify(h_src0, h_src1, h_dst, depth)) {
++errors;
}

// cleanup
std::cout << "cleanup" << std::endl;
cleanup();
Expand Down
14 changes: 14 additions & 0 deletions tests/regression/fp_flags/Makefile
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions tests/regression/fp_flags/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef _COMMON_H_
#define _COMMON_H_

#include <stdint.h>

// 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
Loading
Loading