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
22 changes: 19 additions & 3 deletions sw/common/softfloat_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,10 @@ uint32_t cvt_f32_to_custom(float value, uint32_t exp_bits, uint32_t sig_bits,
const uint32_t mant_mask = sig_bits ? ((1u<<sig_bits) - 1u) : 0u;
const uint32_t sign_shift = exp_bits + sig_bits;
const int32_t bias_out = (int32_t)((1u<<(exp_bits-1u)) - 1u);
const int32_t emax = bias_out;
// OCP E4M3 has no infinity: exponent 1111 holds normal values (up to 448 =
// S.1111.110) and only S.1111.111 is NaN. Out-of-range values saturate.
const bool is_e4m3 = (exp_bits == 4 && sig_bits == 3);
const int32_t emax = is_e4m3 ? bias_out + 1 : bias_out;
const int32_t emin = 1 - bias_out;

auto pack_custom = [&](uint32_t exp, uint32_t mant){
Expand All @@ -509,11 +512,17 @@ uint32_t cvt_f32_to_custom(float value, uint32_t exp_bits, uint32_t sig_bits,
if (fflags) {
*fflags |= flags;
}
if (is_e4m3) {
return pack_custom(exp_max, mant_mask); // NaN
}
return pack_custom(exp_max, sig_bits ? (1u<<(sig_bits-1u)) : 0u); // qNaN
}
if (fflags) {
*fflags |= flags;
}
if (is_e4m3) {
return pack_custom(exp_max, mant_mask - 1u); // max finite
}
return pack_custom(exp_max, 0); // Inf
}
if (exp_ieee == 0 && sig_ieee == 0) {
Expand Down Expand Up @@ -573,9 +582,16 @@ uint32_t cvt_f32_to_custom(float value, uint32_t exp_bits, uint32_t sig_bits,
// exponent unchanged
}

// Overflow?
if (exponent > emax) {
// Overflow? (for E4M3, S.1111.111 is NaN, so 480 and above overflow)
if (exponent > emax
|| (is_e4m3 && exponent == emax && ((uint32_t)main & mant_mask) == mant_mask)) {
flags |= (FLAG_OF | FLAG_NX);
if (is_e4m3) {
if (fflags) {
*fflags |= flags;
}
return pack_custom(exp_max, mant_mask - 1u); // max finite
}
int to_inf =
(frm == RNE || frm == RMM) ? 1 :
(frm == RTZ) ? 0 :
Expand Down
2 changes: 1 addition & 1 deletion tests/unittest/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ROOT_DIR := $(realpath ../..)
include $(ROOT_DIR)/config.mk

PROJECTS := vx_malloc fpcvt linked_list dram_sim dram_bank_model dxa_ooo rvc_decompress gfx_binsort gfx_tex_sw gfx_rast_sw gfx_msaa gfx_om_mrt
PROJECTS := vx_malloc fpcvt fp8_cvt linked_list dram_sim dram_bank_model dxa_ooo rvc_decompress gfx_binsort gfx_tex_sw gfx_rast_sw gfx_msaa gfx_om_mrt

all:
$(foreach p,$(PROJECTS),$(MAKE) -C $(p) &&) true
Expand Down
13 changes: 13 additions & 0 deletions tests/unittest/fp8_cvt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ROOT_DIR := $(realpath ../../..)
include $(ROOT_DIR)/config.mk

PROJECT := fp8_cvt

SRC_DIR := $(VORTEX_HOME)/tests/unittest/$(PROJECT)

SRCS := $(SRC_DIR)/main.cpp $(SW_COMMON_DIR)/rvfloats.cpp $(SW_COMMON_DIR)/softfloat_ext.cpp

CXXFLAGS += -I$(THIRD_PARTY_DIR)/softfloat/source/include
LDFLAGS += $(THIRD_PARTY_DIR)/softfloat/build/Linux-x86_64-GCC/softfloat.a

include ../common.mk
107 changes: 107 additions & 0 deletions tests/unittest/fp8_cvt/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright © 2019-2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Host fp8 conversions (sw/common/rvfloats): every E4M3 and E5M2 code must
// survive decode + encode, and the E4M3 encoder must follow OCP E4M3 (no
// infinity, max finite 448, NaN = S.1111.111; out-of-range values saturate).

#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>

#include "rvfloats.h"

namespace {

int failures = 0;

uint32_t f2u(float f) {
uint32_t u;
std::memcpy(&u, &f, sizeof(u));
return u;
}

float u2f(uint32_t u) {
float f;
std::memcpy(&f, &u, sizeof(f));
return f;
}

void check_round_trip(const char *fmt, uint32_t (*dec)(uint8_t, uint32_t, uint32_t *),
uint8_t (*enc)(uint32_t, uint32_t, uint32_t *)) {
for (int c = 0; c < 256; ++c) {
uint32_t f = dec(uint8_t(c), 0, nullptr);
uint8_t back = enc(f, 0, nullptr);
if (std::isnan(u2f(f))) {
if (!std::isnan(u2f(dec(back, 0, nullptr)))) {
printf("FAILED: %s 0x%02x (NaN) encoded back as 0x%02x\n", fmt, c, back);
++failures;
}
} else if (back != c) {
printf("FAILED: %s 0x%02x (%g) encoded back as 0x%02x\n", fmt, c, u2f(f), back);
++failures;
}
}
}

void expect_e4m3(float x, uint8_t expect) {
uint8_t c = rv_ftoe4m3_s(f2u(x), 0, nullptr);
if (c != expect) {
printf("FAILED: e4m3(%g) = 0x%02x, expected 0x%02x\n", x, c, expect);
++failures;
}
}

void expect_mxfp8(float x, uint8_t expect) {
uint8_t c = rv_ftomxfp8_s(f2u(x), 127, 0, nullptr);
if (c != expect) {
printf("FAILED: mxfp8(%g, scale 2^0) = 0x%02x, expected 0x%02x\n", x, c, expect);
++failures;
}
}

} // namespace

int main() {
check_round_trip("e4m3", rv_e4m3tof_s, rv_ftoe4m3_s);
check_round_trip("e5m2", rv_e5m2tof_s, rv_ftoe5m2_s);

// rounding near the top of the E4M3 range (round to nearest even)
expect_e4m3(247.0f, 0x77);
expect_e4m3(248.0f, 0x78);
expect_e4m3(300.0f, 0x79);
expect_e4m3(304.0f, 0x7a);
expect_e4m3(447.0f, 0x7e);
expect_e4m3(464.0f, 0x7e);
// out of range: saturate; NaN keeps its sign
expect_e4m3(480.0f, 0x7e);
expect_e4m3(1.0e6f, 0x7e);
expect_e4m3(-1.0e6f, 0xfe);
expect_e4m3(INFINITY, 0x7e);
expect_e4m3(-INFINITY, 0xfe);
expect_e4m3(NAN, 0x7f);
expect_e4m3(-NAN, 0xff);

// MX quantiser (E4M3 elements, scale 2^0)
expect_mxfp8(384.0f, 0x7c);
expect_mxfp8(448.0f, 0x7e);

if (failures) {
printf("%d check(s) FAILED!\n", failures);
return 1;
}
printf("PASSED!\n");
return 0;
}
Loading