mirror of
https://github.com/bellard/quickjs.git
synced 2025-09-30 06:54:26 +03:00
added Float16Array (bnoordhuis) - optimized float16 conversion functions
This commit is contained in:
94
quickjs.c
94
quickjs.c
@@ -147,6 +147,7 @@ enum {
|
||||
JS_CLASS_UINT32_ARRAY, /* u.array (typed_array) */
|
||||
JS_CLASS_BIG_INT64_ARRAY, /* u.array (typed_array) */
|
||||
JS_CLASS_BIG_UINT64_ARRAY, /* u.array (typed_array) */
|
||||
JS_CLASS_FLOAT16_ARRAY, /* u.array (typed_array) */
|
||||
JS_CLASS_FLOAT32_ARRAY, /* u.array (typed_array) */
|
||||
JS_CLASS_FLOAT64_ARRAY, /* u.array (typed_array) */
|
||||
JS_CLASS_DATAVIEW, /* u.typed_array */
|
||||
@@ -974,6 +975,7 @@ struct JSObject {
|
||||
uint32_t *uint32_ptr; /* JS_CLASS_UINT32_ARRAY */
|
||||
int64_t *int64_ptr; /* JS_CLASS_INT64_ARRAY */
|
||||
uint64_t *uint64_ptr; /* JS_CLASS_UINT64_ARRAY */
|
||||
uint16_t *fp16_ptr; /* JS_CLASS_FLOAT16_ARRAY */
|
||||
float *float_ptr; /* JS_CLASS_FLOAT32_ARRAY */
|
||||
double *double_ptr; /* JS_CLASS_FLOAT64_ARRAY */
|
||||
} u;
|
||||
@@ -1502,6 +1504,7 @@ static JSClassShortDef const js_std_class_def[] = {
|
||||
{ JS_ATOM_Uint32Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_UINT32_ARRAY */
|
||||
{ JS_ATOM_BigInt64Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_BIG_INT64_ARRAY */
|
||||
{ JS_ATOM_BigUint64Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_BIG_UINT64_ARRAY */
|
||||
{ JS_ATOM_Float16Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_FLOAT16_ARRAY */
|
||||
{ JS_ATOM_Float32Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_FLOAT32_ARRAY */
|
||||
{ JS_ATOM_Float64Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_FLOAT64_ARRAY */
|
||||
{ JS_ATOM_DataView, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_DATAVIEW */
|
||||
@@ -5091,6 +5094,7 @@ static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID clas
|
||||
case JS_CLASS_UINT32_ARRAY:
|
||||
case JS_CLASS_BIG_INT64_ARRAY:
|
||||
case JS_CLASS_BIG_UINT64_ARRAY:
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
case JS_CLASS_FLOAT32_ARRAY:
|
||||
case JS_CLASS_FLOAT64_ARRAY:
|
||||
p->is_exotic = 1;
|
||||
@@ -6482,6 +6486,7 @@ void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s)
|
||||
case JS_CLASS_UINT32_ARRAY: /* u.typed_array / u.array */
|
||||
case JS_CLASS_BIG_INT64_ARRAY: /* u.typed_array / u.array */
|
||||
case JS_CLASS_BIG_UINT64_ARRAY: /* u.typed_array / u.array */
|
||||
case JS_CLASS_FLOAT16_ARRAY: /* u.typed_array / u.array */
|
||||
case JS_CLASS_FLOAT32_ARRAY: /* u.typed_array / u.array */
|
||||
case JS_CLASS_FLOAT64_ARRAY: /* u.typed_array / u.array */
|
||||
case JS_CLASS_DATAVIEW: /* u.typed_array */
|
||||
@@ -8344,6 +8349,9 @@ static JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj,
|
||||
case JS_CLASS_BIG_UINT64_ARRAY:
|
||||
if (unlikely(idx >= p->u.array.count)) goto slow_path;
|
||||
return JS_NewBigUint64(ctx, p->u.array.u.uint64_ptr[idx]);
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
if (unlikely(idx >= p->u.array.count)) goto slow_path;
|
||||
return __JS_NewFloat64(ctx, fromfp16(p->u.array.u.fp16_ptr[idx]));
|
||||
case JS_CLASS_FLOAT32_ARRAY:
|
||||
if (unlikely(idx >= p->u.array.count)) goto slow_path;
|
||||
return __JS_NewFloat64(ctx, p->u.array.u.float_ptr[idx]);
|
||||
@@ -9178,6 +9186,13 @@ static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj,
|
||||
p->u.array.u.uint64_ptr[idx] = v;
|
||||
}
|
||||
break;
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
if (JS_ToFloat64Free(ctx, &d, val))
|
||||
return -1;
|
||||
if (unlikely(idx >= (uint32_t)p->u.array.count))
|
||||
goto ta_out_of_bound;
|
||||
p->u.array.u.fp16_ptr[idx] = tofp16(d);
|
||||
break;
|
||||
case JS_CLASS_FLOAT32_ARRAY:
|
||||
if (JS_ToFloat64Free(ctx, &d, val))
|
||||
return -1;
|
||||
@@ -13240,6 +13255,9 @@ static void js_print_object(JSPrintValueState *s, JSObject *p)
|
||||
case JS_CLASS_BIG_UINT64_ARRAY:
|
||||
js_printf(s, "%" PRIu64, *(uint64_t *)ptr);
|
||||
break;
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
js_print_float64(s, fromfp16(*(uint16_t *)ptr));
|
||||
break;
|
||||
case JS_CLASS_FLOAT32_ARRAY:
|
||||
js_print_float64(s, *(float *)ptr);
|
||||
break;
|
||||
@@ -43888,6 +43906,11 @@ static JSValue js_math_hypot(JSContext *ctx, JSValueConst this_val,
|
||||
return JS_NewFloat64(ctx, r);
|
||||
}
|
||||
|
||||
static double js_math_f16round(double a)
|
||||
{
|
||||
return fromfp16(tofp16(a));
|
||||
}
|
||||
|
||||
static double js_math_fround(double a)
|
||||
{
|
||||
return (float)a;
|
||||
@@ -43991,6 +44014,7 @@ static const JSCFunctionListEntry js_math_funcs[] = {
|
||||
JS_CFUNC_SPECIAL_DEF("cbrt", 1, f_f, cbrt ),
|
||||
JS_CFUNC_DEF("hypot", 2, js_math_hypot ),
|
||||
JS_CFUNC_DEF("random", 0, js_math_random ),
|
||||
JS_CFUNC_SPECIAL_DEF("f16round", 1, f_f, js_math_f16round ),
|
||||
JS_CFUNC_SPECIAL_DEF("fround", 1, f_f, js_math_fround ),
|
||||
JS_CFUNC_DEF("imul", 2, js_math_imul ),
|
||||
JS_CFUNC_DEF("clz32", 1, js_math_clz32 ),
|
||||
@@ -51958,8 +51982,8 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
|
||||
|
||||
static uint8_t const typed_array_size_log2[JS_TYPED_ARRAY_COUNT] = {
|
||||
0, 0, 0, 1, 1, 2, 2,
|
||||
3, 3, /* BigInt64Array, BigUint64Array */
|
||||
2, 3
|
||||
3, 3, // BigInt64Array, BigUint64Array
|
||||
1, 2, 3 // Float16Array, Float32Array, Float64Array
|
||||
};
|
||||
|
||||
static JSValue js_array_buffer_constructor3(JSContext *ctx,
|
||||
@@ -52892,7 +52916,9 @@ static JSValue js_typed_array_fill(JSContext *ctx, JSValueConst this_val,
|
||||
double d;
|
||||
if (JS_ToFloat64(ctx, &d, argv[0]))
|
||||
return JS_EXCEPTION;
|
||||
if (p->class_id == JS_CLASS_FLOAT32_ARRAY) {
|
||||
if (p->class_id == JS_CLASS_FLOAT16_ARRAY) {
|
||||
v64 = tofp16(d);
|
||||
} else if (p->class_id == JS_CLASS_FLOAT32_ARRAY) {
|
||||
union {
|
||||
float f;
|
||||
uint32_t u32;
|
||||
@@ -53023,6 +53049,7 @@ static JSValue js_typed_array_indexOf(JSContext *ctx, JSValueConst this_val,
|
||||
int64_t v64;
|
||||
double d;
|
||||
float f;
|
||||
uint16_t hf;
|
||||
|
||||
len = js_typed_array_get_length_internal(ctx, this_val);
|
||||
if (len < 0)
|
||||
@@ -53185,6 +53212,39 @@ static JSValue js_typed_array_indexOf(JSContext *ctx, JSValueConst this_val,
|
||||
}
|
||||
}
|
||||
break;
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
if (is_bigint)
|
||||
break;
|
||||
if (isnan(d)) {
|
||||
const uint16_t *pv = p->u.array.u.fp16_ptr;
|
||||
/* special case: indexOf returns -1, includes finds NaN */
|
||||
if (special != special_includes)
|
||||
goto done;
|
||||
for (; k != stop; k += inc) {
|
||||
if (isfp16nan(pv[k])) {
|
||||
res = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (d == 0) {
|
||||
// special case: includes also finds negative zero
|
||||
const uint16_t *pv = p->u.array.u.fp16_ptr;
|
||||
for (; k != stop; k += inc) {
|
||||
if (isfp16zero(pv[k])) {
|
||||
res = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (hf = tofp16(d), d == fromfp16(hf)) {
|
||||
const uint16_t *pv = p->u.array.u.fp16_ptr;
|
||||
for (; k != stop; k += inc) {
|
||||
if (pv[k] == hf) {
|
||||
res = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case JS_CLASS_FLOAT32_ARRAY:
|
||||
if (is_bigint)
|
||||
break;
|
||||
@@ -53575,6 +53635,11 @@ static int js_TA_cmp_uint64(const void *a, const void *b, void *opaque) {
|
||||
return (y < x) - (y > x);
|
||||
}
|
||||
|
||||
static int js_TA_cmp_float16(const void *a, const void *b, void *opaque) {
|
||||
return js_cmp_doubles(fromfp16(*(const uint16_t *)a),
|
||||
fromfp16(*(const uint16_t *)b));
|
||||
}
|
||||
|
||||
static int js_TA_cmp_float32(const void *a, const void *b, void *opaque) {
|
||||
return js_cmp_doubles(*(const float *)a, *(const float *)b);
|
||||
}
|
||||
@@ -53615,6 +53680,10 @@ static JSValue js_TA_get_uint64(JSContext *ctx, const void *a) {
|
||||
return JS_NewBigUint64(ctx, *(uint64_t *)a);
|
||||
}
|
||||
|
||||
static JSValue js_TA_get_float16(JSContext *ctx, const void *a) {
|
||||
return __JS_NewFloat64(ctx, fromfp16(*(const uint16_t *)a));
|
||||
}
|
||||
|
||||
static JSValue js_TA_get_float32(JSContext *ctx, const void *a) {
|
||||
return __JS_NewFloat64(ctx, *(const float *)a);
|
||||
}
|
||||
@@ -53740,6 +53809,10 @@ static JSValue js_typed_array_sort(JSContext *ctx, JSValueConst this_val,
|
||||
tsc.getfun = js_TA_get_uint64;
|
||||
cmpfun = js_TA_cmp_uint64;
|
||||
break;
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
tsc.getfun = js_TA_get_float16;
|
||||
cmpfun = js_TA_cmp_float16;
|
||||
break;
|
||||
case JS_CLASS_FLOAT32_ARRAY:
|
||||
tsc.getfun = js_TA_get_float32;
|
||||
cmpfun = js_TA_cmp_float32;
|
||||
@@ -54295,6 +54368,14 @@ static JSValue js_dataview_getValue(JSContext *ctx,
|
||||
return JS_NewBigUint64(ctx, v);
|
||||
}
|
||||
break;
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
{
|
||||
uint16_t v;
|
||||
v = get_u16(ptr);
|
||||
if (is_swap)
|
||||
v = bswap16(v);
|
||||
return __JS_NewFloat64(ctx, fromfp16(v));
|
||||
}
|
||||
case JS_CLASS_FLOAT32_ARRAY:
|
||||
{
|
||||
union {
|
||||
@@ -54356,7 +54437,9 @@ static JSValue js_dataview_setValue(JSContext *ctx,
|
||||
double d;
|
||||
if (JS_ToFloat64(ctx, &d, val))
|
||||
return JS_EXCEPTION;
|
||||
if (class_id == JS_CLASS_FLOAT32_ARRAY) {
|
||||
if (class_id == JS_CLASS_FLOAT16_ARRAY) {
|
||||
v = tofp16(d);
|
||||
} else if (class_id == JS_CLASS_FLOAT32_ARRAY) {
|
||||
union {
|
||||
float f;
|
||||
uint32_t i;
|
||||
@@ -54385,6 +54468,7 @@ static JSValue js_dataview_setValue(JSContext *ctx,
|
||||
break;
|
||||
case JS_CLASS_INT16_ARRAY:
|
||||
case JS_CLASS_UINT16_ARRAY:
|
||||
case JS_CLASS_FLOAT16_ARRAY:
|
||||
if (is_swap)
|
||||
v = bswap16(v);
|
||||
put_u16(ptr, v);
|
||||
@@ -54421,6 +54505,7 @@ static const JSCFunctionListEntry js_dataview_proto_funcs[] = {
|
||||
JS_CFUNC_MAGIC_DEF("getUint32", 1, js_dataview_getValue, JS_CLASS_UINT32_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("getBigInt64", 1, js_dataview_getValue, JS_CLASS_BIG_INT64_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("getBigUint64", 1, js_dataview_getValue, JS_CLASS_BIG_UINT64_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("getFloat16", 1, js_dataview_getValue, JS_CLASS_FLOAT16_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("getFloat32", 1, js_dataview_getValue, JS_CLASS_FLOAT32_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("getFloat64", 1, js_dataview_getValue, JS_CLASS_FLOAT64_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("setInt8", 2, js_dataview_setValue, JS_CLASS_INT8_ARRAY ),
|
||||
@@ -54431,6 +54516,7 @@ static const JSCFunctionListEntry js_dataview_proto_funcs[] = {
|
||||
JS_CFUNC_MAGIC_DEF("setUint32", 2, js_dataview_setValue, JS_CLASS_UINT32_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("setBigInt64", 2, js_dataview_setValue, JS_CLASS_BIG_INT64_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("setBigUint64", 2, js_dataview_setValue, JS_CLASS_BIG_UINT64_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("setFloat16", 2, js_dataview_setValue, JS_CLASS_FLOAT16_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("setFloat32", 2, js_dataview_setValue, JS_CLASS_FLOAT32_ARRAY ),
|
||||
JS_CFUNC_MAGIC_DEF("setFloat64", 2, js_dataview_setValue, JS_CLASS_FLOAT64_ARRAY ),
|
||||
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "DataView", JS_PROP_CONFIGURABLE ),
|
||||
|
Reference in New Issue
Block a user