Skip to content

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

Merged
merged 4 commits into from
Sep 3, 2024
Merged
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
76 changes: 76 additions & 0 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>

#if defined(_WIN32)
#include <windows.h>
Expand Down Expand Up @@ -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) {
Copy link
Contributor

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 😅 )

Copy link
Contributor Author

@bnoordhuis bnoordhuis Aug 26, 2024

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 and mantissa >> (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')

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

double d, s;
int e;
if ((v & 0x7C00) == 0x7C00) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that but... for doubles there's INFINITY from math.h and you can write -INFINITY to get a negative infinity. For FP16_INFINITY that doesn't work because -0x7C00 is 0x8400 and that's a regular number. I don't like that lack of parity.

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 Math.fp16round(32767) to round up to 32,768.

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;
}

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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);

Expand Down
30 changes: 15 additions & 15 deletions gen/function_source.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ const uint8_t qjsc_function_source[384] = {
0x74, 0x75, 0x72, 0x6e, 0x20, 0x34, 0x32, 0x20,
0x7d, 0x0c, 0x00, 0xfa, 0x01, 0x9e, 0x01, 0x00,
0x06, 0x00, 0x03, 0x00, 0x01, 0xa0, 0x01, 0x06,
0xa0, 0x01, 0x00, 0x00, 0x00, 0xb2, 0x03, 0x02,
0x00, 0x30, 0xb4, 0x03, 0x04, 0x00, 0x70, 0xb2,
0xa0, 0x01, 0x00, 0x00, 0x00, 0xb4, 0x03, 0x02,
0x00, 0x30, 0xb6, 0x03, 0x04, 0x00, 0x70, 0xb4,
0x03, 0x04, 0x03, 0x70, 0x10, 0x00, 0x01, 0x00,
0xe0, 0x01, 0x00, 0x01, 0x00, 0x0c, 0x43, 0xfa,
0x01, 0xb4, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x03, 0x00, 0xbb, 0x2a, 0x28, 0xb6, 0x03,
0x01, 0xb6, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x03, 0x00, 0xbb, 0x2a, 0x28, 0xb8, 0x03,
0x03, 0x01, 0x04, 0x02, 0x1e, 0x0c, 0x0e, 0x1a,
0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e,
0x20, 0x66, 0x28, 0x29, 0x20, 0x7b, 0x20, 0x72,
0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x34, 0x32,
0x20, 0x7d, 0x0c, 0x03, 0xc1, 0x05, 0x08, 0xc1,
0x04, 0x3f, 0xdc, 0x00, 0x00, 0x00, 0x80, 0x3f,
0xda, 0x00, 0x00, 0x00, 0x40, 0x3e, 0xdc, 0x00,
0x00, 0x00, 0x80, 0xbe, 0x00, 0x40, 0xda, 0x00,
0x00, 0x00, 0x00, 0x04, 0xdd, 0x00, 0x00, 0x00,
0xc8, 0x04, 0xde, 0x00, 0x00, 0x00, 0x3a, 0xdc,
0x00, 0x00, 0x00, 0x61, 0x01, 0x00, 0x38, 0xda,
0x04, 0x3f, 0xdd, 0x00, 0x00, 0x00, 0x80, 0x3f,
0xdb, 0x00, 0x00, 0x00, 0x40, 0x3e, 0xdd, 0x00,
0x00, 0x00, 0x80, 0xbe, 0x00, 0x40, 0xdb, 0x00,
0x00, 0x00, 0x00, 0x04, 0xde, 0x00, 0x00, 0x00,
0xc8, 0x04, 0xdf, 0x00, 0x00, 0x00, 0x3a, 0xdd,
0x00, 0x00, 0x00, 0x61, 0x01, 0x00, 0x38, 0xdb,
0x00, 0x00, 0x00, 0x42, 0x36, 0x00, 0x00, 0x00,
0x24, 0x00, 0x00, 0xc9, 0x06, 0xc8, 0x62, 0x01,
0x00, 0x38, 0xdc, 0x00, 0x00, 0x00, 0xaf, 0xe9,
0x00, 0x38, 0xdd, 0x00, 0x00, 0x00, 0xaf, 0xe9,
0x0b, 0x38, 0x92, 0x00, 0x00, 0x00, 0x62, 0x01,
0x00, 0xee, 0x2f, 0x61, 0x03, 0x00, 0x61, 0x02,
0x00, 0x38, 0x39, 0x00, 0x00, 0x00, 0x38, 0xdc,
0x00, 0x00, 0x00, 0x04, 0xda, 0x00, 0x00, 0x00,
0x00, 0x38, 0x39, 0x00, 0x00, 0x00, 0x38, 0xdd,
0x00, 0x00, 0x00, 0x04, 0xdb, 0x00, 0x00, 0x00,
0x9d, 0x31, 0x01, 0x00, 0x04, 0x00, 0xca, 0x62,
0x02, 0x00, 0x42, 0x36, 0x00, 0x00, 0x00, 0x24,
0x00, 0x00, 0xcb, 0x06, 0xc8, 0x62, 0x03, 0x00,
0x38, 0xdc, 0x00, 0x00, 0x00, 0xaf, 0xe9, 0x0b,
0x38, 0xdd, 0x00, 0x00, 0x00, 0xaf, 0xe9, 0x0b,
0x38, 0x92, 0x00, 0x00, 0x00, 0x62, 0x03, 0x00,
0xee, 0x2f, 0x68, 0x03, 0x00, 0x68, 0x02, 0x00,
0xc4, 0x28, 0xb6, 0x03, 0x01, 0x01, 0x28, 0x60,
0xc4, 0x28, 0xb8, 0x03, 0x01, 0x01, 0x28, 0x60,
0x01, 0x49, 0x02, 0x21, 0x1a, 0x1b, 0x04, 0x1e,
0x1d, 0x12, 0x26, 0x49, 0x1d, 0x0c, 0x06, 0x11,
0x18, 0x2a, 0x1c, 0x37, 0x41, 0x21, 0x1c, 0x34,
Expand Down
6 changes: 3 additions & 3 deletions gen/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const uint8_t qjsc_hello[89] = {
0x6f, 0x2e, 0x6a, 0x73, 0x0c, 0x00, 0xfa, 0x00,
0x9e, 0x01, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00,
0x14, 0x01, 0xa0, 0x01, 0x00, 0x00, 0x00, 0x38,
0xd9, 0x00, 0x00, 0x00, 0x42, 0xda, 0x00, 0x00,
0x00, 0x04, 0xdb, 0x00, 0x00, 0x00, 0x24, 0x01,
0x00, 0xcc, 0x28, 0xb8, 0x03, 0x01, 0x01, 0x00,
0xda, 0x00, 0x00, 0x00, 0x42, 0xdb, 0x00, 0x00,
0x00, 0x04, 0xdc, 0x00, 0x00, 0x00, 0x24, 0x01,
0x00, 0xcc, 0x28, 0xba, 0x03, 0x01, 0x01, 0x00,
0x00,
};

Expand Down
30 changes: 15 additions & 15 deletions gen/hello_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ const uint8_t qjsc_fib_module[311] = {
0x6c, 0x65, 0x73, 0x2f, 0x66, 0x69, 0x62, 0x5f,
0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2e, 0x6a,
0x73, 0x06, 0x66, 0x69, 0x62, 0x02, 0x6e, 0x0d,
0xb2, 0x03, 0x00, 0x01, 0x00, 0x00, 0xb4, 0x03,
0xb4, 0x03, 0x00, 0x01, 0x00, 0x00, 0xb6, 0x03,
0x00, 0x00, 0x00, 0x0c, 0x20, 0xfa, 0x01, 0x9e,
0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x09,
0x00, 0xb4, 0x03, 0x00, 0x01, 0x0c, 0x43, 0xfa,
0x01, 0xb4, 0x03, 0x01, 0x00, 0x01, 0x04, 0x01,
0x00, 0x1a, 0x01, 0xb6, 0x03, 0x00, 0x01, 0x00,
0xb4, 0x03, 0x00, 0x00, 0xd0, 0xb3, 0xa7, 0xe9,
0x00, 0xb6, 0x03, 0x00, 0x01, 0x0c, 0x43, 0xfa,
0x01, 0xb6, 0x03, 0x01, 0x00, 0x01, 0x04, 0x01,
0x00, 0x1a, 0x01, 0xb8, 0x03, 0x00, 0x01, 0x00,
0xb6, 0x03, 0x00, 0x00, 0xd0, 0xb3, 0xa7, 0xe9,
0x03, 0xb3, 0x28, 0xd0, 0xb4, 0xac, 0xe9, 0x03,
0xb4, 0x28, 0xdc, 0xd0, 0xb4, 0x9e, 0xee, 0xdc,
0xd0, 0xb5, 0x9e, 0xee, 0x9d, 0x28, 0xb2, 0x03,
0xd0, 0xb5, 0x9e, 0xee, 0x9d, 0x28, 0xb4, 0x03,
0x02, 0x08, 0x20, 0x04, 0x00, 0x07, 0x06, 0x07,
0x06, 0x12, 0x09, 0x08, 0x07, 0x07, 0x10, 0x07,
0x06, 0x07, 0x06, 0x12, 0x13, 0x08, 0x07, 0x08,
Expand All @@ -42,7 +42,7 @@ const uint8_t qjsc_fib_module[311] = {
0x31, 0x29, 0x20, 0x2b, 0x20, 0x66, 0x69, 0x62,
0x28, 0x6e, 0x20, 0x2d, 0x20, 0x32, 0x29, 0x3b,
0x0a, 0x7d, 0x08, 0xe9, 0x05, 0xbe, 0x00, 0xe0,
0x29, 0x06, 0x2e, 0xb2, 0x03, 0x01, 0x01, 0x06,
0x29, 0x06, 0x2e, 0xb4, 0x03, 0x01, 0x01, 0x06,
0x01, 0x01, 0x00, 0x07, 0x14, 0x02, 0x00,
};

Expand All @@ -59,17 +59,17 @@ const uint8_t qjsc_hello_module[178] = {
0x6c, 0x6f, 0x67, 0x16, 0x48, 0x65, 0x6c, 0x6c,
0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x10,
0x66, 0x69, 0x62, 0x28, 0x31, 0x30, 0x29, 0x3d,
0x0d, 0xb2, 0x03, 0x01, 0xb4, 0x03, 0x00, 0x00,
0x01, 0x00, 0xb6, 0x03, 0x00, 0x00, 0x0c, 0x20,
0x0d, 0xb4, 0x03, 0x01, 0xb6, 0x03, 0x00, 0x00,
0x01, 0x00, 0xb8, 0x03, 0x00, 0x00, 0x0c, 0x20,
0xfa, 0x01, 0x9e, 0x01, 0x00, 0x00, 0x00, 0x05,
0x01, 0x00, 0x32, 0x00, 0xb6, 0x03, 0x00, 0x0c,
0x08, 0xe9, 0x02, 0x29, 0x38, 0xdc, 0x00, 0x00,
0x00, 0x42, 0xdd, 0x00, 0x00, 0x00, 0x04, 0xde,
0x01, 0x00, 0x32, 0x00, 0xb8, 0x03, 0x00, 0x0c,
0x08, 0xe9, 0x02, 0x29, 0x38, 0xdd, 0x00, 0x00,
0x00, 0x42, 0xde, 0x00, 0x00, 0x00, 0x04, 0xdf,
0x00, 0x00, 0x00, 0x24, 0x01, 0x00, 0x0e, 0x38,
0xdc, 0x00, 0x00, 0x00, 0x42, 0xdd, 0x00, 0x00,
0x00, 0x04, 0xdf, 0x00, 0x00, 0x00, 0x65, 0x00,
0xdd, 0x00, 0x00, 0x00, 0x42, 0xde, 0x00, 0x00,
0x00, 0x04, 0xe0, 0x00, 0x00, 0x00, 0x65, 0x00,
0x00, 0xbb, 0x0a, 0xee, 0x24, 0x02, 0x00, 0x0e,
0x06, 0x2e, 0xb2, 0x03, 0x01, 0x01, 0x0a, 0x01,
0x06, 0x2e, 0xb4, 0x03, 0x01, 0x01, 0x0a, 0x01,
0x01, 0x00, 0x04, 0x0a, 0x02, 0x62, 0x00, 0x4d,
0x30, 0x00,
};
Expand Down
Loading
Loading