1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 02:45:07 +00:00

LibJS: Add Math.clz32()

This commit is contained in:
Linus Groh 2020-05-18 16:04:38 +01:00 committed by Andreas Kling
parent 452dbbc463
commit b3090678a9
3 changed files with 63 additions and 0 deletions

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -53,6 +54,7 @@ MathObject::MathObject()
put_native_function("exp", exp, 1, attr); put_native_function("exp", exp, 1, attr);
put_native_function("expm1", expm1, 1, attr); put_native_function("expm1", expm1, 1, attr);
put_native_function("sign", sign, 1, attr); put_native_function("sign", sign, 1, attr);
put_native_function("clz32", clz32, 1, attr);
put("E", Value(M_E), 0); put("E", Value(M_E), 0);
put("LN2", Value(M_LN2), 0); put("LN2", Value(M_LN2), 0);
@ -245,4 +247,14 @@ Value MathObject::sign(Interpreter& interpreter)
return js_nan(); return js_nan();
} }
Value MathObject::clz32(Interpreter& interpreter)
{
auto number = interpreter.argument(0).to_number(interpreter);
if (interpreter.exception())
return {};
if (!number.is_finite_number() || (unsigned)number.as_double() == 0)
return Value(32);
return Value(__builtin_clz((unsigned)number.as_double()));
}
} }

View file

@ -54,6 +54,7 @@ private:
static Value exp(Interpreter&); static Value exp(Interpreter&);
static Value expm1(Interpreter&); static Value expm1(Interpreter&);
static Value sign(Interpreter&); static Value sign(Interpreter&);
static Value clz32(Interpreter&);
}; };
} }

View file

@ -0,0 +1,50 @@
load("test-common.js");
try {
assert(Math.clz32.length === 1);
assert(Math.clz32(0) === 32);
assert(Math.clz32(1) === 31);
assert(Math.clz32(2) === 30);
assert(Math.clz32(3) === 30);
assert(Math.clz32(4) === 29);
assert(Math.clz32(5) === 29);
assert(Math.clz32(-1) === 0);
assert(Math.clz32(-10) === 0);
assert(Math.clz32(-100) === 0);
assert(Math.clz32(-1000) === 0);
assert(Math.clz32(-0.123) === 32);
assert(Math.clz32(0.123) === 32);
assert(Math.clz32(1.23) === 31);
assert(Math.clz32(12) === 28);
assert(Math.clz32(123) === 25);
assert(Math.clz32(1234) === 21);
assert(Math.clz32(12345) === 18);
assert(Math.clz32(123456) === 15);
assert(Math.clz32(1234567) === 11);
assert(Math.clz32(12345678) === 8);
assert(Math.clz32(123456789) === 5);
assert(Math.clz32(999999999) === 2);
assert(Math.clz32(9999999999) === 1);
assert(Math.clz32(99999999999) === 1);
assert(Math.clz32(999999999999) === 0);
assert(Math.clz32(9999999999999) === 1);
assert(Math.clz32(99999999999999) === 3);
assert(Math.clz32(999999999999999) === 0);
assert(Math.clz32() === 32);
assert(Math.clz32(NaN) === 32);
assert(Math.clz32(Infinity) === 32);
assert(Math.clz32(-Infinity) === 32);
assert(Math.clz32(false) === 32);
assert(Math.clz32(true) === 31);
assert(Math.clz32(null) === 32);
assert(Math.clz32(undefined) === 32);
assert(Math.clz32([]) === 32);
assert(Math.clz32({}) === 32);
assert(Math.clz32("foo") === 32);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}