LibJS: Replace useless use of SignedBigInteger::create_from() with ctor

create_from() casts the value to a 64 bit integer and then creates two
words from it, which is not necessary if we only pass values to it that
fit into a single word (32 bit integer).
Also make them use UnsignedBigInteger as the previously missing SBI
divided_by() overload is now implemented.
This commit is contained in:
Linus Groh 2021-07-09 11:50:42 +01:00
parent a216ea4c8d
commit 9c9813c312
Notes: sideshowbarker 2024-07-18 10:00:41 +09:00

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/Temporal/InstantPrototype.h>
@ -58,7 +58,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_seconds_getter)
auto& ns = instant->nanoseconds();
// 4. Let s be RoundTowardsZero((ns) / 10^9).
auto [s, _] = ns.big_integer().divided_by(Crypto::SignedBigInteger::create_from(1'000'000'000));
auto [s, _] = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000'000 });
// 5. Return 𝔽(s).
return Value((double)s.to_base(10).to_int<i64>().value());
@ -77,7 +77,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_milliseconds_getter)
auto& ns = instant->nanoseconds();
// 4. Let ms be RoundTowardsZero((ns) / 10^6).
auto [ms, _] = ns.big_integer().divided_by(Crypto::SignedBigInteger::create_from(1'000'000));
auto [ms, _] = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000 });
// 5. Return 𝔽(ms).
return Value((double)ms.to_base(10).to_int<i64>().value());
@ -96,7 +96,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_microseconds_getter)
auto& ns = instant->nanoseconds();
// 4. Let µs be RoundTowardsZero((ns) / 10^3).
auto [us, _] = ns.big_integer().divided_by(Crypto::SignedBigInteger::create_from(1'000));
auto [us, _] = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000 });
// 5. Return (µs).
return js_bigint(vm.heap(), move(us));