Skip to content

Add FromNaN32ps() to convert NaN with preserved signal and payload #4

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

Closed
x448 opened this issue Jan 5, 2020 · 1 comment
Closed

Comments

@x448
Copy link
Owner

x448 commented Jan 5, 2020

Fromfloat32() is 100% compatible with AMD and Intel F16C instructions by producing identical results for all 4+ billion conversions. Unfortunately, this means NaN input values are converted to NaN with quiet bit always set.

It can be useful to preserve the original NaN signaling status, so provide FromNaN32ps() to convert 32-bit NaN to 16-bit NaN while preserving both signal and payload.

Additionally, implement the function so it can inline and perform faster than Fromfloat32().

// ErrInvalidNaNValue indicates a NaN was not received.
var ErrInvalidNaNValue = errors.New("float16: invalid NaN value, expected IEEE 754 NaN")

// FromNaN32ps converts nan to IEEE binary16 NaN while preserving both 
// signaling and payload. Unlike Fromfloat32(), which can only return
// qNaN because it sets quiet bit = 1, this can return both sNaN and qNaN.
// If the result is infinity (sNaN with empty payload), then the 
// lowest bit of payload is set to make the result a valid sNaN.
// This function was kept simple to be able to inline.
func FromNaN32ps(nan float32) (Float16, error)
@x448
Copy link
Owner Author

x448 commented Jan 5, 2020

Closed by commit dddfdd4

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

No branches or pull requests

1 participant