Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sail Softfloat Replacement #40

Open
13 tasks
jjscheel opened this issue Jan 30, 2024 · 35 comments
Open
13 tasks

Sail Softfloat Replacement #40

jjscheel opened this issue Jan 30, 2024 · 35 comments
Assignees

Comments

@jjscheel
Copy link
Contributor

jjscheel commented Jan 30, 2024

Technical Group

Applications & Tools HC

ratification-pkg

Technical Debt

Technical Liaison

Bill McSpadden

Task Category

Sail

Task Sub Category

  • gcc
  • binutils
  • gdb
  • intrinsics
  • Java
  • KVM
  • ld
  • llvm
  • Linux kernel
  • QEMU
  • Spike

Ratification Target

3Q2023

Statement of Work (SOW)

Component names:
Sail

Requirements:
In the RISC-V Sail model, floating point operations are implemented (executed) using the Berkley SoftFloat package (written by John Hauser). See: https://github.com/riscv/sail-riscv/tree/master/c_emulator/SoftFloat-3e
This code is written in C in roughly 320 files and 37K lines of code. (Note: some of this work has already been done in support of Vector Crypto by Charalampos Mitrodimas.)

The functionality in SoftFloat needs to be replicated in native Sail code. The primary purpose of this project is to provide a native Sail description of floating point operations which can be used as the canonical definition of FP ops for RISC-V. This Sail code can be imported into the RISC-V ISA documentation.

The API interface for floating point ops must remain the same.

Deliverables:

  1. Set of Sail files that fully implement the functionality of SoftFloat. The code will live in the RISC-V Sail (golden model) repository.

Acceptance Criteria:

  1. Approved PR to the Sail community for all code.
  2. ACT test results equivalency between new and Softfloat (prior) code bases.

Projected timeframe: (best guess date)
6 person months

SOW Signoffs: (delete those not needed)

  • Development partner sign-off
  • Golden Model SIG sign-off (if SAIL work)

Waiver

  • Freeze
  • Ratification

Pull Request Details

@Incarnation-p-lee
Copy link

Thank @jjscheel for the clear description of this task.

Hi @billmcspadden-riscv

Just went through the SoftFloat, and would like to double-check if my understanding is correct before any further actions.

Currently, the sail-riscv c_emulator has riscv_softfloat.c, which leverages the SoftFloat library/package. For example, the function softfloat_f64roundToInt depends on the f64_roundToInt (implemented in SoftFloat-3e/source/f64_roundToInt.c). And we need to migrate the c-implemented code to the sail (aka ./model as I understand). Then the e_emulator can leverage the generated code (by sail model) instead of the SoftFloat library/package.

Unfortunately, my previous experience almost focuses on c, java, c#, and of course the green hand for sail. Could you please help to share some guide or something like that as above mentioned that part of works has done for supporting RVV Crypto. I think I need to try some POC by myself first, and then evaluate the efforts, as well as the plan for the task.

CC @haicheng-li

@Incarnation-p-lee

This comment was marked as outdated.

@billmcspadden-riscv
Copy link

billmcspadden-riscv commented Feb 6, 2024 via email

@Incarnation-p-lee
Copy link

Sure thing, actually I may prefer 200-300(excludes test code) lines per PR for review. By the way, does sail-riscv repo take Merge or Squash Merge for PR?

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented Mar 4, 2024

Status Update Mar 4, 2024

  • PR Float Equal updated in Feb 16, 2024, waiting for further review.

@Incarnation-p-lee
Copy link

Thanks @jjscheel for the SOW description. ACK the SOW from my side.

To clarify, please feel free to comment if any concerns or questions about below open issue.

  • The softfloat_3e_test porting to sail-riscv is NOT in the scope of this SOW due to some more support required from the sail compiler. If so I think we need to record it by a task somewhere.

@billmcspadden-riscv
Copy link

billmcspadden-riscv commented Mar 6, 2024 via email

@Incarnation-p-lee
Copy link

Hi Bill,

Hope you get better and recover soon. To port the TestFloat to the repo sail-riscv, Tim has a proposal requires some update in sail compiler if my understanding is correct.

The idea is that we can port these test cases to sail similar as SoftFloat and find a place to run/execute them(as one of the target of makefile).

@ptomsich
Copy link

ptomsich commented Mar 6, 2024

The compiler changes would be required to automatically run both implementations (i.e., in lockstep) and compare the results.

