diff options
author | Linus Groh <mail@linusgroh.de> | 2020-05-18 16:04:38 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-18 17:57:28 +0200 |
commit | b3090678a9d9d1a8bda18a1781557d0547882da9 (patch) | |
tree | 7001b201e9349fa02d5364506764bf770adee451 /Libraries/LibJS/Runtime | |
parent | 452dbbc463625eee1800d542b6613b4ef875bb80 (diff) | |
download | serenity-b3090678a9d9d1a8bda18a1781557d0547882da9.zip |
LibJS: Add Math.clz32()
Diffstat (limited to 'Libraries/LibJS/Runtime')
-rw-r--r-- | Libraries/LibJS/Runtime/MathObject.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/MathObject.h | 1 |
2 files changed, 13 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 2b173bcc70..73ee7696c0 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2020, Linus Groh <mail@linusgroh.de> * All rights reserved. * * 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("expm1", expm1, 1, attr); put_native_function("sign", sign, 1, attr); + put_native_function("clz32", clz32, 1, attr); put("E", Value(M_E), 0); put("LN2", Value(M_LN2), 0); @@ -245,4 +247,14 @@ Value MathObject::sign(Interpreter& interpreter) 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())); +} + } diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index e9902e6d53..1cd550ca7d 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -54,6 +54,7 @@ private: static Value exp(Interpreter&); static Value expm1(Interpreter&); static Value sign(Interpreter&); + static Value clz32(Interpreter&); }; } |