-
Notifications
You must be signed in to change notification settings - Fork 181
Add Float16Array #491
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
Add Float16Array #491
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
#include <stdlib.h> | ||
#include <string.h> | ||
#include <inttypes.h> | ||
#include <math.h> | ||
|
||
#if defined(_WIN32) | ||
#include <windows.h> | ||
|
@@ -356,6 +357,81 @@ static inline void inplace_bswap32(uint8_t *tab) { | |
put_u32(tab, bswap32(get_u32(tab))); | ||
} | ||
|
||
static inline double fromfp16(uint16_t v) { | ||
double d, s; | ||
int e; | ||
if ((v & 0x7C00) == 0x7C00) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personal preference probably, but I'd define this like FP6_INF or something, for readability... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that but... for doubles there's |
||
d = (v & 0x3FF) ? NAN : INFINITY; | ||
} else { | ||
d = (v & 0x3FF) / 1024.; | ||
e = (v & 0x7C00) >> 10; | ||
if (e == 0) { | ||
e = -14; | ||
} else { | ||
d += 1; | ||
e -= 15; | ||
} | ||
d = scalbn(d, e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's a way to implement fromfp16() and tofp16() without using functions from math.h, I'd love to hear it! My initial approach used plain bit shifting but I couldn't get rounding to work correctly in all cases. Ex. test262 expects For anyone interested, here's my first attempt: static inline double fromfp16(uint16_t v) {
union {
uint16_t v;
struct {
uint16_t f:10;
uint16_t e:5;
uint16_t s:1;
} x;
} h;
union {
double v;
struct {
uint64_t f:52;
uint64_t e:11;
uint64_t s:1;
} x;
} q;
h.v = v;
if (h.x.e == 31) { // +/-Infinity, NaN
q.x.f = h.x.f > 0;
q.x.e = 2047;
} else if (h.x.e == 0 && h.x.f == 0) { // TODO subnormal if h.x.f > 0
q.x.f = 0;
q.x.e = 0;
} else {
q.x.f = (uint64_t)h.x.f << 42;
q.x.e = (h.x.e - 15) + 1023;
}
q.x.s = h.x.s;
return q.v;
}
static inline uint16_t tofp16(double d) {
union {
uint16_t v;
struct {
uint16_t f:10;
uint16_t e:5;
uint16_t s:1;
} x;
} h;
union {
double v;
struct {
uint64_t f:52;
uint64_t e:11;
uint64_t s:1;
} x;
} q;
q.v = d;
if (q.x.e == 2047)
return 0x7C00 | (q.x.s << 15) | (q.x.f > 0); // +/-Infinity, NaN
if (d < -65504 || d > 65504)
return 0x7C00 | (q.x.s << 15); // out of range, return Infinity
if (q.x.e == 0 && q.x.f == 0) {
return 0;
}
h.x.f = q.x.f >> 42;
h.x.e = (q.x.e - 1023) + 15;
h.x.s = q.x.s;
return h.v;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we really want to avoid the dependency on math.h for this very function perhaps we can borrow it from musl? https://git.musl-libc.org/cgit/musl/tree/src/math/scalbn.c There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind, I see we use more math functions below. |
||
} | ||
s = (v & 0x8000) ? -1.0 : 1.0; | ||
return d * s; | ||
} | ||
|
||
static inline uint16_t tofp16(double d) { | ||
uint16_t f, s; | ||
double t; | ||
int e; | ||
s = 0; | ||
if (copysign(1, d) < 0) { // preserve sign when |d| is negative zero | ||
d = -d; | ||
s = 0x8000; | ||
} | ||
if (isinf(d)) | ||
return s | 0x7C00; | ||
if (isnan(d)) | ||
return s | 0x7C01; | ||
if (d == 0) | ||
return s | 0; | ||
d = 2 * frexp(d, &e); | ||
e--; | ||
if (e > 15) | ||
return s | 0x7C00; // out of range, return +/-infinity | ||
if (e < -25) { | ||
d = 0; | ||
e = 0; | ||
} else if (e < -14) { | ||
d = scalbn(d, e + 14); | ||
e = 0; | ||
} else { | ||
d -= 1; | ||
e += 15; | ||
} | ||
d *= 1024.; | ||
f = (uint16_t)d; | ||
t = d - f; | ||
if (t < 0.5) | ||
goto done; | ||
if (t == 0.5) | ||
if ((f & 1) == 0) | ||
goto done; | ||
// adjust for rounding | ||
if (++f == 1024) { | ||
f = 0; | ||
if (++e == 31) | ||
return s | 0x7C00; // out of range, return +/-infinity | ||
} | ||
done: | ||
return s | (e << 10) | f; | ||
} | ||
|
||
static inline int isfp16nan(uint16_t v) { | ||
return (v & 0x7FFF) > 0x7C00; | ||
} | ||
|
||
static inline int isfp16zero(uint16_t v) { | ||
return (v & 0x7FFF) == 0; | ||
} | ||
|
||
/* XXX: should take an extra argument to pass slack information to the caller */ | ||
typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does one find the rules you followed here? (trying to inform myself and do a good review 😅 )
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mostly derived it from first principles; they're identical to floats and doubles, except with smaller exponents and mantissas (5 and 10 bits resp. with an exponent bias of 15. Compare doubles: 11 and 52 bits, bias of 1023.)
To get the floating point value, you compute
pow(2, exponent-15) * (1 + mantissa/1024.0)
. Compare doubles:pow(2, exponent-1023) * (1 + mantissa/pow(2, 52))
To go from double to half-float or vice versa... my intuition was that
exponent - 1023 + 15
andmantissa >> (52 - 10)
would be Good Enough(TM) but alas, it wasn't, so now I'm mostly computing it in the double-precision domain.The Wikipedia page is okay. (edit: what I call 'mantissa', wikipedia calls 'fraction')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and if you're worried about correctness: test262 has pretty good coverage so it should be okay. Everything passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!