The referenced [http://www.jhauser.us/arithmetic/TestFloat.html](Berkeley TestFloat) is a black-box FP test-suite.
Having a test-suite for the IEEE-FP compliance in the repository (and running it as part of the CI/CD against the generated c_simulator) would be a great idea.

@Incarnation-p-lee
Copy link

Status Update Mar 18, 2024

  • PR Float Equal updated in Feb 16, 2024, waiting for further review.

@jjscheel
Copy link
Contributor Author

@billmcspadden-riscv, please reach out to @Incarnation-p-lee and @haicheng-li about the Testing Process which has been proposed by the Golden Model SIG and how to proceed with this work. Thanks!

@jjscheel
Copy link
Contributor Author

jjscheel commented Apr 1, 2024

Note: We've extracted the verification work associated with the new Sail replacement for softfloat into issue #41. So, acceptance of this item will require successful passing of the verification work, but the actual work and discussion associated with building and performing the verification will be in the new issue.

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented Apr 3, 2024

Table of contents

Floating Point API Design for Sail

[TOC]

Motivation

Currently the sail-riscv take the c-implemented softfloat to perform the floating-point related operations. However, it is not the sail code which can be transformed to any other modeling validation tools. Thus, we would like to replace the c-implemented softfloat into the sail native way.

Given that floating-point is one more general concept above the sail-riscv, it is even better to port the c-implemented softfloat as a standalone sail library and then integrated into the sail-riscv when fully verified.

Goal

  • The floating-point API for a standalone sail library.

Pull Request

API(s)

Exception and Rounding Modes

Floating-point Exception Flags

Flags NV DZ OF UF NX
Full-name invalid operation divide by zero overflow underflow inexact
Value 0b1 0b10 0b100 0b1000 0b10000
  • Floating-point exception flags takes 5 bits.

Floating-point Rounding Modes

Modes RNE RNA RDN RUP RTZ
Full-name round to nearest, ties to even round to nearest, ties to away round down round up round towards zero
Value 1 2 3 4 5
  • Floating-point rounding modes take 5 bits.

Mechanism

  • The rounding mode will be implemented as read/write global variables, default to RNE.
  • The exception will be implemented as part of the API return value(s) if required.
  • The rounding mode is sticking, while the exception isn't.

API Details

All API(s) support half, single and double precision floating-point, aka FP16, FP32 and FP64. Each API will leverage the existing rounding mode unless you explicit specific the rounding mode or by float_set_rounding before invoke the API. Meanwhile, the flags_mask allows you to mask some exception flags if you don't want it.

Type(s) Definition

Name Description
FP_N Floating-point with N bits, N can be 16, 32 or 64, aka half, single and double floating-point.
exception_flags Floating-point exception flags within 5 bits.
rounding_mode Floating-point rounding mode within 5 bits.
INT_N Signed Integer with N bits, N can be 16, 32 or 64.
UINT_N Unsigned Integer with N bits, N can be 16, 32 or 64.

float_is_nan

The floating-point is NAN or not, return true if NAN, or false.

bool float_is_nan (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_qnan

The floating-point is QNAN or not, return true if QNAN, or false.

bool float_is_qnan (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_snan

The floating-point is SNAN or not, return true if nan, or false.

bool float_is_snan (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_inf

The floating-point is infinite or not, return true if infinite, or false.

bool float_is_inf (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_positive

The floating-point is positive or not, return true if positive, or false.

bool float_is_positive (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_negative

The floating-point is negative or not, return true if negative, or false.

bool float_is_negative (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_zero

The floating-point is zero or not, return true if z,ero or false.

bool float_is_zero (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_normal

The floating-point is normal or not, return true if normal, or false.

bool float_is_normal (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_is_denormal

The floating-point is denormal or not, return true if denormal, or false.

bool float_is_denormal (FP_N);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_eq

The floating-point equals, return true if op_1 is equal to op_2, or false.

(bool, exception_flags) float_eq ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_ne

The floating-point not equals, return true if op_1 is equal to op_2, or false.
$$
(bool, flags) = op_1 \ne op_2
$$

(bool, exception_flags) float_ne ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_lt

The floating-point less than, return true if op_1 is less than op_2, or false.

(bool, exception_flags) float_lt ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_lt_quiet

Similar to float_lt but only raise NV when op_1 and/or op_2 is SNAN.

(bool, exception_flags) float_lt_quiet ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_le

The floating-point less than or equal, return true if op_1 is less than or equal to op_2, or false.

(bool, exception_flags) float_le ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_le_quiet

Similar to float_le but only raise NV when op_1 and/or op_2 is SNAN.

(bool, exception_flags) float_le_quiet ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_gt

The floating-point greater than, return true if op_1 is greater than op_2, or false.

(bool, exception_flags) float_gt ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_gt_quiet

Similar to float_gt but only raise NV when op_1 and/or op_2 is SNAN.

(bool, exception_flags) float_gt_quiet ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_ge

The floating-point greater than or equal, return true if op_1 is greater than or equal to op_2, or false.

(bool, exception_flags) float_ge ((FP_N, FP_N));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_ge_quiet

Similar to float_ge but only raise NV when op_1 and/or op_2 is SNAN.

(bool, exception_flags) float_ge_quiet ((FP_N, FP_N op_2));
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N N N N
  • Rounding Modes

    • This API doesn't honor rounding modes.

float_add

The floating-point add, aka:

(FP_N, exception_flags) float_add ((FP_N, FP_N));
(FP_N, exception_flags) float_add_by_config ((FP_N, FP_N), rounding_mode mode, exception_flags flags_mask);
  • Exception Flags

    You can leverage flags_mask to disable the exception_flags before return. For example, passing flags_unmask_inexact(0b01111) will never set inexact_flag when add.

Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_sub

The floating-point sub, aka:

(FP_N, exception_flags) float_sub ((FP_N op_1, FP_N op_2));
(FP_N, exception_flags) float_sub_by_config ((FP_N, FP_N), rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_mul

The floating-point mul, aka:

(FP_N, exception_flags) float_mul ((FP_N, FP_N));
(FP_N, exception_flags) float_mul_by_config ((FP_N, FP_N), rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_div

The floating-point div, aka:

(FP_N, exception_flags) float_div ((FP_N, FP_N));
(FP_N, exception_flags) float_div_by_config ((FP_N, FP_N), rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y Y Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_mul_add

The floating-point mul_add, aka:

(FP_N, exception_flags) float_mul_add ((FP_N, FP_N, FP_N));
(FP_N, exception_flags) float_mul_add_by_config ((FP_N, FP_N, FP_N), rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_sqrt

The floating-point sqrt, aka:

(FP_N, exception_flags) float_sqrt (FP_N);
(FP_N, exception_flags) float_sqrt_by_config (FP_N, rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_to_i32

The floating-point converting to signed INT_N, N = 32 aka:

(INT_N, exception_flags) float_to_i32 (FP_N);
(INT_N, exception_flags) float_to_i32_by_config (FP_N, rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_to_i16

Almost the same as float_to_i32 except the INT_N bit size.

float_to_i64

Almost the same as float_to_i32 except the INT_N bit size.


float_to_u32

The floating-point converting to unsigned UINT_N, N = 32 aka:

(UINT_N, exception_flags) float_to_u32 (FP_N);
(UINT_N, exception_flags) float_to_u32_by_config (FP_N, rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_to_u16

Almost the same as float_to_u32 except the UINT_N bit size.

float_to_u64

Almost the same as float_to_u32 except the UINT_N bit size.


float_from_s32

The floating-point converting from signed INT_N, N = 32 aka:

(FP_N, exception_flags) float_from_s32 (INT_N);
(FP_N, exception_flags) float_from_s32_by_config (INT_N, rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_from_i16

Almost the same as float_from_i32 except the INT_N bit size.

float_from_i64

Almost the same as float_from_i32 except the INT_N bit size.


float_from_u32

The floating-point converting from unsigned UINT_N, N = 32 aka:

(FP_N, exception_flags) float_from_u32 (UINT_N);
(FP_N, exception_flags) float_from_u32_by_config (UINT_N, rounding_mode mode, exception_flags flags mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_from_u16

Almost the same as float_from_u32 except the UINT_N bit size.

float_from_u64

Almost the same as float_from_u32 except the UINT_N bit size.


float_to_f32

The floating-point converting to FP_N, N = 32 aka:

(FP_N, exception_flags) float_to_f32 (FP_N);
(FP_N, exception_flags) float_to_f32_by_config (FP_N, rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes

    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_to_f16

Almost the same as float_to_f16 except the FP_N bit size.

float_to_f64

Almost the same as float_to_f16 except the FP_N bit size.


float_round_to_int

The floating-point round to nearest integer in FP_N, aka:

(FP_N, exception_flags) float_round_to_int (FP_N);
(FP_N, exception_flags) float_round_to_int_by_config (FP_N, rounding_mode mode, exception_flags flags_mask);
  • Exception Flags
Flags NV DZ OF UF NX
Raise Y N Y Y Y
  • Rounding Modes
    • This API honors rounding modes.
    • The _by_config API(s) will pollute the global rounding modes.
    • The _by_config API(s) will mask the exception before return.

float_set_rounding

The floating-point set rounding modes.

(rounding_mode) float_set_rounding (rounding_mode mode);
  • Exception Flags
Flags NV DZ OF UF NX
Raise N N N N N
  • Rounding Modes

    • This API will pollute the global rounding modes.

Reference Link

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented Apr 3, 2024

Hi @jjscheel and @billmcspadden-riscv

Just draft the API(s) of sail float, please feel free to comment if any questions or concerns.

CC @haicheng-li for awarness.

@jjscheel
Copy link
Contributor Author

jjscheel commented Apr 3, 2024

Nice, @Incarnation-p-lee. THANKS!

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented Apr 16, 2024

Status Update Apr 16, 2024

  • Draft the API design doc as above.
  • Work out one POC for the standalone float lib.

@jjscheel
Copy link
Contributor Author

@Incarnation-p-lee, how is the POC going?

@Incarnation-p-lee
Copy link

Status Update Apr 30, 2024

  • Most implementation of NAN completed.
  • Working on how to test it in sail mode.

@Incarnation-p-lee
Copy link

@Incarnation-p-lee, how is the POC going?

Sorry for missing the status update, happy holiday, ;)!

@jjscheel
Copy link
Contributor Author

jjscheel commented May 1, 2024

No worries. Happy holiday to you!

@Incarnation-p-lee
Copy link

Hi @jjscheel
Just FYI that all PR can be found around the end of the API design doc. For example:

image

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented May 14, 2024

Status Update Mar 14, 2024

  • The NAN API Merged with test.
  • The Infinite API Under Review.

@jjscheel
Copy link
Contributor Author

Thanks, @Incarnation-p-lee. Can you explain why your SAIL work is in the rems-project and not sail-riscv?

@Incarnation-p-lee
Copy link

Thanks, @Incarnation-p-lee. Can you explain why your SAIL work is in the rems-project and not sail-riscv?

We promote the float to a standalone lib, thus SAIL will be a better place here. Because not only sail-riscv can leverage this library.

@jjscheel
Copy link
Contributor Author

Ah, yes! I now understand. Thanks, @Incarnation-p-lee!!!

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented May 27, 2024

Status Update Mar 28, 2024

  • The minor fix/improvement for the test framework Merged.
  • The Infinite API Merged with test cases.
  • The sign API Merged with test cases.
  • The zero API is Under Review.

@jjscheel
Copy link
Contributor Author

@Incarnation-p-lee, it appears ALL your PRs have now merged. Congrats. Does this mean your library is fully implemented?

@jrtc27
Copy link

jrtc27 commented May 28, 2024

Compare the list of APIs to the much smaller list of things covered by the PRs that exist to date. It's a big chunk of work to get through.

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented May 28, 2024

Thanks @jrtc27 for the explanation. That is correct, there are lots of API that need to be covered, I think the overall progress is about 10%, give or take.

@Incarnation-p-lee
Copy link

Status Update Jun 11, 2024

  • The zero API is Merged with test cases.
  • The normal API is Merged with test cases.
  • The equal API is Under Review.

@Incarnation-p-lee
Copy link

Incarnation-p-lee commented Jun 24, 2024

Status Update Jun 25, 2024

  • The equal API is Merged with test cases.
  • The not equal API is Merged with test cases.
  • The quad float (aka fp128) is supported for all existing APIs, thanks to @jordancarlin.
  • The test data refactor is Under Review.

@Incarnation-p-lee
Copy link

Status Update Jul 9, 2024

  • The test data refactor is Merged.
  • The less than API is Merged with test cases.
  • The duplicated code refactor is Under Review.

@Incarnation-p-lee
Copy link

Status Update Aug 20, 2024

  • The equal related refactor is Merged.
  • The less than or eq (le) API is Under Review.
  • The include files clean up is Under Review.

@Incarnation-p-lee
Copy link

Status Update Sep 3, 2024

  • The less than or eq (le) API is Merged.
  • The less than or eq (le) quiet API is Merged.
  • The including files clean up is Merged.
  • The greater than (gt) API is Under Review.

@Incarnation-p-lee
Copy link

Status Update Sep 17, 2024

  • The greater than (gt) API is Merged.
  • The greater than (gt) quiet API is Merged.
  • The greater than or equal (ge) API is Merged.
  • The greater than or equal (ge) quiet API is Under Review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: As-planned
Development

No branches or pull requests

5 participants