From bcdad5767006ca03728324c656b03888507f75a1 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Mon, 6 Sep 2021 21:20:20 +0300 Subject: [PATCH] LibCrypto: Add naive implementation of {Un,}SignedBigInteger::to_double --- Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp | 8 ++++++++ Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h | 1 + .../Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp | 6 ++++++ Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h | 1 + 4 files changed, 16 insertions(+) diff --git a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp index 3d6bdffa55b..1ff34050a6f 100644 --- a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp @@ -63,6 +63,14 @@ u64 SignedBigInteger::to_u64() const return ~(unsigned_value - 1); // equivalent to `-unsigned_value`, but doesnt trigger UBSAN } +double SignedBigInteger::to_double() const +{ + double unsigned_value = m_unsigned_data.to_double(); + if (!m_sign) + return unsigned_value; + return -unsigned_value; +} + FLATTEN SignedBigInteger SignedBigInteger::plus(const SignedBigInteger& other) const { // If both are of the same sign, just add the unsigned data and return. diff --git a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h index 099348bfd7b..4440dd9f004 100644 --- a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h +++ b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h @@ -66,6 +66,7 @@ public: String to_base(u16 N) const; u64 to_u64() const; + double to_double() const; const UnsignedBigInteger& unsigned_value() const { return m_unsigned_data; } const Vector words() const { return m_unsigned_data.words(); } diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp index 1f036af432b..a2c8d77ef7a 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp @@ -112,6 +112,12 @@ u64 UnsignedBigInteger::to_u64() const return value; } +double UnsignedBigInteger::to_double() const +{ + // FIXME: I am naive + return static_cast(to_u64()); +} + void UnsignedBigInteger::set_to_0() { m_words.clear_with_capacity(); diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h index 9b9e1912472..2d899f96eac 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h @@ -57,6 +57,7 @@ public: String to_base(u16 N) const; u64 to_u64() const; + double to_double() const; const Vector& words() const { return m_words; }